spring boot 的核心为自动装配。
packaging使用jar,springboot自带tomcat集成
引入依赖,这里我们选择最基本的web依赖
mybatis-spring-boot-starter能够帮助我们快速建立基于springboot的mybatis应用
mybatis自动化配置
4.0.0
com.test
mspringboot
0.0.1-SNAPSHOT
jar
MSpringBoot
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.apache.commons
commons-lang3
3.4
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
5.1.40
com.fasterxml.jackson.core
jackson-core
com.fasterxml.jackson.core
jackson-databind
com.fasterxml.jackson.datatype
jackson-datatype-joda
com.fasterxml.jackson.module
jackson-module-parameter-names
com.alibaba
druid-spring-boot-starter
1.1.9
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
tk.mybatis
mapper-spring-boot-starter
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
${basedir}/src/main/resources/generator/generatorConfig.xml
true
true
springboot 官网上使用yml文件,相比properties,文件结构更直观
server:
port: 8080
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://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
username: root
password: 123456
#配置初始化大小/最小/最大
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
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
mybatis:
mapper-locations: classpath:mapping/*.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.test.mspringboot.model # 注意:对应实体类的路径
#pagehelper分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
创建mysql数据库
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
使用mybatis插件通过maven自动生成基本文件 mybatis generator xmlconfig官方文档
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
${basedir}/src/main/resources/generator/generatorConfig.xml
true
true
在指定目录下(${basedir}/src/main/resources/generator/)配置generatorConfig.xml文件
添加配置
运行后生成文件
pagehelper官方
public interface UserService {
PageInfo findAllUsers(int pageNum,int pageSize) throws Exception;
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public PageInfo findAllUsers(int pageNum, int pageSize) throws Exception {
//将参数传给这个方法就可以实现物理分页了,非常简单。
PageHelper.startPage(pageNum, pageSize);
//只有紧跟在 PageHelper.startPage 方法后的第一个 Mybatis 的查询(Select)方法会被分页。
List users = userMapper.selectAllUsers();
PageInfo pageInfo = new PageInfo(users);
return pageInfo;
}
}
@RestController
public class HelloController{
@Autowired
private UserService userService;
@RequestMapping("/")
private String home(){
return "hello world";
}
@RequestMapping("/getAllUsers")
private PageInfo getAllUsers(@RequestParam(name = "pageNum", required = false, defaultValue = "1")
int pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "10")
int pageSize) throws Exception{
return userService.findAllUsers(pageNum,pageSize);
}
}
springboot启动类,需要配置@MapperScan
@SpringBootApplication会默认扫描所有路径下带有@Service,@Component,@Controller,@Repository的注解并注册为bean
@SpringBootApplication
@MapperScan("com.test.mspringboot.mapper") //扫描mapper包
public class MSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MSpringBootApplication.class, args);
}
}
选择一个restful插件测试,使用Firefox的RESTer插件
打包 : 切换到项目所在目录使用 mvn package命令,生成的jar文件在/target目录
运行 :java -jar target/mspringboot-0.0.1-SNAPSHOT.jar
退出 :使用 ctrl -c退出运行