目录
简介
数据库用于处理持久化业务产生的数据,应用程序在运行过程中经常要操作数据库。一般情况下,数据库的操作由持久层来实现。作为扩展性较强的一站式开发框架,Spring也提供了持久层Spring JDBC功能,Spring JDBC可以管理数据库连接资源,简化传统JDBC的操作,进而提升程序数据库操作的效率。
JdbcTemple介绍
针对数据库操作,Spring框架提供了JdbcTemplate类,JdbcTemplate是一个模板类,Spring JDBC中的更高层次的抽象类均在JdbcTemplate模板类的基础上创建。
JdbcTemplate类提供了操作数据库的基本方法,包括添加、删除、查询和更新。在操作数据库时,JdbcTemplate类简化了传统JDBC中的复杂步骤,这可以让开发人员将更多精力投入到业务逻辑中。
准备工作
俗话说:工欲善其事必先利其器,学习spring jdbc之前需要先创建一个简单的数据库
CREATE DATABASE springDB;
USE springDB;
#创建用户表
CREATE TABLE person(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
PASSWORD VARCHAR(30) NOT NULL);
#向用户表中添加数据
INSERT INTO person VALUE (0,'admin','123456');
maven中pom.xml配置文件
<dependencies>
<!-- 具体还要使用spring4个基本依赖,由于我这里使用maven继承特性,就不在添加了!-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- 具体还要进行查询数据库版本-->
<version>x.x.x</version>
</dependency>
</dependencies>
数据库查询版本命令:
查看数据库版本:SELECT VERSION() FROM DUAL
applicationContext.xml配置文件
dataSource配置属性要求
在dataSource的4个属性中,需要根据数据库类型或者系统配置设置相应的属性值。例如,如果数据库类型不同,需要更改驱动名称;如果数据库不在本地,则需要将地址中的localhost替换成相应的主机IP;默认情况下,数据库端口号可以省略,但如果修改过MySQL数据库的端口号,则需要加上修改后的端口号。此外,连接数据库的用户名和密码需要与数据库创建时设置的用户名和密码保持一致。
CRUD的具体完成
下面将要模拟用户的CRUD具体,使用简单的三层架构思想完成并测试。
dao层
public interface PersonDao {
//完成用户的CRUD部分
int insertPerson(Person person);
int updateById(Person person);
int deleteById(int id);
Person selectById(int id);
}
具体实现类:
serivce层
public interface PersonService {
int insertPerson(Person person);
int updateById(Person person);
int deleteById(int id);
Person selectById(int id);
}
实现类
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonDao personDao;
@Override
public int insertPerson(Person person) {
return personDao.insertPerson(person);
}
@Override
public int updateById(Person person) {
return personDao.updateById(person);
}
@Override
public int deleteById(int id) {
return personDao.deleteById(id);
}
@Override
public Person selectById(int id) {
return personDao.selectById(id);
}
}
controller层
@Controller
public class PersonController {
@Autowired
private PersonService personService;
private int keyNumber;
public void insertPerson(Person person){
keyNumber = personService.insertPerson(person);
if(keyNumber>0){
System.out.println("添加成功");
}else
System.out.println("添加失败");
}
public void updateById(Person person){
keyNumber = personService.updateById(person);
if(keyNumber>0){
System.out.println("更新成功");
}else
System.out.println("更新失败");
}
public void deleteById(int id){
keyNumber=personService.deleteById(id);
if(keyNumber>0){
System.out.println("删除成功");
}else
System.out.println("删除失败");
}
public void selectById(int id){
Person person = personService.selectById(id);
if(person==null){
System.out.println("请检查重新查询!");
}else {
System.out.println(person.getId()+"\t"+person.getUsername()+"\t"+person.getPassword());
}
}
}
测试
public class PersonControllerTest {
@Test
public void testInsert(){
//模拟添加数据
Person person = new Person(0, "zs", "123");
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
PersonController personController =
context.getBean("personController", PersonController.class);
personController.insertPerson(person);
}
@Test
public void testUpdateById(){
//模拟更新数据
Person person = new Person(1, "zss", "123");
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
PersonController personController =
context.getBean("personController", PersonController.class);
personController.updateById(person);
}
@Test
public void testDeleteById(){
//模拟删除数据
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
PersonController personController =
context.getBean("personController", PersonController.class);
personController.deleteById(2);
}
@Test
public void testSelectById(){
//模拟查询具体数据
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
PersonController personController =
context.getBean("personController", PersonController.class);
personController.selectById(1);
}
}
小结
数据库编程是互联网编程的基础,Spring框架为开发者提供了JDBC模版模式,它可以简化许多代码,但在实际应用中jdbcTemplate并不常用,而是使用Mybatis技术来解决!