1.Spring中使用JdbcTemplate封装对Jdbc的支持,使用Spring JdbcTeamplate的例子如下:
(1).假设如下SQL表中有数据username=test1,passwd=test1,address=test1 :
[java] view plaincopyprint?
- CREATE TABLE `login` (
- `username` varchar(10) default NULL,
- `passwd` varchar(10) default NULL,
- `address` varchar(10) default NULL
- ) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
(2).在Spring配置文件中添加关于数据源和JdbcTeamplate的配置如下:
[xhtml] view plaincopyprint?
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
- <beans>
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <property name="url">
- <value>jdbc:mysql://localhost:3306/javaee</value>
- </property>
- <property name="username">
- <value>root</value>
- </property>
- <property name="password">
- <value>1234</value>
- </property>
- </bean>
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource">
- <ref local="dataSource"/>
- </property>
- </bean>
- <bean id="personDAO" class="SpringJDBCSupport.ReadData.PersonDAO">
- <property name="jdbcTemplate">
- <ref local="jdbcTemplate"/>
- </property>
- </bean>
- </beans>
(3).Java持久化对象如下:
[java] view plaincopyprint?
- package SpringJDBCSupport.ReadData;
- import com.mysql.jdbc.Driver;
- public class Person {
- private String name;
- private String password;
- private String address;
- public Person(){
- }
- public Person(String name,String password,String address){
- this.name=name;
- this.password=password;
- this.address=address;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String toString(){
- return this.getName()+"-"+this.getPassword()+"-"+this.getAddress();
- }
- }
(4).使用JdbcTeamplate的DAO如下:
[java] view plaincopyprint?
- package SpringJDBCSupport.ReadData;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Types;
- import java.util.List;
- import org.springframework.jdbc.core.BatchPreparedStatementSetter;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.jdbc.core.RowCallbackHandler;
- public class PersonDAO {
- private JdbcTemplate jdbcTemplate;
- public JdbcTemplate getJdbcTemplate() {
- return jdbcTemplate;
- }
- public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
- this.jdbcTemplate = jdbcTemplate;
- }
- public int insertPersonUseUpdate(Person person){
- String sql="insert into login values(?,?,?)";
- Object[] params=new Object[]{
- person.getName(),
- person.getPassword(),
- person.getAddress()
- };
- return this.getJdbcTemplate().update(sql,params);
- }
- public int insertPersonUseExecute(Person person){
- String sql="insert into login values(?,?,?)";
- Object[] params=new Object[]{
- person.getName(),
- person.getPassword(),
- person.getAddress()
- };
- int[] types=new int[]{
- Types.VARCHAR,
- Types.VARCHAR,
- Types.VARCHAR
- };
- return this.getJdbcTemplate().update(sql,params,types);
- }
- public int[] updatePersonUseBatchUpdate( final List persons){
- String sql="insert into login values(?,?,?)";
- BatchPreparedStatementSetter setter=null;
- //使用匿名内部类,实现BatchPreparedStatementSetter接口,实现批量更新
- setter=new BatchPreparedStatementSetter(){
- public int getBatchSize(){
- return persons.size();
- }
- public void setValues(PreparedStatement ps,int index) throws SQLException{
- Person person=(Person)persons.get(index);
- ps.setString(1,person.getName());
- ps.setString(2,person.getPassword());
- ps.setString(3,person.getAddress());
- }
- };
- return this.getJdbcTemplate().batchUpdate(sql,setter);
- }
- public Person getPersonByRowCallbackHandler(String username){
- String sql="select * from login where username=?";
- final Person person=new Person();
- final Object params[]=new Object[]{username};
- //使用匿名内部类,实现RowCallbackHandler接口,即实现查询结果集的RowMapping
- this.getJdbcTemplate().query(sql,params,new RowCallbackHandler(){
- public void processRow(ResultSet rs)throws SQLException{
- person.setName(rs.getString("username"));
- person.setPassword(rs.getString("passwd"));
- person.setAddress(rs.getString("address"));
- }
- });
- return person;
- }
- }