0. 創(chuàng)建數(shù)據(jù)庫(kù)表
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 數(shù)據(jù)庫(kù)驅(qū)動(dòng)的依賴壶运;
<!-- mysql 數(shù)據(jù)庫(kù)驅(qū)動(dòng) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
- 添加 spring boot mybatis 的依賴锣光;
<!-- spring boot mybatis 依賴:注意1.0.0版本還不支持?jǐn)r截器插件 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
2. 配置數(shù)據(jù)庫(kù)
- 在
application.properties
文件中添加數(shù)據(jù)庫(kù)連接配置代碼埃撵;
# 設(shè)置數(shù)據(jù)庫(kù)
spring.datasource.url=jdbc:mysql://域名:端口號(hào)/數(shù)據(jù)庫(kù)名稱?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") // 掃描該包下相應(yīng)的class辐棒,主要是 mybatis 的持久化類
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4. 創(chuàng)建實(shí)體類
package com.mm.spring.demo.bean;
public class Employees {
private Integer id;
private String userName;
private String password;
// 此處省略了 getter / setter 方法
}
5. 創(chuàng)建 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. 創(chuàng)建 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. 創(chuàng)建 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. 測(cè)試接口
- 在 Chrome 中輸入 GET 請(qǐng)求
http://localhost:8080/likeName?name=test
行剂; -
得到如圖的結(jié)果:
屏幕快照 2018-01-17 15.34.28.png
9. 添加分頁(yè)功能
- maven 依賴秕噪;
<!-- MyBatis提供了攔截器接口,我們可以自己實(shí)現(xiàn)攔截器
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 中調(diào)用靜態(tài)方法:
import com.github.pagehelper.PageHelper;
@GetMapping("/likeName")
public List<Employees> likeName(String name, Integer pageNum) {
pageNum = (null == pageNum || pageNum <= 0) ? 1 : pageNum;
// 參數(shù)1:pageNum 參數(shù)2:pageSize
PageHelper.startPage(pageNum, 2);
return employeesService.likeName(name);
}
10. 插入數(shù)據(jù)
- 在
EmployeesMapper
加入抽象方法厚宰;
// 插入數(shù)據(jù)
@Insert("insert into employees (userName) values(#{userName})")
// 設(shè)置主鍵自增
@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;
}
- 訪問(wèn)
http://localhost:8080/save
铲觉;
屏幕快照 2018-01-17 18.12.54.png