开发者学堂课程【SpringBoot快速掌握 - 核心技术:整合MyBatis(一):基础环境搭建】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/612/detail/9275
整合MyBatis(一):基础环境搭建
内容介绍
一、实操演示
二、课堂笔记
一、实操演示
首先使用 idea 创建一个 springboot 项目,加载模块时需要添加web,mysql,JDBD,mybatis 模块
Pom.xml 文件部分代码展示:
<dqpendencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupid>org. springframework.boot</groupia>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependeney
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
Application.yml 部分代码展示:
spring:
datasource:
#数据源基本配置
username: rootpassword: 123456
driver-class-name:com.mysql.jdbc.
Driverur1:jdbc :mysql:// localhost: 3306/ssm_crudtype: com.alibaba.druid.pool.DruidDataSource
数据源其他配置
initia1Size:5minIdle:5
maxActive: 20maxwait: 6000e
timeBetweenEvictionRunsMi11is:60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: true
test0nBorrow: falsetestOnReturn: false
poolPreparedStatements:true
配置监控统计拦截的 filters,去掉后监控界面 sql 无法统计,'wall '用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20useGloba1DataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlNillis=500
DruidConfig 类部分代码展示:
//配置一个
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;
}
}
Employee 类部分的代码展示:
package com. atguigu.springboot.bean;
public class Employee {
private Integer id;
private String lastName;
private Integer gender;private String email;private Integer dId;
public Integer getId(){
return id;
}
public String getLastName() {
return lastName;
}
public Integer getGender() {
return gender;
}
public String getEmail(){
return email;
}
public Integer getdId() {
return dId;
}
}
二、课堂笔记
加入如下依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.1</version>
</dependency>
步骤:
1),配置数据源相关属性
2),给数据源建表
3),创建 JavaBean