0
点赞
收藏
分享

微信扫一扫

SpringBoot: MyBatis自定义类型转换器(TypeHandler)

东林梁 2022-07-27 阅读 95


1.定义自定义类型转换器类,实现TypeHandler接口,

package cn.edu.tju.handler;

import cn.edu.tju.domain.Name;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

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

@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(Name.class)
public class NameHandler implements TypeHandler<Name> {
@Override
public void setParameter(PreparedStatement ps, int i, Name parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i,parameter.getFirstName()+" "+parameter.getLastName());
}

@Override
public Name getResult(ResultSet rs, String columnName) throws SQLException {
String name=rs.getString(columnName);
Name result=new Name();
String[] array=name.split(" ");
result.setFirstName(array[0]);
result.setLastName(array[1]);
return result;
}

@Override
public Name getResult(ResultSet rs, int columnIndex) throws SQLException {
String name=rs.getString(columnIndex);
Name result=new Name();
String[] array=name.split(" ");
result.setFirstName(array[0]);
result.setLastName(array[1]);
return result;
}

@Override
public Name getResult(CallableStatement cs, int columnIndex) throws SQLException {
return null;
}
}

其中的Name 实体类定义如下:

package cn.edu.tju.domain;

public class Name {
private String firstName;
private String lastName;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

@Override
public String toString() {
return "Name{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}

2.在application.properties中配置类型转化器所在的包

mybatis.type-handlers-package=cn.edu.tju.handler

3.在mapper中使用类型转换器

package cn.edu.tju.mapper;

import cn.edu.tju.domain.Name;
import cn.edu.tju.handler.NameHandler;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;



public interface NameMapper {
@Insert("insert into user_info(full_name) values(#{name})")
int addName(Name name);

@Select("select full_name from user_info where id=#{id}")
Name getName(Integer id);
}

其中addName中的name会被转换为字符串,getName中的full_name字段会被转换为Name类对象

4.Controller中使用上述mapper

package cn.edu.tju.controller;

import cn.edu.tju.domain.Name;
import cn.edu.tju.mapper.NameMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class NameController {
@Autowired
private NameMapper nameMapper;
@RequestMapping("/addName")
public String addName(@RequestBody Name name){
nameMapper.addName(name);
return "ok";
}

@RequestMapping("/getName/{id}")
public String getName(@PathVariable("id") int id){
Name name=nameMapper.getName(id);
return name.toString();
}
}


举报

相关推荐

0 条评论