Spring数据访问策略_使用JdbcTemplate

 
Spring数据访问策略_使用JdbcTemplate
2007年12月12日 星期三 15:37

-------------------------------------------------------------------------------------------
Spring数据访问策略_使用JdbcTemplate
-------------------------------------------------------------------------------------------
五个文件
config.xml
User.java
UserDao.java
JdbcTemplateUserDao.java
demo.java
-------------------------------------------------------------------------------------------
config.xml
-------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<!-- 定义DataSource -->

<bean >
   <property value="com.mysql.jdbc.Driver" />
   <property value="jdbc:mysql://localhost:3306/test" />
   <property value="root" />
   <property value="root" />
</bean>

<!-- 使用JdbcTemplate的DAO -->

<bean >
   <property ref="dataSource" />
</bean>

</beans>


-------------------------------------------------------------------------------------------
User.java
-------------------------------------------------------------------------------------------
package dao;
public class User
{
private String name;
private String department;
public String getDepartment()
{
   return department;
}
public void setDepartment(String department)
{
   this.department = department;
}
public String getName()
{
   return name;
}
public void setName(String name)
{
   this.name = name;
}
}

-------------------------------------------------------------------------------------------
UserDao.java

-------------------------------------------------------------------------------------------
package dao;
import java.util.List;
public interface UserDao
{
List<User> queryAll();
}

-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
JdbcTemplateUserDao.java

-------------------------------------------------------------------------------------------
package dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class JdbcTemplateUserDao extends JdbcDaoSupport implements UserDao
{
class UserRowMapper implements RowMapper
{
   public Object mapRow(ResultSet rs, int rowNum) throws SQLException
   {
    User user = new User();
    user.setDepartment(rs.getString("department"));
    user.setName(rs.getString("name"));
    return user;
   }
}
public List<User> queryAll()
{
   return getJdbcTemplate().query("select * from User",
     new UserRowMapper());
}
}


-------------------------------------------------------------------------------------------
demo.java
-------------------------------------------------------------------------------------------
import java.util.Iterator;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dao.User;
import dao.UserDao;
public class demo
{
public static void main(String[] args)
{
   ApplicationContext context = new ClassPathXmlApplicationContext(
     "config.xml");
   UserDao userDao = (UserDao) context.getBean("jdbcTemplateUserDao");
   List list = userDao.queryAll();
   Iterator iter = list.iterator();
   while (iter.hasNext())
   {
    User user = (User) iter.next();
    System.out.println(user.getName());
   }
   System.out.println("===END===");
}
}


-------------------------------------------------------------------------------------------


本篇日志被作者设置为禁止发表新评论


©2008 Baidu



引文来源  Spring数据访问策略_使用JdbcTemplate_熊熊之家
展开阅读全文

没有更多推荐了,返回首页