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. PostgreSQL中的partition-wise aggregation

    partition-wise aggregation允许对每个分区分别执行的分区表进行分组或聚合.如果GROUP BY子句不包括分区键,则只能在每个分区的基础上执行部分聚合,并且必须稍后执行最终处理. ...

  2. uboot中工具buildman的用法

    1. buildman简介 uboot源码中维护的一款多线程编译测试工具 2. buildman的用法 2.1 进入uboot的源码目录 $ cd <path of uboot> 2.2 ...

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

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

  4. 【翻译】Flink Table Api & SQL — 自定义 Source & Sink

    本文翻译自官网: User-defined Sources & Sinks  https://ci.apache.org/projects/flink/flink-docs-release-1 ...

  5. C#反射技术的简单操作(读取和设置类的属性、属性值)

    public class A { public int Property1 { get; set; } } static void Main(){ A aa = new A(); Type type ...

  6. 并行执行任务 Stat-Job

    最近在写一些powershell脚本时候遇到一个问题,那就是要解压十几个zip文件,这样仅执行完解压操作差不多5min的时间就过去了,严重影响了效率,这时就想到了使用多线程的方法来执行这个解压操作,经 ...

  7. Oracle:时间字段模糊查询

    需要查询某一天的数据,但是库里面存的是下图date类型 将Oracle中时间字段转化成字符串,然后进行字符串模糊查询 select * from CAINIAO_MONITOR_MSG t WHERE ...

  8. Spring boot后台搭建二为Shiro权限控制添加缓存

    在添加权限控制后,添加方法 查看 当用户访问”获取用户信息”.”新增用户”和”删除用户”的时,后台输出打印如下信息 , Druid数据源SQL监控 为了避免频繁访问数据库获取权限信息,在Shiro中加 ...

  9. 【VS开发】#pragma pack(push,1)与#pragma pack(1)的区别

    这是给编译器用的参数设置,有关结构体字节对齐方式设置, #pragma pack是指定数据在内存中的对齐方式. #pragma pack (n)             作用:C编译器将按照n个字节对 ...

  10. PHP设计模式 - 享元模式

    运用共享技术有效的支持大量细粒度的对象 享元模式变化的是对象的存储开销 享元模式中主要角色: 抽象享元(Flyweight)角色:此角色是所有的具体享元类的超类,为这些类规定出需要实现的公共接口.那些 ...