MyBatis动态查询
resultMap类型标签
查询的别名:
(例如:数据库表中列名带有下划线,但是实体类一般是驼峰式命名,因此可以通过在映射文件中起别名解决。)
主要针对数据表中的字段和实体类中的属性名称不一致,通过在SQL中的as 别名方式,将表中的查询的结课记录的字段名,重新命名为实体类中的属性名。
<select id="findAll" resultType="user">
select userid , acctName as loginName , password , name from user
</select>
在 select 标签中使用 resultMap 简单来说是自定义的,要先定义类型。同时 resultMap 可以实现将查询结果映射为复杂类型的 pojo,比如在查询结果映射对象中包括 pojo 和 list 实现一对一查询和一对多查询。
<resultMap id="result" type="user">
<id column="userId" property="userId"/>
<result column="acctName" property="loginName"/>
<result column="password" property="password"/>
<result column="name" property="name"/>
<result column="job" property="jobName"/>
</resultMap>
id 标签:不可重复,用于指定主键字段result 标签
column 属性:用于指定数据库中表的列名
property 属性:用于指定实体类属性名
- 配置映射
<!-- 配置查询所有操作 -->
<select id="findAll" resultMap="result">
select * from user
</select>
resultMap=“result”,result就是上面自定义resultMap的id名
MyBatis动态查询映射标签
- 1.< sql>
- 2.< if>
- 3.< where>
- 4.< foreach>
- 5.< set>
- 6.< trim>
- 7.< choose>< when>
1.< sql>标签:
<!-- SQL语句的重用 -->
<sql id="baselist">
select userid,acctName,name,password,job
</sql>
<select id="findAll" resultMap="result">
<include refid="baselist">