1.1 resultType
resultType: execute sql to get the converted type of ResultSet, use the fully qualified name or alias of the type.
Note: If the return is a collection, it should be set to the type contained in the collection, not the collection itself . resultType and resultMap cannot be used at the same time.
A, simple type
Interface method:
int countStudent();
mapper file:
<!--resultType简单类型-->
<select id="countStudent" resultType="int">
select count(*) from student
</select>
or:
<!--resultType简单类型-->
<select id="countStudent" resultType="java.lang.Integer">
select count(*) from student
</select>
Note : the value of resultType result type
- The fully qualified name of the type
- Type alias, for example, java.lang.Integer alias is int
Test file:
@Test
public void testReturnInt(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class); // 这句代码可以自动创建dao接口的实现类对象
int count = dao.countStudent();
System.out.println("学生总人数:"+ count);
}
B. Object type
Interface method:
public Student selectStudentById(@Param("studentId") Integer id);
mapper file:
<select id="selectStudentById" resultType="com.zep.domain.Student">
select id,name,email,age from student where id=#{studentId}
</select>
Test file:
@Test
public void testSelectStudentById() {
/**
* 使用mybatis的动态代理机制,使用SqlSession.getMapper(dao接口)
* getMapper能够获取dao接口对应的实现类对象。
*/
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class); // 这句代码可以自动创建dao接口的实现类对象
//调用dao的方法,执行数据库的操作
Student student = dao.selectStudentById(1001);
System.out.println("学生=" + student);
}
Note: Dao interface method returns a collection type, you need to specify the type in the collection, not the collection itself.
In mybatis, we can also give aliases to our custom types:
method:
1) Define in the mybatis main configuration file to <typeAlias>
define the alias
<!--定义别名-->
<typeAliases>
<!--可以指定一个类型一个自定义别名
type:自定义类型的全限定名称
alis:别名(短小,容易记忆)
-->
<typeAlias type="com.zep.domain.Student" alias="stu"/>
<typeAlias type="com.zep.vo.ViewStudent" alias="vstu"/>
</typeAliases>
2) You can use custom alias interface methods in resultType
:
public Student selectStudentById(@Param("studentId") Integer id);
mapper file:
<select id="selectStudentById" resultType="stu">
select id,name,email,age from student where id=#{studentId}
</select>
Test file:
@Test
public void testSelectStudentById() {
/**
* 使用mybatis的动态代理机制,使用SqlSession.getMapper(dao接口)
* getMapper能够获取dao接口对应的实现类对象。
*/
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class); // 这句代码可以自动创建dao接口的实现类对象
//调用dao的方法,执行数据库的操作
Student student = dao.selectStudentById(1001);
System.out.println("学生=" + student);
}
Or use package scanning <package>
:
1) Define in the mybatis main configuration file to <package>
define the alias
<typeAliases>
<!--
第一种方式:
可以指定一个类型一个自定义别名
type:自定义类型的全限定名称
alis:别名(短小,容易记忆)
-->
<!--<typeAlias type="com.zep.domain.Student" alias="stu"/>
<typeAlias type="com.zep.vo.ViewStudent" alias="vstu"/>-->
<!--
第二种方式
<package> name是包名,这个包中的所有类,类名就是别名(类名不区分大小写)
-->
<package name="com.zep.domain"/>
<package name="com.zep.vo"/>
</typeAliases>
2) You can omit <package>
the content that has been written in the tag name attribute in the value of resultType , you only need to write the class name, and the class name is the alias .
mapper file:
<select id="selectStudentById" resultType="Student">
select id,name,email,age from student where id=#{studentId}
</select>
Test file:
@Test
public void testSelectStudentById() {
/**
* 使用mybatis的动态代理机制,使用SqlSession.getMapper(dao接口)
* getMapper能够获取dao接口对应的实现类对象。
*/
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class); // 这句代码可以自动创建dao接口的实现类对象
//调用dao的方法,执行数据库的操作
Student student = dao.selectStudentById(1001);
System.out.println("学生=" + student);
}
C、 Map
The query result of sql is used as the key and value of the Map. Recommend to use Map<Object,Object>.
Note: Map is the return value of the interface, and the query result of the sql statement can only have one record at most. More than one record is an error.
Interface method:
//定义方法返回Map
Map<Object,Object> selectMapById(Integer id);
mapper file:
<!--
返回Map
1)列名是map的key,列值是map的value
2)只能最多返回一行记录。多于一行会报错
-->
<select id="selectMapById" resultType="map">
select id,name from student where id=#{stuid}
</select>
or:
<select id="selectMapById" resultType="java.util.HashMap">
select id,name from student where id=#{stuid}
</select>
Test file:
// 返回Map
@Test
public void testSelectMap() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class); // 这句代码可以自动创建dao接口的实现类对象
//调用dao的方法,执行数据库的操作
Map<Object, Object> map = dao.selectMapById(1001);
System.out.println("m=" + map);
}
1.2 resultMap
resultMap can customize the mapping relationship between sql results and java object attributes. More flexible assignment of column values to specified attributes.
It is often used when the column name is different from the java object attribute name.
How to use:
- First define resultMap, specify the correspondence between column names and attributes.
- In
<select>
the Replace the resultType resultMap.
Note:
Do not use resultMap and resultType together, choose one of the two
Interface method:
/**
* 使用resultMap定义映射关系
*/
List<Student> selectAllStudents();
mapper file:
<!--使用resultMap
1)先定义resultMap
2)在select标签中,使用resultMap来引用1中定义的。
-->
<!--定义resultMap
id:自定义名称,表示你定义的这个resultMap
type:java类型的全限定名称
-->
<resultMap id="studentMap" type="com.zep.domain.Student">
<!--定义列名和java属性的关系-->
<!--主键列,使用id标签
column:列名
property:java类型的属性名
-->
<id column="id" property="id"/>
<!--非主键列,使用result标签-->
<result column="name" property="name" />
<result column="email" property="email" />
<result column="age" property="age" />
</resultMap>
<select id="selectAllStudents" resultMap="studentMap">
select id,name,email,age from student
</select>
Test file:
/**/
@Test
public void testSelectAllStudents() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
List<Student> students = dao.selectAllStudents();
for (Student student : students) {
System.out.println("学生=" + student);
}
sqlSession.close();
}
The first solution with different column names and attribute names:
Create a new MyStudent.java under the domain folder:
package com.zep.domain;
public class MyStudent {
private Integer stuid;
private String stuname;
private String stuemail;
private Integer stuage;
@Override
public String toString() {
return "MyStudent{" +
"stuid=" + stuid +
", stuname='" + stuname + '\'' +
", stuemail='" + stuemail + '\'' +
", stuage=" + stuage +
'}';
}
public Integer getStuid() {
return stuid;
}
public void setStuid(Integer stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStuemail() {
return stuemail;
}
public void setStuemail(String stuemail) {
this.stuemail = stuemail;
}
public Integer getStuage() {
return stuage;
}
public void setStuage(Integer stuage) {
this.stuage = stuage;
}
}
Interface method:
List<MyStudent> selectMyStudent();
mapper file:
<resultMap id="myStudentMap" type="com.zep.domain.MyStudent">
<id column="id" property="stuid"/>
<!--非主键列,使用result标签-->
<result column="name" property="stuname" />
<result column="email" property="stuemail" />
<result column="age" property="stuage" />
</resultMap>
<!--列名和属性名不一样-->
<select id="selectMyStudent" resultMap="myStudentMap">
select id,name,email,age from student
</select>
Test file:
/**/
@Test
public void testSelectMyStudent() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
List<MyStudent> students = dao.selectMyStudent();
for (MyStudent student : students) {
System.out.println("学生=" + student);
}
sqlSession.close();
}
The second solution for different column names and attribute names:
use column aliases to give the database column names an alias, so that the alias and the attribute name are the same!
Interface method:
List<MyStudent> selectDiffColProperty();
mapper file:
<!--列名和属性名不一样:第二种方式
resultType的默认原则是 同名的列的值赋值给同名的属性,
所以使用列别名(java对象的属性名)即可解决
-->
<select id="selectDiffColProperty" resultType="com.zep.domain.MyStudent">
select id as stuid,name as stuname,email as stuemail ,age as stuage from student
</select>
Test file:
@Test
public void testSelectDiffColProperty() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
List<MyStudent> students = dao.selectDiffColProperty();
for (MyStudent student : students) {
System.out.println("###学生=" + student);
}
sqlSession.close();
}