Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
你要学习的内容:
整合Spring-data-jpa
整合Mybatis
整合Druid数据源,配置Druid监控
项目源码:https://github.com/chenxingxing6/springboot-study/tree/master/demo3
什么是jpa?
JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
2.1 添加依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
2.2 配置数据库连接
在 application.properties 中添加:
2.3 建实体类
package com.example.demo3.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;
/**
* @author lanxinghua
* @date 2018/09/01 14:48
* @description
*/
@Entity
public class Role implements Serializable {
@Id
@GeneratedValue
private Integer id;
@Column
private String name;
@Column
private String describtion;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return describtion;
}
public void setDesc(String describtion) {
this.describtion = describtion;
}
}
2.4 Dao接口
package com.example.demo3.dao;
import com.example.demo3.entity.Role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @author lanxinghua
* @date 2018/09/01 14:50
* @description
*/
@Repository
public interface RoleDao extends JpaRepository<Role, Integer> {
}
2.5 测试类
package com.example.demo3;
import com.alibaba.fastjson.JSON;
import com.example.demo3.dao.RoleDao;
import com.example.demo3.entity.Role;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class jpaTest {
@Resource
private RoleDao roleDao;
@Test
public void testInsert() {
Role role = new Role();
role.setName("cxx");
role.setDesc("desc");
Role save = roleDao.save(role);
System.out.println(JSON.toJSONString(save));
}
@Test
public void testUpdate(){
Role role = new Role();
role.setId(1);
role.setName("cxx");
role.setDesc("desc");
roleDao.save(role);
}
@Test
public void testDelete(){
roleDao.deleteById(1);
}
@Test
public void testSelect(){
Role role = new Role();
role.setName("cxx");
List all = roleDao.findAll(Example.of(role),new Sort(new Sort.Order(Sort.Direction.ASC,"id")));
}
}
数据库中的表会自动创建。
使用 mybatis 官方提供的 Spring Boot 整合包实现。
使用 mybatis-spring 整合的方式,也就是传统的方式(推荐,此方式容易控制 MyBatis 的配置)
使用官方整合包
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
配置数据库连接:
# 数据源配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=tiger
# mybatis 配置
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
在 src/main/resources 下创建 mybatis 文件夹,并在 mybatis 文件夹中创建 “mybatis-config.xml” 配置文件,内容如下:
<configuration>
<settings>
<setting name="useGeneratedKeys" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
settings>
configuration>
然后编写mapper接口,mapper.xml。如果不会,就百度一下吧。
4.1 添加依赖
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.8version>
dependency>
4.2 在 application.properties 中添加:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=tiger
# 修改数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=60000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
spring.datasource.druid.filters=stat,wall,log4j
默认情况下,Druid 的监控统计功能和页面是开启的。
我们启动项目,访问 http://localhost:8080/druid/index.html,如下图: