做网站交付标准,做百度网站,自建站怎么搭建,怎么查询网站是否被降权JDBC详解
Java Data Base Connectivity,是一种用于执行SQL语句的Java API#xff0c;可以为多种关系数据库提供统一访问#xff0c;它由一组用Java语言编写的类和接口组成。不管是Hibernate#xff0c;还是JPA或者MyBatis都是对JDBC做了一次封装。
Spring简化了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 xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.jege.spring.boot/groupIdartifactIdspring-boot-jdbc/artifactIdversion0.0.1-SNAPSHOT/versionpackagingjar/packagingnamespring-boot-jdbc/nameurlhttp://maven.apache.org/url!-- 公共spring-boot配置下面依赖jar文件不用在写版本号 --parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion1.4.1.RELEASE/versionrelativePath //parentpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingjava.version1.8/java.version/propertiesdependencies!-- 持久层 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jdbc/artifactId/dependency!-- h2内存数据库 --dependencygroupIdcom.h2database/groupIdartifactIdh2/artifactIdscoperuntime/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesbuildfinalNamespring-boot-jdbc/finalNamepluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdconfigurationsource${java.version}/sourcetarget${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 ListUser 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 ListUser 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();}}
扫一扫让你获益匪浅