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="tk.mybatis.simple.mapper.UserMapper">
<resultMap id="userMap" type="SysUser">
<id property="id" column="id"/>
<result property="userName" column="user_Name"/>
<result property="userPassword" column="userPassword"/>
<result property="userEmail" column="userEmail"/>
<result property="userInfo" column="userInfo"/>
<result property="headImg" column="headImg" jdbcType="BLOB"/>
<result property="createTime" column="createTime" jdbcType="TIMESTAMP"/>
</resultMap>
<select id="selectByid" resultMap="userMap">
select * from sys_user where id =#{id}
</select>
<select id="selectAll" resultType="tk.mybatis.simple.model.SysUser">
select * from sys_user
</select>
</mapper>
mapper节点中需要指定namespace="tk.mybatis.simple.mapper.UserMapper"
resultMap:用于设置返回值的类型和映射关系
<select> : 映射查询语句使用的标签。
id :命名空间中的唯 标识符,可用来代表这条语句。
'# {id} :MyBatis SQL 中使用预编译参数的一种方式,大括号中的 id 是传入的参数名
image.png
如果传入的是实体类那必须和实体类中字段一致才行,否则报错
image.png
result: 列结果
resultMap节点中的result标签 property表示java属性, column表示数据库字段, jdbcType表示列对应的数据库类型。在下面两个select标签, 一个是根据用户id查询用户,其返回值就是resultMap映射
<select id="selectByid" resultMap="userMap">
select * from sys_user where id =#{id}
</select>
下面一个是根据设置mybatis的自动映射字段成
<select id="selectAll" resultType="SysUser">
select * from sys_user
</select>
BaseMapperTest.java
package simple;
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 java.io.IOException;
import java.io.Reader;
public class BaseMapperTest {
private static SqlSessionFactory sqlSessionFactory;
@BeforeClass
public static void init() {
try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();
} catch (IOException ignore) {
ignore.printStackTrace();
}
}
public SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
@Test
public void testUserSelectById() {
SqlSession sqlSession = getSqlSession();
try {
SysUser sysUser = sqlSession.selectOne("tk.mybatis.simple.mapper.UserMapper.selectByid", 1);
System.out.println(sysUser.getUserName());
} finally {
sqlSession.close();
}
}
image.png
在调用的时候是直接指定名称, 当然还有一种不需要指定名称的方式, 那就是getMapper的方式
@Test
public void testUserSelectById2() {
SqlSession sqlSession = getSqlSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
SysUser sysUser = userMapper.selectByid(Long.valueOf(1));
System.out.println(sysUser.getUserName());
} finally {
sqlSession.close();
}
}
@Test
public void testUserSelectAll2() {
SqlSession sqlSession = getSqlSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<SysUser> sysUsers = userMapper.selectAll();
if (sysUsers != null) {
for (SysUser user : sysUsers) {
System.out.println(user.getUserName());
}
}
} finally {
sqlSession.close();
}
}
网友评论