1.创建项目,项目名称(springdemo9),如图所示
2.在项目中创建目录(src->源码目录,test->测试目录,source->配置文件目录,lib->jar包目录),如图所示
3.在lib中创建相应的jar包目录,主要用于区分jar包.如图所示
4.在lib的相应的jar包目录中添加jar包.如图所示
5.在src目录创建实体Bean Forum,包名(com.mycompany.shequ.bean),如图所示
6.实体Bean Forum的内容如下
package com.mycompany.shequ.bean;public class Forum { private int fid; private String name; public int getFid() { return fid; } public void setFid(int fid) { this.fid = fid; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
7.在src目录创建接口ForumDao,包名(com.mycompany.shequ.dao)如图所示
8.接口ForumDao的内容如下
package com.mycompany.shequ.dao;import java.util.List;import com.mycompany.shequ.bean.Forum;public interface ForumDao { public ListfindAll();}
9.在src目录中创建ForumDao的实现类ForumDaoImpl,包名(com.mycompany.shequ.dao.impl),如图所示
10.ForumDao的实现类ForumDaoImpl的内容如下
package com.mycompany.shequ.dao.impl;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.support.JdbcDaoSupport;import com.mycompany.shequ.bean.Forum;import com.mycompany.shequ.dao.ForumDao;public class ForumDaoImpl extends JdbcDaoSupport implements ForumDao { @Override public ListfindAll() { String sql = "select * from hnsq_forum"; List forumList = new ArrayList (); forumList = getJdbcTemplate().query(sql, new BeanPropertyRowMapper(Forum.class)); return forumList; }}
11.在source目录中创建配置文件spring-datasource.xml,如图所示
12.配置文件spring-datasource.xml的内容如下
13.在source目录中创建配置文件applicationContext.xml,如图所示
14.配置文件applicationContext.xml的内容如下
15.在test目录中创建ForumDaoImplTest测试类,包名(com.mycompany.shequ.dao.impl),如图所示
16.ForumDaoImplTest测试类的内容如下
package com.mycompany.shequ.dao.impl;import java.util.List;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.mycompany.shequ.bean.Forum;import com.mycompany.shequ.dao.ForumDao;public class ForumDaoImplTest { @Test public void testFindAll(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ForumDao forumDao = (ForumDao) context.getBean("forumDao"); ListforumList = forumDao.findAll(); for(Forum forum : forumList){ System.out.println(forum.getFid() + "_____" + forum.getName()); } }}
17.运行测试类中的testFindAll方法,运行结果如图所示