0. 创建数据库表
CREATE TABLE `employees` (
`userName` varchar(10) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
`id` int(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
1. 配置 maven
- 添加 mysql 数据库驱动的依赖;
<!-- mysql 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
- 添加 spring boot mybatis 的依赖;
<!-- spring boot mybatis 依赖:注意1.0.0版本还不支持拦截器插件 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
2. 配置数据库
- 在
application.properties
文件中添加数据库连接配置代码;
# 设置数据库
spring.datasource.url=jdbc:mysql://域名:端口号/数据库名称?characterEncoding=UTF-8
spring.datasource.username=用户名
spring.datasource.password=密码
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
3. 添加 mapper 的扫描注解
- 在 Main class 添加注解;
package com.mm.spring.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.mm.spring.demo.mapper") // 扫描该包下相应的class,主要是 mybatis 的持久化类
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4. 创建实体类
package com.mm.spring.demo.bean;
public class Employees {
private Integer id;
private String userName;
private String password;
// 此处省略了 getter / setter 方法
}
5. 创建 Mapper 接口
package com.mm.spring.demo.mapper;
import com.mm.spring.demo.bean.Employees;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface EmployeesMapper {
@Select("select * from employees where userName = #{name}")
List<Employees> likeName(String name);
@Select("select * from employees where id = #{id}")
Employees getById(int id);
@Select("select name from employees where id = #{id}")
String getNameById(int id);
}
6. 创建 Service 类
package com.mm.spring.demo.service;
import com.mm.spring.demo.bean.Employees;
import com.mm.spring.demo.mapper.EmployeesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmployeesService {
@Autowired
private EmployeesMapper employeesMapper;
public List<Employees> likeName(String name){
return employeesMapper.likeName(name);
}
public Employees getById(int id){
return employeesMapper.getById(id);
}
public String getNameById(int id){
return employeesMapper.getNameById(id);
}
}
7. 创建 Controller 类
package com.mm.spring.demo.controller;
import com.mm.spring.demo.bean.Employees;
import com.mm.spring.demo.service.EmployeesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class EmployeesController {
@Autowired
private EmployeesService employeesService;
@GetMapping("/likeName")
public List<Employees> likeName(String name){
return employeesService.likeName(name);
}
}
8. 测试接口
- 在 Chrome 中输入 GET 请求
http://localhost:8080/likeName?name=test
; -
得到如图的结果:
屏幕快照 2018-01-17 15.34.28.png
9. 添加分页功能
- maven 依赖;
<!-- MyBatis提供了拦截器接口,我们可以自己实现拦截器
https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.2.1</version>
</dependency>
- 加入配置文件
package com.mm.spring.demo.config;
import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class MyBatisConfiguration {
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum","true");
properties.setProperty("rowBoundsWithCount","true");
properties.setProperty("reasonable","true");
properties.setProperty("dialect", "mysql");
pageHelper.setProperties(properties);
return pageHelper;
}
}
- 使用:在 Controller 中调用静态方法:
import com.github.pagehelper.PageHelper;
@GetMapping("/likeName")
public List<Employees> likeName(String name, Integer pageNum) {
pageNum = (null == pageNum || pageNum <= 0) ? 1 : pageNum;
// 参数1:pageNum 参数2:pageSize
PageHelper.startPage(pageNum, 2);
return employeesService.likeName(name);
}
10. 插入数据
- 在
EmployeesMapper
加入抽象方法;
// 插入数据
@Insert("insert into employees (userName) values(#{userName})")
// 设置主键自增
@Options(keyProperty = "id", keyColumn = "id", useGeneratedKeys = true)
void save(Employees employees);
- 在
EmployeesService
添加事物;
import org.springframework.transaction.annotation.Transactional;
@Transactional // 添加事物
public void save(Employees employees) {
employeesMapper.save(employees);
}
- 在
EmployeesController
添加代码;
@GetMapping("/save")
public Employees save() {
Employees employees = new Employees();
employees.setUserName("张三");
employeesService.save(employees);
return employees;
}
- 访问
http://localhost:8080/save
;
屏幕快照 2018-01-17 18.12.54.png
网友评论