网站推广计划书具体包含哪些基本内容?,wordpress导出html,专门型网站,wordpress时间文件夹1.1 resultType
resultType: 执行 sql 得到 ResultSet 转换的类型#xff0c;使用类型的完全限定名或别名。 注意#xff1a;如果返回的是集合#xff0c;那应该设置为集合包含的类型#xff0c;而不是集合本身。resultType 和 resultMap#xff0c;不能同时使用。
A、…1.1 resultType
resultType: 执行 sql 得到 ResultSet 转换的类型使用类型的完全限定名或别名。 注意如果返回的是集合那应该设置为集合包含的类型而不是集合本身。resultType 和 resultMap不能同时使用。
A、简单类型
接口方法
int countStudent();mapper 文件 !--resultType简单类型--select idcountStudent resultTypeintselect count(*) from student/select或者 !--resultType简单类型--select idcountStudent resultTypejava.lang.Integerselect count(*) from student/select注意resultType结果类型的它的值
类型的全限定名称类型的别名 例如 java.lang.Integer别名是int
测试文件
Testpublic void testReturnInt(){SqlSession sqlSession MybatisUtils.getSqlSession();StudentDao dao sqlSession.getMapper(StudentDao.class); // 这句代码可以自动创建dao接口的实现类对象int count dao.countStudent();System.out.println(学生总人数 count);}B、 对象类型
接口方法
public Student selectStudentById(Param(studentId) Integer id);mapper 文件
select idselectStudentById resultTypecom.zep.domain.Studentselect id,name,email,age from student where id#{studentId}
/select测试文件
Testpublic 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);}注意Dao 接口方法返回是集合类型需要指定集合中的类型不是集合本身。 mybatis中也可以对我们自定义的类型起别名 方法 1在mybatis主配置文件中定义使typeAlias定义别名
!--定义别名--typeAliases!--可以指定一个类型一个自定义别名type:自定义类型的全限定名称alis:别名短小容易记忆--typeAlias typecom.zep.domain.Student aliasstu/typeAlias typecom.zep.vo.ViewStudent aliasvstu//typeAliases2可以在resultType中使用自定义别名 接口方法
public Student selectStudentById(Param(studentId) Integer id);mapper 文件
select idselectStudentById resultTypestuselect id,name,email,age from student where id#{studentId}
/select测试文件
Testpublic 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);}或者采用包扫描package: 1在mybatis主配置文件中定义使package定义别名
typeAliases!--第一种方式可以指定一个类型一个自定义别名type:自定义类型的全限定名称alis:别名短小容易记忆--!--typeAlias typecom.zep.domain.Student aliasstu/typeAlias typecom.zep.vo.ViewStudent aliasvstu/--!--第二种方式package name是包名这个包中的所有类类名就是别名类名不区分大小写--package namecom.zep.domain/package namecom.zep.vo//typeAliases2可以在resultType的值中可以省略前面在package标签name属性中已经写过的内容,只需要写类名即可,类名就是别名。
mapper文件
select idselectStudentById resultTypeStudentselect id,name,email,age from student where id#{studentId}
/select测试文件
Testpublic 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
sql 的查询结果作为 Map 的 key 和 value。推荐使用 MapObject,Object。
注意Map 作为接口返回值sql 语句的查询结果最多只能有一条记录。大于一条记录是错误。
接口方法
//定义方法返回MapMapObject,Object selectMapById(Integer id);mapper 文件 !--返回Map1)列名是map的key,列值是map的value2)只能最多返回一行记录。多于一行会报错--select idselectMapById resultTypemapselect id,name from student where id#{stuid}/select或者
select idselectMapById resultTypejava.util.HashMapselect id,name from student where id#{stuid}
/select测试文件 // 返回MapTestpublic void testSelectMap() {SqlSession sqlSession MybatisUtils.getSqlSession();StudentDao dao sqlSession.getMapper(StudentDao.class); // 这句代码可以自动创建dao接口的实现类对象//调用dao的方法执行数据库的操作MapObject, Object map dao.selectMapById(1001);System.out.println(m map);}1.2 resultMap
resultMap 可以自定义 sql 的结果和 java 对象属性的映射关系。更灵活的把列值赋值给指定属性。 常用在列名和 java 对象属性名不一样的情况。
使用方式
先定义 resultMap,指定列名和属性的对应关系。在select中把 resultType 替换为 resultMap。
注意 resultMap和resultType不要一起用二选一
接口方法 /*** 使用resultMap定义映射关系*/ListStudent selectAllStudents();mapper文件 !--使用resultMap1)先定义resultMap2)在select标签中使用resultMap来引用1中定义的。--!--定义resultMapid:自定义名称表示你定义的这个resultMaptype:java类型的全限定名称--resultMap idstudentMap typecom.zep.domain.Student!--定义列名和java属性的关系--!--主键列使用id标签column:列名property:java类型的属性名--id columnid propertyid/!--非主键列使用result标签--result columnname propertyname /result columnemail propertyemail /result columnage propertyage //resultMapselect idselectAllStudents resultMapstudentMapselect id,name,email,age from student/select测试文件
/**/Testpublic void testSelectAllStudents() {SqlSession sqlSession MybatisUtils.getSqlSession();StudentDao dao sqlSession.getMapper(StudentDao.class);ListStudent students dao.selectAllStudents();for (Student student : students) {System.out.println(学生 student);}sqlSession.close();}列名和属性名不同的第一种解决方案
在domain文件夹下新建MyStudent.java
package com.zep.domain;public class MyStudent {private Integer stuid;private String stuname;private String stuemail;private Integer stuage;Overridepublic 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;}
}
接口方法
ListMyStudent selectMyStudent();mapper文件 resultMap idmyStudentMap typecom.zep.domain.MyStudentid columnid propertystuid/!--非主键列使用result标签--result columnname propertystuname /result columnemail propertystuemail /result columnage propertystuage //resultMap!--列名和属性名不一样--select idselectMyStudent resultMapmyStudentMapselect id,name,email,age from student/select测试文件 /**/Testpublic void testSelectMyStudent() {SqlSession sqlSession MybatisUtils.getSqlSession();StudentDao dao sqlSession.getMapper(StudentDao.class);ListMyStudent students dao.selectMyStudent();for (MyStudent student : students) {System.out.println(学生 student);}sqlSession.close();}列名和属性名不同的第二种解决方案 采用列别名的方式给数据库的列名起一个别名让这个别名和属性名相同即可 接口方法 ListMyStudent selectDiffColProperty();mapper文件
!--列名和属性名不一样:第二种方式resultType的默认原则是 同名的列的值赋值给同名的属性所以使用列别名(java对象的属性名)即可解决--select idselectDiffColProperty resultTypecom.zep.domain.MyStudentselect id as stuid,name as stuname,email as stuemail ,age as stuage from student/select测试文件
Testpublic void testSelectDiffColProperty() {SqlSession sqlSession MybatisUtils.getSqlSession();StudentDao dao sqlSession.getMapper(StudentDao.class);ListMyStudent students dao.selectDiffColProperty();for (MyStudent student : students) {System.out.println(###学生 student);}sqlSession.close();}