1 Spring Boot整合连接池

1.1整合HikariCP连接池

在pom.xml文件中添加依赖
1.mysql数据库驱动依赖

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

2.spring对象jdbc支持

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

1.2配置连接池

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

spring.datasource.url=jdbc:mysql:///dbgoods?serverTimezone=GMT%2B8&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root

1.3单元测试(测试包中写)

@SpringBootTest
public class DataSourceTests {
 @Autowired
private DataSource dataSource;
 @Test
public void testConnection() throws Exception{
System.out.println(dataSource.getConnection());
}}

2 Spring Boot整合MyBatis框架

MyBatis是一个优秀的持久层框架,底层基于JDBC实现与数据库的交互。并在JDBC操作的基础上做了封装和优化,它借助灵活的SQL定制,参数及结果集的映射方式,更好的适应了当前互联网技术的发展.

2.1初始配置

2.1.1添加mybatis启动依赖

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>

2.1.2MyBatis简易配置(可选,暂时可以不配)

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

2.1.3配置mybatis中的sql日志的输出:(com.cy为项目的根包)

logging.level.com.cy=DEBUG

3 Spring Boot 整合SpringMVC应用

3.1概述

MVC是软件工程中的一种软件架构模式,基于此模式把软件系统分为3个基本部分:模型(Model)、视图(View)、和控制器(Controller)。目的是通过这样的设计使程序结构更加简洁、直观,降低问题的复杂度。
!视图(View)-UI设计人员进行图形界面设计,负责实现与用户交互
!控制器(Controller)-负责获取请求,处理请求,响应结果
!模型(Model)-实现业务逻辑,数据逻辑实现

3.2初始配置

添加Spring Web依赖.
Web依赖(提供了Spring MVC核心API,同时会嵌入一个Tomcat服务器)

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Thymeleaf依赖(提供了一个视图解析器对象以及数据绑定机制).

<dependency>
<groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3.2.1配置spring mvc核心对象

在application.properties文件中添加图解析器配置(假如没有配置也会默认配置,在默认配置中prefix默认值为classpath:/templates/,后缀默认为.html)。

spring.thymeleaf.prefix=classpath:/templates/pages/
spring.thymeleaf.suffix=.html

说明:要基于配置在src/main/resources目录下创建templates/pages目录


FiveMonthsDays
1 声望0 粉丝

« 上一篇
Spring AOP
下一篇 »
Ajax 技术应用