Java EE数据持久化框架mybatis练习——获取id值为1的角色信息。
实现要求:
获取id值为1的角色信息。
实现思路:
创建角色表sys_role所对应的实体类sysRole。
package entity;
public class SysRole {
private long id;
private String roleName;
private int enabled;
private String createBy;
private String createTime;
public SysRole() {
super();
}
public SysRole(long id, String roleName, int enabled, String createBy, String createTime) {
super();
this.id = id;
this.roleName = roleName;
this.enabled = enabled;
this.createBy = createBy;
this.createTime = createTime;
}
@Override
public String toString() {
return "SysRole [id=" + id + ", roleName=" + roleName + ", enabled=" + enabled + ", createBy=" + createBy
+ ", createTime=" + createTime + "]";
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public int getEnabled() {
return enabled;
}
public void setEnabled(int enabled) {
this.enabled = enabled;
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
}
创建操作sysRole的映射器接口RoleMapper,并在其中定义SysRole selectById(Long id)方法。
package mapper;
import entity.SysRole;
public interface RoleMapper {
SysRole selectById(long id);
}
定义RoleMapper.xml,在其中定义<select>标签,注意返回结果的resultMap映射说明。
<?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">
<!-- SysRole类的映射 -->
<mapper namespace="mapper.RoleMapper">
<resultMap type="SysRole" id="RoleMap">
<id property="id" column="id"/>
<result property="roleName" column="role_name"/>
<result property="enabled" column="enabled"/>
<result property="cretaBy" column="creta_by"/>
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
</resultMap>
<select id="selectById" resultMap="RoleMap">
select * from sys_role where id=#{id}
</select>
</mapper>
编写测试类测试方法调用。
package test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
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.BeforeClass;
import org.junit.Test;
import mapper.RoleMapper;
public class RoleMapperTest {
private static SqlSessionFactory sqlSessionFactory;
/* 完成mybatis配置的加载,创建得到SqlSessionFactory */
@BeforeClass
public static void init() {
try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/* 获取SqlSession对象,用于调用方法得到数据 */
@Test
public void testSelectAll() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
roleMapper.selectById(1L);
} finally {
// 不要忘记关闭sqlSession
sqlSession.close();
}
}
}
本文来自博客园,作者:明金同学,转载请注明原文链接:https://www.cnblogs.com/vmuu/p/15663323.html
公众号:【明金同学】