MyBatis的学习(四):封装Mybatis的输出结果
resultType的使用
简单类型/定义别名返回Map resultMap的使用
解决列名和属性名不一样的第二种方法 模糊查询Like
第一种方法模糊查询第二种方法模糊查询
resultType的使用执行sql语句之后得到的ResultSet转换的类型,使用类名的全限定名称或者别名。
注意:如果返回的是集合,那么应该设置为集合的类型,而不是集合的本身。resultType和resultMap不能同时使用。
- mybatis执行语句,然后调用类的无参构造,创建对象。mybatis把ResultSet指定列值赋值给同名的属性。
举个例子:
- 编写一个返回类,注意属性名称要和数据库的列名保持一致,不然无法赋值。
package com.liang.vo;
public class ViewStudent {
private Integer id;
private String name;
public ViewStudent() {
}
public ViewStudent(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "ViewStudent{" +
"id=" + id +
", name='" + name + ''' +
'}';
}
}
- 剩下的按步骤填写
public ViewStudent selectViewStudent(@Param("myname") String name);
@Test
public void testselectQueryParam(){
//获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//动态代理获取dao的实体类
StudentDao dao = sqlSession.getMapper(StudentDao.class);
//执行语句
ViewStudent student = dao.selectViewStudent("小梁");
//输出结果
System.out.println("创建的学生信息:"+student);
sqlSession.close();
}
简单类型/定义别名
返回简单类型
接口方法:int countStudent();
public int countStudent();
mapper
测试方法
@Test
public void testselectcountStudent(){
//获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//动态代理获取dao的实体类
StudentDao dao = sqlSession.getMapper(StudentDao.class);
//执行语句
int count = dao.countStudent();
//输出结果
System.out.println("学生的数量:"+count);
sqlSession.close();
}
定义自定义类型的别名(不建议使用别名,如果必须要用,那实际应用中建议使用第二种)
- 方法一:需要在mybatis的主配置文件的配置typeAliases下的typeAlias。
- 需要在mybatis的主配置文件的配置typeAliases下的typeAlias。
返回Map
列名是map的key,列值就是map的value只能最多返回一行记录,多余一行都是错误的,不推荐使用
- 定义dao接口
Map
- mapper文件
- 编写测试代码
@Test
public void testselectselectMapById(){
//获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//动态代理获取dao的实体类
StudentDao dao = sqlSession.getMapper(StudentDao.class);
//执行语句
Map
- 测试结果
结果映射,指定列名和java对象的属性对应关系。
你自定义列值赋值给哪个属性当列名和属性名不一样的时侯,一定要使用resultMap
举列子:使用resultMap指定映射关系
- 定义dao接口
List selectAllStu();
- mapper文件
- 编写测试方法
@Test
public void testSelectAllStu(){
//获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//动态代理获取dao的实体类
StudentDao dao = sqlSession.getMapper(StudentDao.class);
//执行语句
List students = dao.selectAllStu();
//输出结果
for (Student stu:students) {
System.out.println("查询到的学生是:"+stu);
}
sqlSession.close();
}
- 测试结果
- dao接口
List selectMyStu();
- map文件
- 编写测试方法
@Test
public void testSelectMyStu(){
//获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//动态代理获取dao的实体类
StudentDao dao = sqlSession.getMapper(StudentDao.class);
//执行语句
List Params = dao.selectMyStu();
//输出结果
for (QueryParam param:Params) {
System.out.println("创建的学生是:"+param);
}
sqlSession.close();
}
- 测试结果
在java代码中直接指定like的内容
举例子:
- dao接口
List selectListStu(String name);
- mapper文件
- 测试方法
@Test
public void testSelectListStu(){
//获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//动态代理获取dao的实体类
StudentDao dao = sqlSession.getMapper(StudentDao.class);
//执行语句
//准备号like的内容
String name = "%赵%";
List students = dao.selectListStu(name);
//输出结果
for (Student stu:students) {
System.out.println("查询的学生是:"+stu);
}
sqlSession.close();
}
- 测试结果
- dao接口
List selectLikeStu(String name);
- mapper文件
- 测试方法
@Test
public void testSelectLikeStu(){
//获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//动态代理获取dao的实体类
StudentDao dao = sqlSession.getMapper(StudentDao.class);
//执行语句
//准备号like的内容
List students = dao.selectLikeStu("赵");
//输出结果
for (Student stu:students) {
System.out.println("查询的学生是:"+stu);
}
sqlSession.close();
}
- 测试结果



