1 一对一查询
1.1 需求
查询订单信息,关联查询创建订单的用户信息
1.2 resultType
1.2.1 sql语句
确定要查询的主表:订单表 order
再确定要查询的关联表: 用户表 user
SELECT orders.*,user.`username`,user.`sex`,user.`address` FROM orders,USER WHERE orders.`user_id` = user.`id`
1.2.2 创建pojo对象
将上面的sql查询结果映射到pojo中,pojo要包含sql查询的所有字段
原始的pojo不能映射全部字段,需要新创建pojo
创建一个pojo继承包括查询字段较多的pojo类
1.2.3 mapper.xml
<!-- 查询订单 关联查询用户 -->
<select id="findOrderUser" resultType="pojo.OrderCustom">
SELECT orders.*,user.username,user.sex,user.address FROM orders,USER WHERE orders.user_id = user.id
</select>
1.2.4 mapper.java
//查询订单关联查询用户
public List<OrderCustom> findOrderUser() throws Exception;
1.2.5 测试代码
@Test
public void testFindOrderUser() throws Exception{
SqlSession sqlSession = sqlSessionFactory.openSession();
//创建代理对象
OrderMapperCustom orderMapperCustom = sqlSession.getMapper(OrderMapperCustom.class);
//调用mapper的方法
List<OrderCustom> list = orderMapperCustom.findOrderUser();
System.out.println(list);
}
1.2.6执行结果
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG [main] - Class not found: org.jboss.vfs.VFS
DEBUG [main] - JBoss 6 VFS API is not available in this environment.
DEBUG [main] - Class not found: org.jboss.vfs.VirtualFile
DEBUG [main] - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
DEBUG [main] - Using VFS adapter org.apache.ibatis.io.DefaultVFS
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
DEBUG [main] - Reader entry: Order.class
DEBUG [main] - Reader entry: OrderCustom.class
DEBUG [main] - Reader entry: User.class
DEBUG [main] - Reader entry: UserCustom.class
DEBUG [main] - Reader entry: UserQueryVo.class
DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
DEBUG [main] - Reader entry: ���� 4 L
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
DEBUG [main] - Reader entry: ���� 4 "
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
DEBUG [main] - Reader entry: ���� 4 L
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
DEBUG [main] - Reader entry: ���� 4
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
DEBUG [main] - Reader entry: ���� 4 %
DEBUG [main] - Checking to see if class pojo.Order matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pojo.OrderCustom matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pojo.User matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pojo.UserCustom matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pojo.UserQueryVo matches criteria [is assignable to Object]
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
DEBUG [main] - Reader entry: OrderMapperCustom.class
DEBUG [main] - Reader entry: OrderMapperCustom.xml
DEBUG [main] - Reader entry: UserMapper.class
DEBUG [main] - Reader entry: UserMapper.xml
DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
DEBUG [main] - Reader entry: ���� 4
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
DEBUG [main] - Reader entry: ���� 4 findUserByIdResultMap (I)Lpojo/User;
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
DEBUG [main] - Checking to see if class mapper.OrderMapperCustom matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class mapper.UserMapper matches criteria [is assignable to Object]
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1227074340.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@4923ab24]
DEBUG [main] - ==> Preparing: SELECT orders.*,user.username,user.sex,user.address FROM orders,USER WHERE orders.user_id = user.id
DEBUG [main] - ==> Parameters:
DEBUG [main] - <== Total: 4
[Order{id=6, userId=null, number='3', createtime=Thu Jun 22 21:30:02 CST 2017, note='null'}, Order{id=7, userId=null, number='6', createtime=Thu Jun 22 21:30:12 CST 2017, note='null'}, Order{id=8, userId=null, number='9', createtime=Thu Jun 22 21:30:22 CST 2017, note='null'}, Order{id=9, userId=null, number='11', createtime=Thu Jun 22 21:30:33 CST 2017, note='null'}]
Process finished with exit code 0
注意:在此处,没有在mapper.xml里配置输入参数类型,因为我们只要结果数据,所以没有配置,sql语句也是原封不动的sql查询语句,不需要传入参数所以不要更改,只需
需要创建与输出结果字段相匹配的pojo扩展类
1.3 resultMap
1.3.1 sql语句
SELECT orders.*,user.`username`,user.`sex`,user.`address` FROM orders,USER WHERE orders.`user_id` = user.`id`
1.3.2 使用resultMap进行映射的思路
使用resultMap将查询的结果的订单信息映射至Order对象中,在Order类中添加User属性,将关联查询出来的用户信息映射到Order对象的User对象属性中
1.3.3 在Order类中添加User属性
//用户信息
private User user;
同时生成get和set方法
1.3.4 mapper.xml
定义resultMap
<!-- 查询订单关联查询用户ResultMap -->
<resultMap id="OrderUserResultMap" type="Order">
<!-- 配置要映射的订单信息 -->
<id column="id" property="id" />
<result column="user_id" property="userId" />
<result column="number" property="number" />
<result column="createtime" property="createtime" />
<result column="note" property="note" />
<!-- 配置要映射的用户信息 -->
<!--
association:用户映射关联查询单个对象的信息
property:要降关联查询的用户信息映射到order中的哪个属性
javaType:pojo对象路径 可用别名
-->
<association property="user" javaType="User">
<!--
关联查询用户的唯一标识
column:用于唯一标识user信息的列
property:映射到user的哪个属性
-->
<id column="user_id" property="id" />
<result column="username" property="username" />
<result column="sex" property="sex" />
<result column="address" property="address" />
</association>
</resultMap>
定义statement <!-- 查询订单 关联查询用户 使用resultMap -->
<select id="findOrderUserResultMap" resultMap="OrderUserResultMap">
SELECT orders.*,user.username,user.sex,user.address FROM orders,USER WHERE orders.user_id = user.id
</select>
<!-- 查询订单 关联查询用户 使用resultMap -->
<select id="findOrderUserResultMap" resultMap="OrderUserResultMap">
SELECT orders.*,user.username,user.sex,user.address FROM orders,USER WHERE orders.user_id = user.id
</select>
1.3.5 mapper.java
//查询订单关联查询用户使用resultMap
public List<Order> findOrderUserResultMap() throws Exception;
1.3.6 测试代码
@Test
public void testFindOrderUserResultMap() throws Exception{
SqlSession sqlSession = sqlSessionFactory.openSession();
OrderMapperCustom orderMapperCustom = sqlSession.getMapper(OrderMapperCustom.class);
List<Order> list = orderMapperCustom.findOrderUserResultMap();
System.out.println(list);
}
1.3.7 输出结果
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG [main] - Class not found: org.jboss.vfs.VFS
DEBUG [main] - JBoss 6 VFS API is not available in this environment.
DEBUG [main] - Class not found: org.jboss.vfs.VirtualFile
DEBUG [main] - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
DEBUG [main] - Using VFS adapter org.apache.ibatis.io.DefaultVFS
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
DEBUG [main] - Reader entry: Order.class
DEBUG [main] - Reader entry: OrderCustom.class
DEBUG [main] - Reader entry: User.class
DEBUG [main] - Reader entry: UserCustom.class
DEBUG [main] - Reader entry: UserQueryVo.class
DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
DEBUG [main] - Reader entry: ���� 4 T
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
DEBUG [main] - Reader entry: ���� 4 "
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
DEBUG [main] - Reader entry: ���� 4 L
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
DEBUG [main] - Reader entry: ���� 4
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
DEBUG [main] - Reader entry: ���� 4 %
DEBUG [main] - Checking to see if class pojo.Order matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pojo.OrderCustom matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pojo.User matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pojo.UserCustom matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pojo.UserQueryVo matches criteria [is assignable to Object]
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
DEBUG [main] - Reader entry: OrderMapperCustom.class
DEBUG [main] - Reader entry: OrderMapperCustom.xml
DEBUG [main] - Reader entry: UserMapper.class
DEBUG [main] - Reader entry: UserMapper.xml
DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
DEBUG [main] - Reader entry: ���� 4
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
DEBUG [main] - Reader entry: ���� 4 findUserByIdResultMap (I)Lpojo/User;
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
DEBUG [main] - Checking to see if class mapper.OrderMapperCustom matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class mapper.UserMapper matches criteria [is assignable to Object]
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1227074340.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@4923ab24]
DEBUG [main] - ==> Preparing: SELECT orders.*,user.username,user.sex,user.address FROM orders,USER WHERE orders.user_id = user.id
DEBUG [main] - ==> Parameters:
DEBUG [main] - <== Total: 4
[Order{id=6, userId=28, number='3', createtime=Thu Jun 22 21:30:02 CST 2017, note='null'}, Order{id=7, userId=29, number='6', createtime=Thu Jun 22 21:30:12 CST 2017, note='null'}, Order{id=8, userId=30, number='9', createtime=Thu Jun 22 21:30:22 CST 2017, note='null'}, Order{id=9, userId=31, number='11', createtime=Thu Jun 22 21:30:33 CST 2017, note='null'}]
Process finished with exit code 0
1.4 resultType和resultMap实现一对一查询小结
实现一对一查询:
resultType:
使用resultType实现较为简单,如果pojo中没有包括查询的列名,需要增加对应pojo属性,即可完成映射
如果没有查询结果的特殊要求没建议使用resultType
resultMap:
使用resultMap,实现比较麻烦,如果对于查询结果有特殊的要求,可以使用resultMap完成将关联查询结果映射到pojo属性中去。
resultMap可以实现延迟加载,resultType