怎么样使用JdbcTemplate增加一组数据?
怎么样使用JdbcTemplate增加一组数据?
一个使用JdbcTemplate增加一组数据的完整的例子,简单的代码不再解释:
1) 首先来创建一个数据库和表,这里用的是Sql server数据库:
create database Human
go
create table Person
(
id int primary key,
firstName varchar(20),
lastName varchar(20)
)
go
2) 创建一个Person类:
package cn.itcast;
public class Person {
private int id;
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
3) 创建一个 MyBatchPreparedStatementSetter 类。此类实现 BatchPreparedStatementSetter 接口。
package cn.itcast;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
public class MyBatchPreparedStatementSetter implements
BatchPreparedStatementSetter {
//这里用于测试的是一组Person对象。
List<Person> persons;
public void setPersons(List<Person> persons) {
this.persons = persons;
}
//重写 BatchPreparedStatementSetter 的 setValues() 和 getBatchSize() 方法。
//setValues() 方法用于设置 sql 语句里面的参数
public void setValues(PreparedStatement ps, int index) throws SQLException {
Person person = persons.get(index);
ps.setInt(1, person.getId());
ps.setString(2, person.getFirstName());
ps.setString(3, person.getLastName());
}
//getBatchSize()返回这一组数据的大校
public int getBatchSize() {
return persons.size();
}
}
4) 创建一个 MyJdbcTemplate 类:
package cn.itcast;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class MyJdbcTemplate {
//定义一个 JdbcTemplate 对象,用于调用 batchUpdate()方法。
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int[] updatePersons() {
//从xml配置文件中读取出注册好的 MyBatchPreparedStatementSetter 对象
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
String sql = "insert into Person values (?,?,?)";
MyBatchPreparedStatementSetter setter = (MyBatchPreparedStatementSetter)
context
.getBean("myBatchPreparedStatementSetter");
return jdbcTemplate.batchUpdate(sql, setter);
}
}
5) 一个客户端的类:
package cn.itcast;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
private static Log log = LogFactory.getLog(Client.class);
public static void main(String[] args) {
ApplicationContext context = null;
//从xml文件中读取出已经注册好的MyJdbcTemplate对象:
context = new ClassPathXmlApplicationContext("/applicationContext.xml");
MyJdbcTemplate myJdbcTemplate = (MyJdbcTemplate) context
.getBean("myJdbcTemplate");
//调用MyJdbcTemplate的updatePersons()方法。此方法返回一个整型数组,数组里的每一个数如果是 1
就说明sql语句执行成功。
int[] arr = myJdbcTemplate.updatePersons();
for (int n : arr) {
if (n == 1) {
log.info("增加成功");
}
}
}
}
6) 数据库连接配置信息的属性文件 jdbc.properties:
url=jdbc:sqlserver://127.0.0.1:1433;database=Study
driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
username=sa
password=
7) XML 文件 applicationContext.xml:
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring
-beans.dtd">
<beans>
//注册一个PropertyPlaceholderConfigurer,此Bean的location属性告诉Spring去哪找配置信息。
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>jdbc.properties</value>
</property>
</bean>
//注册一个DataSource,此例用的是Spring自带的DriverManagerDataSource
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${driverClassName}</value>
</property>
<property name="url">
<value>${url}</value>
</property>
<property name="username">
<value>${username}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
</bean>
//注册一个JdbcTemplate,此Bean的dataSource属性引用上面的dataSource
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
//注册一个我们自定义的MyJdbcTemplate,此Bean的jdbcTemplate属性引用上面的jdbcTemplate
<bean id="myJdbcTemplate" class="cn.itcast.MyJdbcTemplate">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate" />
</property>
</bean>
//注册5个Person对象
<bean id="person1" class="cn.itcast.Person">
<property name="id">
<value>1</value>
</property>
<property name="firstName">
<value>田</value>
</property>
<property name="lastName">
<value>三</value>
</property>
</bean>
<bean id="person2" class="cn.itcast.Person">
<property name="id">
<value>2</value>
</property>
<property name="firstName">
<value>常</value>
</property>
<property name="lastName">
<value>四</value>
</property>
</bean>
<bean id="person3" class="cn.itcast.Person">
<property name="id">
<value>3</value>
</property>
<property name="firstName">
<value>金</value>
</property>
<property name="lastName">
<value>五</value>
</property>
</bean>
<bean id="person4" class="cn.itcast.Person">
<property name="id">
<value>4</value>
</property>
<property name="firstName">
<value>徐</value>
</property>
<property name="lastName">
<value>六</value>
</property>
</bean>
<bean id="person5" class="cn.itcast.Person">
<property name="id">
<value>5</value>
</property>
<property name="firstName">
<value>张</value>
</property>
<property name="lastName">
<value>七</value>
</property>
</bean>
//注册一个MyBatchPreparedStatementSetter,设置它的persons属性,此属性是一个包含Person的List
,
//这里的就引用上面注册好的5个Person对象
<bean id="myBatchPreparedStatementSetter"
class="cn.itcast.MyBatchPreparedStatementSetter">
<property name="persons">
<list>
<ref bean="person1" />
<ref bean="person2" />
<ref bean="person3" />
<ref bean="person4" />
<ref bean="person5" />
</list>
</property>
</bean>
</beans>
8) Log4j配置文件log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="false" xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="systemout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.SimpleLayout" />
</appender>
<root>
<level value="info" />
<appender-ref ref="systemout" />
</root>
</log4j:configuration>
9)运行:
INFO - 增加成功
INFO - 增加成功
INFO - 增加成功
INFO - 增加成功
INFO - 增加成功
我们看到输出了5条增加成功的信息,
再来看看数据库中Person表:
select * from Person
1田三
2常四
3金五
4徐六
5张七