Spring Boot 中使用MyBatis
Contents
pom.xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
注意上面的,如果你的mapper
接口与xml
文件是放在同一个目录下的,那需要注意配置上面的resource/main/java
里要include
你的xml
文件。不然,编译后你会看到,并没有将xml
文件拷贝过去.
application.properties
##mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=your-user-name
spring.datasource.password=your-user-passwd
spring.datasource.url=jdbc:mysql://mysql-host-ip:mysql-port/yoourDBName?useUnicode=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
mybatis.config-location=classpath:mybatis-config.xml
mybatis.mapperLocations[0]=classpath:com/yourcompany/mapper/*.xml
mybatis.mapperLocations[1]=classpath:com/yourcompany/other/mapper/*.xml
# 自动初始化 schema
mybatis.configuration.cache-enabled=false
mybatis.configuration.use-generated-keys=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=none
使用
@Service
public class OperationLogService {
@Autowired
private OperationLogMapper operationLogMapper;
....
}
记录语句
logging.level.org.springframework=WARN
logging.level.com.uniweibo.dao.clickhouse.ClickHouseTaskMapper=DEBUG