Spring JDBC 示例
Posted 爱吃西瓜的番茄酱
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring JDBC 示例相关的知识,希望对你有一定的参考价值。
准备工作
-
创建一个 Spring 应用程序
-
在项目中添加 Spring JDBC 指定的最新的库
mysql-connector-java.jar,
org.springframework.jdbc.jar
org.springframework.transaction.jar。 -
创建 DAO 接口 StudentDAO 并列出所有必需的方法
-
创建其他的必需的 Java 类 Student,StudentMapper,StudentJDBCTemplate 和 MainApp
-
确保你已经在 TEST 数据库中创建了 Student 表。并确保你的 MySQL 服务器运行正常,且你可以使用给出的用户名和密码读/写访问数据库。
示例
以下是数据访问对象接口文件 StudentDAO.java 的内容:
package hello;
import java.util.List;
import javax.sql.DataSource;
public interface StudentDAO {
/**
* This is the method to be used to initialize
* database resources ie. connection.
*/
public void setDataSource(DataSource ds);
/**
* This is the method to be used to create
* a record in the Student table.
*/
public void create(String name, int age);
/**
* This is the method to be used to list down
* a record from the Student table corresponding
* to a passed student id.
*/
public Student getStudent(int id);
/**
* This is the method to be used to list down
* all the records from the Student table.
*/
public List<Student> listStudents();
/**
* This is the method to be used to delete
* a record from the Student table corresponding
* to a passed student id.
*/
public void delete(int id);
/**
* This is the method to be used to update
* a record into the Student table.
*/
public void update(int id, int age);
}
下面是 Student.java 文件的内容:
package hello;
//import org.springframework.beans.factory.annotation.Autowired;
public class Student {
private int age;
private String name;
private int id;
public void setAge(int age){
this.age = age;
}
public int getAge(){
//System.out.println("age:"+age);
return age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
//System.out.println("name:"+name);
return name;
}
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
}
以下是 StudentMapper.java 文件的内容:
package hello;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public class StudentMapper implements RowMapper<Student> {
public Student mapRow(ResultSet rs, int rowNum) throws SQLException{
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
return student;
}
}
下面是为定义的 DAO 接口 StudentDAO 的实现类文件 StudentJDBCTemplate.java:
package hello;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
public class StudentJDBCTemplate implements StudentDAO {
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource){
this.dataSource = dataSource;
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public void create(String name, int age){
String SQL = "insert into Student(name, age) values(? ,?)";
jdbcTemplate.update(SQL,name, age);
System.out.println("Create Record Name = " + name + " Age = " + age);
return;
}
public Student getStudent(int id){
String SQL = "select * from Student where id =?";
Student student = jdbcTemplate.queryForObject(SQL,
new Object[]{id}, new StudentMapper());
return student;
}
public List<Student> listStudents(){
String SQL = "select * from Student";
List<Student> students = jdbcTemplate.query(SQL,
new StudentMapper());
return students;
}
public void delete(int id){
String SQL="delete from Student where id = ?";
jdbcTemplate.update(SQL,id);
System.out.println("Delete Record with ID = "+ id);
return;
}
public void update(int id, int age){
String SQL = "update Student set age = ? where id = ?";
jdbcTemplate.update(SQL,age, id);
System.out.println("Update Record with ID = "+ id);
return;
}
}
以下是 MainApp.java 文件的内容:
package hello;
import java.util.List;
import hello.StudentJDBCTemplate;
//import org.springframework.context.support.AbstractApplicationContext;
//import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//import org.springframework.context.annotation.*;
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate)context.getBean("studentJDBCTemplate");
System.out.println("=====Record Creation=====");
studentJDBCTemplate.create("番茄",11);
studentJDBCTemplate.create("鸡蛋", 2);
studentJDBCTemplate.create("西瓜", 15);
System.out.println("=====Listing Multiple Records=====");
List<Student> students = studentJDBCTemplate.listStudents();
for (Student record: students){
System.out.println("ID: "+ record.getId());
System.out.println("Name: "+ record.getName());
System.out.println("Age: "+record.getAge());
}
System.out.println("====Updateing Record with ID = 2====");
studentJDBCTemplate.update(2, 20);
System.out.println("===Listing Record with ID = 2====");
Student student = studentJDBCTemplate.getStudent(2);
System.out.println("ID: "+ student.getId());
System.out.println("Name: "+ student.getName());
System.out.println("Age: "+ student.getAge());
}
}
下述是配置文件 Beans.xml 的内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- Initialization for data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/TEST?&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- Definition for studentJDBCTemplate bean -->
<bean id="studentJDBCTemplate"
class="hello.StudentJDBCTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
运行应用程序:
=====Record Creation=====
Create Record Name = 番茄 Age = 11
Create Record Name = 鸡蛋 Age = 2
Create Record Name = 西瓜 Age = 15
=====Listing Multiple Records=====
ID: 1
Name: 番茄
Age: 11
ID: 2
Name: 鸡蛋
Age: 20
ID: 3
Name: 西瓜
Age: 15
ID: 4
Name: 番茄
Age: 11
ID: 5
Name: 鸡蛋
Age: 2
ID: 6
Name: 西瓜
Age: 15
ID: 7
Name: 番茄
Age: 11
ID: 8
Name: 鸡蛋
Age: 2
ID: 9
Name: 西瓜
Age: 15
====Updateing Record with ID = 2====
Update Record with ID = 2
===Listing Record with ID = 2====
ID: 2
Name: 鸡蛋
Age: 20
Process finished with exit code 0
每天学习一点点,每天进步一点点。
以上是关于Spring JDBC 示例的主要内容,如果未能解决你的问题,请参考以下文章
使用 Spring Integration JDBC Outbound Gateway 更新数据