日期:2014-05-16 浏览次数:20437 次
?
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="springDSN" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"> </property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false" lazy-init="false" autowire="default" dependency-check="default"> <property name="dataSource"> <ref bean="springDSN" /> </property> </bean> </beans>
?public final class SpringUtil {
private static ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
public static Object getBean(String beanName) {
return ctx.getBean(beanName);
}
}
public class TestDao { private JdbcTemplate jdbcT = (JdbcTemplate) SpringUtil .getBean("jdbcTemplate"); public List findALL() { String sql = "select * from test"; return jdbcT.queryForList(sql); } public List<Test> findALLBooks() { String sql = "select * from test"; List list = jdbcT.queryForList(sql); Iterator iterator = list.iterator(); Book book = null; while (iterator.hasNext()) { Map map = (Map) iterator.next(); System.out.println((String)map.get("id")); System.out.println((String)map.get("name")); } return null; } public int delete(int bid) { String sql = "delete from BookInfo where bid =?"; return jdbcT.update(sql, new Object[] { bid }); } public static void main(String[] args) { new TestDao().findALLBooks(); } }