上次我们使用mybatis进行了简单的操作,今天增强了学习
一、批量操作
mybatis-config.xml配置
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 5 <!--XML必须有个根--> 6 <configuration> 7 <!--引入properties--> 8 <properties resource="jdbc.properties"/> 9 <!--环境们--> 10 <environments default="development"> 11 <!--环境--> 12 <environment id="development"> 13 <!--事务--> 14 <transactionManager type="JDBC" /> 15 <!--连接池--> 16 <dataSource type="POOLED"> 17 <!--四大--> 18 <property name="driver" value="${jdbc.driver}"/> 19 <property name="url" value="${jdbc.url}"/> 20 <property name="username" value="${jdbc.username}"/> 21 <property name="password" value="${jdbc.password}"/> 22 </dataSource> 23 </environment> 24 </environments> 25 <!--映射domain配置文件--> 26 <mappers> 27 <!--<mapper resource="cn/susu/mapper/EmployeeMapper.xml"/>--> 28 29 <!--多对一--> 30 <mapper resource="cn/liuguangbin/many2one/mapper/ProductMapper.xml"/> 31 <mapper resource="cn/liuguangbin/many2one/mapper/ProductDirMapper.xml"/> 32 <!--一对多--> 33 <!--<mapper resource="cn/liuguangbin/one2many/mapper/ProductDirMapper.xml"/> 34 <mapper resource="cn/liuguangbin/one2many/mapper/ProductMapper.xml"/>--> 35 </mappers> 36 </configuration>
类中的方法名需要和XML中配置的ID相同
1 public interface EmployeeMapper { 2 //方法名findOne与XML中的id对应 3 Employee findOne(Long id); 4 //批量插入 5 void batchSave(List<Employee> list); 6 //批量删除 7 void batchDelete(List<Long> id); 8 //高级查询 9 List<Employee> queryAll(EmloyeeQuery query); 10 //修改 11 void update(Employee employee); 12 }
1.批量添加
collection:要遍历的集合
item:集合里面的元素
separator:自动添加分隔符
1 <!--批量插入--> 2 <insert id="batchSave" parameterType="list" > 3 insert employee (name,age) values 4 <foreach collection="list" item="emp" separator=","> 5 (#{emp.name},#{emp.age}) 6 </foreach> 7 </insert>
2.批量删除
open、close 数据前后的值
1 <!--批量删除--> 2 <delete id="batchDelete" parameterType="list"> 3 delete from employee where id in 4 <foreach collection="list" item="id" separator="," open="(" close=")"> 5 #{id} 6 </foreach> 7 </delete>
3.高级查询
准备一个query类,封装高级查询的条件信息
1 private String name; 2 private Integer minAge; 3 private Integer maxAge;
1 <!--代码片段--> 2 <sql id="whereSql"> 3 /*自动将最后一个and 转为 where*/ 4 <where> 5 <if test="name!=null and name!=''"> 6 and name like concat("%",#{name},"%") 7 </if> 8 <if test="minAge!=null"> 9 and age >= #{minAge} 10 </if> 11 <if test="maxAge!=null"> 12 /* 特殊字符转译*/ 13 and <![CDATA[ age <=#{maxAge}]]> 14 </if> 15 </where> 16 </sql> 17 18 <!--高级查询--> 19 <select id="queryAll" parameterType="cn.susu.query.EmloyeeQuery" resultType="cn.susu.domain.Employee"> 20 select * from employee <include refid="whereSql"></include> 21 </select>
4.解决数据丢失的修改操作
传入什么数据,update就只修改什么数据,没有修改的数据就会成null,造成了数据丢失。
1 <!--修改解决数据丢失--> 2 <update id="update" parameterType="cn.susu.domain.Employee"> 3 update employee 4 /*自动去掉最后一个逗号*/ 5 <set> 6 <if test="name!=null and name!=''"> 7 name = #{name}, 8 </if> 9 <if test="age!=null"> 10 age=#{age}, 11 </if> 12 </set> 13 where id=#{id} 14 </update>
二、表结构(利用mybatis配置多对一)
1 public class Product { 2 3 private Long id; 4 private String productName; 5 6 private ProductDir productDir; 7 }
1 <mapper namespace="cn.liuguangbin.many2one.mapper.ProductMapper"> 2 3 <resultMap id="productMap" type="cn.liuguangbin.many2one.domain.Product"> 4 <!--用手动映射自动映射失效了--> 5 <id property="id" column="id"/> 6 <result property="productName" column="productName"/> 7 <!-- 8 嵌套结果 9 property: 属性 javaType:属性类型 10 property:属性 column:别名 11 --> 12 <association property="productDir" javaType="cn.liuguangbin.many2one.domain.ProductDir"> 13 <id property="id" column="pid"></id> 14 <result property="dirName" column="pname"></result> 15 </association> 16 </resultMap> 17 18 <!--查询--> 19 <select id="findAll" resultMap="productMap"> 20 select p.* ,pd.id pid,pd.dirName pname from product p join productdir pd on p.dir_id=pd.id 21 </select> 22 23 </mapper>
2.嵌套查询
1 <!-- 2 嵌套查询:依然需要做结果映射 3 column="department_id" :列名(待会执行sql需要传入的数据) 4 select:去获取相应的SQL 5 --> 6 <resultMap id="employeeMap" type="cn.itsource._03_many2one.domain.Employee"> 7 <id column="id" property="id" /> 8 <result column="name" property="name" /> 9 <result column="age" property="age" /> 10 <association property="department" javaType="cn.itsource._03_many2one.domain.Department" 11 column="department_id" select="cn.itsource._03_many2one.mapper.DepartmentMapper.findOne" 12 ></association> 13 </resultMap>
三、表结构(mybatis创建一对多)
1.嵌套查询
1 <mapper namespace="cn.liuguangbin.one2many.mapper.ProductDirMapper"> 2 3 <!--手动映射--> 4 <resultMap id="productDirMapper" type="cn.liuguangbin.one2many.domain.ProductDir"> 5 <!--手动映射自动的失效--> 6 <id property="id" column="id"></id> 7 <result property="dirName" column="dirName"></result> 8 <!-- 9 一对多嵌套查询 10 property:属性 ofType 类 11 column:传入关联表的值 12 select:嵌套查询 13 --> 14 <collection property="list" ofType="cn.liuguangbin.one2many.domain.Product" 15 column="id" select="cn.liuguangbin.one2many.mapper.ProductMapper.findOne" 16 ></collection> 17 </resultMap> 18 19 <select id="findAll" parameterType="list" resultMap="productDirMapper"> 20 select * from productdir 21 </select>
2.嵌套结果
1 <mapper namespace="cn.itsource._04_one2many.mapper.DepartmentMapper"> 2 3 <resultMap id="departmentMap" type="cn.itsource._04_one2many.domain.Department"> 4 <id column="did" property="id" /> 5 <result column="dname" property="name" /> 6 <collection property="employees" ofType="cn.itsource._04_one2many.domain.Employee"> 7 <id column="eid" property="id" /> 8 <result column="ename" property="name" /> 9 <result column="age" property="age" /> 10 </collection> 11 </resultMap> 12 13 <!--嵌套结果:先用一条sql查出来,再设置--> 14 <select id="findAll" resultMap="departmentMap"> 15 SELECT d.id did,d.name dname,e.id eid,e.name ename,e.age FROM department d 16 LEFT JOIN employee e ON d.id = e.department_id 17 </select>
四、缓存
sqlSessionfactory:重量级,有二级缓存(需要配置)、连接池等
配置二级缓存
1 <mapper namespace="cn.it._05_cache.EmployeeMapper"> 2 <!--开启二级缓存(二级缓存的对象一定要序列化)--> 3 <cache /> 4 5 <select id="findOne" parameterType="long" resultType="cn.it_05_cache.Employee"> 6 SELECT * FROM employee WHERE id= #{id} 7 </select> 8 9 </mapper>
sqlSession:轻量级,有二级缓存(自带)