Mybatis最入门---ResultMaps实例篇(一对一查询)

来源:互联网 发布:淘宝三钻店铺多少钱 编辑:程序博客网 时间:2024/06/06 11:44

前面我们花了两篇文章的篇幅叙述了Mybatis中最核心的resultMap配置,本文开始,我们来演示在实际开发中,如何配置和使用resultMap提供给我们强大功能。

准备工作:

a.操作系统 :win7 x64

b.基本软件:MySQL,Mybatis,spring,SQLyog,Tomcat,web基础

特别的,作为演示程序,还请各位看官不要纠结数据库的细节内容

-----------------------------------------------------------------------------------------------------------------------------------------------------------

1.首先,我们现在数据库中建立一张用户信息表。【之前我们已经有一张sysuser表】,如下:


2.创建Mybatis04工程,工程结构图,如下:


3.修改pom文件,如下:(各位看官可以直接从前面工程复制过来即可)

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <groupId>com.java.mybatis</groupId>  
  6.     <artifactId>mybatis04</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <packaging>jar</packaging>  
  9.   
  10.     <name>mybatis01</name>  
  11.     <url>http://maven.apache.org</url>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.     </properties>  
  16.   
  17.     <dependencies>  
  18.         <dependency>  
  19.             <groupId>junit</groupId>  
  20.             <artifactId>junit</artifactId>  
  21.             <version>4.12</version>  
  22.             <scope>test</scope>  
  23.         </dependency>  
  24.         <dependency>  
  25.             <groupId>org.mybatis</groupId>  
  26.             <artifactId>mybatis</artifactId>  
  27.             <version>3.3.1</version>  
  28.         </dependency>  
  29.         <dependency>  
  30.             <groupId>mysql</groupId>  
  31.             <artifactId>mysql-connector-java</artifactId>  
  32.             <version>5.1.26</version>  
  33.         </dependency>  
  34.         <dependency>  
  35.             <groupId>log4j</groupId>  
  36.             <artifactId>log4j</artifactId>  
  37.             <version>1.2.17</version>  
  38.         </dependency>  
  39.     </dependencies>  
  40. </project>  
4.修改User.java,具体内容如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.csdn.ingo.entity;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. /** 
  6. *@author 作者 E-mail:ingo 
  7. *@version 创建时间:2016年4月17日下午6:25:27 
  8. *类说明 
  9. */  
  10. @SuppressWarnings("serial")  
  11. public class User implements Serializable{  
  12.       
  13.     private String id;  
  14.     private String password;  
  15.     private UserInfo userInfo;  
  16.       
  17.     public String getId() {  
  18.         return id;  
  19.     }  
  20.     public void setId(String id) {  
  21.         this.id = id;  
  22.     }  
  23.     public String getPassword() {  
  24.         return password;  
  25.     }  
  26.     public void setPassword(String password) {  
  27.         this.password = password;  
  28.     }  
  29.     public UserInfo getUserInfo() {  
  30.         return userInfo;  
  31.     }  
  32.     public void setUserInfo(UserInfo userInfo) {  
  33.         this.userInfo = userInfo;  
  34.     }  
  35.     public User() {  
  36.         super();  
  37.         // TODO Auto-generated constructor stub  
  38.     }  
  39.     public User(String id, String password, UserInfo userInfo) {  
  40.         super();  
  41.         this.id = id;  
  42.         this.password = password;  
  43.         this.userInfo = userInfo;  
  44.     }  
  45.     @Override  
  46.     public String toString() {  
  47.         return "User [id=" + id + ", password=" + password + ", userInfo=" + userInfo.toString() + "]";  
  48.     }  
  49. }  
