Spring持久层(一)Spring JdbcTemplate 的例子

    技术2022-05-19  24

    使用SQL2005的数据库。

     

    1 Spring Bean定义文件test-cintext.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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:db.properties </value> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db.driverClassName}" /> <property name="url" value="${db.url}NIHAO" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> <property name="initialSize" value="2" /> <property name="maxActive" value="20" /> <property name="maxWait" value="-1" /> <property name="maxIdle" value="10" /> <property name="removeAbandoned" value="true" /> <property name="removeAbandonedTimeout" value="30000" /> <property name="logAbandoned" value="true" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="productDao" class="springapp.repository.JdbcTemplateProductDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> </beans>

     

    2 数据库连接的属性文件db.properties

    #Created by JInto - www.guh-software.de #Thu Dec 02 12:02:06 CST 2010 db.password=1234 db.port=1433 db.url=jdbc/:sqlserver/://127.0.0.1/:1433;databaseName/= db.username=sa db.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver

     

    3 数据库名字NOHAO,表名PRODUCTS,表结构如下

     

     

    4 Product.java源码

     public class Product implements Serializable { private static final long serialVersionUID = 2914160291772344637L; private int id; private String description; private Double price; public void setId(int i) { id = i; } public int getId() { return id; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append("Description: " + description + ";"); buffer.append("Price: " + price); return buffer.toString(); } }

     

    5 ProductDao.java接口的源码

    public interface ProductDao { public List<Product> getProductList(); public void saveProduct(Product prod); }

     

    6 JdbcTemplateProductDao.java的源码

    public class JdbcTemplateProductDao implements ProductDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public List<Product> getProductList() { String sql = "select id, description, price from products"; List result = jdbcTemplate.query(sql, new RowMapper() { @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { // List<Product> list = new ArrayList<Product>(); // for (int i = 0; i < rowNum; i++) { Product pro = new Product(); pro.setDescription(rs.getString(2)); pro.setId(rs.getInt(1)); pro.setPrice(rs.getDouble(3)); return pro; // } } }); return result; } @Override public void saveProduct(Product prod) { String sql = "insert into products (id, price, description) values (?,?,?)"; jdbcTemplate.update(sql, new Object[] { prod.getId(), prod.getPrice(), prod.getDescription() }); } }

     

    7 测试程序的代码

    public class JdbcProductDaoTest { static ApplicationContext ctx = new ClassPathXmlApplicationContext( new String[] { "file:src/test-context.xml" }); public static void main(String[] args) { ProductDao dao = (ProductDao) ctx.getBean("productDao"); Product product = new Product(); product.setId(1); product.setPrice(12.1); product.setDescription("description"); dao.saveProduct(product); List<Product> list = dao.getProductList(); for (Product pro : list) { System.out.println("JdbcProductDaoTest.main()" + pro.getDescription()); } } }


    最新回复(0)