1.注解版
0.配置文件
spring:
datasource:
username: root
password: 123456
# ?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf-8&useUnicode=true&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
# 指定数据源类型
type: com.alibaba.druid.pool.DruidDataSource
# 数据源其他配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
# filters: stat,wall,log4j
filters:
commons-log.connection-logger-name: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#自动生成表结构
# schema:
# - classpath:sql/department.sql
# - classpath:sql/employee.sql
# initialization-mode: ALWAYS
1.pom文件
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2.实体类
package com.atguigu.springbootmybaties.bean;
/**
* 作者:
* 创建时间:2019/7/15
* 项目名字:com.atguigu.springbootmybaties.bean
* 版本:1.0
**/
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender;
private Integer d_id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Integer getD_id() {
return d_id;
}
public void setD_id(Integer d_id) {
this.d_id = d_id;
}
}
3.mapper文件
@Mapper//(或者在启动器上面写个注册---@MapperScan(basePackages = {"com.atguigu.springbootmybaties.mapper"}))
public interface EmployeeMapper {
@Select("select * from employee where id=#{id}")
public Employee getEmployeeById(Integer id);
}
4.Controller
package com.atguigu.springbootmybaties.controller;
import com.atguigu.springbootmybaties.bean.Employee;
import com.atguigu.springbootmybaties.mapper.DepMapper;
import com.atguigu.springbootmybaties.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* 作者:
* 创建时间:2019/7/16
* 项目名字:com.atguigu.springbootmyServicebaties.controller
* 版本:1.0
**/
@RestController
public class DeptController {
@Autowired
EmployeeMapper employeeMapper;
@GetMapping(value = "/demp/{id}")
public Employee getEmployee(@PathVariable(value = "id") Integer id){
return this.employeeMapper.getEmployeeById(id);
}
}
二.配置版本(只写注释版的区别)
1.全局配置文件
mybatis:
##全局配置文件的位置
config-location: classpath:mybatis/mybatis-config.xml
### mapper文件
mapper-locations: classpath:mybatis/mapper/*.xml
2.mapper文件
public interface DepMapper {
public Employee getEmployeeById(Integer id);
}
3.Controller文件
package com.atguigu.springbootmybaties.controller;
import com.atguigu.springbootmybaties.bean.Employee;
import com.atguigu.springbootmybaties.mapper.DepMapper;
import com.atguigu.springbootmybaties.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* 作者:
* 创建时间:2019/7/16
* 项目名字:com.atguigu.springbootmyServicebaties.controller
* 版本:1.0
**/
@RestController
public class DeptController {
@Autowired
DepMapper depMapper;
@GetMapping(value = "/demp1/{id}")
public Employee getEmployee1(@PathVariable(value = "id") Integer id){
System.out.println("我是配置文件的");
return this.depMapper.getEmployeeById(id);
}
}
最重要的在启动器上加注解
@MapperScan(basePackages = {"com.atguigu.springbootmybaties.mapper"})【mapper接口的包的位置】