mybatis执行了sql语句,得到java对象。
1.resultType
resultType结果类型,指sql语句执行完毕后,数据转为的java对象,java类型是任意的。
resultType结果类型它的值:1.类型的全限定名称。2.类型的别名。例如java.lang.Integer别名是int。
处理方式:
- 1.mybatis执行sql语句,然后mybatis调用类的无参构造方法,创造对象。
- 2.mybatis把ResultSet指定列值付给同名的属性。
<select id="selectMultiPosition" resultType="com.putao.domian.Student">
select id,name,email,age from student where
name = #{arg0} or age = #{arg1}
</select>
对应的jdbc
ResultSet rs = executeQuery("select id,name,email,age from student");
while(rs.next()){
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
}
1.1简单类型
- dao层
int countStudent();
- dao层xml
<!-- sql执行后返回一行一列-->
<!-- <select id="countStudent" resultType="int">-->
<select id="countStudent" resultType="java.lang.Integer">
select count(*) from student
</select>
- Test
@Test
public void testSelectCount(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
int counts = dao.countStudent();
System.out.println("学生的数量="+counts);
}
1.2对象类型
- domain
public class ViewStudent {
private Integer id;
private String name;
}
//==============================================
public class Student {
private Integer id;
private String name;
private String email;
private Integer age;
}
- dao层
public Student selectStudentById(@Param("studentID") Integer id);
List<Student> selectMulitParam(@Param("myname") String name,@Param("myage") Integer age);
ViewStudent selectStudentReturnViewStudent(@Param("sid") Integer id);
- dao层xml
<select id="selectStudentById" resultType="Student">
select id,name,email,age from student where id=${studentID}
</select>
<!-- 多个参数,使用@Param命名-->
<select id="selectMulitParam" resultType="com.putao.domian.Student">
select id,name,email,age from student where name=#{myname} or age=#{myage}
</select>
<!-- selectStudentReturnViewStudent-->
<select id="selectStudentReturnViewStudent" resultType="ViewStudent">
select id,name from student where id=#{sid}
</select>
- Test
@Test
public void testSelectStudentById(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
Student student = dao.selectStudentById(1001);
System.out.println("student="+student);
}
@Test
public void testSelectMultiParam(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
List<Student> students = dao.selectMulitParam("李四",20);
students.forEach(student -> System.out.println("学生="+student));
sqlSession.close();
}
@Test
public void testSelectViewStudent(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
ViewStudent student = dao.selectStudentReturnViewStudent(1005);
System.out.println("1005 student="+student);
}
1.3Map
返回Map
1)列名是Map的key,列值是Map的value
2)只能最多返回一行记录。多余一行是错误的
- dao层
//定义方法返回Map
Map<Object,Object> selectMapById(Integer id);
- dao层xml
<select id="selectMapById" resultType="java.util.HashMap">
select id,name,email from student where id=#{stuid}
</select>
- Test
//返回Mao
@Test
public void testSelectMap(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
Map<Object,Object> map = dao.selectMapById(1001);
System.out.println("map=="+map);
}
2.resultMap
使用resultMap
1)先定义resultMap
2)在select标签中,使用resultMap来引用1定义的。
2.1列名和属性名一样
- dao层
/**
* 使用resultMap定义映射关系
*/
List<Student> selectAllStudents();
- dao层xml
定义resultMap
id:自定义名称,表示你定义的这个resultType
type:java类型的全限定名称
<resultMap id="studentMap" type="com.putao.domian.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
@Test
public void testSelectAllStudent(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
List<Student> students = dao.selectAllStudents();
students.forEach(student -> System.out.println("学生="+student));
sqlSession.close();
}
2.2列名和属性名不一样
第一种
- dao层
List<MyStudent> selectMyStudent();
- dao层xml
<resultMap id="myStudentMap" type="com.putao.domian.MyStudent">
<!-- 列名和java属性关系-->
<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
@Test
public void testSelectAllStudent2(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
List<MyStudent> students = dao.selectMyStudent();
students.forEach(student -> System.out.println("学生="+student));
sqlSession.close();
}
结果:
第二种
- domain
public class MyStudent {
private Integer stuid;
private String stuname;
private String stuemail;
private Integer stuage;
}
- dao层
List<MyStudent> selectDiffColProperty();
- dao层xml
<!--列名和属性名不一样:第二种方式
resultType的默认原则是 同名的列值赋值给同名的属性,使用列别名(java对象的属性名)
-->
<select id="selectDiffColProperty" resultType="com.putao.domian.MyStudent">
select id as stuid,name as stuname,email as stuemail,age as stuage from student
</select>
- Test
@Test
public void testSelectDiffColProperty(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
List<MyStudent> students = dao.selectDiffColProperty();
students.forEach(student -> System.out.println("####学生="+student));
sqlSession.close();
}
3.定义别名
在配置文件中<configuration下定义。
- 第一种方式:
可以指定一个类型一个自定义别名
type:自定义类型的全限定名称
alias:别名(短小,容易记忆的)
<typeAliases>
<typeAlias type="com.putao.domian.Student" alias="stu"/>
<typeAlias type="com.putao.vo.ViewStudent" alias="vstu"/>
</typeAliases>
- 第二种方式:
name是包名,这个包中的所有类,类名就是别名(类名不区分大小写)’
<typeAliases>
<package name="com.putao.domian"/>
<package name="com.putao.vo"/>
</typeAliases>