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. Red Hat Enterprise Linux ISO 全镜像下载

    1.iso网盘下载地址:https://wanghualang.pipipan.com/dir/13133650-26232498-a8efb3/ 2.中国大陆开源镜像站汇总,企业贡献: 搜狐开源镜像 ...

  2. python 把带小数的浮点型字符串转换为整数的解决方案

    以下内容在python中完全可以接受: 将整数的字符串表示形式传递给 int 将float的字符串表示形式传递给 float 但是,如果你将float型的字符串传递给int将会得到错误. >&g ...

  3. 012-Shell 提示确认(Y / N,YES / NO)

    例1:确认提示(一次) 这个示例代码将为确认提示一次,如果你给输入错误,程序会以状态1退出.这个例子将只接受Y或N或YES或NO(不区分大小写). #!/bin/bash read -r -p &qu ...

  4. 泡泡一分钟:GEN-SLAM - Generative Modeling for Monocular Simultaneous Localization and Mapping

    张宁  GEN-SLAM - Generative Modeling for Monocular Simultaneous Localization and Mapping GEN-SLAM  - 单 ...

  5. GSON工具类

    import java.util.Map; import com.google.gson.reflect.TypeToken; import com.google.gson.FieldNamingPo ...

  6. LINUX企业应用案例精解 第2版 李晨光

    LINUX企业应用案例精解 第2版 李晨光 下载地址:https://pan.baidu.com/s/1AAKpc-l-qGTSX5h03M01XA 关注微信公众号获取提取码: 输入:lin7 获取提 ...

  7. stylelint那些事儿

    一.参考文档 -   http://stylelint.cn/ -   https://stylelint.io/ -   https://stylelint.io/user-guide/exampl ...

  8. php连接mysql8报错如何解决

    php版本为5.6,连接mysql8.0时报错,但是连接其他mysql8前的版本是正常的 原因可能是mysql8默认的使用密码认证方式不一样,mysql8.0默认使用caching_sha2_pass ...

  9. linux中安装robot环境

    https://www.cnblogs.com/lgqboke/p/8252488.html(文中验证robotframework命令应该为 robot --version) 可能遇到的问题: 1.p ...

  10. Java设置文件权限

    今天遇到一个问题: java写的API,ppt转图片生成的目录及文件 在使用php调用API完成后,再使用php进行删除时,遇到了删除失败的问题(php删除的部分  查看) 部署的环境是Ubuntu ...