1.整合Mybatis
1.整合包,启动器:
<!--整合Mybatis-->
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
2.添加数据库连接驱动
server:
port: 8080
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/ssm?serverTimezone=GMT&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource # 自定义数据源
#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
initialSize: 5
minIdle: 10
maxActive: 15
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.Priority
#则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
3.创建类
package com.spring_01.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Account {
private int id;
private String name;
private String money;
}
4.创建mapper接口
在这里插入代码片
package com.spring_01.mapper;
import com.spring_01.domain.Account;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
//这个注解表示了这是一mybatis的接口类:dao
@Mapper
@Repository
public interface AccountMapper {
List<Account> queryAccountList();
Account queryAccountById(int id);
int addAccount(Account account);
int updateUser(Account account);
int deleteAccount(int id);
}
5.添加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.spring_01.mapper.AccountMapper">
<select id="queryAccountList" resultType="Account">
select * from account;
</select>
<select id="queryAccountById" resultType="Account">
select * from account where id = #{id};
</select>
<insert id="addAccount" parameterType="Account">
insert into account(id,name,money) values (#{id},#{name},#{money})
</insert>
</mapper>
6.springboot整合配置yaml添加配置
#整合mybatis
mybatis:
type-aliases-package: com.spring_01.domain
mapper-locations: classpath:mybatis/mapper/*.xml
7.controller添加测试
package com.spring_01.controller;
import com.spring_01.domain.Account;
import com.spring_01.mapper.AccountMapper;
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;
@RestController
public class AccountController {
@Autowired
AccountMapper accountMapper;
//查询全部账户
@GetMapping("/queryAccountList")
public List<Account> queryAccountList(){
System.out.println("进入controller了-------------");
List<Account> accountList = accountMapper.queryAccountList();
System.out.println("打印结果"+accountList);
return accountMapper.queryAccountList();
}
//查询账户
@GetMapping("/queryAccountById")
public Account queryAccountById(){
System.out.println("进入controller了-------------");
return accountMapper.queryAccountById(3);
}
//添加账户
@GetMapping("/addAccount")
public String addAccount(){
Account account = new Account(10,"测试人员","100");
System.out.println("进入controller了-------------");
return "结果:"+accountMapper.addAccount(account);
}
}
目录
运行结果: