目录一、作用二、环境准备三、XML无接口开发四、Mapper接口开发五、查询一、作用代替jdbc解决jdbc的硬编码、繁杂代码问题1. 硬编码注册驱动获取连接 - mybatis-conf.xml2. 繁杂代码定义sql语句 - XxxMapper.xml二、环境准备一三步走1. maven配置创建maven项目设置 - 构建工具 - maven主路径、配置路径2. pom.xml导入依赖mybatis、mysql、junit、slf4j-api、logback-classic、logback-core3. resource配置文件mybatis核心配置、sql映射、logback配置二mybatis核心配置文件注意核心配置文件标签要遵循MyBatis官网的结构顺序1. 加载数据源配置驱动信息、数据库连接信息environments default默认数据源id environment id数据源id transactionManager typeJDBC/ dataSource typePOOLED property namedriver valuecom.mysql.cj.jdbc.Driver/ property nameurl valuejdbc:mysql://localhost:3306/数据库名?useSSLfalse/ property nameusername value用户名/ property namepassword value密码/ /dataSource /environment /environments2. 加载sql映射1逐个加载映射mapperresource属性指定sql映射路径2批量加载映射packagename属性指定sql映射包全名!--1. 逐个加载映射-- mappers mapper resourcecom/liu/mybatis/mapper/XxxMapper.xml/ /mappers !--2. 批量加载映射-- mappers package namecom.liu.mybatis.mapper/ /mappers3. 起类别名typeAliases typeAlias type类全限定名 alias类别名/ /typeAliases三sql映射1. 映射根配置namespace 属性作用区分不同映射1 XML 无接口自定义唯一名称区分映射2 Mapper 接口填写接口全类名绑定接口!--1. XML无接口开发自定义-- mapper namespaceXxxMapper /mapper !--2. Mapper接口开发接口全限定名-- mapper namespacecom.liu.mybatis.mapper.XxxMapper /mapper2. sql声明标签1标签类型select/insert/update/delete2属性idresultType / resultMap注意resultType和resultMap - type填实体类的全限定名/别名!--1. resultType-- select idsqlID resultType结果集要封装成的目标实体类 select * from tb_user /select !--2. resultMap-- resultMap idresultMapID type结果集要封装成的目标实体类 result column字段名 property字段别名/ /resultMap select idsqlID resultMapresultMapID /select3. sql内容编写1参数占位符① #{}预编译占位符防 SQL 注入自动加引号② ${}字符串直接拼接有注入风险不加引号2特殊字符处理①转义字符lt; gt; amp;②CDATA 区![CDATA[ 内容 ]]3模糊查询 like关键字4动态 SQL 标签①where剔除开头多余 and/or无查询条件时不生成where语句②if条件成立拼接 SQL③choose多条件单选类似if-else if-else!--1. if标签-- select * from tb_brand where if test条件/if /where !--2. choose标签-- select * from tb_brand where choose when test条件/when otherwise/otherwise /choose /where三、XML无接口开发一准备pojo起类别名二核心配置文件三sql映射四测试执行sql1. 加载配置文件获取SqlSessionFactory对象1InputStream inputStream Resources.getResourceAsStream(mybatis核心配置文件路径);2SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);2. 获取SqlSession对象用于执行sqlsqlSessionFactory.openSession()3. 执行sqlsqlSession.selectList(namespace.sqlId)4. 释放资源sqlSession.close()?xml version1.0 encodingUTF-8 ? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd !--XML无接口开发namespace为自定义-- mapper namespaceUserMapper select idselectAll resultTypecom.liu.mybatis.pojo.User select * from tb_user /select /mapperpublic class selectAll { public static void main(String[] args) throws IOException { //1. 加载核心配置文件获取SqlSessionFactory对象 String resource mybatis-config.xml; InputStream inputStream Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream); //2. 获取SqlSession对象用于执行sql SqlSession sqlSession sqlSessionFactory.openSession(); //3. 执行sql ListUser sl sqlSession.selectList(UserMapper.selectAll); System.out.println(sl); //4. 释放资源 sqlSession.close(); } }四、Mapper接口开发一准备pojo起类别名二Mapper接口三sql映射四执行sql1. 加载配置文件获取SqlSessionFactory对象2. 获取SqlSession对象3. 获取Mapper代理对象用于执行sqlsqlSession.getMapper(XxxMapper.class)4. 执行sqlxxxMapper.接口方法()5. 释放资源public interface BrandMapper { //返回值类型与sql封装类型对应MyBatis根据接口返回值类型自动封装sql查询结果。 //方法名与sql声明ID一致 ListBrand selectAll(); }!--Mapper接口开发namespace为接口全限定名-- mapper namespacecom.liu.mybatis.mapper.BrandMapper resultMap idbrandResultMap typeBrand result columnbrand_name propertybrandName/ result columncompany_name propertycompanyName/ /resultMap select idselectAll resultMapbrandResultMap select id,brand_name,company_name,ordered,description,status from tb_brand /select /mapperTest public void testSelectBrand() throws IOException { //1. 加载核心配置文件获取SqlSessionFactory对象 String resource mybatis-config.xml; InputStream inputStream Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream); //2. 获取SqlSession对象用于执行sql SqlSession sqlSession sqlSessionFactory.openSession(); //3. 获取UserMapper接口的代理对象 BrandMapper brandMapper sqlSession.getMapper(BrandMapper.class); //4. 执行sql ListBrand brands brandMapper.selectAll(); System.out.println(brands); //5. 释放资源 sqlSession.close(); }五同名规则1. 接口与映射的名字、目录相同2. 接口方法名与sql声明ID相同3. 接口方法返回值类型与sql结果集要封装成的目标实体类对应4. 传递参数1散装参数散装参数名与sql占位符名称相同2实体类参数实体类属性名与sql占位符名称相同3Map集合参数键名与sql占位符名称相同五、查询1. 查询所有2. 通过id查询3. 多条件查询4. 多条件动态查询5. 单条件动态查询