改进了-------spring用JdbcTemplate写的DAO

    技术2022-05-11  70

     1。**************************************首先看DAO类怎么具体实现***********************************************package com.yinbodotcc;/* * 说明,在下面的例子中,凡是用?的均是我喜欢的,至少以后不用平凡用单引号了,呵呵; *  * 下面这两种格式是通用的, * jdbcTemplate.update(sql, new Object[]{new Integer(year)}); * jdbcTemplate.queryForXX(sql,new Object[]{name}) *  */import java.util.*;import javax.sql.DataSource;

    import org.springframework.jdbc.core.JdbcTemplate;import java.sql.*;import org.springframework.jdbc.core.PreparedStatementCreator;import org.springframework.jdbc.core.PreparedStatementSetter;import org.springframework.jdbc.core.BatchPreparedStatementSetter;

     

    public class BookDao2 implements IBookDao{ private JdbcTemplate jdbcTemplate;  //设置DataSource,并生成JdbcTemplate实例 public void setDs(DataSource ds) {  jdbcTemplate=new JdbcTemplate(ds); }  //以书名为依据来找寻某本/些书 public List<Book> select(String name) {  List<Book> books=new ArrayList<Book>();    //下面这个也可以,但是我还是觉得用?的方式比较好  //String sql="select * from book where name='"+name+"'";  //Iterator it=jdbcTemplate.queryForList(sql).iterator();      String sql="select * from book where name=?";  Iterator it=jdbcTemplate.queryForList(sql,new Object[]{name}).iterator();    while(it.hasNext())  {   Map m=(Map)it.next();   String theName=m.get("name").toString();   int theBirthday=new Integer(m.get("pbYear").toString());   Book tempBook=new Book();   tempBook.setName(theName);   tempBook.setPbYear(theBirthday);   books.add(tempBook);     }  return books;      }  //插入某本书 public void insertBook(Book book) {  //String sql="insert into book(name,pbYear) values('"+book.getName()+"',"+book.getPbYear()+")";  //jdbcTemplate.execute(sql);  //或者用update();    String sql="insert into book(name,pbYear) values(?,?)";  jdbcTemplate.update(sql, new Object[]{book.getName(),book.getPbYear()});    }  //插入很多书 public void insertBooks(List<Book> book) {  final List<Book> tempBook=book;  String sql="insert into book(name,pbYear) values(?,?)";  jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter()  {   public void setValues(PreparedStatement ps,int i)throws SQLException   {    String name=tempBook.get(i).getName();    int pbYear=tempBook.get(i).getPbYear();    ps.setString(1, name);    ps.setInt(2, pbYear);   }   public int getBatchSize()   {    return tempBook.size();   }  });     }  //更新书的出版年月 public void updateBookPbYear(Book book,int year) {  //内部类要访问得外围类得变量,必须是final  final String name=book.getName();     final String sql="update book set pbYear=? where name='"+name+"'";     final int temYear=year;    jdbcTemplate.update(new PreparedStatementCreator()  {   public PreparedStatement createPreparedStatement(Connection con)throws SQLException   {    PreparedStatement ps=con.prepareStatement(sql);    ps.setInt(1,temYear);    return ps;   }  });    jdbcTemplate.update(sql, new PreparedStatementSetter()  {   public void setValues(PreparedStatement ps)throws SQLException   {    ps.setInt(1,temYear);   }  });            //虽然需要得是int,但是我们这里给了它Integer是可以得  //jdbcTemplate.update(sql, new Object[]{new Integer(year)});    //当然直接updae里面放sql语句也是可以的。  }  //从库中删除某书 public void delete(Book book) {  String sql="delete from book where name=?";  jdbcTemplate.update(sql, new Object[]{book.getName()}); }  //查询库中所有的书 public List<Book> selectAll() {  List<Book> books=new ArrayList<Book>();        String sql="select * from book";  Iterator it=jdbcTemplate.queryForList(sql).iterator();    while(it.hasNext())  {   Map m=(Map)it.next();   String theName=m.get("name").toString();   int theBirthday=new Integer(m.get("pbYear").toString());   Book tempBook=new Book();   tempBook.setName(theName);   tempBook.setPbYear(theBirthday);   books.add(tempBook);     }  return books;  }}

    2。***************************************看看接口DAO的具体方法****************************************package com.yinbodotcc;import java.util.*;

    public interface IBookDao { public List<Book> select(String name); public void insertBook(Book book); public void updateBookPbYear(Book book,int year); public void insertBooks(List<Book> book); public void delete(Book book); public List<Book> selectAll();

    }

    3。**************************************看看具体配置文件*************************************************

    <?xml version="1.0" encoding="GBK"?><!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" destroy-method="close"> <property name="driverClassName">  <value>com.mysql.jdbc.Driver</value> </property>  <property name="url">  <value>jdbc:mysql://localhost:3306/yay</value> </property>  <property name="username">  <value>root</value> </property>  <property name="password">  <value>qwe123</value> </property></bean>

    <bean id="bookDao" class="com.yinbodotcc.BookDao"> <property name="ds">  <ref bean="datasource"/> </property></bean><bean id="bookDao2" class="com.yinbodotcc.BookDao2"> <property name="ds">  <ref bean="datasource"/> </property></bean></beans>

     

    最新回复(0)