MyBatis 比传统的方式更加方便,但是技术没有高低之分;
mybatis是一个版Orm的框架,他内部封装了JDBC;
开发时候只需要关注sql语句本身,不需要花费精力去加载驱动,创建链接,创建statement等繁琐过程。
优点 :
基于sql编程,灵活,不会对应用程序或者数据库的现有设计造成任何影响,SQL写在xml中。解除了代码和sql语句的耦合度,便于统一管理,提供xml标签,支持编写动态sql语句,可以重用高。
与jdbc比,减少了50%的代码量,消除了大量的JDBC大量沉余的代码,不需要手动开关链接。
与数据库具有很好的兼容性
能和spring很好的集成
提供映射标签,支持与数据库的orm字段关系映射;提供对象关系映射标签,支持对象关系组件维护。
缺点:
注重于sql本身,当子段多,关联表多时,对开发人员编写sql语句有一定功底。
sql语句依赖于数据库,导致数据库移植性差,不能随意更改数据库。
mybatis框架适合场景:
适合于dao层
如果安装标准来的话 那么
bin —> binImpl
dao --> daoImpl mybatis中将dao 改成**mapper.java
对性能要求比较高的话,或者需求比较多且改动较多的情况之下适用mybatis
简单介绍完了,开始重头戏 第一步,先拿到数据库,这里提供一个实例数据库: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 create database if not exists MyBatis_stu default character set utf8 collate utf8_bin; use MyBatis_stu ; create table if not exists classinfo( cid int primary key auto_increment, cname varchar (100 ) not null unique comment '班级编号' )ENGINE= InnoDB auto_increment= 101 default charset= utf8 collate = utf8_bin; create table if not exists stuinfo ( sid int primary key auto_increment, sname varchar (100 ) not null comment '学生姓名' , cid int comment '所在班级编号' , tel varchar (15 ) unique comment '联系方式' , addr varchar (100 ) comment '家庭住址' , constraint FK_stuinfo_cid foreign key(cid) references classinfo(cid) )ENGINE= InnoDB auto_increment= 101 default charset= utf8 collate = utf8_bin;
这一步,我觉得不要多说了,毕竟你学java到现在,这一步还是没看懂的话,我觉得你可以准备考虑考虑其他路了。
==第二步前台条件,你要创建好一个maven项目,并导入相关依赖,别告诉我你忘记了maven工程如何创建了?==
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <groupId > com.xh</groupId > <artifactId > Maven</artifactId > <version > 0.0.1-SNAPSHOT</version > <packaging > jar</packaging > <name > Maven</name > <url > http://maven.apache.org</url > <properties > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > </properties > <dependencies > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 8.0.28</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis</artifactId > <version > 3.5.8</version > </dependency > <dependency > <groupId > org.apache.logging.log4j</groupId > <artifactId > log4j-slf4j-impl</artifactId > <version > 2.17.2</version > <scope > test</scope > </dependency > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > 3.8.1</version > <scope > test</scope > </dependency > </dependencies > </project >
第二步,根据我们的数据库创建Bean类,什么?你告诉我你不知道什么事Bean类?额,你该恶补一下java了。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 public class ClassInfo { private Integer cid; private String cname; public Integer getCid () { return cid; } public void setCid (Integer cid) { this .cid = cid; } public String getCname () { return cname; } public void setCname (String cname) { this .cname = cname; } @Override public int hashCode () { final int prime = 31 ; int result = 1 ; result = prime * result + ((cid == null ) ? 0 : cid.hashCode()); result = prime * result + ((cname == null ) ? 0 : cname.hashCode()); return result; } @Override public boolean equals (Object obj) { if (this == obj) return true ; if (obj == null ) return false ; if (getClass() != obj.getClass()) return false ; ClassInfo other = (ClassInfo) obj; if (cid == null ) { if (other.cid != null ) return false ; } else if (!cid.equals(other.cid)) return false ; if (cname == null ) { if (other.cname != null ) return false ; } else if (!cname.equals(other.cname)) return false ; return true ; } @Override public String toString () { return "ClassInfo [cid=" + cid + ", cname=" + cname + "]" ; } public ClassInfo ( String cname) { this .cname = cname; } public ClassInfo (Integer cid, String cname) { super (); this .cid = cid; this .cname = cname; } public ClassInfo () { super (); } }
==注意,这个类,我实际上在本篇没用上,我会在下篇才用到,你可以自行选择创建或者不创建==
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 public class StudentInfo { private Integer sid; private String sname; private Integer cid; private String tel; private String addr; public Integer getSid () { return sid; } public void setSid (Integer sid) { this .sid = sid; } public String getSname () { return sname; } public void setSname (String sname) { this .sname = sname; } public Integer getCid () { return cid; } public void setCid (Integer cid) { this .cid = cid; } public String getTel () { return tel; } public void setTel (String tel) { this .tel = tel; } public String getAddr () { return addr; } public void setAddr (String addr) { this .addr = addr; } @Override public int hashCode () { final int prime = 31 ; int result = 1 ; result = prime * result + ((addr == null ) ? 0 : addr.hashCode()); result = prime * result + ((cid == null ) ? 0 : cid.hashCode()); result = prime * result + ((sid == null ) ? 0 : sid.hashCode()); result = prime * result + ((sname == null ) ? 0 : sname.hashCode()); result = prime * result + ((tel == null ) ? 0 : tel.hashCode()); return result; } @Override public boolean equals (Object obj) { if (this == obj) return true ; if (obj == null ) return false ; if (getClass() != obj.getClass()) return false ; StudentInfo other = (StudentInfo) obj; if (addr == null ) { if (other.addr != null ) return false ; } else if (!addr.equals(other.addr)) return false ; if (cid == null ) { if (other.cid != null ) return false ; } else if (!cid.equals(other.cid)) return false ; if (sid == null ) { if (other.sid != null ) return false ; } else if (!sid.equals(other.sid)) return false ; if (sname == null ) { if (other.sname != null ) return false ; } else if (!sname.equals(other.sname)) return false ; if (tel == null ) { if (other.tel != null ) return false ; } else if (!tel.equals(other.tel)) return false ; return true ; } @Override public String toString () { return "StudentInfo [sid=" + sid + ", sname=" + sname + ", cid=" + cid + ", tel=" + tel + ", addr=" + addr + "]" ; } public StudentInfo (Integer sid, String sname, Integer cid, String tel, String addr) { super (); this .sid = sid; this .sname = sname; this .cid = cid; this .tel = tel; this .addr = addr; } public StudentInfo () { super (); } }
第三步,我们要准备数据库配置文件 这个自行确定,账号密码请你更改为自己安装数据库的账号密码,我给出我的实例==db.properties==
1 2 3 4 driver =com.mysql.cj.jdbc.Driver url =jdbc:mysql://127.0.0.1:3306/mybatis_stu?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useOldAliasMetadataBehavior=true username =root password =123456
第四步,创建mybatis配置文件 有了上边三步的基础之后,我们需要配置mybatis-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration > <properties resource ="db.properties" > </properties > <environments default ="development" > <environment id ="development" > <transactionManager type ="JDBC" /> <dataSource type ="POOLED" > <property name ="driver" value ="${driver}" /> <property name ="url" value ="${url}" /> <property name ="username" value ="${username}" /> <property name ="password" value ="${password}" /> </dataSource > </environment > </environments > <mappers > <mapper resource ="mapper/ClassInfoMapper.xml" /> <mapper resource ="mapper/StudentInfoMapper.xml" > </mapper > </mappers > </configuration >
至此,第四步就完成。
第五步,配置映射 我想说的这上面的每一步都很重要,但是这一步也很重要,我们将相关SQL语句全部写在这里,这一步,我们其实可以说是直接对数据库进行操作,我的理解是这样的,但是不同的人有不同的理解,我也不能去把我的思想强加给别人,对吧?
==对了,如果你是初学者,我推荐你了解一下我在本项目的注释== ==ClassInfoMapper.xml==
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace ="ClassInfo" > <select id ="findAll" resultType ="com.xh.mybatis.bean.ClassInfo" > select cid,cname from classinfo; </select > <select id ="find" resultType ="com.xh.mybatis.bean.ClassInfo" > select cid,cname from classinfo where cname=#{cname}; </select > <insert id ="add" parameterType ="com.xh.mybatis.bean.ClassInfo" > insert into classinfo values(0,#{cname}); </insert > <select id ="findByVague" resultType ="com.xh.mybatis.bean.ClassInfo" > select cid,cname from classinfo where cname like '_${parameters}'; </select > <update id ="update" parameterType ="com.xh.mybatis.bean.ClassInfo" > update classinfo set cname = #{cname} where cid = #{cid}; </update > <delete id ="delete" parameterType ="com.xh.mybatis.bean.ClassInfo" > delete from classinfo where cid = #{cid}; </delete > </mapper >
另一个文件StudentInfoMapper.xml我不会在本篇使用,故而我不在本篇展示,如果你感兴趣的话,请查看下篇。
第六步,测试类的编写 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 package com.xh.mybatis;import java.io.IOException;import java.io.InputStream;import java.util.List;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.jupiter.api.Test;import com.xh.mybatis.bean.ClassInfo;public class MyTest { @Test public void test1 () throws IOException { try (InputStream reader = Resources.getResourceAsStream("mybatis-config.xml" ) ){ SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ().build(reader); System.out.println("-----------" ); SqlSession sqlSession = sqlSessionFactory.openSession(); List<ClassInfo> cls = sqlSession.selectList("ClassInfo.findAll" ); cls.forEach(System.out::println); sqlSession.close(); } } @Test public void test2 () throws IOException { try (InputStream reader = Resources.getResourceAsStream("mybatis-config.xml" )){ SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ().build(reader); SqlSession sqlSession = sqlSessionFactory.openSession(); int result = sqlSession.insert("ClassInfo.add" ,new ClassInfo ("相关" )); System.out.println(result); sqlSession.commit(); sqlSession.close(); } } @Test public void test3 () throws IOException { InputStream reader = Resources.getResourceAsStream("mybatis-config.xml" ); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ().build(reader); SqlSession sqlSession = sqlSessionFactory.openSession(); ClassInfo obj = sqlSession.selectOne("ClassInfo.find" ,"四班" ); System.out.println(obj); sqlSession.commit(); sqlSession.close(); } @Test public void test4 () throws IOException { InputStream reader = Resources.getResourceAsStream("mybatis-config.xml" ); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ().build(reader); System.out.println("-----------" ); SqlSession sqlSession = sqlSessionFactory.openSession(); List<ClassInfo> cls = sqlSession.selectList("ClassInfo.findByVague" ,"班" ); cls.forEach(System.out::println); sqlSession.commit(); sqlSession.close(); } @Test public void test5 () throws IOException { InputStream reader = Resources.getResourceAsStream("mybatis-config.xml" ); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ().build(reader); System.out.println("-----------" ); SqlSession sqlSession = sqlSessionFactory.openSession(); ClassInfo obj = new ClassInfo (); obj.setCid(1 ); obj.setCname("一班" ); int result = sqlSession.update("ClassInfo.update" ,obj); if (result > 0 ) { System.out.println("更新成功" ); }else { System.out.println("更新失败" ); } List<ClassInfo> cls = sqlSession.selectList("ClassInfo.findAll" ); cls.forEach(System.out::println); sqlSession.commit(); sqlSession.close(); } @Test public void test6 () throws IOException { InputStream reader = Resources.getResourceAsStream("mybatis-config.xml" ); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ().build(reader); SqlSession sqlSession = sqlSessionFactory.openSession(); List<ClassInfo> cls = sqlSession.selectList("ClassInfo.findAll" ); System.out.println("删除之前的数据" ); cls.forEach(System.out::println); int result = sqlSession.delete("ClassInfo.delete" ,8 ); if (result > 0 ) { System.out.println("删除成功" ); }else { System.out.println("删除失败" ); } sqlSession.commit(); System.out.println("删除之后的数据" ); List<ClassInfo> cls1 = sqlSession.selectList("ClassInfo.findAll" ); cls1.forEach(System.out::println); sqlSession.close(); } }
mybatis 注意事项:
注意:#{}是预编译处理,${}是字符替换
注意:#{}会在mybatis运行的过程中替换为?,调用PreparedStatement的set方法来赋值。
mybatis 在处理$()会将他替换为变量的值。
多使用#{}可以防止sql注入,提高系统安全性。
mybatis 推荐还是使用分页插件
一般mybatis是使用RowBounds对象进行分页,他是针对ResultSet结果集执行的内存分页,而非物理分页,可以在sql内直接书写带有物理分页的参数来完成物理分页功能,也可以使用分页插件来完成物理分页。
分页插件的原理是,在mybatis提供的接口,实现自定义插件,在将插件的拦截方法内拦截执行的sql,然后重写sql,根据dialect方言,添加对应的物理分页语句和物理分页查询。
如果使用mysql的limit的话会可能会出现数据丢失,索引失效等错误。
动态查询和动态添加以及动态修改 接上一节,注意本博客所有的代码都是建立在上一讲之上的,如果你没有上一讲的配置,这一讲代码我推荐你还是看看,直接搭建环境需要你有上一讲配置完毕,否则会出现许多错误,另外本篇并没有完成Mybatis动态SQL拼接的全部,只是举例一部分进行说明,我们在工作上或者实际生活中使用Mybatis,也是给我自己记录笔记,将来可以进行重构,然后慢慢完善。
废话不多说,我们开始:
第一步,完善上一篇没完善的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 public class StudentInfo { private Integer sid; private String sname; private Integer cid; private String tel; private String addr; private List<Integer> sidList; public List<Integer> getSidList () { return sidList; } public void setSidList (List<Integer> sidList) { this .sidList = sidList; } public StudentInfo (List<Integer> sidList) { super (); this .sidList = sidList; } public Integer getSid () { return sid; } public void setSid (Integer sid) { this .sid = sid; } public String getSname () { return sname; } public void setSname (String sname) { this .sname = sname; } public Integer getCid () { return cid; } public void setCid (Integer cid) { this .cid = cid; } public String getTel () { return tel; } public void setTel (String tel) { this .tel = tel; } public String getAddr () { return addr; } public void setAddr (String addr) { this .addr = addr; } @Override public int hashCode () { final int prime = 31 ; int result = 1 ; result = prime * result + ((addr == null ) ? 0 : addr.hashCode()); result = prime * result + ((cid == null ) ? 0 : cid.hashCode()); result = prime * result + ((sid == null ) ? 0 : sid.hashCode()); result = prime * result + ((sname == null ) ? 0 : sname.hashCode()); result = prime * result + ((tel == null ) ? 0 : tel.hashCode()); return result; } @Override public boolean equals (Object obj) { if (this == obj) return true ; if (obj == null ) return false ; if (getClass() != obj.getClass()) return false ; StudentInfo other = (StudentInfo) obj; if (addr == null ) { if (other.addr != null ) return false ; } else if (!addr.equals(other.addr)) return false ; if (cid == null ) { if (other.cid != null ) return false ; } else if (!cid.equals(other.cid)) return false ; if (sid == null ) { if (other.sid != null ) return false ; } else if (!sid.equals(other.sid)) return false ; if (sname == null ) { if (other.sname != null ) return false ; } else if (!sname.equals(other.sname)) return false ; if (tel == null ) { if (other.tel != null ) return false ; } else if (!tel.equals(other.tel)) return false ; return true ; } @Override public String toString () { return "StudentInfo [sid=" + sid + ", sname=" + sname + ", cid=" + cid + ", tel=" + tel + ", addr=" + addr + "]" ; } public StudentInfo (Integer sid, String sname, Integer cid, String tel, String addr) { super (); this .sid = sid; this .sname = sname; this .cid = cid; this .tel = tel; this .addr = addr; } public StudentInfo () { super (); } }
实体bean类,作为我们mybatis的映射文件,==注意,本篇和上篇都是简单的进行Mybatis的理解,在正式生产环境会有所差别==,本文档并没有十分规范的按照MVC模式进行,故而请各位体谅。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace ="StudentInfo" > <select id ="findAll" resultType ="com.xh.mybatis.bean.StudentInfo" > select sid,sname,cname,tel,addr from classinfo c, stuinfo s where c.cid = s.cid order by sid asc; </select > <select id ="findByValue" resultType ="com.xh.mybatis.bean.StudentInfo" > select sid,sname,tel,addr from stuinfo where sid != 0 <if test ="sname != null" > and sname = #{sname} </if > <if test ="tel != null and addr != null" > and tel = #{tel} and addr = #{addr} </if > order by sid asc; </select > <insert id ="add" parameterType ="com.xh.mybatis.bean.StudentInfo" > insert into stuinfo values(0,#{sname},#{cid},#{tel},#{addr}); </insert > <delete id ="deleteValue" parameterType ="com.xh.mybatis.bean.StudentInfo" > delete from stuinfo where cid != 0 and sid = #{sid}; </delete > <update id ="update" parameterType ="com.xh.mybatis.bean.StudentInfo" > update stuinfo set <if test ="sname != null" > sname = #{sname} </if > <if test ="cid != null" > , cid = #{cid} </if > <if test ="tel != null" > , tel = #{tel} </if > <if test ="addr != null" > , addr = #{addr} </if > where sid = #{sid}; </update > </mapper >
这里我们主要使用if语句来判断用户是否传值,如果传值,我们采用mybatis判断进行动态拼接。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 public class StuTest { @Test public void test1 () throws IOException { InputStream reader = Resources.getResourceAsStream("mybatis-config.xml" ); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ().build(reader); SqlSession sqlSession = sqlSessionFactory.openSession(); List<StudentInfo> cls = sqlSession.selectList("StudentInfo.findAll" ); cls.forEach(System.out::println); sqlSession.commit(); sqlSession.close(); } @Test public void test2 () throws IOException { InputStream reader = Resources.getResourceAsStream("mybatis-config.xml" ); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ().build(reader); SqlSession sqlSession = sqlSessionFactory.openSession(); StudentInfo stu = new StudentInfo (); stu.setSname("王老五" ); List<StudentInfo> cls = sqlSession.selectList("StudentInfo.findByValue" , stu); cls.forEach(System.out::println); sqlSession.commit(); sqlSession.close(); } @Test public void test3 () throws IOException { InputStream reader = Resources.getResourceAsStream("mybatis-config.xml" ); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ().build(reader); SqlSession sqlSession = sqlSessionFactory.openSession(); List<StudentInfo> cls = sqlSession.selectList("StudentInfo.findAll" ); cls.forEach(System.out::println); StudentInfo stu = new StudentInfo (); stu.setSname("王老五" ); stu.setCid(1 ); stu.setTel("16682187574" ); stu.setAddr("湖南长沙" ); int result = sqlSession.insert("StudentInfo.add" ,stu); if (result > 0 ) { System.out.println("添加成功" ); }else { System.out.println("添加失败" ); } sqlSession.commit(); List<StudentInfo> cls1 = sqlSession.selectList("StudentInfo.findAll" ); cls1.forEach(System.out::println); sqlSession.close(); } @Test public void test4 () throws IOException { InputStream reader = Resources.getResourceAsStream("mybatis-config.xml" ); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ().build(reader); SqlSession sqlSession = sqlSessionFactory.openSession(); List<StudentInfo> cls = sqlSession.selectList("StudentInfo.findAll" ); cls.forEach(System.out::println); StudentInfo stu = new StudentInfo (); stu.setSid(8 ); int result = sqlSession.delete("StudentInfo.deleteValue" ,stu); if (result > 0 ) { System.out.println("删除成功" ); }else { System.out.println("删除失败" ); } sqlSession.commit(); List<StudentInfo> cls1 = sqlSession.selectList("StudentInfo.findAll" ); cls1.forEach(System.out::println); sqlSession.close(); } @Test public void test5 () throws IOException { InputStream reader = Resources.getResourceAsStream("mybatis-config.xml" ); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ().build(reader); SqlSession sqlSession = sqlSessionFactory.openSession(); StudentInfo stu = new StudentInfo (); stu.setSname("王老六" ); stu.setCid(2 ); stu.setSid(9 ); stu.setTel("15973121103" ); stu.setAddr("湖南衡阳" ); int result = sqlSession.update("StudentInfo.update" ,stu); if (result > 0 ) { System.out.println("更新成功" ); }else { System.out.println("更新失败" ); } List<StudentInfo> cls = sqlSession.selectList("StudentInfo.findByValue" , stu); cls.forEach(System.out::println); sqlSession.commit(); sqlSession.close(); } }
mybatis + Spring框架 1 2 3 4 5 6 7 8 9 create database mybatis if not exists ;use mybatis; create table user ( id int (20 ) not null primary key , name varchar (30 ) default null , pwd varchar (30 ) default null )engine= innoDB default charset= utf8;
安装mybatis
1 2 3 4 5 <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis-spring</artifactId > <version > 2.0.7</version > </dependency >
快速上手
1 2 3 <bean id ="sqlSessionFactory" class ="org.mybatis.spring.SqlSessionFactoryBean" > <property name ="dataSource" ref ="dataSource" /> </bean >
1 2 3 4 5 6 7 8 9 @Configuration public class MyBatisConfig { @Bean public SqlSessionFactory sqlSessionFactory () throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean (); factoryBean.setDataSource(dataSource()); return factoryBean.getObject(); } }
注意:SqlSessionFactory
需要一个 DataSource
(数据源)。这可以是任意的 DataSource
,只需要和配置其它 Spring 数据库连接一样配置它就可以了。
假设你定义了一个如下的 mapper 接口:
1 2 3 4 public interface UserMapper { @Select("SELECT * FROM users WHERE id = #{userId}") User getUser (@Param("userId") String userId) ; }
那么可以通过 MapperFactoryBean
将接口加入到 Spring 中:
1 2 3 4 <bean id ="userMapper" class ="org.mybatis.spring.mapper.MapperFactoryBean" > <property name ="mapperInterface" value ="org.mybatis.spring.sample.mapper.UserMapper" /> <property name ="sqlSessionFactory" ref ="sqlSessionFactory" /> </bean >
需要注意的是:所指定的映射器类必须 是一个接口,而不是具体的实现类。在这个示例中,通过注解来指定 SQL 语句,但是也可以使用 MyBatis 映射器的 XML 配置文件。
配置好之后,你就可以像 Spring 中普通的 bean 注入方法那样,将映射器注入到你的业务或服务对象中。MapperFactoryBean
将会负责 SqlSession
的创建和关闭。 如果使用了 Spring 的事务功能,那么当事务完成时,session 将会被提交或回滚。最终任何异常都会被转换成 Spring 的 DataAccessException
异常。
使用 Java 代码来配置的方式如下:
1 2 3 4 5 6 7 8 @Configuration public class MyBatisConfig { @Bean public UserMapper userMapper () throws Exception { SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate (sqlSessionFactory()); return sqlSessionTemplate.getMapper(UserMapper.class); } }
要调用 MyBatis 的数据方法,只需一行代码:
1 2 3 4 5 6 7 8 9 10 11 12 public class FooServiceImpl implements FooService { private final UserMapper userMapper; public FooServiceImpl (UserMapper userMapper) { this .userMapper = userMapper; } public User doSomeBusinessStuff (String userId) { return this .userMapper.getUser(userId); } }
Mybatis-Spring MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中。它将允许 MyBatis 参与到 Spring 的事务管理之中,创建映射器 mapper 和 SqlSession
并注入到 bean 中,以及将 Mybatis 的异常转换为 Spring 的 DataAccessException
。 最终,可以做到应用代码不依赖于 MyBatis,Spring 或 MyBatis-Spring。
MyBatis-Spring
MyBatis
Spring Framework
Spring Batch
Java
2.0
3.5+
5.0+
4.0+
Java 8+
1.3
3.4+
3.2.2+
2.1+
Java 6+
初始配置文件
1 2 3 4 5 6 7 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration > </configuration >
枚举类型配置案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace ="org.apache.ibatis.submitted.rounding.Mapper" > <resultMap type ="org.apache.ibatis.submitted.rounding.User" id ="usermap" > <id column ="id" property ="id" /> <result column ="name" property ="name" /> <result column ="funkyNumber" property ="funkyNumber" /> <result column ="roundingMode" property ="roundingMode" /> </resultMap > <select id ="getUser" resultMap ="usermap" > select * from users </select > <insert id ="insert" > insert into users (id, name, funkyNumber, roundingMode) values ( #{id}, #{name}, #{funkyNumber}, #{roundingMode} ) </insert > <resultMap type ="org.apache.ibatis.submitted.rounding.User" id ="usermap2" > <id column ="id" property ="id" /> <result column ="name" property ="name" /> <result column ="funkyNumber" property ="funkyNumber" /> <result column ="roundingMode" property ="roundingMode" typeHandler ="org.apache.ibatis.type.EnumTypeHandler" /> </resultMap > <select id ="getUser2" resultMap ="usermap2" > select * from users2 </select > <insert id ="insert2" > insert into users2 (id, name, funkyNumber, roundingMode) values ( #{id}, #{name}, #{funkyNumber}, #{roundingMode, typeHandler=org.apache.ibatis.type.EnumTypeHandler} ) </insert > </mapper >
mybatis链接数据库的xml书写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <environments default ="development" > <environment id ="development" > <transactionManager type ="JDBC" /> <dataSource type ="POOLED" > <property name ="driver" value ="${driver}" /> <property name ="url" value ="${url}" /> <property name ="username" value ="${username}" /> <property name ="password" value ="${password}" /> </dataSource > </environment > </environments > <mappers > <mapper resource ="org/mybatis/example/BlogMapper.xml" /> </mappers >
链接mysql链接依赖url
1 2 <property name ="url" value ="jdbc:mysql://localhost:3306/mybatis?useSSL=true& useUnicode=true& characterEncoding=UTF-8" />