处理使用resultType之外,也可以使用resultMap
,即当我们java类的属性名和数据库的名字不一致的时候可以一用。
1.resultMap解决属性值和表列名不一致的情况
1.1 StudentDao
public MyStudent selectStudentResultMap(Integer id);
1.2 StudentDao.xml
<resultMap id="myStudent" type="com.yuhl.domain.MyStudent">
<id property="myid" column="id"/>
<result property="myname" column="name"/>
<result property="myage" column="age"/>
<result property="myemail" column="email"/>
</resultMap>
<!-- ResultMap 表中类名和类中属性名不一致的时候-->
<select id="selectStudentResultMap" resultMap="myStudent">
select id,name,email,age from student where id = #{id}
</select>
1.3 测试
@Test
public void test14() {
SqlSession sqlsession = MybatisUtil.getSqlsession();
StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
//可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
MyStudent myStudent = studentDao.selectStudentResultMap(1001);
System.out.println(myStudent);
}
1.4 测试结果
Checking to see if class com.yuhl.vo.StudentVo matches criteria [is assignable to Object]
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 188576144.
==> Preparing: select id,name,email,age from student where id = ?
==> Parameters: 1001(Integer)
<== Columns: id, name, email, age
<== Row: 1001, 张三, zhangsan@qq.com, 20
<== Total: 1
MyStudent{myid=1001, myname='张三', myemail='zhangsan@qq.com', myage=20}
2.属性值和表列名不一致第二种解决方案as
2.1 StudentDao
public MyStudent selectStudentResultMap2(Integer id);
2.2 StudentDao.xml
<resultMap id="myStudent" type="com.yuhl.domain.MyStudent">
<id property="myid" column="id"/>
<result property="myname" column="name"/>
<result property="myage" column="age"/>
<result property="myemail" column="email"/>
<select id="selectStudentResultMap2" resultType="com.yuhl.domain.MyStudent">
select id as myid,name as myname,email as myemail,age as myage from student where id = #{id}
</select>
2.3 测试
@Test
public void test15() {
SqlSession sqlsession = MybatisUtil.getSqlsession();
StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
//可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
MyStudent myStudent = studentDao.selectStudentResultMap2(1001);
System.out.println(myStudent);
}
2.4 测试结果
Checking to see if class com.yuhl.vo.StudentVo matches criteria [is assignable to Object]
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 188576144.
==> Preparing: select id as myid,name as myname,email as myemail,age as myage from student where id = ?
==> Parameters: 1001(Integer)
<== Columns: myid, myname, myemail, myage
<== Row: 1001, 张三, zhangsan@qq.com, 20
<== Total: 1
MyStudent{myid=1001, myname='张三', myemail='zhangsan@qq.com', myage=20}
3.总结
resultMap和as可以解决java类属性名称和表类名不一致的情况。做到解耦!
脑图:
https://www.processon.com/view/link/5fae2e01f346fb2d03b384d3
下一篇:14-Mybatis中like使用的两种情况https://blog.csdn.net/fsjwin/article/details/109674722