mybaits中查询中对象中存在对象(可能是单一对象,可能是集合)
2017-05-15 11:22
344 查看
User和Department数据为多对一
实体对象:
User
属性:
id Integer,name String,Department department
Department
属性
id Integer,name String,sn String
mapper.xml中配置问题件
<?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.tabchanj.mybatis.domain.UserMapper"><resultMap type="com.tabchanj.mybatis.domain.User" id="UserMap"><!-- User的基本属性 --><id property="id" column="id"/><result property="name" column="name"/><!-- property:表示本类中关联的对象属性 并将关联对象的列名的别名与关联对象的属性做对应--><association property="dept" javaType="Department"><id column="dept_id" property="id" /><result column="dept_name" property="name" /><result column="dept_sn" property="sn" /></association></resultMap><select id="get" parameterType="Long" resultMap="UserMap"><!-- 此时先将department的数据一并查询出来 -->select u.*,d.name dept_name,d.sn dept_sn from t_user u LEFT JOIN t_department d on u.dept_id=d.id where u.id = #{cc}</select><select id="list" resultMap="UserMap">select u.*,d.name dept_name,d.sn dept_sn from t_user u LEFT JOIN t_department d on u.dept_id=d.id</select></mapper>
-----------Department的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.tabchanj.mybatis.domain.DepartmentMapper"><resultMap type="Department" id="DepartmentMap"/><select id="get" parameterType="Long" resultMap="DepartmentMap">select * from t_department where id = #{id}</select><select id="list" resultMap="DepartmentMap">select * from t_department</select></mapper>改进的User的Mapper.xml配置文件
<?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.tabchanj.mybatis.domain.UserMapper"><resultMap type="com.tabchanj.mybatis.domain.User" id="UserMap"><!-- User的基本属性 --><id property="id" column="id" /><result property="name" column="name" /><!--property:表示本类中关联的对象属性columnPrefix:表示关联对象的列名前都有dept_ resultMap:指明返回的类型时department类型,之一resultMap路径department的mapper映射配置文件中的resultMap的路径--><association property="dept" javaType="Department"columnPrefix="dept_"resultMap="com.tabchanj.mybatis.domain.DepartmentMapper.DepartmentMap"><!-- <id column="dept_id" property="id" /> --><!-- <result column="dept_name" property="name" /> --><!-- <result column="dept_sn" property="sn" /> --></association></resultMap><select id="get" parameterType="Long" resultMap="UserMap"><!-- 此时先将department的数据一并查询出来 -->select u.*,d.name dept_name,d.sn dept_sn from t_user u LEFT JOINt_department d on u.dept_id=d.id where u.id = #{cc}</select><select id="list" resultMap="UserMap">select u.*,d.name dept_name,d.sndept_sn from t_user u LEFT JOIN t_department d on u.dept_id=d.id</select></mapper>Department对User是一对多
-----------------department的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.tabchanj.mybatis.domain.DepartmentMapper"><resultMap type="Department" id="DepartmentMap"><id column="id" property="id" /><result column="name" property="name" /><result column="sn" property="sn" /><!--property:指明本类users属性ofType:指明users集合中装的时User类型的数据select:该查询语句还是使用的是user的mapper映射配置文件中的查询语句,从路径就可以看出column:指明该查询语句中的参数时本类中的id列的值--><collection property="users" ofType="User"select="com.tabchanj.mybatis.domain.UserMapper.getByDeptId" column="id"></collection></resultMap><select id="get" parameterType="Long" resultMap="DepartmentMap">select * fromt_department where id = #{id}</select><select id="list" resultMap="DepartmentMap">select * from t_department</select></mapper>
-------User的mapper映射配置文件(只保留了id="getByDeptId"的sql查询语句)--------------------
<?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.tabchanj.mybatis.domain.UserMapper"><resultMap type="com.tabchanj.mybatis.domain.User" id="UserMap"><!-- User的基本属性 --><id property="id" column="id" /><result property="name" column="name" /></resultMap><!-- 该语句在department的mapper映射配置文件中被引用--><select id="getByDeptId" parameterType="Long" resultMap="UserMap">select * from t_user where dept_id=#{id}</select></mapper>
相关文章推荐
- LINQ查询对象集合
- 跟据查询语句集合升成一个DataTable对象
- Hibernate中关于多表连接查询hql 和 sql 返回值集合中对象问题
- JavaScript对象、属性、事件手册集合方便查询
- JDBC和DBUtils区别(查询时jdbc只能返回ResultSet需要po转vo,dbutils返回的BeanListHandler与BeanHandler对应集合与对象)
- DataGridViewComboBoxColumn 绑定对象集合时存在BUG?
- SpringMVC查询返回集合,不能自定义字段对象的解决方法
- LINQ - 查询对象集合
- 用LINQ在集合中查询特定对象
- Hibernate中关于多表连接查询hql 和 sql 返回值集合中对象问题
- 可能存在断点,查询出连续的记录来
- JavaScript对象、属性、事件手册集合方便查询
- JavaScript对象、属性、事件手册集合方便查询
- QBC如何通过某个对象中集合的属性来查询结果集
- 查询返回的是对象集合
- 查询集合已修改;可能无法执行枚举操作
- .NET/C#中对对象集合进行查询的方法 以及相关的 Predicate<T> 及 Action<T> 的用法
- .NET/C#中对对象集合进行查询的方法 以及相关的 Predicate<T> 及 Action<T> 的用法
- Map 集合里 判断里面参数是否存在 hibernate String时间查询
- Hibernate中关于多表连接查询hql 和 sql 返回值集合中对象问题