1、整合MyBatis
在pom.xml
中配置数据库驱动、连接池
<!--mybatis集成-->
<!--jdbc依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- mysql驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>Compile</scope>
</dependency>
<!--durid依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
在application.yml
配置数据源
spring:
# 数据源配置
datasource:
name: mysql_test
type: com.alibaba.druid.pool.DruidDataSource
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
driver-class-name: com.mysql.jdbc.Driver
#基本属性
url: jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8&useSSL=false
username: root
password: root
#配置初始化大小/最小/最大
initial-size: 1
min-idle: 1
max-active: 20
#获取连接等待超时时间
max-wait: 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
time-between-eviction-runs-millis: 60000
#一个连接在池中最小生存的时间
min-evictable-idle-time-millis: 300000
validation-query: SELECT 'x'
#空闲连接是否被回收
test-while-idle: true
#申请连接时是否检测有效性
test-on-borrow: false
#归还连接时是否检测有效性
test-on-return: false
1.2配置mybatis映射文件
在application.yml
配置映射文件的路径以及自身配置文件的路径;
如果在mybatis的配置文件中设置了别名包路径,这里就不用再次设置了;
#mybatis的配置
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:com/example/test/mapper/*.xml
# type-aliases-package: com.javasm.boot
mybatis-config.xml
配置文件
基本都在application.yml
中配置了,这里就配置一下延迟加载之类的。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="lazyLoadTriggerMethods" value=""/>
<setting name="logImpl" value="log4j2"/>
<setting name="cacheEnabled" value="true"/>
</settings>
<typeAliases>
<package name="com.example"/>
</typeAliases>
</configuration>
这里设置了别名,所以application.yml
不用再配置了。
1.3、mapper类生成代理对象
@Mapper注解用来注解dao层的接口,使它的实现类可以被自动注入;
@Mapper
public interface SysuserMapper {
int deleteByPrimaryKey(Integer uid);
int insert(Sysuser record);
int insertSelective(Sysuser record);
Sysuser selectByPrimaryKey(Integer uid);
int updateByPrimaryKeySelective(Sysuser record);
int updateByPrimaryKey(Sysuser record);
}
除了使用@Mapper注解每一个dao接口,还可以在主程序类上加@MapperScan注解,扫描mapper包下的所有接口;
Application
主程序
@SpringBootApplication
@MapperScan("com.java.boot.*.mapper") //扫描mapper包下的所有注解
@EnableTransactionManagement //开启注解式事务
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication a = new SpringApplication(Application.class);
a.setBannerMode(Banner.Mode.OFF);//关闭banner
a.run(args);
// SpringApplication.run(Application.class, args);
}
}
2、使用事务
在主程序类上加@EnableTransactionManagement开启注解式事务;(见3中的Application主程序代码)
在服务层方法上加Transactional注解即可;
public class SysuserServiceImpl{
@Transactional
public Sysuser update(Sysuser user){
//一个开启了事务的service方法
}
}
3、pageHelper分页支持
在pom.xml
中添加分页启动器;
<!--pagehelper分页启动器-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
在application.yml
中设置分页合理化;
pagehelper:
reasonable: true
分页合理化就是你无法翻到第-1页,也不会超出总页数;
4、逆向工程
在pom.xml
中添加逆向工程插件;
<!--mybatis逆向工程插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
</dependencies>
<configuration>
<configurationFile>${basedir}/src/main/resources/mybatis/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>
添加generatorConfig.xml
配置文件;
change只有,在右侧就可以看到新增的插件;
其他:
generatorConfig.xml
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/how2java" userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成实体 -->
<javaModelGenerator targetPackage="com.example.test.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成mapper -->
<sqlMapGenerator targetPackage="com.example.test.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成dao接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.test.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="sysuser" domainObjectName="Sysuser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>