1. resultType
使用要求
使用resultType进行结果映射时,需要查询出的列名和映射的对象的属性名一致,才能映射成功。
如果查询的列名和对象的属性名全部不一致,那么映射的对象为空。
如果查询的列名和对象的属性名有一个一致,那么映射的对象不为空,但是只有映射正确那一个属性才有值。
如果查询的sql的列名有别名,那么这个别名就是和属性映射的列名。
注:对简单类型的结果映射也是有要求的,查询的列必须是一列,才能映射为简单类型。
UserMapper接口
public interface UserMapper {
int findUserCount();
}
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="com.test.mapper.UserMapper">
<select id="findUserCount" resultType="int">
SELECT count(1) FROM USER
</select>
</mapper>
测试代码
@Test
public void test_06(){
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int count = userMapper.findUserCount();
System.out.println(count);
}
2. resultMap
使用要求
使用resultMap进行结果映射时,不需要查询的列名和映射的属性名必须一致。但是需要声明一个resultMap,来对列名和属性名进行映射。
对以下sql语句进行对象映射
Select id id_,username username_,sex sex_ from user where id = 1;
UserMapper接口
public interface UserMapper {
User findUserById(int id);
}
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="com.test.mapper.UserMapper">
<resultMap type="com.test.domain.User" id="mapId">
<id column="id_" property="id"/>
<result column="username_" property="username" />
<result column="sex_" property="sex" />
<result column="birthday_" property="birthday" />
</resultMap>
//注:返回结果为resultMap,resultMap值为上面resultMap标签的id
<select id="findUserById" parameterType="int" resultMap="mapId">
SELECT id id_, username username_, sex sex_, birthday birthday_ FROM USER WHERE id =#{id}
</select>
测试代码
@Test
public void test_02() throws Exception{
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.findUserById(1);
System.out.println(user);
}
注:如果返回resultMap写成resultType并应用resultMap标签的id,会报如下错误
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in mapper/UserMapper.xml
### The error occurred while processing mapper_resultMap[mapId]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'mapId'. Cause: java.lang.ClassNotFoundException: Cannot find class: mapId
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:82)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:66)
at com.test.test.Test.init(Test.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
//一下省略。。。