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)

配置和之前版本有所不同

项目结构:

从上到下依次创建

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

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

  1. springboot学习入门简易版三---springboot2.0启动方式

    2.4使用@componentscan方式启动 2.4.1 @EnableAutoConfiguration 默认只扫描当前类 @EnableAutoConfiguration 默认只扫描当前类,如果 ...

  2. springboot学习入门简易版二---springboot2.0项目创建

    2 springboot项目创建(5) 环境要求:jdk1.8+ 项目结构: 2.1创建maven工程 Group id :com.springbootdemo Artifact id: spring ...

  3. springboot学习入门简易版六---springboot2.0整合全局捕获异常及log4j日志(12-13)

    使用Aop实现 1创建异常请求 在原有项目基础上,jspController中创建一个可能发生异常的请求: /** * 全局捕获异常测试 * @param i * @return */ @Reques ...

  4. springboot学习入门简易版五---springboot2.0整合jsp(11)

    springboot对jsp支持不友好,内部tomcat对jsp不支持,需要使用外部tomcat,且必须打包为war包. 1 创建maven项目 注意:必须为war类型,否则找不到页面. 且不要把js ...

  5. springboot学习入门简易版九---springboot2.0整合多数据源mybatis mysql8+(22)

    一个项目中配置多个数据源(链接不同库jdbc),无限大,具体多少根据内存大小 项目中多数据源如何划分:分包名(业务)或注解方式.分包名方式类似多个不同的jar,同业务需求放一个包中. 分包方式配置多数 ...

  6. springboot学习入门简易版四---springboot2.0静态资源访问及整合freemarker视图层

    2.4.4 SpringBoot静态资源访问(9) Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则 /static  /public  /resourc ...

  7. springboot学习入门简易版一---springboot2.0介绍

    1.1为什么用springboot(2) 传统项目,整合ssm或ssh,配置文件,jar冲突,整合麻烦.Tomcat容器加载web.xml配置内容 springboot完全采用注解化(使用注解方式启动 ...

  8. springboot学习入门简易版七---springboot2.0使用@Async异步执行方法(17)

    1启动类开启异步调用注解 @SpringBootApplication @EnableAsync //开启异步调用 public class StartApplication { 不开启则异步调用无效 ...

  9. Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行(含系列目录)。

    前言: Taurus.MVC 微服务版本已经发布了:Taurus.MVC V3.0.3 微服务开源框架发布:让.NET 架构在大并发的演进过程更简单. 以前都是框架发布时写点相关功能点的文章,没有形成 ...

随机推荐

  1. 环境变量path的值大于1024的解决办法

    原文传送门:https://blog.csdn.net/jytxj111/article/details/43916421 1.打开Path,点击默认文本(WIN 10),将所有路径备份下来 2.新建 ...

  2. easyui 如何为datagrid添加自定义列属性(如:width,align,editor)

    我在实际业务需要为datagrid添加一个自定义属性 原先的datagrid列属性包括:title.width.align.formattter.editor等 我们可以通过datagrid的一个方法 ...

  3. Mxnet学习笔记(3)--自定义Op

    https://blog.csdn.net/u011765306/article/details/54562282 前言 今天因为要用到tile操作(类似np.tile,将数据沿axises进行数据扩 ...

  4. [转]使用 curl 发送 POST 请求的几种方式

    HTTP 的 POST 请求通常是用于提交数据,可以通过这篇文章来了解各种提交方式:四种常见的 POST 提交数据方式.做 Web 后端开发时,不可避免地要自己给自己发请求来调试接口,这里要记录的内容 ...

  5. [转]详解Linux(centos7)下安装OpenSSL安装图文方法

    OpenSSL是一个开源的ssl技术,由于我需要使用php相关功能,需要获取https的文件所以必须安装这个东西了,下面我整理了两种关于OpenSSL安装配置方法. 安装环境:  操作系统:CentO ...

  6. elementui---日期格式的选择

    在用elementui做数据提交的时候,默认的时间格式一个对象,好麻烦,主要对时间进行格式限制,具体方法如下: <el-form-item :label="$t('oneCard.bi ...

  7. Python3基础 yield StopIteration.value获取函数的返回值

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  8. [LeetCode] 351. Android Unlock Patterns 安卓解锁模式

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  9. springmvc数据的封装

    spring封装是进行orm封装,可以进行定义数据类型,数据名与接收名相同,进行接收,或者定义类,类的属性名与接收名相同 单个数据类型如图下: 对象类型封装: 其他:乱码处理 在中文字符乱码,需要规定 ...

  10. P-R曲线深入理解

    P-R曲线就是精确率precision vs 召回率recall 曲线,以recall作为横坐标轴,precision作为纵坐标轴.首先解释一下精确率和召回率. 解释精确率和召回率之前,先来看下混淆矩 ...