mybatis扩展之类型处理器(枚举类型处理)

Posted 西西嘛呦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("mike@qq.com");
            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), 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"/>
    </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), bob@qq.com(String), 1(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 01-23 15:46:12,705 <==    Updates: 1  (BaseJdbcLogger.java:145) 
保存成功:10

数据库中:

 存储的就是索引了。

以上是关于mybatis扩展之类型处理器(枚举类型处理)的主要内容,如果未能解决你的问题,请参考以下文章

mybatis 处理枚举类型

MyBatis配置项--处理枚举类型

mybatis中对枚举类型的处理

MyBatis对于Java对象里的枚举类型处理

关于Mybatis中使用自定义类型通过 自定义TypeHandler类型处理器进行类型转换的两种配置方案-枚举类型示例

mybatis自定义类型处理器