mybatis 使用resultMap实现数据库的操作

时间:2022-04-14 23:04:32

resultType:直接表示返回类型

resultMap:对外部resultMap的引用

二者不能同时使用

创建一个实体类Role和User

public class Role {
private Integer id;
private String roleCode;
private String roleName;
//省略set、get方法

创建User类(在User中有roleId   1对多关系)

public class User {
private Integer id;
private String userName;
private String userCode;
private String userPassword;
private Integer roleId;
private String roleName;
  //省略set、get方法

创建RoleMapper接口

public interface RoleMapper {

    public void  add(Role role);

    public void update(Role role);

    public void delete(Role role);

    public List<Role> getRoleList();

}

创建UserMapper接口

//接口的名字和xml的名字一样,这样xml中的namespace就不用改
public interface UserMapper {
//接口名的方法名一定要和xml中的id名一样
public int count(); public void add(User user); public void update(User user); public void delete(User user); public List<User> getUserList(); //根据roleid获取用户列表
public List<User> getUserByRoleId(Role role);
}

创建RoleMapper.xml

<?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="cn.bdqn.dao.RoleMapper">
<select id="getRoleList" resultType="Role">
select * from role
</select>
<insert id="add" parameterType="Role">
insert into role (roleCode,roleName)
values (#{roleCode},#{roleName})
</insert>
<update id="update" parameterType="Role">
update role set roleCode=#{roleCode},roleName=#{roleName} where id=#{id}
</update>
<delete id="delete" parameterType="Role">
delete from role where id=#{id}
</delete>
</mapper>

创建UserMapper.xml

<?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+id(方法名)的名字就可以访问 -->
<mapper namespace="cn.bdqn.dao.UserMapper">
<!-- id要唯一的,一般是下拉类的方法名 -->
<!-- 返回的是什么类型int -->
<select id="count" resultType="int">
select count(1) from user
</select>
<!-- 增加 -->
<insert id="add" parameterType="User">
insert into user(userCode,userName,userPassword)
values (#{userCode},#{userName},#{userPassword})
</insert>
<!-- 修改 -->
<update id="update" parameterType="User">
update user set userCode=#{userCode},userName=#{userName},
userPassword=#{userPassword} where id=#{id}
</update>
<!-- 删除 -->
<delete id="delete" parameterType="User">
delete from user where id=#{id}
</delete>
<!-- 查询 -->
<select id="getUserList" resultType="User">
select * from user
</select>
</mapper>

加入这个方法用resultMap

 <!-- resultMap中的id随便取,但要保证id唯一就行 -->
<!-- type指的是后台的javabean的类名 映射到哪里 -->
<resultMap type="User" id="userMap">
<!-- 显示的字段 -->
<result property="id" column="id"/>
<result property="userCode" column="userCode"/>
<result property="userName" column="userName"/>
<result property="roleName" column="roleName"/> </resultMap>
<!-- 拿roleID -->
<select id="getUserByRoleId" parameterType="User" resultMap="userMap">
select u.*,r.roleName as roleName from user u,role r where u.roleId = r.id and u.roleId=#{id}
</select>

在mybatis-config.xml中添加<mapper resource="cn/bdqn/dao/RoleMapper.xml"/>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <!-- 通过这个配置文件完成mybatis与数据库的连接 -->
<configuration>
<!-- 引入 jdbc.properties 文件-->
<properties resource="jdbc.properties"/>
<!-- alias别名 --> <!-- 配置mybatis的log实现LOG4J -->
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>
<typeAliases>
<!-- <typeAlias type="cn.bdqn.pojo.User" alias="User"/> -->
<!-- 用这个比较方便,不用一个一个写。包下的就是他的类名 -->
<package name="cn.bdqn.pojo"/>
</typeAliases> <environments default="development">
<environment id="development">
<!--配置事务管理,采用JDBC的事务管理 -->
<transactionManager type="JDBC"></transactionManager>
<!-- POOLED:mybatis自带的数据源,JNDI:基于tomcat的数据源 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments> <!-- 将mapper文件加入到配置文件中 将来mapper文件很多所以是mappers -->
<mappers>
<mapper resource="cn/bdqn/dao/UserMapper.xml"/>
<mapper resource="cn/bdqn/dao/RoleMapper.xml"/>
</mappers>
</configuration>

测试

//查询
@Test
public void getUserListTest(){
SqlSession sqlSession = null;
try {
List<User> userList = new ArrayList<User>();
sqlSession = MyBatisUtil.createSqlSession();
userList = sqlSession.selectList("cn.bdqn.dao.UserMapper.getUserList");
for(User user:userList){
logger.debug("user的id==="+user.getId());
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
sqlSession.rollback();
}finally{
MyBatisUtil.closeSqlSession(sqlSession);
}
}