MyBatis 自定义类型处理器处理枚举类

代码

枚举类

package mybatis.cn.util;

/**
 * @Author yanyg
 * @Date 2020/6/30 14:15
 * @Descripetion admin
 */
public enum Gender {

    MALE(1, "男"), FEMALE(2, "女");
    private Integer code;
    private String name;

    Gender(Integer code, String name) {
        this.code = code;
        this.name = name;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static Gender getGender(Integer code) {
        for (Gender gender : Gender.values()) {
            if (gender.getCode() == code) {
                return gender;
            }
        }
        return null;
    }
}

自定义类型处理器 GenderTypeHandler

package mybatis.cn.handler;

import mybatis.cn.util.Gender;
import org.apache.ibatis.type.*;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author yanyg
 * @Date 2020/6/30 14:47
 * @Descripetion admin
 */
@MappedJdbcTypes(JdbcType.INTEGER)
@MappedTypes(value = Gender.class)
public class GenderTypeHandler implements TypeHandler<Gender> {

    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, Gender gender, JdbcType jdbcType) throws SQLException {
        preparedStatement.setInt(i, gender.getCode());
    }

    @Override
    public Gender getResult(ResultSet resultSet, String s) throws SQLException {
        int code = resultSet.getInt(s);
        return Gender.getGender(code);
    }

    @Override
    public Gender getResult(ResultSet resultSet, int i) throws SQLException {
        int code = resultSet.getInt(i);
        return Gender.getGender(code);
    }

    @Override
    public Gender getResult(CallableStatement callableStatement, int i) throws SQLException {
        int code = callableStatement.getInt(i);
        return Gender.getGender(code);
    }
}

配置信息

spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

server.port=8088
server.tomcat.uri-encoding=UTF-8

#经过查阅:
#如果引用mybatis-plus-boot-starter 依赖,需要配置 mybatis-plus.mapper-locations
#如果引用mybatis-plus 依赖,需要配置 mybatis.mapper-locations
#mybatis.mapper-locations = classpath:mapper/*.xml
mybatis-plus.mapper-locations= classpath:mapper/*.xml
mybatis.configuration.type-handlers-package=mybatis.cn.handler
mybatis-plus.type-handlers-package=mybatis.cn.handler

XML 映射文件



<mapper namespace="mybatis.cn.dao.PersonsMapper">

    <insert id="savePersons" parameterType="mybatis.cn.entity.Persons">
	    insert into persons (name, age, sex) values (
	    	#{name}, #{age}, #{sex, typeHandler=mybatis.cn.handler.GenderTypeHandler}
	    )
	insert>

mapper>

测试类

package mybatis.cn;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import mybatis.cn.dao.PersonsMapper;
import mybatis.cn.entity.Persons;
import mybatis.cn.util.Gender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * @Author yanyg
 * @Date 2020/6/30 14:22
 * @Descripetion admin
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MybatisPlusDemoApplication.class)
public class PersonsTest {

    @Autowired
    private PersonsMapper personsMapper;

    @Test
    public void testSelect() {
        System.out.println("===========按条件查询============");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("name", "七仙女");
        List<Persons> userList = personsMapper.selectList(wrapper);
        userList.forEach(System.out::println);
    }

    @Test
    public void insertPerson() {
        try {
            Persons persons = new Persons();
            persons.setName("七仙女");
            persons.setAge(22);
            persons.setSex(Gender.FEMALE);
            int num = personsMapper.savePersons(persons);
            System.out.println(num);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }
}

你可能感兴趣的:(常用工具类,mybatis,TypeHandler)