MyBatis详解 一篇就够啦
1.1 typeHandlers类型转换器

1.1.1 自定义类型转换器
[url=]
[/url]
1 package com.chenyanbin.beans; 2 3 public class Dept { 4 private Integer deptNo; 5 private String dname; 6 private String loc; 7 private boolean flag; 8 public Integer getDeptNo() { 9 return deptNo;10 }11 publicboolean isFlag() {12 return flag;13 }14 public void setFlag(boolean flag) {15 this.flag = flag;16 }17 public void setDeptNo(Integer deptNo) {18 this.deptNo = deptNo;19 }20 public String getDname() {21 return dname;22 }23 public void setDname(String dname) {24 this.dname = dname;25 }26 public String getLoc() {27 return loc;28 }29 public void setLoc(String loc) {30 this.loc = loc;31 }32 }[url=]
[/url]

[url=]
[/url]
1 package com.chenyanbin.util; 2 3 import java.sql.CallableStatement; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 8 import org.apache.ibatis.jdbc.Null; 9 import org.apache.ibatis.type.JdbcType;10 import org.apache.ibatis.type.TypeHandler;11 /*12 * setParameter:这个方法在生成SQL语句时才被调用13 * 14 * getResult:查询结束之后,在将ResultSet数据行转换为实体类对象时,通知TypeHandler将当前数据行某个字段转换为何种类型15 * 16 * 17 */18 public class MyTypeHandler implements TypeHandler {19 20 public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {21 if (parameter==null) { //dept.flag=null insertsql flag设置022 ps.setInt(i, 0);23 return;24 }25 Boolean flag=(Boolean)parameter;26 if (flag==true) {27 ps.setInt(i, 1);28 }29 else {30 ps.setInt(i, 0);31 }32 }33 34 public Object getResult(ResultSet rs, String columnName) throws SQLException {35 int flag = rs.getInt(columnName); //1 or 036 Boolean myFlag=Boolean.FALSE;37 if (flag==1) {38 myFlag=Boolean.TRUE;39 }40 return myFlag;41 }42 43 public Object getResult(ResultSet rs, intcolumnIndex) throws SQLException {44 // TODO Auto-generated method stub45 return null;46 }47 48 public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {49 // TODO Auto-generated method stub50 return null;51 }52 53 }[url=]
[/url]
[url=]
[/url]
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!-- 属性配置 --> 7 <properties resource="config.properties"></properties> 8 <!-- 别名配置 --> 9 <typeAliases>10 <package name="com.chenyanbin.beans" />11 <package name="com.chenyanbin.dao" />12 </typeAliases>13 <!-- 类型处理器 -->14 <typeHandlers>15 <!-- 从java中的Boolean转jdbc中的NUMERIC -->16 <typeHandler handler="com.chenyanbin.util.MyTypeHandler"17 javaType="Boolean" jdbcType="NUMERIC" />18 </typeHandlers>19 <!-- 环境配置 -->20 <environments default="development">21 <!-- 环境配置 -->22 <environment id="development">23 <!-- 事务管理器 -->24 <transactionManager type="JDBC"></transactionManager>25 <!-- 数据源 -->26 <dataSource type="pooled">27 <property name="driver" value="${jdbc.driver}" />28 <property name="url" value="${jdbc.url}" />29 <property name="username" value="${jdbc.username}" />30 <property name="password" value="${jdbc.password}"/>31 </dataSource>32 </environment>33 </environments>34 <!-- 映射器 -->35 <mappers>36 <package name="com.chenyanbin.dao" />37 </mappers>38 </configuration>[url=]
[/url]
1 jdbc.driver=com.mysql.jdbc.Driver2 jdbc.url=jdbc:mysql://localhost:3306/sam3 jdbc.username=root4 jdbc.password=root
[url=]
[/url]
1 package com.chenyanbin.dao; 2 3 import java.util.List; 4 import com.chenyanbin.beans.Dept; 5 6 public interfaceDeptMapper { 7 public void deptSave(Dept dept); 8 9 public List<Dept> deptFind();10 }[url=]
[/url]
[url=]
[/url]
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.chenyanbin.dao.DeptMapper"> 6 <insert id="deptSave"> 7 insert into dept (DEPTNO,DNAME,LOC,flag) 8 values(#{deptNo},#{dname},#{loc},#{flag}) 9 </insert>10 <select id="deptFind" resultType="Dept">11 select deptNo,dname,loc,flag from dept12 </select>13 </mapper>[url=]
[/url]
方式二:myBatis-config.xml
方式二:DeptMapper.xml
[url=]
[/url]
1 package com.chenyanbin.test; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.List; 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;10 import org.junit.After;11 import org.junit.Before;12 import org.junit.Test;13 import com.chenyanbin.beans.Dept;14 import com.chenyanbin.dao.DeptMapper;15 16 public class TestMain_01 {17 private SqlSession session;18 19 @Before20 public void Start() {21 try {22 InputStream inputStream = Resources.getResourceAsStream("myBatis-config.xml");23 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);24 session = factory.openSession();25 } catch (Exception e) {26 e.printStackTrace();27 }28 }29 30 @After31 public void end() {32 if (session == null) {33 session.close();34 }35 }36 37 @Test38 public voidtest01() throws IOException {39 Dept d2 = new Dept();40 d2.setDname("上海事业部");41 d2.setLoc("上海");42 d2.setFlag(false);43 session.insert("deptSave", d2);44 session.commit();45 session.close();46 }47 48 @Test49 public void test02() { 50 DeptMapper dao=session.getMapper(DeptMapper.class);51 List<Dept> deptList=dao.deptFind();52 System.out.println("ok"); 53 }54 55 }[url=]
[/url]

1.2 objectFactory 对象工厂
1.2.1 自定义对象工厂

[url=]
[/url]
1 package com.chenyanbin.beans; 2 3 public class Dept { 4 private Integer deptNo; 5 private String dname; 6 private String loc; 7 private Boolean flag; 8 private String country; 9 public String getCountry() {10 returncountry;11 }12 public void setCountry(String country) {13 this.country = country;14 }15 public Integer getDeptNo() {16 return deptNo;17 }18 public Boolean getFlag() {19 return flag;20 }21 public voidsetFlag(Boolean flag) {22 this.flag = flag;23 }24 public void setDeptNo(Integer deptNo) {25 this.deptNo = deptNo;26 }27 public String getDname() {28 return dname;29 }30 public void setDname(String dname) {31 this.dname = dname;32 }33 public String getLoc() {34 return loc;35 }36 public void setLoc(String loc) {37 this.loc = loc;38 }39 }[url=]
[/url]
[url=]
[/url]
1 package com.chenyanbin.util; 2 3 import org.apache.ibatis.reflection.factory.DefaultObjectFactory; 4 5 importcom.chenyanbin.beans.Dept; 6 7 public class MyObjectFactory extends DefaultObjectFactory { 8 9 @Override10 publicObject create(Class type) {// 重新定义Dept类实例对象创建规则,其他类实例对象创建规则不想改变11 if (Dept.class == type) {12 // 依靠父类提供create方法创建Dept对象13 Dept dept = (Dept) super.create(type);14 // 设置自定义规则15 dept.setCountry("China");16 return dept;17 }18 return super.create(type);19 }2021 }[url=]
[/url]
[url=]
[/url]
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!-- 属性配置 --> 7 <properties resource="config.properties"></properties> 8 <!-- 别名配置 --> 9 <typeAliases>10 <packagename="com.chenyanbin.beans" />11 <package name="com.chenyanbin.dao" />12 </typeAliases>13 <!-- ObjectFactory对象工厂 -->14 <objectFactory type="com.chenyanbin.util.MyObjectFactory"></objectFactory>15 <!-- 类型处理器 -->16 <!-- <typeHandlers>17 从java中的Boolean转jdbc中的NUMERIC18 <typeHandler handler="com.chenyanbin.util.MyTypeHandler"19 javaType="Boolean" jdbcType="NUMERIC" />20 </typeHandlers> -->21 <!-- 环境配置 -->22 <environments default="development">23 <!-- 环境配置 -->24 <environment id="development">25 <!-- 事务管理器 -->26 <transactionManager type="JDBC"></transactionManager>27 <!-- 数据源 -->28 <dataSource type="pooled">29 <property name="driver" value="${jdbc.driver}" />30 <property name="url" value="${jdbc.url}" />31 <property name="username" value="${jdbc.username}" />32 <property name="password" value="${jdbc.password}" />33 </dataSource>34 </environment>35 </environments>36 <!-- 映射器 -->37 <mappers>38 <package name="com.chenyanbin.dao" />39 </mappers>40 </configuration>[url=]
[/url]
1.3 Plugins 拦截器

[url=]
[/url]
1 package com.chenyanbin.util; 2 3 import java.util.Properties; 4 5 import org.apache.ibatis.executor.Executor; 6 importorg.apache.ibatis.mapping.MappedStatement; 7 import org.apache.ibatis.plugin.Interceptor; 8 importorg.apache.ibatis.plugin.Intercepts; 9 import org.apache.ibatis.plugin.Invocation;10 importorg.apache.ibatis.plugin.Plugin;11 import org.apache.ibatis.plugin.Signature;12 importorg.apache.ibatis.session.ResultHandler;13 import org.apache.ibatis.session.RowBounds;14 15 @Intercepts({ @Signature(method = "query", type = Executor.class, args = { MappedStatement.class, Object.class,16 RowBounds.class, ResultHandler.class }) })17 public class MySimpleInterceptor implements Interceptor {18 /*19 * 参数:Invocation {代理对象,被监控的方法对象,当前被监控方法运行时需要实参}20 */21 public Object intercept(Invocation invocation) throws Throwable {22 // TODO Auto-generated method stub23 System.out.println("被拦截方法执行之前,做的辅助服务。。。。。");24 Object object = invocation.proceed(); // 执行被拦截方法25 System.out.println("被拦截方法执行之后,做的辅助服务。。。。。");26 return object;27 }28 29 /*30 * 参数:target 表示被拦截的对象,应该是Executor接口实例对象 作用: 如果 被拦截的对象所在的类是有实现接口就为当前拦截对象生成一个代理对象31 * 如果被拦截的对象所在的类没有指定接口,这个对象之后的行为不会被代理操作32 */33 public Object plugin(Object target) {34 // TODO Auto-generated method stub35 return Plugin.wrap(target, this);36 }37 38 public voidsetProperties(Properties properties) {39 // TODO Auto-generated method stub40 41 }42 43 }[url=]
[/url]
[url=]
[/url]
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!-- 属性配置 --> 7 <properties resource="config.properties"></properties> 8 <!-- 别名配置 --> 9 <typeAliases>10 <packagename="com.chenyanbin.beans" />11 <package name="com.chenyanbin.dao" />12 </typeAliases>13 <!-- ObjectFactory对象工厂 -->14 <objectFactory type="com.chenyanbin.util.MyObjectFactory"></objectFactory>15 <!-- Plugins拦截器 -->16 <plugins>17 <plugin interceptor="com.chenyanbin.util.MySimpleInterceptor"></plugin>18 </plugins>19 <!-- 类型处理器 -->20 <!-- <typeHandlers> 从java中的Boolean转jdbc中的NUMERIC <typeHandler handler="com.chenyanbin.util.MyTypeHandler" 21 javaType="Boolean" jdbcType="NUMERIC" /> </typeHandlers> -->22 <!-- 环境配置 -->23 <environments default="development">24 <!-- 环境配置 -->25 <environment id="development">26 <!-- 事务管理器 -->27 <transactionManager type="JDBC"></transactionManager>28 <!-- 数据源 -->29 <dataSource type="pooled">30 <property name="driver" value="${jdbc.driver}" />31 <property name="url" value="${jdbc.url}" />32 <property name="username" value="${jdbc.username}" />33 <property name="password" value="${jdbc.password}" />34 </dataSource>35 </environment>36 </environments>37 <!-- 映射器 -->38 <mappers>39 <package name="com.chenyanbin.dao" />40 </mappers>41 </configuration>[url=]
[/url]
[url=]
[/url]
1 package com.chenyanbin.test; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.List; 6 importorg.apache.ibatis.io.Resources; 7 import org.apache.ibatis.plugin.Interceptor; 8 import org.apache.ibatis.session.SqlSession;9 import org.apache.ibatis.session.SqlSessionFactory;10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;11import org.junit.After;12 import org.junit.Before;13 import org.junit.Test;14 import com.chenyanbin.beans.Dept;15 importcom.chenyanbin.dao.DeptMapper;16 17 public class TestMain_01 {18 private SqlSession session;19 20 @Before21 public void Start() {22 try {23 InputStream inputStream = Resources.getResourceAsStream("myBatis-config.xml");24 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);25 session = factory.openSession();26 } catch (Exception e) {27 e.printStackTrace();28 }29 }30 31 @After32 public void end() {33 if (session == null) {34 session.close();35 }36 }37 38 @Test39 public voidtest01() throws IOException {40 Dept d2 = new Dept();41 d2.setDname("上海事业部");42 d2.setLoc("上海");43 d2.setFlag(false);44 session.insert("deptSave", d2);45 session.commit();46 session.close();47 }48 49 @Test50 public void test02() { 51 Interceptor ccInterceptor;52 DeptMapper dao=session.getMapper(DeptMapper.class);53 List<Dept> deptList=dao.deptFind();54 System.out.println("ok"); 55 }56 57 }[url=]
[/url]

log4j.properties
- Executor.class
- StatementHandler.class
- ParameterHandler.class
- ResultSetHandler.class
第二章 MyBatis框架Mapper配置文件详解2.1 参数(#{参数名})




2.2 #{}和${}区别




2.3 resultMap

2.4 Sql标签



第三章 MyBatis动态SQL3.1 什么是MyBatis动态SQL
3.2 动态SQL依赖标签
- if
- choose、when、otherwise
- trim、where、set
- foreach
if使用

choose、when、otherwise

when的使用

set使用

trim使用

foreach的使用






浙公网安备 33010602011771号