email
varchar(255) DEFAULT NULL,
gender
int(2) DEFAULT NULL,
d_id
int(11) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
2. 导入需要的依赖
pom.xml
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
org.springframework.boot
spring-boot-starter-data-jdbc
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.1
mysql
mysql-connector-java
runtime
com.alibaba
druid
1.1.8
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
3. 编写数据源的相关配置
application.yml
spring:
datasource:
数据源基本配置
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot_mybatis?serverTimezone=GMT%2B8
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,slf4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
initialization-mode: always
schema:
- classpath:sql/department.sql
- classpath:sql/employee.sql
编写一个配置类,将数据源引入过来
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = “spring.datasource”)
@Bean
public DataSource druid(){
return new DruidDataSource();
}
// 配置Druid的监控
// 配置一个管理后台的Servlet
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), “/druid/*”);
Map<String,String> initParams = new HashMap<>();
initParams.put(“loginUsername”,“admin”);
initParams.put(“loginPassword”,“123456”);
initParams.put(“allow”,"");//默认就是允许所有访问
bean.setInitParameters(initParams);
return bean;
}
// 配置一个web监控的filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String,String> initParams = new HashMap<>();
initParams.put(“exclusions”,".js,.css,/druid/*");
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
二、利用注解的形式整合
1. 编写实体类
Employee
public class Employee {
private Integer id; // 员工id
private String lastName; // 员工姓名
private Integer gender; // 员工性别
private String email; // 员工邮箱
private Integer dId; // 部门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 Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getdId() {
return dId;
}
public void setdId(Integer dId) {
this.dId = dId;
}
}
Department
public class Department {
private Integer id; // 部门id
private String departmentName; // 部门名称
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
2. 编写 Mapper接口层
DepartmentMapper
@Mapper // 指定这是一个操作数据库的mapper,将接口扫描到容器中
public interface DepartmentMapper {
/**
-
查询
-
@param id
-
@return
*/
@Select(“select * from department where id=#{id}”)
public Department getDeptById(Integer id);
/**
-
删除
-
@param id
-
@return
*/
@Delete(“delete from department where id=#{id}”)
public int deleteDeptById(Integer id);
/**
-
新增
-
@param department
-
@return
*/
@Options(useGeneratedKeys = true,keyProperty = “id”)
@Insert(“Insert into department(department_name) values(#{departmentName})”)
public int insertDept(Department department);
/**
-
更新
-
@param department
-
@return
*/
@Update(“update department set department_name=#{departmentName} where id=#{id}”)
public int updateDept(Department department);
}
3. 编写 Controller 层
DeptController
@RestController
public class DeptController {
@Autowired
DepartmentMapper departmentMapper;
/**
-
根据id进行查询
-
@param id
-
@return
*/
@GetMapping("/dept/{id}")
public Department getDepartment(@PathVariable(“id”) Integer id) {
return departmentMapper.getDeptById(id);
}
/**
-
新增部门信息
-
@param department
-
@return
*/
@GetMapping("/dept")
public Department insertDept(Department department) {
departmentMapper.insertDept(department);
return department;
}
}
在不写配置文件的情况 自定义mybatis的配置规则,开启驼峰命名法
@org.springframework.context.annotation.Configuration
public class MybatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return new ConfigurationCustomizer() {
@Override
public void customize(Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
4. 测试
插入一条数据
查询刚才插入的数据
去数据库表表里面查询,数据插入进去了
二、利用配置文件的形式整合
1. 编写 Mapper 接口层
EmployeeMapper
@Mapper
public interface EmployeeMapper {
/**
-
根据id查询员工信息
-
@param id
-
@return
*/
public Employee getEmpById(Integer id);
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享