5.创建UserInfo.java,具体内容如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.csdn.ingo.entity;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. /** 
  6. *@author 作者 E-mail:ingo 
  7. *@version 创建时间:2016年4月20日下午7:19:18 
  8. *类说明 
  9. */  
  10. @SuppressWarnings("serial")  
  11. public class UserInfo implements Serializable {  
  12.       
  13.     private String userid;  
  14.     private String department;  
  15.     private String position;  
  16.     private String mobile;  
  17.     private String gender;  
  18.     private String email;  
  19.     public String getUserid() {  
  20.         return userid;  
  21.     }  
  22.     public void setUserid(String userid) {  
  23.         this.userid = userid;  
  24.     }  
  25.     public String getDepartment() {  
  26.         return department;  
  27.     }  
  28.     public void setDepartment(String department) {  
  29.         this.department = department;  
  30.     }  
  31.     public String getPosition() {  
  32.         return position;  
  33.     }  
  34.     public void setPosition(String position) {  
  35.         this.position = position;  
  36.     }  
  37.     public String getMobile() {  
  38.         return mobile;  
  39.     }  
  40.     public void setMobile(String mobile) {  
  41.         this.mobile = mobile;  
  42.     }  
  43.     public String getGender() {  
  44.         return gender;  
  45.     }  
  46.     public void setGender(String gender) {  
  47.         this.gender = gender;  
  48.     }  
  49.     public String getEmail() {  
  50.         return email;  
  51.     }  
  52.     public void setEmail(String email) {  
  53.         this.email = email;  
  54.     }  
  55.     public UserInfo(String userid, String department, String position, String mobile, String gender, String email) {  
  56.         super();  
  57.         this.userid = userid;  
  58.         this.department = department;  
  59.         this.position = position;  
  60.         this.mobile = mobile;  
  61.         this.gender = gender;  
  62.         this.email = email;  
  63.     }  
  64.     public UserInfo() {  
  65.         super();  
  66.         // TODO Auto-generated constructor stub  
  67.     }  
  68.     @Override  
  69.     public String toString() {  
  70.         return "UserInfo [userid=" + userid + ", department=" + department + ", position=" + position + ", mobile="  
  71.                 + mobile + ", gender=" + gender + ", email=" + email + "]";  
  72.     }  
  73.       
  74. }  
6.修改UserDao.java,具体内容如下:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.csdn.ingo.dao;  
  2.   
  3. import com.csdn.ingo.entity.User;  
  4.   
  5. /**  
  6. *@author 作者 E-mail:ingo  
  7. *@version 创建时间:2016年4月17日下午6:26:40  
  8. *类说明  
  9. */  
  10. public interface UserDao {  
  11.     User findUserInfoById(String id);  
  12. }  
7.修改UserMapper.xml,具体内容如下;

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper  
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5. <mapper namespace="com.csdn.ingo.dao.UserDao">  
  6.     <resultMap type="User" id="UserResult">  
  7.         <id property="id" column="id"/>  
  8.         <result property="password" column="password"/>  
  9.         <result property="userInfo.userid" column="userid"/>  
  10.         <result property="userInfo.department" column="department"/>  
  11.         <result property="userInfo.position" column="position"/>  
  12.         <result property="userInfo.mobile" column="mobile"/>  
  13.         <result property="userInfo.gender" column="gender"/>  
  14.         <result property="userInfo.email" column="email"/>  
  15.     </resultMap>  
  16.     <select id="findUserInfoById" parameterType="String" resultMap="UserResult">  
  17.         select * from sysuser u,userinfo i where u.id=i.userid and u.id=#{id}  
  18.     </select>  
  19. </mapper>   
8.我们这里再给出SqlSessionFactoryUtil.java的内容,如下:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.csdn.ingo.util;  
  2.   
  3. import java.io.InputStream;  
  4.   
  5. import org.apache.ibatis.io.Resources;  
  6. import org.apache.ibatis.session.SqlSession;  
  7. import org.apache.ibatis.session.SqlSessionFactory;  
  8. import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  9.   
  10. public class SqlSessionFactoryUtil {  
  11.   
  12.     private static SqlSessionFactory sqlSessionFactory;  
  13.       
  14.     public static SqlSessionFactory getSqlSessionFactory(){  
  15.         if(sqlSessionFactory==null){  
  16.             InputStream inputStream=null;  
  17.             try{  
  18.                 inputStream=Resources.getResourceAsStream("mybatis-config.xml");  
  19.                 sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);  
  20.             }catch(Exception e){  
  21.                 e.printStackTrace();  
  22.             }  
  23.         }  
  24.         return sqlSessionFactory;  
  25.     }  
  26.       
  27.     public static SqlSession openSession(){  
  28.         return getSqlSessionFactory().openSession();  
  29.     }  
  30. }  
9。修改单元测试方法,如下:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Test  
  2.     public void testSelet() {  
  3.         UserDao userDao = sqlSession.getMapper(UserDao.class);  
  4.         String id = "admin";  
  5.         User curUser = userDao.findUserInfoById(id);  
  6.         if(curUser!=null){  
  7.             log.info("成功找到用户:"+curUser.toString());  
  8.         }  
  9.     }  

10。鉴于篇幅的关系,其他文件请参考前文基本增改删查工程中的配置。

11.测试方法:运行单元测试方法即可。观察控制台输出,如下:


------------------------------------------------------------------------------------------------------------------------------------------------------

如果有阅读过前文的观众,一定知道这种写法是我们极为不推荐的做法。下面,介绍能够重用的写法:

------------------------------------------------------------------------------------------------------------------------------------------------------
1.为了使得集合能够重用,我们把userinfo的结果集合的写法,改造成如下内容:【其他内容保持不变】

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <resultMap type="user" id="UserResult">  
  2.         <id property="id" column="id"/>  
  3.         <result property="password" column="password"/>  
  4.         <association property="userInfo" resultMap="UserInfoResult"></association>  
  5.     </resultMap>  
  6.     <resultMap type="userInfo" id="UserInfoResult">  
  7.         <id property="userid" column="userid"/>  
  8.         <result property="department" column="department"/>  
  9.         <result property="position" column="position"/>  
  10.         <result property="mobile" column="mobile"/>  
  11.         <result property="gender" column="gender"/>  
  12.         <result property="email" column="email"/>  
  13.     </resultMap>  
2.注意这里的别名配置,如果各位看官没有配置别名的话,最好使用全路径对象名。防止找不到对象,即type属性中的值

3.重新运行单元测试方法即可。

------------------------------------------------------------------------------------------------------------------------------------------------------

假设UserInfo的结果集合只与User集合关联查询,下面的写法也是正确的,如下:

------------------------------------------------------------------------------------------------------------------------------------------------------
1.把结果集合的写法,改造成如下内容:【其他内容保持不变】

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <resultMap type="user" id="UserResult">  
  2.     <id property="id" column="id" />  
  3.     <result property="password" column="password" />  
  4.     <association property="userInfo" javaType="UserInfo">  
  5.         <id property="userid" column="userid" />  
  6.         <result property="department" column="department" />  
  7.         <result property="position" column="position" />  
  8.         <result property="mobile" column="mobile" />  
  9.         <result property="gender" column="gender" />  
  10.         <result property="email" column="email" />  
  11.     </association>  
  12. </resultMap>  
【特别注意,我们这里指定了<association>中的javaType属性,没有该属性,将会导致空指针异常】

-------------------------------------------------------------------------------------------------------------------------------------------------------

上面的这些做法都是,使用user的id属性来先查询user表,userinfo表。但是在实际开发中,我们经常是对每一个表都会创建对应的增改删查功能,并且也会配置其结果集合,即resultMap。所以,实际开发时,我们推荐下面的用法,仅供参考!

-------------------------------------------------------------------------------------------------------------------------------------------------------

1.我们增加对UserInfo查询的接口,UserInfoDao.Java的具体内容如下:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public interface UserInfoDao {  
  2.     UserInfo findUserInfoById(String id);  
  3. }  
2.创建UserInfoMapper.xml,具体内容如下:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper  
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5. <mapper namespace="com.csdn.ingo.dao.UserInfoDao">  
  6.     <resultMap type="userInfo" id="UserInfoResult">  
  7.         <id property="userid" column="userid" />  
  8.         <result property="department" column="department" />  
  9.         <result property="position" column="position" />  
  10.         <result property="mobile" column="mobile" />  
  11.         <result property="gender" column="gender" />  
  12.         <result property="email" column="email" />  
  13.     </resultMap>  
  14.     <select id="findUserInfoById" parameterType="String" resultMap="UserInfoResult">  
  15.         select * from userinfo where userid=#{id}  
  16.     </select>  
  17. </mapper>   
3.修改UserMapper.xml中的resultMap配置,具体内容如下:【其他内容不变】

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <resultMap type="user" id="UserResult">  
  2.         <id property="id" column="id" />  
  3.         <result property="password" column="password" />  
  4.         <association property="userInfo" column="userid"  
  5.             select="com.csdn.ingo.dao.UserInfoDao.findUserInfoById"></association>  
  6.     </resultMap>  
4.注意,这里请将UserInfoMapper.xml加入到mybatis-config.xml中的xml文件路径中,如下:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <mappers>  
  2.     <mapper resource="mappers/UserMapper.xml" />  
  3.     <mapper resource="mappers/UserInfoMapper.xml" />  
  4. </mappers>  
或者采用包名配置即可

5.执行单元测试方法,观察控制台输出即可。

--------------------------------------------------------------------------------------------------------------------------------------------------------

至此,Mybatis最入门---ResultMaps实例篇(一对一查询)结束

0 0
原创粉丝点击