不用备案的网站哪里买,代理公司注册费用,昆明网站seo技术厂家,贵州省住房和城乡建设厅门户网站目录 MyBatis简介
MyBatis整体架构及运行流程
1.数据源配置文件
2.Sql映射文件
3.会话工厂与会话
4.运行流程
mybatis实现增删改查
Mybatis的获取参数的方式
mapper中自定义映射
mybatis注解开发
mybatis缓存
mybatis分页插件 MyBatis简介
MyBatis 是一款优秀的持久…目录 MyBatis简介
MyBatis整体架构及运行流程
1.数据源配置文件
2.Sql映射文件
3.会话工厂与会话
4.运行流程
mybatis实现增删改查
Mybatis的获取参数的方式
mapper中自定义映射
mybatis注解开发
mybatis缓存
mybatis分页插件 MyBatis简介
MyBatis 是一款优秀的持久层框架它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJOPlain Old Java Objects普通老式 Java 对象为数据库中的记录
2010年这个项目由apache software foundation 迁移到了google code并且改名为MyBatis 。2013年11月迁移到Github。
回顾JDBC的使用步骤 1.加载驱动
Class.forName(com.mysql.jdbc.Driver); 2.获取连接对象
DiverManerger.getConnection(url,username,password) 3.获取执行sql的Statement对象
connection.CreateStatement(); 4.执行sql语句 5.处理结果集 6.释放连接
connection.Close()
与JDBC相比 Mybatis通过参数映射方式可以将参数灵活的配置在SQL语句中的配置文件中避免在Java类中配置参数 Mybatis通过输出映射机制将结果集的检索自动映射成相应的Java对象避免对结果集手工检索 Mybatis可以通过Xml配置文件对数据库连接进行管理
MyBatis整体架构及运行流程
Mybatis整体构造由 数据源配置文件、Sql映射文件、会话工厂、会话、执行器和底层封装对象组成
1.数据源配置文件
通过配置的方式将数据库的配置信息从应用程序中独立出来由独立的模块管理和配置。Mybatis的数据源配置文件包含数据库驱动、数据库连接地址、用户名密码、事务管理等还可以配置连接池的连接数、空闲时间等
一个MapConfig.xml基本的配置信息如下
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd
configurationenvironments defaultdevelopmentenvironment iddevelopment!-- 事务的管理 JDBC MANAGED--transactionManager typeJDBC/!-- 数据源 POOLED UNPOOLED JNDI --dataSource typePOOLEDproperty namedriver value${driver}/property nameurl value${url}/property nameusername value${username}/property namepassword value${password}//dataSource/environment/environmentsmappersmapper resourceorg/mybatis/example/BlogMapper.xml//mappers
/configuration
mybatis mapper文件映射 设置资源文件路径
Maven中默认是只会打包resource下的资源文件。如果我们的文件不放在resource 则需要通过配置告知Maven 2.Sql映射文件
Mybatis中所有数据库的操作都会基于该映射文件和配置的sql语句在这个配置文件中可以配置任何类型的sql语句。框架会根据配置文件中的参数配置完成对sql语句以及输入输出参数的映射配置。
Mapper.xml配置文件大致如下
?xml version1.0 encodingutf-8 ?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespace命名空间select id方法名 resultMap返回值类型 parameterType参数类型-- sql语句/select/mapper 3.会话工厂与会话
Mybatis中会话工厂SqlSessionFactory类可以通过加载资源文件读取数据源配置MapConfig.xml信息从而产生一种可以与数据库交互的会话实例SqlSession会话实例SqlSession根据Mapper.xml文件中配置的sql,对数据库进行操作。
4.运行流程
会话工厂SqlSessionFactory通过加载资源文件获取MapConfig.xml配置文件信息然后生成可以与数据库交互的会话实例SqlSession。会话实例可以根据Mapper配置文件中的Sql配置去执行相应的增删改查操作
执行流程图 mybatis实现增删改查
public class TestStudent {Testpublic void test01(){try {//加载配置文件InputStream in Resources.getResourceAsStream(config/mybatis-config.xml);//获取sqlSession对象SqlSession sqlSession new SqlSessionFactoryBuilder().build(in).openSession();//查询所有学生信息ListStudent students sqlSession.selectList(cn.kgc.mybatis.dao.StudentDao.getAll); students.forEach(student - System.out.println(student) );} catch (IOException e) {e.printStackTrace();}}Testpublic void test02(){//查询一个学生信息try {//加载配置文件InputStream in Resources.getResourceAsStream(config/mybatis-config.xml);//获取sqlSession对象SqlSession sqlSession new SqlSessionFactoryBuilder().build(in).openSession();//根据用户名查询一个学生信息Student student sqlSession.selectOne(cn.kgc.mybatis.dao.StudentDao.findOne,tom);System.out.println(student);sqlSession.close();} catch (IOException e) {e.printStackTrace();}}Testpublic void test03() {//添加一个学生信息try {//加载配置文件InputStream in Resources.getResourceAsStream(config/mybatis-config.xml);//获取sqlSession对象SqlSession sqlSession new SqlSessionFactoryBuilder().build(in).openSession();//添加一个学生信息Student student Student.builder().stuBirth(new Date()).stuName(lilei).stuNo(2021073001).stuSex(男).build();int i sqlSession.insert(cn.kgc.mybatis.dao.StudentDao.addOne, student);System.out.println(i);sqlSession.commit();sqlSession.close();} catch (IOException e) {e.printStackTrace();}}Testpublic void test04() {//删除一个学生信息try{InputStream in Resources.getResourceAsStream(config/mybatis-config.xml);//获取sqlSession对象 同时可设置事务的自动提交 openSession(true)SqlSession sqlSession new SqlSessionFactoryBuilder().build(in).openSession();int delete sqlSession.delete(cn.kgc.mybatis.dao.StudentDao.delOne,lilei );sqlSession.commit();sqlSession.close();System.out.println(delete);}catch (Exception e){e.printStackTrace();}}Testpublic void test05() {//修改一个学生信息try{InputStream in Resources.getResourceAsStream(config/mybatis-config.xml);//获取sqlSession对象SqlSession sqlSession new SqlSessionFactoryBuilder().build(in).openSession();Student student Student.builder().stuBirth(new Date()).stuName(lili).stuNo(2021073001).stuSex(女).build();int delete sqlSession.update(cn.kgc.mybatis.dao.StudentDao.updateStudent,student);sqlSession.commit();sqlSession.close();System.out.println(delete);}catch (Exception e){e.printStackTrace();}}}mybatis中使用log4j日志工具
1.配置mybatis-config.xml 开启驼峰命名
设置别名 2.配置log4j.properties文件放置在resources目录下
log4j.rootLoggerDEBUG,Console#Console
log4j.appender.Consoleorg.apache.log4j.ConsoleAppender
log4j.appender.console.TargetSystem.out
log4j.appender.Console.layoutorg.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern%d [%t] %-5p [%c] - %m%nlog4j.logger.org.apacheERROR
log4j.logger.org.mybatisERROR
log4j.logger.org.springframeworkERROR#这个需要
log4j.logger.log4jdbc.debugERROR
log4j.logger.com.gk.mapperERROR
log4j.logger.jdbc.auditERROR
log4j.logger.jdbc.resultsetERROR
#这个打印SQL语句非常重要
log4j.logger.jdbc.sqlonlyDEBUG
log4j.logger.jdbc.sqltimingERROR
log4j.logger.jdbc.connectionFATAL 使用mapper代理对象实现增删改查
public class TestStudentMapper {SqlSessionFactory factory;Beforepublic void init(){try {InputStream in Resources.getResourceAsStream(config/mybatis-config.xml);SqlSessionFactoryBuilder sfb new SqlSessionFactoryBuilder();factory sfb.build(in);} catch (IOException e) {e.printStackTrace();}}Testpublic void test01(){//开启事务自动提交SqlSession sqlSession factory.openSession(true);//获取dao层的代理对象StudentDao studentDao sqlSession.getMapper(StudentDao.class);ListStudent students studentDao.getAll();students.forEach(student - System.out.println(student));}Testpublic void test02(){ //添加数据SqlSession sqlSession factory.openSession(true);Student student Student.builder().stuSex(男).stuNo(2021073100).stuName(李四).stuBirth(new Date()).build();StudentDao studentDao sqlSession.getMapper(StudentDao.class);Integer line studentDao.addOne(student);System.out.println(line);}Testpublic void test03(){//删除一条数据SqlSession sqlSession factory.openSession(true);StudentDao studentDao sqlSession.getMapper(StudentDao.class);Integer line studentDao.delOne(李四);System.out.println(line);}Testpublic void test04() { //修改数据SqlSession sqlSession factory.openSession(true);StudentDao studentDao sqlSession.getMapper(StudentDao.class);Student student Student.builder().stuSex(女).stuNo(2021073100).stuName(李四).stuBirth(new Date()).build();Integer line studentDao.updateOne(student);System.out.println(line);}Testpublic void test06(){//模糊查询一个学生信息 一个参数SqlSession sqlSession factory.openSession(true);StudentDao studentDao sqlSession.getMapper(StudentDao.class);ListStudent students studentDao.selectLikeName(li);System.out.println(students.toString());}Testpublic void test07(){//模糊查询一个学生信息 两个参数SqlSession sqlSession factory.openSession(true);StudentDao studentDao sqlSession.getMapper(StudentDao.class);ListStudent students studentDao.selectLikeName2(li, 2021072902);System.out.println(students.toString());}Testpublic void test08(){//模糊查询一个学生信息 一个集合参数 mapper文件中按照key取值SqlSession sqlSession factory.openSession(true);StudentDao studentDao sqlSession.getMapper(StudentDao.class);HashMapString, String map new HashMap();map.put(stuname,li);map.put(stuno, 2021072902);ListStudent students studentDao.selectLikeName3(map);System.out.println(students.toString());}}Mybatis的获取参数的方式
方式1${} 字符串拼接
方式2#{} 占位符
1.Mapper接口参数为单个参数 2.Mapper接口参数是多个参数的获取
mybatis在处理多个参数时,会将多个参数保存到map集合中会 以 agr0 agr1或param1 param2为键 以参数位置进行存储 3.将参数设为map集合进行数据的传递 4.通过注解param(键名)设置参数的获取名字 5.将多个参数设置成对象进行数据的传递 Mybatis中的模糊查询 6.批量删除 mapper中自定义映射
自定义映射的用法之一解决表格查询的字段名和实体类中不一致的情况 mybatis对象的关联关系
一对多关系处理(一方)
resultMap idselectByEIdMap typecn.kgc.mybatis.entity.Empresult columnid propertydept.id /resultresult columndname propertydept.dName/result/resultMapresultMap idselectByEIdMap2 typecn.kgc.mybatis.entity.Empid columneid propertyeId/idresult columnename propertyEName/resultresult columnage propertyage/resultresult columndeptno propertydeptNo/result!--实体对象标识--association propertydept javaTypedeptid columnid propertyid/idresult columndname propertydName/result/association/resultMap!-- 分步查询 --resultMap idselectByEIdMap3 typecn.kgc.mybatis.entity.Empid columneid propertyeId/idresult columnename propertyEName/resultresult columnage propertyage/resultresult columndeptno propertydeptNo/resultassociation propertydept selectcn.kgc.mybatis.mapper.DeptMapper.selectById columndeptno fetchTypeeager/association/resultMap注延迟加载设置 1. setting namelazyLoadingEnabled valuetrue/ 2. setting nameaggressiveLazyLoading valuefalse/ 3.4.1之前的版本需要设置select idselectByEId resultMapselectByEIdMap2select * from emp left join dept on emp.deptno dept.id where eid #{eid}/selectselect idselectByEId2 resultMapselectByEIdMap3select * from emp where eid #{eid}/select 一对多关系处理(多方) resultMap idBaseResultMap typecn.kgc.mybatis.entity.Deptid propertyid columnid /result propertydName columndname /collection propertyemps ofTypeempid columneid propertyeId/idresult columnename propertyEName/resultresult columnage propertyage/resultresult columndeptno propertydeptNo/result/collection/resultMapresultMap idBaseResultMap2 typecn.kgc.mybatis.entity.Deptid propertyid columnid /result propertydName columndname /collection propertyemps selectcn.kgc.mybatis.mapper.EmpMapper.selectByDeptId columnid/collection/resultMapselect idselectById resultTypedeptselect * from dept where id #{id}/selectselect idselectById2 resultMapBaseResultMapselect emp.* ,dept.* from dept left join emp on dept.id emp.deptno where id #{id}/selectselect idselectById3 resultMapBaseResultMap2select dept.* from dept where id #{id}/select
动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架你应该能理解根据不同条件拼接 SQL 语句有多痛苦例如拼接时要确保不能忘记添加必要的空格还要注意去掉列表最后一个列名的逗号。利用动态 SQL可以彻底摆脱这种痛苦 动态SQL:if 语句 根据 stu_name 和 stu_sex 来查询数据。如果stu_name为空那么将只根据stu_sex来查询反之只根据stu_name来查询
首先不使用 动态SQL 来书写 可以发现如果 #{stu_name} 为空那么查询结果也是空如何解决这个问题呢使用 if 来判断 这样写我们可以看到如果 sex 等于 null那么查询语句为 select * from student where stu_name#{stu_name},但是如果stu_name 为空呢那么查询语句为 select * from user where and stu_sex#{sex}这是错误的 SQL 语句如何解决呢 ifwhere 语句 这个where标签会知道如果它包含的标签中有返回值的话它就插入一个where。此外如果标签返回的内容是以AND 或OR 开头的则它会剔除掉 动态SQL:ifset 语句 上面的对于查询 SQL 语句包含 where 关键字如果在进行更新操作的时候含有 set 关键词 如果第一个条件 username 为空那么 sql 语句为update user u set u.sex? where id? 如果第一个条件不为空那么 sql 语句为update user u set u.username ? ,u.sex ? where id? 动态SQL:choose(when,otherwise) 语句 有时候我们不想用到所有的查询条件只想选择其中的一个查询条件有一个满足即可使用 choose 标签可以解决此类问题类似于 Java 的 switch 语句 这里我们有三个条件id,username,sex只能选择一个作为查询条件 如果 id 不为空那么查询语句为select * from user where id? 如果 id 为空那么看username 是否为空如果不为空那么语句为 select * from user where username?; 如果 username 为空那么查询语句为 select * from user where sex? 动态SQL:trim 语句 trim标记是一个格式化的标记可以完成set或者是where标记的功能
用 trim 改写上面的 ifwhere 语句 prefix前缀 prefixoverride去掉第一个and或者是or
用 trim 改写上面的 ifset 语句 suffix后缀 suffixoverride去掉最后一个逗号也可以是其他的标记就像是上面前缀中的and一样 动态SQL: foreach 语句 需求我们需要查询 user 表中 id 分别为1,2,3的用户,sql语句 用 foreach 来改写 select * from user where id1 or id2 or id3 用 foreach 来改写 select * from user where id in (1,2,3) 动态SQL: SQL 片段 有时候可能某个 sql 语句我们用的特别多为了增加代码的重用性简化代码我们需要将这些代码抽取出来然后使用时直接调用。
假如我们需要经常根据用户名和性别来进行联合查询那么我们就把这个代码抽取出来如下 注意①、最好基于 单表来定义 sql 片段提高片段的可重用性 ②、在 sql 片段中最好不要包括 where
mybatis注解开发
public interface StudentDao2 {Select(select * from student where stu_no #{stu_no})Results({Result(property stuNo ,columnstu_no),Result(property stuSex,column stu_sex),Result(property birth,column stu_birth)})ListStudent getAll(String stu_no);Insert(insert into student (stu_no,stu_name,stu_sex,stu_birth)values(#{stuNo},#{stuName},#{stuSex},#{birth}))int addStudent(Student student);Delete(delete from student where stu_no #{stu_no})int delOne(String stu_no);Update(update student set stu_name #{stuName} where stu_no #{stuNo})int uptStudent(Student student);} mybatis缓存
MyBatis 中的缓存就是说 MyBatis 在执行一次SQL查询或者SQL更新之后这条SQL语句并不会消失而是被MyBatis 缓存起来当再次执行相同SQL语句的时候就会直接从缓存中进行提取而不是再次执行SQL命令。
mybatis的缓存机制有两级
1一级缓存一级缓存mybatsi已经为我们自动开启不用我们手动操作而且我们是关闭不了的但是我们可以手动清除缓存。SqlSession级别.提交事务缓存清空
一级缓存失效的情况 2二级缓存二级缓存需要我们手动开启。全局级别 SqlSessionFactory Testpublic void test02(){//验证mybatis的缓存机制 一级缓存 默认开启 sqlsession级别try {InputStream resource Resources.getResourceAsStream(config/mybatis-config.xml);SqlSessionFactory factory new SqlSessionFactoryBuilder().build(resource);SqlSession sqlSession factory.openSession(true);StudentDao mapper1 sqlSession.getMapper(StudentDao.class);StudentDao mapper2 sqlSession.getMapper(StudentDao.class);System.out.println(mapper1);System.out.println(mapper2);ListStudent a1 mapper1.getAll();System.out.println(a1);//手动提交事务 清空缓存sqlSession.commit();ListStudent a2 mapper2.getAll();System.out.println(a2);sqlSession.close();} catch (IOException e) {e.printStackTrace();}}Testpublic void test03(){//验证mybatis的缓存机制 二级缓存 需要配置mybatis-config.xml 和mapper文件try {InputStream resource Resources.getResourceAsStream(config/mybatis-config.xml);SqlSessionFactory factory new SqlSessionFactoryBuilder().build(resource);SqlSession sqlSession1 factory.openSession(true);SqlSession sqlSession2 factory.openSession(true);SqlSession sqlSession3 factory.openSession(true);StudentDao mapper1 sqlSession1.getMapper(StudentDao.class);StudentDao mapper2 sqlSession2.getMapper(StudentDao.class);StudentDao mapper3 sqlSession3.getMapper(StudentDao.class);ListStudent a1 mapper1.getAll();System.out.println(a1);//关闭session将查询结果写入二级缓存sqlSession1.close();//当对同一张表进行增删改操作后 二级缓存清除mapper3.delStudent(2021072901);// sqlSession3.close();ListStudent a2 mapper2.getAll();System.out.println(a2);} catch (IOException e) {e.printStackTrace();}}
mybatis分页插件 实例 今天的分享就到此结束了
创作不易点赞评论互关三连