mybatis扩展之类型处理器(枚举类型处理)
Employee.java(省略getter和setter方法)
private Integer id; private String lastName; private String gender; private String email; private EmpStatus empStatus=EmpStatus.LOGOUT; private Department dept;
EmpStatus.java
package com.gong.mybatis.bean; public enum EmpStatus { LOGIN,LOGOUT,REMOVE }
EmployeeMapper.java
public 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>
测试:
package com.gong.mybatis.test; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.gong.mybatis.bean.Department; import com.gong.mybatis.bean.Employee; import com.gong.mybatis.dao.EmployeeMapper; import com.gong.mybatis.mapper.EmployeeMapperDynamicSql; public class TestMybatis5 { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(is); } @Test public void test() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); //第一个参数为当前页码,第二个参数是每页的记录数 Page<Object> page = PageHelper.startPage(1,2); List<Employee> emps = mapper.getAllEmp(); //第一个参数为传入的数据,第二个参数为连续显示多少页 PageInfo<Employee> pageInfo = new PageInfo<>(emps,3); for(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 public void testEnum() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); Employee employee = new Employee(); employee.setLastName("mike"); employee.setGender("1"); employee.setEmail(""); mapper.addEmp(employee); System.out.println("保存成功:"+employee.getId()); openSession.commit(); } finally { // TODO: handle finally clause openSession.close(); } } }
输出:
DEBUG 01-23 15:27:43,329 ==> Preparing: insert into tbl_employee(last_name,gender,email,empStatus) values(?,?,?,?) (BaseJdbcLogger.java:145) DEBUG 01-23 15:27:43,396 ==> Parameters: mike(String), 1(String), (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"/> </typeHandlers>
再进行测试,输出结果为:
DEBUG 01-23 15:46:12,577 ==> Preparing: insert into tbl_employee(last_name,gender,email,empStatus) values(?,?,?,?) (BaseJdbcLogger.java:145) DEBUG 01-23 15:46:12,629 ==> Parameters: bob(String), 1(String), (String), 1(Integer) (BaseJdbcLogger.java:145) DEBUG 01-23 15:46:12,705 <== Updates: 1 (BaseJdbcLogger.java:145) 保存成功:10
数据库中:
存储的就是索引了。
相关推荐
xiuyangsong 2020-11-16
Nishinoshou 2020-11-09
jimgreatly 2020-09-01
dongxurr 2020-08-18
Dullonjiang 2020-08-15
Dullonjiang 2020-08-11
Dullonjiang 2020-08-09
dongxurr 2020-08-08
yunzhonmghe 2020-08-07
jimgreatly 2020-08-03
Dullonjiang 2020-07-30
jimgreatly 2020-07-27
liqiancao 2020-07-26
xiuyangsong 2020-07-26
dongxurr 2020-07-26
mcvsyy 2020-07-26
helloxusir 2020-07-25
牧场SZShepherd 2020-07-20