mybatis扩展之类型处理器(枚举类型处理)
发布时间:2020-12-14 18:02:30 所属栏目:大数据 来源:网络整理
导读:Employee.java(省略getter和setter方法) private Integer id; String lastName; String gender; String email; private EmpStatus empStatus=EmpStatus.LOGOUT; private Department dept; EmpStatus.java package com.gong.mybatis.bean; public enum EmpS
Employee.java(省略getter和setter方法) private Integer id; String lastName; String gender; String email; private EmpStatus empStatus=EmpStatus.LOGOUT; EmpStatus.java package com.gong.mybatis.bean; public enum EmpStatus { LOGIN,LOGOUT,REMOVE } EmployeeMapper.java void addEmp(Employee employee);
EmployeeMapper.xml <insert id="addEmp" parameterType="com.gong.mybatis.bean.Employee" useGeneratedKeys="true" keyProperty="id"> insert into tbl_employee(last_name,gender,email,empStatus) values(#{lastName},#{gender},#{email},#{empStatus}) </insert> 测试: com.gong.mybatis.test; import java.io.IOException; java.io.InputStream; java.util.ArrayList; java.util.Arrays; java.util.HashMap; java.util.List; java.util.Map; org.apache.ibatis.io.Resources; org.apache.ibatis.session.SqlSession; org.apache.ibatis.session.SqlSessionFactory; org.apache.ibatis.session.SqlSessionFactoryBuilder; org.junit.Test; com.github.pagehelper.Page; com.github.pagehelper.PageHelper; com.github.pagehelper.PageInfo; com.gong.mybatis.bean.Department; com.gong.mybatis.bean.Employee; com.gong.mybatis.dao.EmployeeMapper; com.gong.mybatis.mapper.EmployeeMapperDynamicSql; class TestMybatis5 { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(is); } @Test void test() IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.); //第一个参数为当前页码,第二个参数是每页的记录数 Page<Object> page = PageHelper.startPage(1,2); List<Employee> emps = mapper.getAllEmp(); 第一个参数为传入的数据,第二个参数为连续显示多少页 PageInfo<Employee> pageInfo = new PageInfo<>(emps,3for(Employee emp:emps) { System.out.println(emp); } System.out.println("当前页码:"+page.getPageNum()); System.out.println("总记录数:"+page.getTotal()); System.out.println("每页几条记录:"+page.getPageSize()); System.out.println("总页码数:"+page.getPages()); System.out.println("当前页码:"+pageInfo.getPageNum()); System.out.println("总记录数:"+pageInfo.getTotal()); System.out.println("每页几条记录:"+pageInfo.getPageSize()); System.out.println("总页码数:"+pageInfo.getPages()); System.out.println("是否第一页:"+pageInfo.isIsFirstPage()); System.out.println("是否最后一页:"+pageInfo.isIsLastPage()); System.out.println("连续显示的页码:"int[] navigatepageNums = pageInfo.getNavigatepageNums(); for (int i = 0; i < navigatepageNums.length; i++) { System.out.println(navigatepageNums[i]); } } finally { TODO: handle finally clause openSession.close(); } } @Test void testEnum() ); Employee employee = Employee(); employee.setLastName("mike"); employee.setGender("1"); employee.setEmail("mike@qq.com"); mapper.addEmp(employee); System.out.println("保存成功:"+employee.getId()); openSession.commit(); } openSession.close(); } } } 输出: DEBUG 01-23 15:27:43,329 ==> Preparing: insert into tbl_employee(last_name,empStatus) values(?,?,?) (BaseJdbcLogger.java:145) DEBUG 01-23 15:27:43,396 ==> Parameters: mike(String),1(String),mike@qq.com(String),LOGOUT(String) (BaseJdbcLogger.java:145) DEBUG 01-23 15:27:43,472 == Updates: 1 (BaseJdbcLogger.java:145) 保存成功:9 数据库中: 插入的是枚举类型的值。 我们也可以在mybatis全局配置文件中处理枚举类型的Handler: typeHandlers> typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.gong.mybatis.bean.EmpStatus"/> > 再进行测试,输出结果为: DEBUG 01-23 15:46:12,577 ==> Preparing: insert into tbl_employee(last_name,?) (BaseJdbcLogger.java:145) DEBUG 01-23 15:46:12,629 ==> Parameters: bob(String),bob@qq.com(String),1(Integer) (BaseJdbcLogger.java:145) DEBUG 01-23 15:46:12,705 1 (BaseJdbcLogger.java:145) 保存成功:10 数据库中: ?存储的就是索引了。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |