本文将介绍Spring Boot集成MyBatis、分页插件PageHelper、通用Mapper以及Druid。
新建一个maven项目,最终项目结构如下:

POM中增加如下依赖:
<!--JSON-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<!--mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
<!--pagehelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
增加Druid相关的配置
1、在application.yml中增加配置
druid:
url: jdbc:mysql://127.0.0.1:3306/antsoft?characterEncoding=UTF-8@&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
username: root
password: root
initial-size: 5
min-idle: 1
max-active: 100
test-on-borrow: true
2、增加Druid的属性装载类
@ConfigurationProperties(prefix = "druid")
public class DruidProperties {
private String url;
private String username;
private String password;
private String driverClass;
private int maxActive;
private int minIdle;
private int initialSize;
private boolean testOnBorrow;
....
}
3、增加Druid的配置类
@Configuration
@EnableConfigurationProperties(DruidProperties.class)
@ConditionalOnClass(DruidDataSource.class)
@ConditionalOnProperty(prefix = "druid", name = "url")
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
public class DruidAutoConfiguration {
@Autowired
private DruidProperties properties;
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(properties.getUrl());
dataSource.setUsername(properties.getUsername());
dataSource.setPassword(properties.getPassword());
if (properties.getInitialSize() > 0) {
dataSource.setInitialSize(properties.getInitialSize());
}
if (properties.getMinIdle() > 0) {
dataSource.setMinIdle(properties.getMinIdle());
}
if (properties.getMaxActive() > 0) {
dataSource.setMaxActive(properties.getMaxActive());
}
dataSource.setTestOnBorrow(properties.isTestOnBorrow());
try {
dataSource.init();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return dataSource;
}
}
以上就完成了Druid的相关配置
Mybatis的集成
1、application.yml增加mybatis配置
mybatis:
type-aliases-package: com.antsoft.model #实体类的包
mapper-locations: classpath:mapper/*.xml #Mapper的xml文件
2、在Applicaton类中增加mapper的scan
@MapperScan(basePackages = "com.antsoft.mapper") //mapper的包
public class Application {
}
集成分页插件PageHelper
仅需在application.yml增加相关配置
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
集成通用Mapper
仅需在application.yml增加相关配置
mapper:
mappers:
- com.antsoft.database.BaseMapper
not-empty: false
identity: MYSQL
以上完成MyBatis的集成。如果需要事务,只需在service相关方法上增加@Transactional(项目中有相关的测试)
一个简单的获取分页用户列表的例子
相关的实体
@Table(name="t_user")
public class User extends BaseEntity {
/** * 账号*/
private String account;
/*** 密码*/
private String password;
...
}
BaseEntity中包含通用字段id,page,rows等(可详见代码)
Mapper
public interface UserMapper extends BaseMapper<User> {
}
由于继承了通用Mapper。这里已经包含了通用的增删改查的方法
Service
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> getAll(User user){
if (user.getPage() != null && user.getRows() != null) {
PageHelper.startPage(user.getPage(), user.getRows());
}
return userMapper.selectAll();
}
}
Controller
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/all")
public PageInfo<User> getALL(User user){
List<User> userList = userService.getAll(user);
return new PageInfo<>(userList);
}
}
完成上述步骤后。通过mvn spring-boot:run启动应用。即可在http://localhost:9090/users/all访问到用户列表
以下是相关文档链接
MyBatis-Spring-Boot-Starter
通用Mapper文档
PageHelper
网友评论