版权声明:本文为博主编写文章,未经博主允许转载,转载请注明出处。 https://blog.csdn.net/u012373815/article/details/89296273
1. 导入依赖的jar包
新建maven项目导入 springboot和mybatis所需依赖 配置pom.xml文件如下:
<?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>cn.abel</groupId>
<artifactId>springboot-mybatis2</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
</project>
2. 配置文件
在resource 下新建配置文件application.properties,添加数据库连接信息如下
## tomcat配置
server.port=8009
#server.tomcat.maxHttpHeaderSize=8192
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.risk.encoding=UTF-8
# tomcat最大线程数,默认为200
server.tomcat.max-threads=800
# session最大超时时间(分钟),默认为30
server.session-timeout=60
## spring 配置
spring.application.name=springboot-mybatis2
application.main=cn.abel.Application
## LOG
logging.file=./logs/springboot-mybatis2.log
## 主数据源,默认的
spring.datasource.url=jdbc:mysql://localhost:3306/oldMan?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#最小空闲连接
spring.datasource.min-idle=5
#最大连接数量
spring.datasource.max-active=100
#检测数据库的查询语句
spring.datasource.validation-query=select 1 from dual
#等待连接池分配连接的最大时长(毫秒)
spring.datasource.connection-timeout=60000
#一个连接的生命时长(毫秒)
spring.datasource.max-left-time=60000
#生效超时
spring.datasource.validation-time-out=3000
#一个连接idle状态的最大时长(毫秒)
spring.datasource.idle-time-out=60000
#设置默认字符集
spring.datasource.connection-init-sql= set names utf8mb4
logging.level.cn.abel.dao=debug
#Mapper.xml所在的位置
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
smybatis.type-aliases-package=cn.abel.bean
#Mapper.xml所在的位置
## pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
此时Springboot+mybatis的配置阶段已经完成,接下来就可以进行编写业务代码了。
3. 实战
配置大致就是如此,然后就是新建java bean(省略,文章底部有源码地址)
新建mapper.xml文件(省略,文章底部有源码地址,关于mapper.xml 文件编写的疑问可以看我以前的springmvc+mybatis 系列文章)
- 新建dao层。代码如下: 要注意 @Repository @Mapper 注解不要漏掉
import java.util.List;
import java.util.Map;
import cn.abel.bean.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Repository
@Mapper
public interface UserDao {
List<User> getByMap(Map<String, Object> map);
User getById(Integer id);
Integer create(User user);
int update(User user);
int delete(Integer id);
}
service层要在实现类上添加@service注解,代码如下:
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.abel.dao.UserDao;
import cn.abel.bean.User;
@Service
public class UserService {
@Autowired
private UserDao userDao;
public List<User> getByMap(Map<String,Object> map){
return userDao.getByMap(map);
}
public User getById(Integer id){
return userDao.getById(id);
}
public User create(User user){
userDao.create(user);
return user;
}
public User update(User user){
userDao.update(user);
return user;
}
public int delete(Integer id){
return userDao.delete(id);
}
}
controller层也要加@controller注解 (文章底部源码中未包含controller 部分)
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.abel.bean.User;
import cn.abel.service.UserService;
@RequestMapping(value="/users")
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public List<User> list(HttpServletRequest request){
return userService.getByMap(null);
}
@RequestMapping(value="/{id}", method = RequestMethod.GET)
@ResponseBody
public User detail(@PathVariable Integer id){
return userService.getById(id);
}
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public User create(@RequestBody User user){
return userService.create(user);
}
@RequestMapping(method = RequestMethod.PUT)
@ResponseBody
public User update(@RequestBody User user){
return userService.update(user);
}
@RequestMapping(value="/{id}", method = RequestMethod.DELETE)
@ResponseBody
public int delete(@PathVariable Integer id){
return userService.delete(id);
}
}
然后在启动入口类中扫描定义的这些配置类如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author yyb
* @time 2019/3/26
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
本文完整源代码:https://github.com/527515025/springBoot/tree/master/springboot-mybatis2