springboot整合mybatis、springboot整合web、及jdbc依赖是否必要?

目录

    • 1. Spring Boot 整合数据源
      • 1.1 使用默认HikariCP
        • 1.1.1 添加依赖
        • 1.1.2 连接池简易配置
        • 1.1.3 单元测试
      • 1.2 整合Druid连接池
        • 1.2.1 添加druid启动依赖
        • 1.2.2 配置数据源
        • 1.2.3 单元测试
    • 2. Spring Boot 整合ORM框架
      • 2.1 整合mybatis框架
        • 2.1.1 添加mybatis启动依赖
        • 2.1.2 配置mybatis框架并测试
        • 2.1.3 基本业务实现及单元测试
        • 2.1.4 拓展业务实现及单元测试
      • 2.2 整合mybatis框架进阶
        • 2.2.1 日志进阶配置
    • 3. Spring Boot 整合Web资源
      • 3.1 添加项目依赖
      • 3.2 项目业务编写测试
      • 3.3 启动并测试
    • 4. JDBC依赖必须要?
      • 4.1 一个web项目所需要的的依赖如下
      • 4.2 测试一:不添加依赖
        • 4.2.1分析:核心依赖、测试依赖
      • 4.3 测试二:只添加web依赖
        • 4.3.1 分析:web依赖包含核心依赖
      • 4.4 测试三:只添加mybatis依赖
        • 4.4.1 分析:mybatis依赖包含核心依赖、jdbc依赖
      • 4.5 测试四:只添加mysql驱动依赖
      • 4.6 测试五:只添加mysql、mybatis、web依赖
        • 4.6.1分析:web依赖包含核心依赖;mybatis依赖包含jdb依赖

1. Spring Boot 整合数据源

1.1 使用默认HikariCP

1.1.1 添加依赖

  1. 添加mysql数据库驱动依赖 — MySQL Drive
		<dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
  1. spring对象jdbc支持(可不要)— JDBC API
<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-jdbcartifactId>
dependency>

1.1.2 连接池简易配置

打开application.properties配置文件,添加如下内容

spring.datasource.url=jdbc:mysql:///jtsys?useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root

1.1.3 单元测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class CgbSbootApplicationTests {
    @Autowired
	private DataSource dataSource;
	@Test
	public void testDataSource() throws Exception{
		System.out.println(dataSource.getConnection());
	}
}

1.2 整合Druid连接池

1.2.1 添加druid启动依赖

<dependency>
			<groupId>com.alibabagroupId>
			<artifactId>druid-spring-boot-starterartifactId>
			<version>1.1.13version>
dependency>

1.2.2 配置数据源

打开application.properties配置文件,修改连接池内容配置。

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql:///jtsys?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

简化写法

spring.datasource.url=jdbc:mysql:///jtsys?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=root

1.2.3 单元测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class CgbSbootApplicationTests {
    @Autowired
	private DataSource dataSource;
	@Test
	public void testDataSource() throws Exception{
		System.out.println(dataSource.getConnection());
	}
}

问题:以上为单数据源整合实现,多数据源整合实现?

2. Spring Boot 整合ORM框架

2.1 整合mybatis框架

2.1.1 添加mybatis启动依赖

参考官网: http://mybatis.org/spring/
添加mybatis依赖 — MyBatis Framework

        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.4version>
        dependency>

2.1.2 配置mybatis框架并测试

在mybatis的配置文件application.properties文件中添加如下内容‘

mybatis.configuration.default-statement-timeout=30
mybatis.configuration.map-underscore-to-camel-case=true

2.1.3 基本业务实现及单元测试

定义业务接口

package com.cy.pj.dao;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
@Mapper //3.4 
public interface SysLogDao {
	 @Delete("delete from sys_logs where id=#{id}")
	 int deleteObject(Integer id);
}

定义测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class SysLogDaoTests {    
    @Autowired
    private SysLogDao sysLogDao;
	@Test
	public void testSysLogDao() {
		int rows=sysLogDao.deleteObject(10);
		System.out.println("rows="+rows);
	}
}

2.1.4 拓展业务实现及单元测试

  1. 在SysLogDao中定义添加如下方法:
   int deleteObjects(
        @Param("ids")Integer... ids);

说明:当接口方法对应的映射语句相对比较复杂时,建议将映射语句写到对应映射文件

  1. 在src/main/resources目录下创建mapper/sys目录,然后在其目录添加

SysLogMapper.xml,并添加如下内容:



<mapper namespace="com.cy.pj.sys.dao.SysLogDao">
   <delete id="deleteObjects">
       delete from sys_logs
       where id in 
       <foreach collection="ids"
                open="("
                close=")"
                separator=","
                item="id">
               #{id}
       foreach>
   delete>
mapper>

  1. 在application.properties文件中添加如下配置
