


<!--springboot mybatis持久层框架-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!--springboot mybatis通用工具mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<!--springboot mybatis分页PageHelper助手类-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>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">
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>application.properties追加
#springboot整合mybatis
mybatis.type-aliases-package=com.xuwen.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xmlresources>mybatis>mapper>UserMapper.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">
<mapper namespace="com.xuwen.mapper.UserMapper">
<select id="queryUserList" resultType="com.xuwen.pojo.User">
select * from koa2weibo.user
</select>
<select id="queryUserById" resultType="com.xuwen.pojo.User">
select * from koa2weibo.user where id=#{id}
</select>
<insert id="addUser" parameterType="com.xuwen.pojo.User">
insert into koa2weibo.user (id,name,pwd) values(#{id},#{name},#{pwd})
</insert>
<update id="UpdateUser" parameterType="com.xuwen.pojo.User">
update koa2weibo.user set name=#{name}, pwd=#{pwd} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from koa2weibo.user where id=#{id}
</delete>
</mapper>controller层
package com.xuwen.controller;
import com.xuwen.mapper.UserMapper;
import com.xuwen.pojo.User;
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;
/**
* author:xuwen
* Created on 2021/8/3
*/
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
//查询全部用户
@GetMapping("/queryUserList")
private List<User> queryUserList(){
List<User> userList = userMapper.queryUserList();
for (User user : userList) {
System.out.println(user);
}
return userList;
}
//增加
@GetMapping("/addUser")
public String addUser(){
userMapper.addUser(new User(8,"毛毛","jfejife55"));
return "ok";
}
//修改
@GetMapping("/updateUser")
public String updateUser(){
userMapper.UpdateUser(new User(8,"毛毛","jfejife55"));
return "ok";
}
//删除
@GetMapping("/deleteUser")
public String deleteUser(){
userMapper.deleteUser(5);
return "ok";
}
}

applicaiton.yml
spring:
datasource:
username: koa2weibo
password: passMYblog123
url: jdbc:mysql://rm-uf6t9m0yx22dxrya5no.mysql.rds.aliyuncs.com:3306/koa2weibo?serverTimezone=Asia/Shanghai&useSSL=true&useUnicode=true&characterEncoding=UTF8
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#SpringBoot默认是不注入这些的,需要自己绑定
#druid数据源专有配置
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:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许报错,java.lang.ClassNotFoundException: org.apache.Log4j.Properity
#则导入log4j 依赖就行
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionoProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500