resultType的对于关联查询是实时查询的。
使用resultType来开发的话,就要将sql查询出来的结果映射到某个pojo中。
resultType的开发比较简单,就是查询结果是什么,就去定义一个pojo类,添加对应的属性名进行映射就OK。
resultMap对于管理查询,可以设置一些延迟加载的配置。
resultMap里配置一对一关联,主要使用到association这个标签,指定类型用javaType。
订单pojo类:
public class OrderInfos {
private String id;
private String orderNo; //订单号
private String mark; //留言(描述)
private String address; //地址(具体地址)
private String phone; //收货人电话
private Integer quantity; //数量
private Double totalSum; //订单总金额
private Date createTime; //订单生成时间
private User user;
//....get set
用户信息pojo类:
public class User {
private String id;
private String userName; //用户名
private String nickName; //昵称
private String password; //登录密码
private Integer sex; //性别
private Date birthday; //出生日期
private String province; //省
private String city; //市
private String county; //县
private Date createTime; //创建时间
//... get set
配置文件内容:
配置一对多关联,需要在resultMap里使用collection标签来实现,指定类型用ofType
订单pojo类:
public class OrderInfos {
private String id;
private String orderNo; //订单号
private String mark; //留言(描述)
private String address; //地址(具体地址)
private String phone; //收货人电话
private Integer quantity; //数量
private Double totalSum; //订单总金额
private Date createTime; //订单生成时间
private List detailList;
//... get set
订单明细pojo类:
public class OrderInfosDetail {
private String id;
private String color; //颜色
private String size; //尺寸
private Double unitPrice; //价格
private Integer quantity; //数量
private Double totalSum; //总金额
//... get set
配置文件内容:
使用extends属性设置了继承以后,配置文件内容变为:
多多对多的关联查询其实都是基于多对一,一对多的基础上进行配置的。
会了一对一、一对多,多对多也就会了。这里不再多说。
@Test
public void test() throws Exception {
//获取代理对象
OrderMapper orderMapper = (OrderMapper) context.getBean("orderMapper");
List list = orderMapper.findOrderAll();
for(OrderInfos order :list){
User user = order.getUser();
System.out.println(user.getUserName());
}
System.out.println(list);
}
}
结果:
2016-04-04 16:16:04,370 [main] DEBUG [mapper.OrderMapper.findOrderAll] - ==> Preparing: select * from tab_orderinfos
2016-04-04 16:16:04,418 [main] DEBUG [mapper.OrderMapper.findOrderAll] - ==> Parameters:
2016-04-04 16:16:04,980 [main] DEBUG [mapper.OrderMapper.findOrderAll] - <== Total: 22
2016-04-04 16:16:04,987 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4fb686]
2016-04-04 16:16:04,987 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
2016-04-04 16:16:18,278 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
2016-04-04 16:16:18,279 [main] DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@104f568 [wrapping: com.mysql.jdbc.JDBC4Connection@1bd7522]] will not be managed by Spring
2016-04-04 16:16:18,281 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Preparing: select * from tab_user where id=?
2016-04-04 16:16:18,284 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Parameters: 2c9381ed460abc1101460abddf830000(String)
2016-04-04 16:16:18,302 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - <== Total: 1
2016-04-04 16:16:18,303 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
abc
2016-04-04 16:16:30,678 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
2016-04-04 16:16:30,679 [main] DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@43690f [wrapping: com.mysql.jdbc.JDBC4Connection@1bd7522]] will not be managed by Spring
2016-04-04 16:16:30,681 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Preparing: select * from tab_user where id=?
2016-04-04 16:16:30,684 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Parameters: 2c9381ed460abc1101460abddf830000(String)
2016-04-04 16:16:30,702 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - <== Total: 1
2016-04-04 16:16:30,703 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
abc
2016-04-04 16:16:41,684 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
2016-04-04 16:16:41,685 [main] DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@160e1bb [wrapping: com.mysql.jdbc.JDBC4Connection@1bd7522]] will not be managed by Spring
2016-04-04 16:16:41,685 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Preparing: select * from tab_user where id=?
2016-04-04 16:16:41,685 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Parameters: 2c9381ed460abc1101460abddf830000(String)
2016-04-04 16:16:41,689 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - <== Total: 1
2016-04-04 16:16:41,690 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
abc
2016-04-04 16:16:41,690 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
2016-04-04 16:16:41,690 [main] DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@163eb77 [wrapping: com.mysql.jdbc.JDBC4Connection@1bd7522]] will not be managed by Spring
2016-04-04 16:16:41,690 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Preparing: select * from tab_user where id=?
2016-04-04 16:16:41,691 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Parameters: 2c9381ed460abc1101460abddf830000(String)
2016-04-04 16:16:41,695 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - <== Total: 1
2016-04-04 16:16:41,696 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
abc
2016-04-04 16:16:41,696 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
2016-04-04 16:16:41,696 [main] DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@edd5f4 [wrapping: com.mysql.jdbc.JDBC4Connection@1bd7522]] will not be managed by Spring
2016-04-04 16:16:41,697 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Preparing: select * from tab_user where id=?
2016-04-04 16:16:41,698 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Parameters: 2c9381ed460abc1101460abddf830000(String)
2016-04-04 16:16:41,702 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - <== Total: 1
2016-04-04 16:16:41,702 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
abc
2016-04-04 16:16:41,702 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
2016-04-04 16:16:41,702 [main] DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@b759cd [wrapping: com.mysql.jdbc.JDBC4Connection@1bd7522]] will not be managed by Spring
2016-04-04 16:16:41,702 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Preparing: select * from tab_user where id=?
2016-04-04 16:16:41,703 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Parameters: 2c9381ed460abc1101460abddf830000(String)
2016-04-04 16:16:41,705 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - <== Total: 1
2016-04-04 16:16:41,705 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
abc
2016-04-04 16:16:41,705 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
2016-04-04 16:16:41,705 [main] DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@1497c25 [wrapping: com.mysql.jdbc.JDBC4Connection@175982a]] will not be managed by Spring
2016-04-04 16:16:41,705 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Preparing: select * from tab_user where id=?
2016-04-04 16:16:41,706 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Parameters: 2c9381ed460abc1101460abddf830000(String)
2016-04-04 16:16:41,727 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - <== Total: 1
2016-04-04 16:16:41,727 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
abc
2016-04-04 16:16:41,728 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
2016-04-04 16:16:41,728 [main] DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@1825bfb [wrapping: com.mysql.jdbc.JDBC4Connection@175982a]] will not be managed by Spring
2016-04-04 16:16:41,728 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Preparing: select * from tab_user where id=?
2016-04-04 16:16:41,728 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - ==> Parameters: 2c9381ed460abc1101460abddf830000(String)
2016-04-04 16:16:41,731 [main] DEBUG [mapper.OrderMapper.lazyFindUserById] - <== Total: 1
2016-04-04 16:16:41,731 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
abc