mybatis
https://mybatis.org/mybatis-3/zh/index.html
http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
https://blog.didispace.com/springbootmybatis/
https://www.w3cschool.cn/mybatis/f4uw1ilx.html
第一步
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
@MapperScan("org.zjs.openfeign.mappers")
public class App
第二步
spring:
application:
name: HelloClient
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xxx:3306/app_test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false&&useSSL=false
username: xxx
password: xxx
druid:
filter: log4j # 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:监控统计用的filter:stat、日志用的filter:log4j、防御sql注入的filter:wall
defaultAutoCommit: true
initialSize: 2 # 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
max-active: 10 # 最大连接池数量
min-idle: 2 # 最小连接池数量
max-pool-prepared-statement-per-connection-size: -1 # 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100
max-wait: 5000 # 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。
pool-prepared-statements: false # 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。
test-on-borrow: false # 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
test-on-return: false # 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
test-while-idle: true # 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
validation-query: SELECT 1 FROM DUAL # 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。
mybatis:
mapper-locations: classpath:mapper/*.xml
第三步
package org.zjs.openfeign.domains;
import java.io.Serializable;
import java.util.Date;
public class AppDomain implements Serializable {
/**
*
*/
private static final long serialVersionUID = 6566589665786586377L;
private Integer id;
private String title;
private Date times;
private Integer setid;
private Integer ver;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getTimes() {
return times;
}
public void setTimes(Date times) {
this.times = times;
}
public Integer getSetid() {
return setid;
}
public void setSetid(Integer setid) {
this.setid = setid;
}
public Integer getVer() {
return ver;
}
public void setVer(Integer ver) {
this.ver = ver;
}
}
package org.zjs.openfeign.mappers;
import org.apache.ibatis.annotations.Mapper;
import org.zjs.openfeign.domains.AppDomain;
public interface AppDao {
void insert(AppDomain a);
}
第四步
/zjs-openfeign/src/main/resources/mapper/app.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="org.zjs.openfeign.mappers.AppDao">
<insert id="insert" parameterType="org.zjs.openfeign.domains.AppDomain" >
insert into app(title,times,setid,ver)values(#{title},#{times},#{setid},#{ver})
</insert>
</mapper>
import org.zjs.openfeign.domains.AppDomain;
import org.zjs.openfeign.mappers.AppDao;
@Autowired
private AppDao userMapper;//这里会报错,但是并不会影响