mybatis的输出结果
resultType(A、简单类型),(B、 对象类型),(C、 Map)
resultMap
实体类属性名和列名不同的处理方式
1.resultType(resultType: 执行 sql 得到 ResultSet 转换的类型,使用类型的完全限定名或别名。 注意如果返回的是集
合,那应该设置为集合包含的类型,而不是集合本身。
resultType
和
resultMap
,不能同时使用。
)
1)简单类型
接口方法:
int countStudent();
mapper
文件:
<!--sql执行后返回一行一列-->
<!--resultType可以是别名或者全限定名称-->
<!--<select id="countStudent" resultType="int">-->
<select id="countStudent" resultType="java.lang.Integer">
select count(*) from student
</select>
测试方法:
@Test
public void countStudent(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
int countStudent = dao.countStudent();
System.out.println("一共有"+countStudent+"列");
}
2) 对象类型
接口方法:
public Student selectStudentById(Integer id);
mapper
文件:
<select id="selectStudentById" resultType="com.zsz.domain.Student">
select id,name,email,age from student where id=#{id}
</select>
测试方法:
@Test
public void testSelectStudentById(){
SqlSession sqlSession= MybatisUtils.getSqlSession();
StudentDao dao=sqlSession.getMapper(StudentDao.class);
//调用dao的方法来执行数据库的操作
Student student= dao.selectStudentById(1001);
System.out.println("student="+student);
}
3) Map
(sql 的查询结果作为 Map 的 key 和 value。推荐使用 Map<Object,Object>。
注意:
Map
作为接口返回值,
sql
语句的查询结果最多只能有一条记录。大于一条记录是错误。
)
接口方法:
//定义方法返回map
Map<Object,Object> selestStudentById(Integer id);
mapper
文件:
<!--返回map
1)列名是map的key,列值是map的value
2)返回map的时候只能返回一行数据,大于一行会报错 TooManyResultsException
-->
<select id="selestStudentById" resultType="java.util.HashMap">
select id,name,email,age from student where id=#{id}
</select>
测试方法:
@Test
public void testSelestStudentById(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
Map<Object, Object> objectObjectMap = dao.selestStudentById(1003);
System.out.println("objectObjectMap==="+objectObjectMap);
}
2.resultMap(resultMap 可以自定义 sql 的结果和 java 对象属性的映射关系。更灵活的把列值赋值给指定属性。
常用在列名和
java
对象属性名不一样的情况。
使用方式:
1.
先定义
resultMap,
指定列名和属性的对应关系。
2.
在
<select>
中把
resultType
替换为
resultMap
。
)
接口方法:
/**
* 使用resultMap定义映射关系
*
*/
List<Student> selectAllStudnets();
mapper
文件:
<!--使用resultMap
1)先定义resultMap
2)在select标签中,使用resultMap来引用已定义的
-->
<!--定义resultMap
id:自定义名称,表示你自定义的这个resultMap
type:java类型的全限定名称
-->
<resultMap id="studentMap" type="com.zsz.domain.Student">
<!--列名和java属性的关系-->
<!--主键列,使用id标签
column:列名
property:java类型的属性名
-->
<id column="id" property="id"></id>
<!--非主键列使用result标签-->
<result column="name" property="name"></result>
<result column="email" property="email"/>
<result column="age" property="age"/>
</resultMap>
<select id="selectAllStudnets" resultMap="studentMap">
select id,name,email,age from student
</select>
测试方法:
@Test
public void testSelectAllStudents(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
List<Student> resultMap=dao.selectAllStudnets();
for (Student student:resultMap) {
System.out.println("学生="+student);
}
sqlSession.close();
}
3.实体类属性名和列名不同的处理方式
1) 使用列别名和<resultType>
创建新的实体类(MyStudent)
private Integer stuId;
private String stuName;
private String stuEmail;
private Integer stuAge;
接口方法:
List<MyStudent> selectDiffColProperty();
mapper
文件:
<!--列名和属性名不一样:resultType解决方式
resultType的默认原则是 数据库中同名的列赋值给java代码中实体类中同名的属性(同名的列赋值给同名的属性),使用列别名(java对象的属性名)
-->
<select id="selectDiffColProperty" resultType="com.zsz.domain.MyStudent">
select id as stuId,name as stuName,email as stuEmail,age as stuAge from student
</select>
测试方法:
@Test
public void testSelectDiffColProperty(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
List<MyStudent> resultMap=dao.selectDiffColProperty();
for (MyStudent student:resultMap) {
System.out.println("学生="+student);
}
sqlSession.close();
}
2) 使用<resultMap>
接口方法:
List<MyStudent> selectAllMyStudnet();
mapper
文件:
<resultMap id="myStudentMap" type="com.zsz.domain.MyStudent">
<id column="id" property="stuId"></id>
<!--非主键列使用result标签-->
<result column="name" property="stuName"></result>
<result column="email" property="stuEmail"/>
<result column="age" property="stuAge"/>
</resultMap>
<!--列名和属性名不一样:第一种解决方式-->
<select id="selectAllMyStudnet" resultMap="myStudentMap">
select id,name,email,age from student
</select>
测试方法:
@Test
public void testSelectAllMyStudent(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
List<MyStudent> resultMap=dao.selectAllMyStudnet();
for (MyStudent student:resultMap) {
System.out.println("学生="+student);
}
sqlSession.close();
}