您的位置:首页 > 大数据 > 人工智能

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: