Mybatis(5):使用sql映射文件,将返回结果封装为ResulMap
2017-12-17 11:14
519 查看
四部曲:
1.写接口 + 2.写映射sql + 3.把mapper注册到mybatis的配置文件 + 4.写单元测试和运行
今天通过使用Mybatis框架·,将MySQL查询得到的数据封装为ResulMap,并映射到实体类User.java。这个过程是通过设置<select ></select >的resultMap属性为<resultMap></resultMap>的id,从而调用了它。
下面是具体的代码:
(1)首先新建一个User.java文件,作为这次的实体类,注意一定要有setter方法(mybatis通过setter访问器进行读取):
(2)然后,新建一个接口类 UserMapper.java,定义一个新的方法 public List<User> getUserList(User user) ;:
(3)然后,新建一个UserMapper.xml文件,用来编写sql映射语句:
【业务模型传递参数到sql执行查询操作:parameterType = "com.smbms.entities.User“;】
【数据库返回的结果到业务模型,调用了 resultMap:resultMap="userlist" 】
(4)编写测试用例,导入JUnit4.jar后,可以新建测试用例UserTest.java:
由此完成一个小Demo。
注意之处:
(1)ResulMap 具有两个元素:type 和 id ,type对应结果映射的实体类,id 是Map的唯一标识;
(2)result 是 ResulMap 下的标签元素,具有两个属性:property 和 column, column 对应于数据库的列名,property 对应于实体类定义的属性变量。
1.写接口 + 2.写映射sql + 3.把mapper注册到mybatis的配置文件 + 4.写单元测试和运行
今天通过使用Mybatis框架·,将MySQL查询得到的数据封装为ResulMap,并映射到实体类User.java。这个过程是通过设置<select ></select >的resultMap属性为<resultMap></resultMap>的id,从而调用了它。
下面是具体的代码:
(1)首先新建一个User.java文件,作为这次的实体类,注意一定要有setter方法(mybatis通过setter访问器进行读取):
package com.smbms.entities; import java.util.Date; import java.util.List; public class User { private Integer id; private String userCode; private String userName; private String userPassword; private Integer gender; private Date birthday; private String phone; private String address ; private Integer userRole; private Integer createdBy; private Date creationDate; private Integer modifyBy; private Date modifyDate; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserCode() { return userCode; } public void setUserCode(String userCode) { this.userCode = userCode; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public Integer getGender() { return gender; } public void setGender(Integer gender) { this.gender = gender; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Integer getUserRole() { return userRole; } public void setUserRole(Integer userRole) { this.userRole = userRole; } public Integer getCreatedBy() { return createdBy; } public void setCreatedBy(Integer createdBy) { this.createdBy = createdBy; } public Date getCreationDate() { return creationDate; } public void setCreationDate(Date creationDate) { this.creationDate = creationDate; } public Integer getModifyBy() { return modifyBy; } public void setModifyBy(Integer modifyBy) { this.modifyBy = modifyBy; } public Date getModifyDate() { return modifyDate; } public void setModifyDate(Date modifyDate) { this.modifyDate = modifyDate; } public User(Integer id, String userCode, String userName, String userPassword, Integer gender, Date birthday, String phone, String address, Integer userRole, Integer createdBy, Date creationDate, Integer modifyBy, Date modifyDate) { super(); this.id = id; this.userCode = userCode; this.userName = userName; this.userPassword = userPassword; this.gender = gender; this.birthday = birthday; this.phone = phone; this.address = address; this.userRole = userRole; this.createdBy = createdBy; this.creationDate = creationDate; this.modifyBy = modifyBy; this.modifyDate = modifyDate; } public User() { super(); // TODO 自动生成的构造函数存根 } @Override public String toString() { return "User [id=" + id + ", userCode=" + userCode + ", userName=" + userName + ", userPassword=" + userPassword + ", gender=" + gender + ", birthday=" + birthday + ", phone=" + phone + ", address=" + address + ", userRole=" + userRole + ", createdBy=" + createdBy + ", creationDate=" + creationDate + ", modifyBy=" + modifyBy + ", modifyDate=" + modifyDate + "]"; } }
(2)然后,新建一个接口类 UserMapper.java,定义一个新的方法 public List<User> getUserList(User user) ;:
package com.smbms.dao; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Param; import com.smbms.entities.User; public interface UserMapper { public List<User> getUserList(User user); }
(3)然后,新建一个UserMapper.xml文件,用来编写sql映射语句:
【业务模型传递参数到sql执行查询操作:parameterType = "com.smbms.entities.User“;】
【数据库返回的结果到业务模型,调用了 resultMap:resultMap="userlist" 】
<?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.smbms.dao.UserMapper"> <select id="getUserList" resultMap="userlist" parameterType="com.smbms.entities.User"> select u.*,r.rolename from smbms_user u ,smbms_role r where u.username like CONCAT('%',#{username},'%') and u.userRole = #{userRole} and u.userRole = r.id </select> <resultMap type="com.smbms.entities.User" id="userlist"> <result property="id" column="id"/> <result property="userCode" column="userCode"/> <result property="userName" column="username"/> <result property="phone" column="phone"/> <result property="birthday" column="birthday"/> <result property="gender" column="gender"/> <result property="userRole" column="userRole"/> <result property="userRoleName" column="userRoleName"/> </resultMap> </mapper>
(4)编写测试用例,导入JUnit4.jar后,可以新建测试用例UserTest.java:
package com.smbms.entities; import java.io.IOException; import java.io.InputStream; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import com.smbms.dao.ProviderMapper; import com.smbms.dao.UserMapper; public class UserTest { public SqlSession getSqlSession() throws IOException{ String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream); SqlSession openSession = build.openSession(); return openSession; }
//使用resultMap完成查询结果的展现 @Test public void test06() throws IOException{ User user = new User(); user.setUserName("mmb"); user.setUserRole(110); SqlSession session = getSqlSession(); UserMapper mapper = session.getMapper(UserMapper.class); List<User> userByUsername = mapper.getUserList(user); for(User user1:userByUsername){ System.out.println("+*+:" +user 4000 1.getUserRoleName()+user1.getPhone()+user1.getUserCode()+user1.getUserName()+user1.getUserRole()); } }
}
由此完成一个小Demo。
注意之处:
(1)ResulMap 具有两个元素:type 和 id ,type对应结果映射的实体类,id 是Map的唯一标识;
(2)result 是 ResulMap 下的标签元素,具有两个属性:property 和 column, column 对应于数据库的列名,property 对应于实体类定义的属性变量。
相关文章推荐
- MyBatis笔记:使用resultMap映射sql返回的结果
- SSM框架之Mybatis SQL映射文件基本使用
- 针对IDEA使用JRebel热部署修改mybatis映射文件sql语句热部署失败的解决方案
- Hibernate之使用sql语句查询多个表,返回多种字段,将返回结果映射到自定义的普通pojo类中
- Mybatis(7):返回结果封装ResulMap的高级属性--Collection标签
- MyBatis中,传String类型参数到xml配置文件,并执行SQL语句后返回String类型的结果写法(jeesite)
- 在使用Hibernate时,因为一个查询需要更多的表连接而要使用SQL来解决性能问题。然而返回的结果集中包含了没有映射的Entity类中的表字段,在这个SQL中还有使用如何将层次关系的父子结点显示为横行
- Mybatis(6):返回结果封装ResulMap的高级属性--association标签
- MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存
- MyBatis之SQL语句映射文件增删改查和Java参数如何对应
- MyBatis学习 之 二、SQL语句映射文件(1)resultMap
- MyBatis学习 之 二、SQL语句映射文件(1)resultMap
- MyBatis学习 之 二、SQL语句映射文件(1)resultMap
- MyBatis学习 之 二、SQL语句映射文件(1)resultMap
- MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存
- MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存
- mybatis学习之二、SQL语句映射文件(2)增删改查、参数、缓存
- mybatis返回HashMap结果类型与映射
- MyBatis学习 之 二、SQL语句映射文件(1)resultMap
- MyBatis学习 之 二、SQL语句映射文件-resultMap