springboot学习入门简易版八---springboot2.0多环境配置、整合mybatis mysql8+(19-20)

时间:2022-03-02 04:31:48

2.11 SpringBoot多环境配置(19)

 application.properties中配置

Spring.profiles.active=prd

配置环境:

Application-dev.properties 开发环境

Application-test.properties 测试环境

Application-uat.properties 用户测试环境

Application-prd.properties 生产环境

2.12 SpringBoot整合mybatis(20)

注意:使用springboot2和mysql8+(8.0.11),jdk8+(jdk8)

配置和之前版本有所不同

项目结构:

springboot学习入门简易版八---springboot2.0多环境配置、整合mybatis mysql8+(19-20)

从上到下依次创建

2.12.1 实体对象

public class Employee implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
private String lastName;
省略get/set

2.12.2 controller

@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Resource
EmployeeService employeeService; @RequestMapping("/insert")
public String insert(String lastName){
Employee emp=new Employee();
emp.setLastName(lastName);
int i=employeeService.insert(emp);
return i+"";
}
}

2.12.3 service

@Service
public class EmployeeService {
@Resource
EmployeeDao employeeDao; public Integer insert(Employee emp) {
return employeeDao.insert(emp);
}
}

省去了接口类写法,直接写service类

2.12.4 dao

public interface EmployeeDao {

    int insert(Employee emp);

}

2.12.5 mapping

<?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.springboot2.dao.EmployeeDao">
<insert id="insert" parameterType="com.springboot2.bean.Employee">
insert into myemployeee(last_name) values (#{lastName,jdbcType=VARCHAR})
</insert>
</mapper>

注意:mapping和dao不在同一个包下,配置时通过

mapper-locations: classpath:com/springboot2/mapping/*.xml

扫描xml文件

2.12.6 application配置文件

mybatis:
mapper-locations: classpath:com/springboot2/mapping/*.xml spring:
datasource:
url: jdbc:mysql://localhost:3306/mytest?useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=GMT%2B8&allowMultiQueries=true
username: root
password: (****)
driver-class-name: com.mysql.cj.jdbc.Driver

注意:

1)url连接与之前版本有所不同,如添加serverTimezone时区,useSSL

2)Url中有时报错,可能是&不识别,需要转为&但有时&也会报错,改为&再尝试。本例即改为&不报错。

3)Jdbc驱动变动为com.mysql.cj.jdbc.Driver,之前为com.mysql.jdbc.Driver

https://blog.csdn.net/qq_22076345/article/details/81952035

2.12.7 启动类

@SpringBootApplication
@MapperScan("com.springboot2.dao")
public class StartApplication { public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
} }

2.12.8 pom文件

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.9.RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- mysql 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>

如果不需要start-web,仅使用mybatis-spring-boot-starter和mysql-connector-java即可。

2.12.9 测试

http://localhost:8080/employee/insert?lastName=test2019

入口成功返回1

2.12.10 遇到问题

1)提示找不到jdbc驱动。清除maven仓库中的mysql-connnector包,重新下载。

2)新版本驱动名称变更:

为com.mysql.cj.jdbc.Driver,之前为com.mysql.jdbc.Driver

2.13 SpringBoot整合@Transactional(21)

springboot事务分类:
1)声明事务

2)编程事务

事务原理:AOP技术环绕通知。

注意点:不能捕捉异常,事务通过异常回滚。

Springboot默认集成事务,只要在方法或类上加上@TransacTional即可,

不需要在启动类上@EnableTransactionManagement