第一步:
导入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
第二步:配置数据源application.yml文件
spring:
datasource:
username: xxx
password: xxx
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxx?characterEncoding=utf-8&serverTimezone=UTC
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: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
initialization-mode: always
第三步:编写druid数据源后台监控
package com.haihai.springbootmybatistest02.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DruidConfig {
//注入一个DruidDataSource数据源bean
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
@Bean
//配置一个管理后台的servlet
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;
}
@Bean
//配置一个web监控的filter
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;
}
}
第四步:编写mybatis的配置文件
这些配置文件可以去官网抄写,然后修改参数,也可以用下面我的配置文件再修改参数。
这里是官方文档
https://blog.csdn.net/weixin_42336011/article/details/104439172
mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
EmpMapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace写你的mapper接口的全类名-->
<mapper namespace="com.haihai.springbootmybatistest02.mapper.EmpMapper">
<!--//根据id查出相应内容
Emp getEmpById(int eid);-->
<select id="getEmpById" resultType="com.haihai.springbootmybatistest02.bean.Emp">
select * from emp where eid=#{eid}
</select>
<!--//增加一个记录
int insert(Emp emp);-->
<insert id="insert">
insert into emp values (#{eid},#{ename})
</insert>
<!-- //删除一个记录
int delete(int eid);-->
<delete id="delete">
delete from emp where eid=#{eid}
</delete>
<!--//修改一个记录
int update(Emp emp);-->
<update id="update">
update emp set ename=#{ename} where eid=#{eid}
</update>
</mapper>
第五步:在全局配置文件application.yml中配置mybatis的文件路径
并且在SpringBoot启动类中添加mapper的扫描,如下图
也可以在mapper接口上面写mapper注解(上面和这里二选一就可以了),如下图:
第六步:编写controller测试
import com.haihai.springbootmybatistest02.bean.Emp;
import com.haihai.springbootmybatistest02.mapper.EmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class EmpController {
@Autowired
EmpMapper empMapper;
//查询
@ResponseBody
@RequestMapping("/emp/{eid}")
public Emp getEmpById(@PathVariable("eid") int eid){
return empMapper.getEmpById(eid);
}
//当测试增删改的时候要将最上面的Controller注解换成RestController
@RequestMapping("/empinsert")
public void insertInto(){
Emp emp=new Emp(199,"第十三个");
empMapper.insert(emp);
}
@RequestMapping("/deleteh")
public void delete(){
//Emp emp=new Emp(18,"第十三个");
int num=empMapper.delete(1);
System.out.println(num);
}
@RequestMapping("/updateh")
public void update(){
Emp emp=new Emp(18,"第1个");
int num=empMapper.update(emp);
System.out.println(num);
}
}
注解版使用起来会比配置版简单,快速。在实现简单的sql语句时(就是简单的增删查改),比较有优势。整合配置文件版本的mybatis,过程会稍微复杂一些,但是在处理复杂sql语句,比如动态sql的时候(也就是可以根据传入的字段(可多可少)进行相应的查询或者其他操作),会比较有优势。
如果想要查看SpringBoot整合注解版的mybatis可以去下面的链接,下面是超链接,直接点击就可以了。
亲,点我,点我查看注解版