MyBatis的查询
查询一个实体类对象
<!--User getUserById(@Param("id") int id);--> <select id="getUserById" resultType="User"> select * from t_user where id = #{id} </select>
查询一个List集合
<!--List<User> getAllUser();--> <select id="getAllUser" resultType="User"> select * from t_user </select>
查询单个数据
<!-- * 在MyBatis中,对于Java中常用的类型都设置了类型别名 * 例如:java.lang.Integer->int|integer * 例如:int->_int|_integer * 例如:Map->map,List->list --> <!--int getCount();--> <select id="getCount" resultType="int"> select count(1) from t_user </select>
查询一条数据为map集合
<!--Map<String,Object> getUserById(@Param("id") int id);--> <select id="getUserById" resultType="map"> select * from t_user where id = #{id} </select>
5. 查询多条数据为map
第一种方式:
<!--List<Map<String,Object>> getAllUser();--> <select id="getAllUser" resultType="map"> select * from t_user </select> <!--将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合,此 时可以将这些map放在一个list集合中获取-->
第二种方式
<!--Map<String,Object> getAllUser();--> <select id="getAllUser" resultType="map"> select * from t_user </select> <!-- 将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合, 并且最终要以一个map的方式返回数据,此时需要通过@MapKey注解设置map集合的键,值是每条数据所对应的 map集合 -->
@MapKey("id") Map<String,Object> getAllUser(); /* 查询结果 { 1={password=123, sex=女, id=1, age=23, email=11222@qq.com, username=admin}, 2={password=123, sex=女, id=2, age=23, email=11222@qq.com, username=admin}, 3={password=123456, sex=女, id=3, age=18, email=111@qq.com, username=admin} } */
自定义sql的执行
模糊查询
<!--List<User> testMohu(@Param("mohu") String mohu);--> <select id="testMohu" resultType="User"> <!--select * from t_user where username like '%${mohu}%'--> <!--select * from t_user where username like concat('%',#{mohu},'%')--> select * from t_user where username like "%"#{mohu}"%" </select>
批量删除
<!--int deleteMore(@Param("ids") String ids);--> <delete id="deleteMore"> delete from t_user where id in (${ids}) </delete>
动态设置表名
<!--List<User> getAllUser(@Param("tableName") String tableName);--> <select id="getAllUser" resultType="User"> select * from ${tableName} </select>
添加功能获取自增的主键
<!--int insertUser(User user);--> <insert id="insertUser" useGeneratedKeys="true" keyProperty="id"> insert into t_user values(null,#{username},#{password},#{age},#{sex}) </insert>
User user = new User(-1, "admin", "123456", 18, "女", "111@qq.com"); userMapper.insertUser(user); System.out.println(user); /* User{id=7, username='admin', password='123456', age=18, sex='女', email='111@qq.com'} */
添加功能获取uuid的主键
<!--int insertUser(User user);--> <insert id="insert" parameterType="User"> <selectKey keyProperty="id" resultType="java.lang.String" order="BEFORE"> select uuid() </selectKey> insert into t_user values(#{id},#{username},#{password},#{age},#{sex}) </insert>
属性和字段名不一致
mybatis-config.xml添加
数据库字段为 emp_name,属性就为 empName
<settings> <!--将表中字段的下划线自动转换为驼峰--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
通过sql起别名
<!--List<Emp> getAllEmp();--> <select id="getAllEmp" res="empResultMap"> select eid, emp_name empName,did from t_emp </select>
resultMap处理字段和属性的映射关系
<resultMap id="empResultMap" type="Emp"> <id property="eid" column="eid"></id> <result property="empName" column="emp_name"></result> <result property="did" column="did"></result> </resultMap> <!--List<Emp> getAllEmp();--> <select id="getAllEmp" resultMap="empResultMap"> select * from t_emp </select>
多对一映射处理
级联方式处理映射关系
<resultMap id="getAllEmpDeptMap" type="Emp"> <id property="eid" column="eid"></id> <result property="empName" column="emp_name"></result> <result property="dept.did" column="did"></result> <result property="dept.deptName" column="dept_name"></result> </resultMap> <!--List<Emp> getAllEmpDept(@Param("eid") int eid);--> <select id="getAllEmpDept" resultMap="getAllEmpDeptMap"> select * from t_emp e left join t_dept d on e.did = d.did where e.eid = #{eid} </select>
Emp.java
public class Emp { private int eid; private String empName; private Dept dept; }
association处理映射关系
<resultMap id="getAllEmpDeptMap2" type="Emp"> <id property="eid" column="eid"></id> <result property="empName" column="emp_name"></result> <association property="dept" javaType="Dept"> <id property="did" column="did"></id> <result property="deptName" column="dept_name"></result> </association> </resultMap> <!--List<Emp> getAllEmpDept(@Param("eid") int eid);--> <select id="getAllEmpDept" resultMap="getAllEmpDeptMap2"> select * from t_emp e left join t_dept d on e.did = d.did where e.eid = #{eid} </select>
分步查询
EmpMapper.xml(员工)
<resultMap id="empDeptStepMapOne" type="Emp"> <id property="eid" column="eid"></id> <result property="empName" column="emp_name"></result> <association property="dept" select="com.atguigu.mybatis.mapper.DeptMapper.getEmpDeptByStep" column="did"> </association> </resultMap> <!--List<Emp> getAllEmpDept(@Param("eid") int eid);--> <select id="getAllEmpDept" resultMap="empDeptStepMapOne"> select * from t_emp e where e.eid = #{eid} </select>
DeptMapper.xml(部门)
<!--Dept getEmpDeptByStep(@Param("did") int did);--> <select id="getEmpDeptByStep" resultType="Dept"> select * from t_dept where did=#{did} </select>
延迟加载
<settings> <!--将表中字段的下划线自动转换为驼峰--> <setting name="mapUnderscoreToCamelCase" value="true"/> <!--开启延迟加载--> <setting name="lazyLoadingEnabled" value="true"/> </settings>
lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。
注:例如上边分步查询中,调用了 emp.getDepe()才会去查询关联的对象,不调用默认只查询emp对象
aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。 否则,每个属性会按需加载此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。
此时可通过 association 和 collection 中的fetchType属性设置当前的分步查询是否使用延迟加载,fetchType=“lazy(延迟加载)|eager(立即加载)”
一对多映射处理
collection
<resultMap id="deptEmpMap" type="Dept"> <id property="did" column="did"></id> <result property="deptName" column="dept_name"></result> <!--ofType:集合中的类型--> <collection property="emps" ofType="Emp"> <id property="eid" column="eid"></id> <result property="empName" column="emp_name"></result> </collection> </resultMap> <!--Dept getDeptById(@Param("did") int did);--> <select id="getDeptById" resultMap="deptEmpMap"> select * from t_dept d left join t_emp e on d.did=e.did where d.did=#{did} </select>
结果
一对多查询(只查询关联id)
@Data @Accessors(chain = true) public class BookListBO { private Long bookListId; private List<Long> book; }
xml
<resultMap id="queryBookListIdListMap" type="com.syzton.book.list.bo.BookListBO"> <id property="bookListId" column="list_id"></id> <collection property="bookIdList" ofType="Long"> <result column="book_id" jdbcType="TINYINT"></result> </collection> </resultMap> <!-- 查询书单id和图书id集合 --> <select id="queryBookListIdList" resultMap="queryBookListIdListMap"> SELECT bl.list_id, bil.book_id FROM book_list bl LEFT JOIN book_inlist bil ON bl.list_id = bil.book_list_id WHERE FIND_IN_SET(bl.list_id, #{bookListIds}) > 0 </select>