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

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

原文地址:https://www.cnblogs.com/cslj2013/p/10852352.html

时间: 2024-08-01 14:13:07

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

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

2.4使用@componentscan方式启动 2.4.1 @EnableAutoConfiguration 默认只扫描当前类 @EnableAutoConfiguration 默认只扫描当前类,如果再新建一个indexcontroller类,将无法被扫描. 新建indexcontroller类: /** * 测试index类 * @author admin * */ @RestController public class IndexController { @RequestMapping("

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

springboot对jsp支持不友好,内部tomcat对jsp不支持,需要使用外部tomcat,且必须打包为war包. 1 创建maven项目 注意:必须为war类型,否则找不到页面. 且不要把jsp页面存放在resources(原因:可能被别人访问,其次不在classes类路径中),因此,一般自行创建目录存放(一般/WEB-INF/下.  2 pom文件 <packaging>war</packaging> <!-- 注意为war包!!! --> <!-- s

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

1.1为什么用springboot(2) 传统项目,整合ssm或ssh,配置文件,jar冲突,整合麻烦.Tomcat容器加载web.xml配置内容 springboot完全采用注解化(使用注解方式启动springmvc,没有web.xml,springmvc3后采用注解方式启动springmvc),简化配置,快速整合第三方框架(maven依赖继承),内嵌http服务器(tomcat,jetty,通过java创建tomcat),构建微服务应用.最终以java应用程序进行执行. 1.2Springb

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

2.4.4 SpringBoot静态资源访问(9) Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则 /static  /public  /resources  /META-INF/resources 可以在src/main/resources目录下创建static,在该位置放置一个图片文件. 启动程序后,尝试访问http://localhost:8080/D.JPG,如能显示图片,配置成功. 2.5 SpringBoot整合freemarker视图

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

1启动类开启异步调用注解 @SpringBootApplication @EnableAsync //开启异步调用 public class StartApplication { 不开启则异步调用无效 2编写异步调用方法 @RestController public class AsyncController { private final static Logger logger=LoggerFactory.getLogger(WebLogAspect.class); @Autowired p

Hadoop2.2.0分布式环境配置笔记1-编译64位hadoop源码

我的测试环境是三台机器 分别是 Hadoop-Yarn.Hark.Com 192.168.1.200 Hadoop-Slave1.Hark.Com 192.168.1.201 Hadoop.Slave2.Hark.Com 192.168.1.202 我为了自己能充分练习hadoop 64位源码编译,所以三台机器都进行了下边的配置 环境: vmware9+centos6.4+hadoop2.2.0 0.创建hadoop帐号,大部分操作都要使用hadoop帐号的 1.关闭窗口模式 使用root账号

【一步一步入门Spring+Maven+MongoDB】一:环境配置

[一步一步入门Spring+Maven+MongoDB]一:环境配置 一.开发环境 Eclipse(Java EE 版) 二.环境配置 1.jdk配置 1.1.1下载JDK 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html,找到对应版本,下载安装. 1.1.2 配置环境变量 打开环境变量,系统变量,新建,变量名JAVA_HOME,变量值:上述安装路径(到bin父目录为止). 找到P

Berkeley DB (VC6.0 编译环境配置)

操作系统:winxp VC环境:VC6.0 必需文件:Berkeley DB安装文件(db-.msi)下载地址http://www.oracle.com/technology/software/products/berkeley-db/index.html 步骤: 1.运行db-4.7.25.msi,安装Berkeley DB 2.Build Berkeley DB 1〉用VC6.0 打开工程 (C:\Program Files\Oracle\Berkeley DB \db-4.7.25\bui

Hadoop2.2.0分布式环境配置笔记2-编译64位hadoop源码

11.配置hadoop分布式环境!  三台机器都需要这么配置 1).将/opt/modules/hadoop-2.2.0-src重命名  mv /opt/modules/hadoop-2.2.0-src/ /opt/modules/hadoop-2.2.0-src_x32_back 2).解压64位源码 tar -zxvf /opt/modules/hadoop-2.2.0-src_x32_back/hadoop-dist/target/hadoop-2.2.0.tar.gz -C /opt/m