DBMS关系型数据库
JDBC、Spring DAO、MyBatis、JPA、Hibernate等技术
连接池应用
SpringBoot通过自动配置创建连接池对象,自动配置组件DataSourceAutoConfiguration.自动配置组件创建连接池机制:
默认连接池的使用方法:
- 首先创建Hikari连接池对象
- 如果Hikari创建失败(未找到jar包),尝试创建Tomcat连接池
- 如果Tomcat创建失败,尝试创建dbcp2连接池
- 如果指定type属性,尝试创建底层通用type(如果指定type属性,按type类型创建)
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration查看源码可以看出连接池顺序
默认连接池的使用方法:
- 在pom.xml定义spring-boot-starter-jdbc、驱动、junit、spring-boot-starter-test包定义
- 在application.properties定义数据库参数
- 定义一个启动类,使用@SpringBootApplication
- 创建Spring容器,测试dataSource
测试的两种方法:一种是通过自己把容器创建出来的方式、一种是通过注入的方式进行测试
1.自己把容器创建出来
import cn.xdl.MyBootApplication;
public class TestDataSource {
@Test
public void test() {
ApplicationContext acc
=SpringApplication.run(MyBootApplication.class);
DataSource ds = acc.getBean("dataSource",DataSource.class);
System.out.println(ds);
}
}
2.通过注入的方式进行测试
import cn.xdl.MyBootApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes={MyBootApplication.class})
public class TestDataSource {
@Autowired
private DataSource ds;
@Test
public void test() {
System.out.println(ds);
}
}
测试结果:
小技巧:maven项目工具集排除jar包。右键要排除的jar包例如HikariCP包之后点击Ex点击Exclude Mavne Project就可以把jar包排除出去!或手写<exclusions>
<exclusion>
<artifactId></artifactId>
<groupId></groupId>
</exclusion>
</exclusions>
其他连接池使用方法:
第一种方法:是将Hikari从boot-jdbc工具排除
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<artifactId>HikariCP</artifactId>
<groupId>com.zaxxer</groupId>
</exclusion>
</exclusions>
</dependency>
第二种方法:在application.properties指定type类型
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
使用dbcp2链接后测试结果:
第三种方法:自定义连接池对象,自动配置会失效
@SpringBootApplication
public class MyBootApplication {
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource druid(){
DruidDataSource ds = new DruidDataSource();
return ds;
}
@Bean
@ConfigurationProperties(prefix="spring.datasource1")
public DataSource druid1(){
DruidDataSource ds = new DruidDataSource();
return ds;
}
}
SpringDAO应用
spring-jdbc,提供了JdbcTemplate对象,封装JDBC操作,AOP事务控制。
SpringBoot中提供了一个自动配置组件JdbcTemplateAutoConfiguration,会自动创建JdbcTemplate对象。
TransactionAutoConfiguration组件会自动创建DataSourceTransactionManager对象。
在SpringBoot中使用过程:
- 配置DataSource连接池使用(spring-boot-starter-jdbc)
- 编写实体类
- 编写DAO接口和实现类,注入JdbcTemplate对象操作
- Service需要事务控制的方法直接加@Transactional标记
DAO接口
public interface DeptDao {
public List<Dept> findAll();
public Dept findById(int no);
}
DAO接口实现类
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import cn.xdl.entity.Dept;
@Repository
public class JdbcDeptDao implements DeptDao {
@Autowired
private JdbcTemplate template;
@Override
public List<Dept> findAll() {
String sql = "select * from dept";
RowMapper<Dept> rowMapper = new BeanPropertyRowMapper<>(Dept.class);
return template.query(sql, rowMapper);
}
@Override
public Dept findById(int no) {
String sql = "select * from dept where deptno=?";
Object[] params ={no};
RowMapper<Dept> rowMapper = new BeanPropertyRowMapper<>(Dept.class);
return template.queryForObject(sql, params, rowMapper);
}
}
测试类
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import cn.xdl.MyBootApplication;
import cn.xdl.dao.DeptDao;
import cn.xdl.entity.Dept;
@RunWith(SpringRunner.class)
@SpringBootTest(classes={MyBootApplication.class})
public class TestJdbcDeptDao {
@Autowired
private DeptDao deptDao;
@Test
public void test1() {
List<Dept> findAll = deptDao.findAll();
for (Dept dept : findAll) {
System.out.println(dept.getDeptno()+" "+dept.getDname());
}
}
}
结果截图:
MyBatis应用
SpringBoot整合Mybatis操作
1.配置DataSource连接池使用
2.在pom.xml添加mybatis-spring-boot-starter定义
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
3. 定义实体类
4. 定义Mapper映射器接口和SQL语句
public interface EmpMapper {
@Select("select * from EMP")
public List<Emp> findAll();
@Select("select * from EMP where EMPNO=#{no}")
public Emp findById(int no);
}
5. 在启动类前加@MapperScan标记
@SpringBootApplication
@MapperScan(basePackages={"cn.xdl.mapper"})
public class MyBootApplication {
}
6. 测试和结果
package boot02;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import cn.xdl.MyBootApplication;
import cn.xdl.entity.Emp;
import cn.xdl.mapper.EmpMapper;
@RunWith(SpringRunner.class)
@SpringBootTest(classes={MyBootApplication.class})
public class TestEmpMapper {
@Autowired
private EmpMapper empDao;
@Test
public void test1(){
List<Emp> emps = empDao.findAll();
for (Emp emp : emps) {
System.out.println(emp.getEmpno()+" "+emp.getEname());
}
}
}
加入pagehelper-spring-boot-starterb包,可以加入分页查询功能。测试类直接调用PageHelper.startPage(1,5);实现分页功能
JPA应用(实体类麻烦,但Dao接口简单)
JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
- 配置DataSource连接池使用
-
在pom.xml添加spring-boot-starter-data-jpa定义
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
3.定义实体类,添加映射信息@Entity、@Table、@Column、@Id
@Entity
@Table(name="XDL_RECEIVE_ADDRESS")
public class Address {
@Id//主键
@Column(name="ID")
private Integer id;
@Column(name="USER_ID")
private Integer user_id;
@Column(name="RECEIVE_NAME")
private String receive_name;
@Column(name="PROVINCE")
private String province;
@Column(name="ADDRESS")
private String address;
@Column(name="ZIPCODE")
private String zipcode;
@Column(name="MOBILE")
private String mobile;
@Column(name="TELEPHONE")
private String telephone;
@Column(name="EMAIL")
private String email;
@Column(name="AREA")
private String area;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
public String getReceive_name() {
return receive_name;
}
public void setReceive_name(String receive_name) {
this.receive_name = receive_name;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}
4. 定义Dao接口,继承JpaRepository、PagingAndSortingRepository、CrudRepository、Repository。其中已经包含常用的增删查改,需要更加复杂的方法则在Dao接口中添加方法。
public interface AddressRepository
extends JpaRepository<Address, Integer>{
}
5.测试和结果
NoSQL非关系型数据库
Jedis、MongoClient、SpringData-Redis、SpringData-MongoDB等技术