Spring进行JDBC操作
前言:传统的JDBC操作非常的繁琐,为了使JDBC更加便于使用,Spring整合了JDBC,以此建立一个JDBC存取框架: JdbcTemplate
1.开发环境
基本开发环境: 1.JDK:-8u101-windows-x64 2.Mysql:5.5 3.Maven配置
注意事项:1.jdk-9.0.4会报错:空指针异常尽量用jdk1.8
2.如果用idea2018或以上版本记得加上pom.xml的插件
pom.xml配置
4.0.0
org.example
spring_jdbc_zsgc
pom
1.0-SNAPSHOT
mysql
mysql-connector-java
5.1.32
junit
junit
4.12
test
org.springframework
spring-jdbc
5.0.2.RELEASE
org.springframework
spring-context
5.0.2.RELEASE
c3p0
c3p0
0.9.1.2
org.aspectj
aspectjweaver
1.8.7
mysql
mysql-connector-java
5.1.32
UTF-8
1.7
1.7
org.apache.maven.plugins
maven-compiler-plugin
3.2
1.8
UTF-8
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
utf-8
80
/
2.Mysql库名表名
2.1数据库名>数据库字段
-- 建库
create database travel;
-- 建表
create table key_table(
id int not null,
`name` varchar(20),
dates varchar(20)
);
3.创建包以及接口和类
分层思想:
com.xxx
dao(接口层)
keyTableDao(接口)
impl
- keyTableDaoImpl(实现类)
pojo
- keyTable(实体类)
service(服务层)
keyTableService(接口)
- impl
- keyTableServiceImpl
4.配置beans.xml以及properties
beans.xml(配置文件)
jdbc.properties(属性文件)
jdbc.driverClass = com.mysql.jdbc.Driver
jdbc.jdbcUrl = jdbc:mysql://localhost:3306/travel
jdbc.user = root
jdbc.password = root
注意事项:路径、账号、密码请用自己的。
5.实现dao层
5.1pojo
package com.xxx.pojo;
public class keyTable {
private int id;
private String name;
private String dates;
public keyTable() {
}
public keyTable(int id, String name, String dates) {
this.id = id;
this.name = name;
this.dates = dates;
}
@Override
public String toString() {
return "keyTable{" +
"id=" + id +
", name='" + name + '\'' +
", dates='" + dates + '\'' +
'}';
}
// get set
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDates() {
return dates;
}
public void setDates(String dates) {
this.dates = dates;
}
}
package com.xxx.dao;
import com.xxx.pojo.keyTable;
import java.util.List;
// 接口
public interface keyTableDao {
/*
增加
依据id删除
修改
依据id查询
*/
int AddTable(keyTable keyTable);
int DeleteByIdTable(int id);
int UpdateTable(keyTable keyTable);
List FindById(int id);
}
// 实现
package com.xxx.dao.impl;
import com.xxx.dao.keyTableDao;
import com.xxx.pojo.keyTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository("keyTableDao")
public class keyTableDaoImpl implements keyTableDao {
@Resource(name = "JdbcTemplate")
private JdbcTemplate template;
@Override
public int AddTable(keyTable keyTable) {
String sql = "insert into key_table values(?,?,?)";
int result = template.update(sql, keyTable.getId(), keyTable.getName(), keyTable.getDates());
return result;
}
@Override
public int DeleteByIdTable(int id) {
String sql = "delete from key_table where id = ?";
int result = template.update(sql,id);
return result;
}
@Override
public int UpdateTable(keyTable keyTable) {
String name = keyTable.getName();
int id = keyTable.getId();
String sql = "update key_table set name = ? where id = ?";
int result = template.update(sql, name, id);
return result;
}
@Override
public List FindById(int id) {
String sql = "select * from key_table where id = ?";
List query = template.query(sql, new RowMapper() {
@Override
public keyTable mapRow(ResultSet rs, int rowNum) throws SQLException {
keyTable k = new keyTable();
k.setId(rs.getInt(1));
k.setName(rs.getString(2));
k.setDates(rs.getString(3));
return k;
}
},id);
return query;
}
}
6.实现service层
package com.xxx.service;
import com.xxx.pojo.keyTable;
import java.util.List;
// 接口
public interface keyTableService {
/*
增加
依据id删除
修改
依据id查询
*/
int AddTable(keyTable keyTable);
int DeleteByIdTable(int id);
int UpdateTable(keyTable keyTable);
List FindById(int id);
}
package com.xxx.service.impl;
import com.xxx.dao.keyTableDao;
import com.xxx.pojo.keyTable;
import com.xxx.service.keyTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
// 实现类
@Service("keyTableService") // spring帮我创建的实现类
public class keyTableServiceImpl implements keyTableService{
@Autowired // 注入bean
private keyTableDao keyTableDao;
@Override
public int AddTable(keyTable keyTable) {
int result = keyTableDao.AddTable(keyTable);
return result;
}
@Override
public int DeleteByIdTable(int id) {
int result = keyTableDao.DeleteByIdTable(id);
return result;
}
@Override
public int UpdateTable(keyTable keyTable) {
int result = keyTableDao.UpdateTable(keyTable);
return result;
}
@Override
public List FindById(int id) {
List list = keyTableDao.FindById(id);
return list;
}
}
7.测试jdcb是否成功
package com.test;
import com.xxx.pojo.keyTable;
import com.xxx.service.keyTableService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestKeyService {
// 增加
@Test
public void AddTableTest(){
ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
keyTableService keyTableService = (keyTableService) app.getBean("keyTableService");
keyTable k = new keyTable();
k.setId(3);
k.setName("我是被增加的对象");
k.setDates("2021-12-23");
int result = keyTableService.AddTable(k);
if (result > 0){
System.out.println("执行成功");
} else {
System.out.println("失败了");
}
}
// 依据id删除
@Test
public void DeleteTableTest(){
ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
keyTableService keyTableService = (keyTableService) app.getBean("keyTableService");
int result = keyTableService.DeleteByIdTable(111);
if (result > 0){
System.out.println("执行成功");
} else {
System.out.println("失败了");
}
}
// 修改
@Test
public void UpdateTableTest(){
ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
keyTableService keyTableService = (keyTableService) app.getBean("keyTableService");
keyTable keyTable = new keyTable();
keyTable.setName("修改了");
keyTable.setId(17);
int result = keyTableService.UpdateTable(keyTable);
if (result > 0){
System.out.println("执行成功");
} else {
System.out.println("失败了");
}
}
// 依据id查询
@Test
public void FindAddTableTest(){
ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
keyTableService keyTableService = (keyTableService) app.getBean("keyTableService");
List list = keyTableService.FindById(1);
for (keyTable k:list) {
System.out.println(k);
}
}
}
完!