mybatis.mapper-locations=classpath:/mapper/sys/*.xml
  1. 在SysLogDaoTests类中添加如下单元测试方法进行单元测试:
@Test
public void testDeleteObjects() {
		int rows=
		sysLogDao.deleteObjects(17,18);
		System.out.println(rows);
}

2.2 整合mybatis框架进阶

2.2.1 日志进阶配置

在spring boot整合mybatis框架时,假如希望在测试运行时输出sql信息,则需要进行日志的相关配置,例如打开application.properties文件然后添加如下日志级别配置.

logging.level.com.cy=DEBUG

说明:其中level单词后面为字节项目的包结构

3. Spring Boot 整合Web资源

3.1 添加项目依赖

添加web和actuator依赖。其中actuator在spring项目中主要负责健康检查以及一些监控功能。

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

3.2 项目业务编写测试

编写HelloController类并将其交给spring管理。

package com.cy.pj.sys.controller;
@Controller
public class HelloController {
	  @RequestMapping("doSayHello")
@ResponBody
	  public String doSayHello() {
		  return "hello spring";
	  }
}
}

3.3 启动并测试

打开chrome浏览器,在地址栏直接输入如下地址
http://localhost:8080/doSayHello

健康检查分析:在浏览器中输入
http://localhost:8080/actuator/health
假如希望查看更多actuator选项,可以在spring boot中配置文件application.properties中添加如下语句:

management.endpoints.web.exposure.include=*

此时在浏览器地址栏可以输入http://localhost:8080/actuator/beans 查看所有的spring 容器中的bean信息

4. JDBC依赖必须要?

总结:无论springboot整合mybatis框架,还是springboot整合web资源,都不用添加jdbc依赖。
下面做测试:

4.1 一个web项目所需要的的依赖如下

添加依赖
springboot整合mybatis、springboot整合web、及jdbc依赖是否必要?_第1张图片


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.4.1version>
        <relativePath/> 
    parent>
    <groupId>com.jtgroupId>
    <artifactId>mybatis02artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>mybatis02name>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.4version>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                        exclude>
                    excludes>
                configuration>
            plugin>
        plugins>
    build>

project>

springboot整合mybatis、springboot整合web、及jdbc依赖是否必要?_第2张图片

4.2 测试一:不添加依赖

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.4.1version>
        <relativePath/> 
    parent>
    <groupId>com.jtgroupId>
    <artifactId>mybatis08nullartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>mybatis08nullname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

springboot整合mybatis、springboot整合web、及jdbc依赖是否必要?_第3张图片

在这里插入图片描述

4.2.1分析:核心依赖、测试依赖

spring-boot-starter //核心依赖、
spring-boot-starter-test //测试依赖

4.3 测试二:只添加web依赖


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.4.1version>
        <relativePath/> 
    parent>
    <groupId>com.jtgroupId>
    <artifactId>mybatis09webartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>mybatis09webname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

springboot整合mybatis、springboot整合web、及jdbc依赖是否必要?_第4张图片

springboot整合mybatis、springboot整合web、及jdbc依赖是否必要?_第5张图片

4.3.1 分析:web依赖包含核心依赖

web依赖:spring-boot-starter-web
内部关联
spring-boot-starter

4.4 测试三:只添加mybatis依赖


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.4.1version>
        <relativePath/> 
    parent>
    <groupId>com.jtgroupId>
    <artifactId>mybatis07mybatisartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>mybatis07mybatisname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.4version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

springboot整合mybatis、springboot整合web、及jdbc依赖是否必要?_第6张图片

springboot整合mybatis、springboot整合web、及jdbc依赖是否必要?_第7张图片

4.4.1 分析:mybatis依赖包含核心依赖、jdbc依赖

mybatis依赖:mybatis-spring-boot-starter

  1. 内部关联
    spring-boot-starter
  2. 内部关联
    spring-boot-starter-jdbc

4.5 测试四:只添加mysql驱动依赖


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.4.1version>
        <relativePath/> 
    parent>
    <groupId>com.jtgroupId>
    <artifactId>mybatis04artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>mybatis04name>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

springboot整合mybatis、springboot整合web、及jdbc依赖是否必要?_第8张图片
在这里插入图片描述

4.6 测试五:只添加mysql、mybatis、web依赖


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.4.1version>
        <relativePath/> 
    parent>
    <groupId>com.jtgroupId>
    <artifactId>mybatis05webmybatismysqlartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>mybatis05webmybatismysqlname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.4version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

springboot整合mybatis、springboot整合web、及jdbc依赖是否必要?_第9张图片
springboot整合mybatis、springboot整合web、及jdbc依赖是否必要?_第10张图片

4.6.1分析:web依赖包含核心依赖;mybatis依赖包含jdb依赖

你可能感兴趣的:(spring,boot,web,java,spring,后端)