写一个typeHandle(具体JDBC和JAVA映射查看表)
package com.gyg.converter;
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;
public class BooleanAndIntConverter extends BaseTypeHandler<Boolean> {
//从java到数据库
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType) throws SQLException {
if (parameter) {
ps.setInt(i, 1);
} else {
ps.setInt(i, 0);
}
}
//从DB->Java代码
@Override
public Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException {
int man = rs.getInt(columnName);
return man == 1 ? true : false;
}
@Override
public Boolean getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
int man = rs.getInt(columnIndex);
return man == 1 ? true : false;
}
//通过存储过程取数据
@Override
public Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
int man = cs.getInt(columnIndex);
return man == 1 ? true : false;
}
}
配置yml文件
这里 mapper-locations 配置时加classpath更好,因为是具体到某个xml文件,所以采用,包名的那种形式不好
且java和resource编译之后都会放在classes中,classpath指代的路径之一就是这个classpanth。
提一嘴。编译的时候不会编译java里面的非java文件(例如mapper.xml),所以需要在pom文件配置如文章末尾
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/gyg_school?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
mybatis:
type-aliases-package: com.gyg.entity
mapper-locations: classpath:/com/gyg/mapper/xml/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
type-handlers-package: com.gyg.config.mybatis
logging:
level:
com.gyg: info
文件路径
这里采用的是,mapper和xml同意目录的情况,即使目录不同,也没关系,配置 mapper-locations即可
mapper的xml文件编写
提一嘴,这里的id要对应mapper接口的抽象方法。
插入需要在sql语句中生命转换器,查询需要在resultMap里面转换
<?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.gyg.mapper.GradesMapper">
<insert id="insertOne" parameterType="grades">
INSERT INTO grades(uid, tid, lid, score, pass)
VALUES (#{uid}, #{tid}, #{lid}, #{score}, #{pass,typeHandler=com.gyg.config.mybatis.BooleanToTinyintTypeHandle})
</insert>
<insert id="insertOne2" parameterType="grade">
INSERT INTO grades(uid, tid, lid, score, pass)
VALUES (#{uid}, #{tid}, #{lid}, #{score}, #{pass})
</insert>
<select id="selectOne" parameterType="int" resultMap="map">
select *
from grades
where id=#{id}
</select>
<resultMap id="map" type="grades">
<id column="id" property="id"/>
<result property="pass" column="pass" jdbcType="TINYINT" javaType="boolean" typeHandler="com.gyg.config.mybatis.BooleanToTinyintTypeHandle"/>
</resultMap>
</mapper>
@mapper和@Properties的区别
@Mapper是mybatis的注解,@properties是spring的注解,随便写哪一个都会在spring中生成bean。因为都是基于cglib
动态代理,需要在Application启动入口配置mapperScan
@Repository
//@Mapper
public interface GradesMapper {
/**
* XXX
* @author gyg
* @param grades
* @return
**/
void insertOne(Grades grades);
void insertOne2(Grade grade);
Grades selectOne(Integer id);
}
启动类配置MapperScan
@SpringBootApplication
@MapperScan("com.gyg.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
测试
@SpringBootTest
@RunWith(SpringRunner.class)
public class GradesServiceImplTest {
@Autowired
GradesService gradesService;
@Test
public void insertOne(){
Grades grades = new Grades();
grades.setLid(1);
grades.setTid(1);
grades.setUid(3);
grades.setScore(58.5);
grades.setPass(true);
try {
gradesService.insertOne(grades);
}catch (Exception ignored){};
System.out.println("插入完成!");
}
@Test
public void insertOne2(){
Grade grades = new Grade();
grades.setLid(1);
grades.setTid(1);
grades.setUid(2);
grades.setScore(98.1);
grades.setPass(1);
try {
gradesService.insertOne2(grades);
System.out.println("插入完成!");
}catch (Exception ignored){};
}
@Test
public void selectOne(){
Grades grades = gradesService.selectOne(1);
if (grades!=null) {
System.out.println(grades.toString());
}
}
}
7. 源码和pom
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>school</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
github地址
转换器demo