Spring boot 整合持久层
持久层是Java EE中访问数据库的核心操作,Spring boot中对常见的持久层框架都提供了自动优化配置。例如JdbcTemplate、JPA等,MyBatis的自动化配置则是MyBatis官方提供的。
spring boot项目,添加如下依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
</dependencies>
spring-boot-starter-jdbc中提供了spring-jdbc,另外还加入了数据库驱动依赖和数据库连接池依赖。
数据库配置
在application.properties中配置数据库连接基本信息
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql:///chapter05
spring.datasource.username=root
spring.datasource.password=123
创建实体类Book.java
package com.xintianweng.springboot2;
/**
* 小毛
* 2021/4/6 10:11 下午
**/
public class Book {
private Integer id;
private String name;
private String author;
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 getAuthor () {
return author;
}
public void setAuthor (String author) {
this.author = author;
}
}
创建数据库访问层
package com.xintianweng.springboot2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
/**
* 小毛
* 2021/4/6 10:12 下午
**/
@Repository
public class BookDao {
@Autowired
JdbcTemplate jdbcTemplate;
public int addBook(Book book){
return jdbcTemplate.update("insert into book(name,author) values(?,?)",book.getName(),book.getAuthor());
}
public int updateBook(Book book){
return jdbcTemplate.update("update book set name=?,author=? where id=?",book.getName(),book.getAuthor(),book.getId());
}
public Book getBookById(Integer id){
return jdbcTemplate.queryForObject("select * from book where id=?",new BeanPropertyRowMapper<>(Book.class),id);
}
public int deleteBookById(Integer id){
return jdbcTemplate.update("delete from book where id=?",id);
}
}
代码解释:
1⃣️创建BookDao,注入jdbcTemplate,由于已经添加了spring-jdbc相关的依赖,jdbctemplate会被自动注册到spring容器中,因此这里可以直接注入jdbcTemplate使用
2⃣️增删改三种类型的操作主要使用update和batchUpdate方法来完成,query和queryForObject方法主要用来完成查询功能,另外还有execute方法来执行任意的sql。call方法用来调用存储过程等。
3⃣️在执行查询操作时,需要有一个rowmapper将查询出来的列和尸体类中的属性一一对应起来,如果列名和属性名都相同的,那么可以直接使用,BeanPropertyRowMapper;如果列名和属性名不同,就需要开发者自己实现RowMapper接口,将列和实体类属性一一对象起来。
创建service
package com.xintianweng.springboot2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 小毛
* 2021/4/6 10:31 下午
**/
@Service
public class BookService {
@Autowired
BookDao bookDao;
public int addBook(Book book){
return bookDao.addBook(book);
}
public int updateBook(Book book){
return bookDao.updateBook(book);
}
public int deleteBookById(Integer id){
return bookDao.deleteBookById(id);
}
public Book getBookById(Integer id){
return bookDao.getBookById(id);
}
}
创建controller
package com.xintianweng.springboot2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 小毛
* 2021/4/6 10:35 下午
**/
@RestController
public class BookController {
@Autowired
BookService bookService;
@GetMapping("/bookOptions")
public void bookOptions(){
Book book1 = new Book();
book1.setName("逆商");
book1.setAuthor("阳飞杨");
int i = bookService.addBook(book1);
System.out.println("新增book>>>"+i);
Book book2 = new Book();
book2.setName("我在未来等你");
book2.setAuthor("刘同");
int i0 = bookService.addBook(book2);
System.out.println("新增book>>>"+i0);
}
}