当我向我的一个朋友推荐spring+hibernate+Struts这个模式时,他提供出一些看法。虽然他接受了我的spring推荐,但还是认为使用hibernate太麻烦,并问能否在spring中使用jdbc。我说,那好吧。我来写个例子吧。以下就是这个例子,注意这只是个例子,功能很弱且只是代码和配置,没有讲解。
一,spring配置applicationContext.xml
<?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.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:microsoft:sqlserver://192.168.0.18:1433</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value>jfc</value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="userDAO" class="com.yang.spring.jdbc.UserDAO" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate" />
</property>
</bean>
</beans>
二,UserDAO.java
/**
*
*/
package com.yang.spring.jdbc;
import java.util.List;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
/**
* @author lanxincao
*
*/
public class UserDAO extends JdbcDaoSupport {
public List getAllUserList() {
logger.debug("getting all users");
try {
return getJdbcTemplate().queryForList("select userid,username,password from [books].[dbo].[user]");
}catch (RuntimeException re) {
logger.error("getting users failed",re);
throw re;
}
}
public int saveOrUpdate(String sql,Object[] params,int[] types){
logger.debug("save or update database");
try {
return getJdbcTemplate().update(sql,params,types);
} catch (RuntimeException re) {
logger.error("save or udpate database failed");
throw re;
}
}
}
三,测试类Main.java
/**
*
*/
package com.yang.spring.jdbc;
import java.util.Map;
import java.util.Iterator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author lanxincao
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/com/yang/spring/jdbc/applicationContext.xml");
UserDAO userDAO = (UserDAO)ctx.getBean("userDAO");
Iterator iterator = userDAO.getAllUserList().iterator();
System.out.println("user password");
while(iterator.hasNext()){
Map user = (Map)iterator.next();
System.out.println(user.get("username")+" "+user.get("password"));
}
}
}
附注:如果可以,希望还是用hibernate。这个写总觉得有点不顺。使用hibernate也不会增加学习成本。呵呵。
一,spring配置applicationContext.xml
<?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.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:microsoft:sqlserver://192.168.0.18:1433</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value>jfc</value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="userDAO" class="com.yang.spring.jdbc.UserDAO" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate" />
</property>
</bean>
</beans>
二,UserDAO.java
/**
*
*/
package com.yang.spring.jdbc;
import java.util.List;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
/**
* @author lanxincao
*
*/
public class UserDAO extends JdbcDaoSupport {
public List getAllUserList() {
logger.debug("getting all users");
try {
return getJdbcTemplate().queryForList("select userid,username,password from [books].[dbo].[user]");
}catch (RuntimeException re) {
logger.error("getting users failed",re);
throw re;
}
}
public int saveOrUpdate(String sql,Object[] params,int[] types){
logger.debug("save or update database");
try {
return getJdbcTemplate().update(sql,params,types);
} catch (RuntimeException re) {
logger.error("save or udpate database failed");
throw re;
}
}
}
三,测试类Main.java
/**
*
*/
package com.yang.spring.jdbc;
import java.util.Map;
import java.util.Iterator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author lanxincao
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/com/yang/spring/jdbc/applicationContext.xml");
UserDAO userDAO = (UserDAO)ctx.getBean("userDAO");
Iterator iterator = userDAO.getAllUserList().iterator();
System.out.println("user password");
while(iterator.hasNext()){
Map user = (Map)iterator.next();
System.out.println(user.get("username")+" "+user.get("password"));
}
}
}
附注:如果可以,希望还是用hibernate。这个写总觉得有点不顺。使用hibernate也不会增加学习成本。呵呵。