一对一映射:
例如:我们根据订单关联查询用户信息
sql代码如下:
SELECT orders.`id`, orders.`user_id`, orders.`number`, user.`username`, user.`sex` FROM orders, USER WHERE orders.`user_id` = user.`id` 我们在前面学习的时候,知道,mybatis的映射结果又两种,一种是resultType的映射,另一种是resultMap的映射resultType结果映射 因为订单类中没有用户的属性,所以根据我们的需要包装Orders类,如下所示:
public class OrdersExt extends Orders{ private String username; private String sex; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }sqlMapper.xml配置: 其使用了别名来保持其与属性名相同 --> <select id="findOrdersAndRt" resultType="com.study.first.OrdersExt"> SELECT orders.`id`, orders.`user_id` userId, orders.`number`, user.`username`, user.`sex` FROM orders, USER WHERE orders.`user_id` = user.`id` </select> 测试: @Test public void test9() throws Exception{ String resource="SqlMapConfig.xml"; //读取配置文件 InputStream inputStream = Resources.getResourceAsStream(resource); //建立sqlsessionFactory SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream); //创建session SqlSession session=sqlSessionFactory.openSession(); //使用代理方式 OrderExtMapper mapper = session.getMapper(OrderExtMapper.class); System.out.println(mapper.findOrdersAndRt()); //提交事物 session.commit(); session.close(); }resultMap结果映射:
首先,设置resultMap:
包装orders类:
public class OrdersExt extends Orders{ private User user; //一对一用户信息 public User getUser() { return user; } public void setUser(User user) { this.user = user; } }resultMap 配置:
<resultMap type="com.study.first.OrdersExt" id="OrdersAndUserRstMap"> <id column="id" property="id"/> <result column="user_Id" property="userId"/> <result column="number" property="number"/> <!-- 用户信息一对一映射 association:表示一对一映射 property:表示其要映射的属性名, javaType:表示其对应的java类型 result:column表示要映射的那个列,property表示其要映射到那个属性 id可以不写,但会影响性能 --> <association property="user" javaType="com.study.mpo.User" > <id column="user_Id" property="id"/> <result column="username" property="username"/> <result column="sex" property="sex"/> </association> </resultMap>
sqlMapper映射改写为ResultMap结果映射
<select id="findOrdersAndUserRstMap" resultMap="OrdersAndUserRstMap"> SELECT orders.`id`, orders.`user_id`, orders.`number`, user.`username`, user.`sex` FROM orders, USER WHERE orders.`user_id` = user.`id` </select> 测试: public void test7() throws Exception{ String resource="SqlMapConfig.xml"; //读取配置文件 InputStream inputStream = Resources.getResourceAsStream(resource); //建立sqlsessionFactory SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream); //创建session SqlSession session=sqlSessionFactory.openSession(); //调用session的增删改查 OrderExtMapper mapper = session.getMapper(OrderExtMapper.class); System.out.println(mapper.findOrdersAndUserRstMap()); //提交事物 session.commit(); session.close(); } 一对多映射:订单关联查询订单详情,orders包装
public class OrdersExt extends Orders{ private List<Orderdetail> detailList; //订单详情明细 } public List<Orderdetail> getDetailList() { return detailList; } public void setDetailList(List<Orderdetail> detailList) { this.detailList = detailList; } }resultMap书写
<!-- 一对多映射 extends:继承,注意,继承时,只能继承type相同的 collection表示其一对多映射 property:映射的属性值 ofType:对应的单条语句的类型 --> <resultMap type="com.study.first.OrdersExt" id="OrdersAndOrderDetailResMap" extends="OrdersAndUserRstMap"> <collection property="detailList" ofType="com.study.mpo.Orderdetail"> <id column="detailid" property="id"/> <result column="items_id" property="itemsId"/> <result column="items_num" property="itemsNum"/> </collection> </resultMap> sqlMapper映射; <select id="findOrdersAndDetailRstMap" resultMap="OrdersAndOrderDetailResMap"> SELECT orders.`id`,orders.`number`,orderdetail.`id` detailid,orderdetail.`orders_id` ,orderdetail.`items_id` FROM orders,orderdetail WHERE orderdetail.`orders_id`=orders.`id` </select> 测试; public void test8() throws Exception{ String resource="SqlMapConfig.xml"; //读取配置文件 InputStream inputStream = Resources.getResourceAsStream(resource); //建立sqlsessionFactory SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream); //创建session SqlSession session=sqlSessionFactory.openSession(); //调用session的增删改查 OrderExtMapper mapper = session.getMapper(OrderExtMapper.class); System.out.println(mapper.findOrdersAndDetailRstMap().size()); //提交事物 session.commit(); session.close(); }