本文主要是介绍mybatis中BaseTypeHandler的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
mybatis中BaseTypeHandler的使用
自定义TypeHandler
package com.cdn.demo.handler;import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;/*** @author 蔡定努* @date 2023/11/04 16:51*/
public class StringTypeHandler<T> extends BaseTypeHandler<T> {@Overridepublic void setNonNullParameter(PreparedStatement preparedStatement, int i, T integer, JdbcType jdbcType) throws SQLException {preparedStatement.setObject(i, integer);}@Overridepublic T getNullableResult(ResultSet resultSet, String s) throws SQLException {System.out.println(resultSet.getObject(s));return (T) resultSet.getObject(s);}@Overridepublic T getNullableResult(ResultSet resultSet, int i) throws SQLException {return (T) resultSet.getObject(i);}@Overridepublic T getNullableResult(CallableStatement callableStatement, int i) throws SQLException {return (T) callableStatement.getObject(i);}
}
实体类
package com.cdn.demo.entity;import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;import com.cdn.demo.handler.StringTypeHandler;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;/*** @author author* @since 2023-10-25*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value = "person",autoResultMap = true)
public class Person implements Serializable {private static final long serialVersionUID = 1L;@TableId(value = "id", type = IdType.AUTO)private Integer id;private Integer userId;@TableField(typeHandler = StringTypeHandler.class)private String job;private Integer age;}
Mapper
package com.cdn.demo.mapper;import com.cdn.demo.entity.Person;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cdn.demo.handler.StringTypeHandler;
import org.apache.ibatis.annotations.*;import java.math.BigDecimal;
import java.util.List;/*** <p>* Mapper 接口* </p>** @author author* @since 2023-10-25*/
public interface PersonMapper extends BaseMapper<Person> {@Select("select * from person where id = 1")@Results({@Result(property = "job", column = "job", javaType = BigDecimal.class, typeHandler = StringTypeHandler.class)})List<Person> person();List<Person> anno();}
XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cdn.demo.mapper.PersonMapper"><!-- 通用查询映射结果 --><resultMap id="BaseResultMap" type="com.cdn.demo.entity.Person"><id column="id" property="id" /><result column="user_id" property="userId" /><result column="job" property="job" typeHandler="com.cdn.demo.handler.StringTypeHandler"/><result column="age" property="age" /></resultMap><!-- 通用查询结果列 --><sql id="Base_Column_List">id, user_id, job, age</sql><select id="anno" resultMap="BaseResultMap">select <include refid="Base_Column_List"/> from person where id =1</select></mapper>
Controller
package com.cdn.demo.controller;import cn.hutool.core.util.RandomUtil;
import com.cdn.demo.entity.Person;
import com.cdn.demo.mapper.PersonMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;/*** <p>* 前端控制器* </p>** @author author* @since 2023-10-25*/
@RestController
@RequestMapping("/person")
public class PersonController {@Resourceprivate PersonMapper personMapper;/*** xml方式* @author 蔡定努*/@GetMapping("add")public void add() {Person person1 = new Person();person1.setUserId(0);person1.setJob(RandomUtil.randomString(3));person1.setAge(0);personMapper.insert(person1);}/*** xml的方式* @author 蔡定努*/@GetMapping("xml")public Object xml() {return personMapper.person();}/*** 注解的方式* @return*/@GetMapping("anno")public Object anno() {return personMapper.anno();}/*** plus的方式* @return*/@GetMapping("plus")public Object plus() {return personMapper.selectById(1);}}
这篇关于mybatis中BaseTypeHandler的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!