mybatis基本使用(查询)
Select 元素的属性:
id 在命名空间中唯一的标识符,可以被用来引用这条语句。 parameterType 将会传入这条语句的参数的类全限定名或别名。这个属性是可选的,因为 MyBatis可以通过类型处理器(TypeHandler)推断出具体传入语句的参数,默认值为未设置(unset) resultType 期望从这条语句中返回结果的类全限定名或别名。 注意,如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身的类型。 resultType 和 resultMap 之间只能同时使用一个。 resultMap 对外部 resultMap 的命名引用。结果映射是 MyBatis 最强大的特性,如果你对其理解透彻,许多复杂的映射问题都能迎刃而解。 resultType 和 resultMap 之间只能同时使用一个。 flushCache 将其设置为 true 后,只要语句被调用,都会导致本地缓存和二级缓存被清空,默认值:false。 useCache 将其设置为 true 后,将会导致本条语句的结果被二级缓存缓存起来,默认值:对 select 元素为 true。 statementType 使用存储过程才会设置CALLABLE
多对一查询:
<?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="com.vn.vnmybatis.mapper.EmpMapper"> <!-- 通用查询结果列--> <sql id="Base_Column_List"> id , username, age,gmt_create,gmt_modified </sql> <!-- 多对一查询 : 第一种方法 --> <resultMap id="EmpFirstResultMap" type="com.vn.vnmybatis.dto.EmpDTO"> <id column="id" property="id"></id> <result column="user_name" property="userName"></result> <result column="create_date" property="createDate"></result> <result column="dept_id" property="deptId"></result> <!-- 对象属性名.属性 --> <result column="dept_id" property="dept.id"></result> <result column="dept_name" property="dept.deptName"></result> </resultMap> <!-- 多对一查询 : 第二种方法 --> <resultMap id="EmpSecondResultMap" type="com.vn.vnmybatis.dto.EmpDTO"> <id column="id" property="id"></id> <result column="user_name" property="userName"></result> <result column="create_date" property="createDate"></result> <result column="dept_id" property="deptId"></result> <!-- association 映射多对一查询 property 指定的 “一” javaType 类型(自定义映射需要指定类型) columnPrefix="dept_" 去掉前缀 dept_ association 的使用:强行使我们的结果 多对一; 当数据不是 多对一 也会强制映射为多对一 无论使用 collection 或者 association 都必须查询 id(指 select 查询时,必须查询id); 否则mybatis将无法正常组织数据 --> <association property="dept" javaType="com.vn.vnmybatis.entity.Dept"> <id column="id" property="id"></id> <result column="dept_name" property="deptName"></result> <!-- <result column="dept_id" property="id"></result> 加下面的 主要是为了解决 emp 和 dept 都需要查出各自id的问题 --> <result column="dept_id" property="id"></result> </association> </resultMap> <!-- 多对一查询 : 第三种方法 --> <resultMap id="EmpThreeResultMap" type="com.vn.vnmybatis.dto.EmpDTO"> <id column="id" property="id"></id> <result column="user_name" property="userName"></result> <result column="create_date" property="createDate"></result> <result column="dept_id" property="deptId"></result> <!-- association 映射多对一查询 resultMap 调用已存在的映射,重用resultMap columnPrefix="dept_" 去掉前缀 dept_ association 的使用:强行使我们的结果 多对一; 当数据不是 多对一 也会强制映射为多对一 无论使用 collection 或者 association 都必须查询 id(指 select 查询时,必须查询id); 否则mybatis将无法正常组织数据 --> <!-- columnPrefix="dept_" 主要为解决 dept 和 emp 都要查出各自的 id 而用 --> <association property="dept" columnPrefix="dept_" resultMap="com.vn.vnmybatis.mapper.DeptMapper.BaseResultMap"> </association> </resultMap> <select id="selectEmp" resultMap="EmpFirstResultMap"> SELECT e.id, e.user_name, e.create_date, d.dept_name, d.id as dept_id FROM DEPT d LEFT JOIN EMP e ON d.id = e.dept_id </select> </mapper>
一对多查询:
<?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="com.vn.vnmybatis.mapper.DeptMapper"> <resultMap id="BaseResultMap" type="com.vn.vnmybatis.entity.Dept"> <id column="id" property="id"></id> <result column="dept_name" property="deptName"></result> </resultMap> <!-- 通用查询结果列--> <sql id="Base_Column_List"> id, deptName </sql> <!-- 一对多查询:第一种方法 --> <resultMap id="DeptFirstResultMap" type="com.vn.vnmybatis.dto.DeptDTO"> <id column="id" property="id"></id> <result column="dept_name" property="deptName"></result> <!-- collection 指定 “多”的一方的属性 映射方式: 1、自定义映射 ofType 2、resultMap 复用方式 无论使用 collection 或者 association 都必须查询 id(指 select 查询时,必须查询id); 否则mybatis将无法正常组织数据 --> <collection property="emps" ofType="com.vn.vnmybatis.entity.Emp"> <id column="e_id" property="id"></id> <result column="user_name" property="userName"></result> <result column="create_date" property="createDate"></result> <result column="dept_id" property="deptId"></result> </collection> </resultMap> <!-- 一对多查询:第二种方法 --> <resultMap id="DeptSecodResultMap" extends="BaseResultMap" type="com.vn.vnmybatis.dto.DeptDTO"> <!-- collection 指定 “多”的一方的属性 映射方式: 1、自定义映射 ofType 2、resultMap 复用方式 无论使用 collection 或者 association 都必须查询 id(指 select 查询时,必须查询id); 否则mybatis将无法正常组织数据 --> <collection property="emps" ofType="com.vn.vnmybatis.entity.Emp"> <id column="e_id" property="id"></id> <result column="user_name" property="userName"></result> <result column="create_date" property="createDate"></result> <result column="dept_id" property="deptId"></result> </collection> </resultMap> <select id="selectDept" resultMap="DeptSecodResultMap"> SELECT d.*,e.user_name,e.id as e_id,e.create_date,e.dept_id FROM dept d LEFT JOIN emp e ON d.id = e.dept_id </select> <!-- 嵌套查询:一对多查询 --> <resultMap id="NestDeptResultMap" type="com.vn.vnmybatis.dto.DeptDTO"> <id column="id" property="id"></id> <result column="dept_name" property="deptName"></result> <!-- 区别: 1、相比于 collection 一对多查询,嵌套查询编写更麻烦 2、在某些分页场景中,由于mybatis会去重新组织嵌套结果数据,会导致分页错误;此时就需要使用 桥套查询 3、嵌套查询,支持懒加载(延迟查询),在嵌套结果中是不支持(join) --> <collection property="emps" ofType="com.vn.vnmybatis.entity.Emp" column="id" select="com.vn.vnmybatis.mapper.DeptMapper.selectEmps"> </collection> </resultMap> <select id="selectEmps" resultType="com.vn.vnmybatis.entity.Emp"> SELECT * FROM Emp where dept_id = #{id} </select> <select id="selectDeptAndEmp" resultMap="NestDeptResultMap"> SELECT * FROM dept </select> </mapper>

浙公网安备 33010602011771号