當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot JDBC
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot JDBC
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
JDBC詳解
Java Data Base Connectivity,是一種用于執行SQL語句的Java API,可以為多種關系數據庫提供統一訪問,它由一組用Java語言編寫的類和接口組成。不管是Hibernate,還是JPA或者MyBatis都是對JDBC做了一次封裝。
Spring簡化了JDBC那些內容
Spring JDBC抽象框架所帶來的價值將在以下幾個方面得以體現:(注:使用了Spring JDBC抽象框架之后,應用開發人員只需要完成斜體字部分的編碼工作。)
定義數據庫連接參數打開數據庫連接聲明SQL語句預編譯并執行SQL語句遍歷查詢結果(如果需要的話)處理每一次遍歷操作處理拋出的任何異常處理事務關閉數據庫連接項目圖片
pom.xml
只需要在pom.xml引入需要的數據庫配置,就會自動訪問此數據庫,如果需要配置其他數據庫,可以在application.properties進行添加
默認使用org.apache.tomcat.jdbc.pool.DataSource創建連接池
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.jege.spring.boot</groupId><artifactId>spring-boot-jdbc</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>spring-boot-jdbc</name><url>http://maven.apache.org</url><!-- 公共spring-boot配置,下面依賴jar文件不用在寫版本號 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.1.RELEASE</version><relativePath /></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><!-- 持久層 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- h2內存數據庫 --><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><finalName>spring-boot-jdbc</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>${java.version}</source><target>${java.version}</target></configuration></plugin></plugins></build> </project>模型對象User
package com.jege.spring.boot.jdbc.entity;/*** 模型對象*/ public class User {private Long id;private String name;private Integer age;public User() {}public User(String name, Integer age) {this.name = name;this.age = age;}持久層實現類UserDaoImpl
package com.jege.spring.boot.jdbc.dao.impl;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.stereotype.Repository;import com.jege.spring.boot.jdbc.dao.IUserDao; import com.jege.spring.boot.jdbc.entity.User;/*** jdbc CRUD*/ @Repository public class UserDaoImpl implements IUserDao {@AutowiredJdbcTemplate jdbcTemplate;@Overridepublic void dropTable() {jdbcTemplate.update("drop table t_user if exists");}@Overridepublic void createTable() {jdbcTemplate.update("create table t_user (id bigint generated by default as identity, age integer, name varchar(255), primary key (id))");}@Overridepublic void save(User user) {jdbcTemplate.update("insert into t_user(name,age) values(?,?)", user.getName(), user.getAge());}@Overridepublic List<User> findAll() {return jdbcTemplate.query("select id,name,age from t_user", BeanPropertyRowMapper.newInstance(User.class));}@Overridepublic void deleteAll() {jdbcTemplate.update("delete from t_user");}@Overridepublic List<User> findByNameLike(String name) {return jdbcTemplate.query("select id,name,age from t_user where name like ?", new Object[] { name },BeanPropertyRowMapper.newInstance(User.class));}}不需要application.properties
測試類UserDaoTest
package com.jege.spring.boot.data.jpa;import static org.assertj.core.api.Assertions.assertThat;import org.junit.After; import org.junit.Before; 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.SpringJUnit4ClassRunner;import com.jege.spring.boot.jdbc.dao.IUserDao; import com.jege.spring.boot.jdbc.entity.User;@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest() public class UserDaoTest {@AutowiredIUserDao userDao;// 每次執行Test之前先刪除表,創建表@Beforepublic void before() throws Exception {userDao.dropTable();userDao.createTable();}@Testpublic void save() throws Exception {for (int i = 0; i < 10; i++) {User user = new User("jege" + i, 25 + i);userDao.save(user);}}@Testpublic void all() throws Exception {save();assertThat(userDao.findAll()).hasSize(10);}@Testpublic void findByName() throws Exception {save();assertThat(userDao.findByNameLike("jege%")).hasSize(10);}@Afterpublic void destroy() throws Exception {userDao.deleteAll();}}掃一掃讓你獲益匪淺!!!
總結
以上是生活随笔為你收集整理的Spring Boot JDBC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ const对象
- 下一篇: Spring事务管理接口