1.创建项目,项目名称(springdemo6),如图所示

wKiom1jR3AbQhVHkAAAVFKz7WoQ437.png-wh_50


2.在项目中创建目录(src->源码目录,test->测试目录,source->配置文件目录,lib->jar包目录),如图所示

wKiom1jR3E6ixobtAAAva2qfx6w223.png-wh_50


3.在lib中创建相应的jar包目录,主要用于区分jar包.如图所示

wKiom1jR3HfQVFa6AAA7HOGPIxA306.png-wh_50


4.在lib的相应的jar包目录中添加jar包.如图所示

wKiom1jR3K6Rr9aiAACbv6AMkJA744.png-wh_50


5.在src目录创建实体Bean Forum,包名(com.mycompany.shequ.bean),如图所示

wKiom1jR3NuDqfKLAABEOtis2u8247.png-wh_50


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)如图所示

wKioL1jR3T6hdq8WAABFIrMNXvk496.png-wh_50


8.接口ForumDao的内容如下

package com.mycompany.shequ.dao;

import com.mycompany.shequ.bean.Forum;

public interface ForumDao {
	public Forum findByForumId(int fid);
}


9.在src目录中创建RowMapper的实现类ForumRowMapper,包名(com.mycompany.shequ.dao.impl),如图所示

wKioL1jR3bnjODa_AABG381i_Pw775.png-wh_50


10.RowMapper的实现类ForumRowMapper的内容如下

package com.mycompany.shequ.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.mycompany.shequ.bean.Forum;

public class ForumRowMapper implements RowMapper<Forum> {

	@Override
	public Forum mapRow(ResultSet rs, int rowNum) throws SQLException {
		Forum forum = new Forum();
		forum.setFid(rs.getInt("fid"));
		forum.setName(rs.getString("name"));
		return forum;
	}
	
}


11.在src目录中创建ForumDao的实现类ForumDaoImpl,包名(com.mycompany.shequ.dao.impl),如图所示

wKioL1jR3hiQXAD0AABMFyg0eM0365.png-wh_50


12.ForumDao的实现类ForumDaoImpl的内容如下

package com.mycompany.shequ.dao.impl;


import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
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 Forum findByForumId(int fid) {
		String sql = "select * from hnsq_forum where fid = ?";
		Forum forum = (Forum)getJdbcTemplate().queryForObject(sql, new Object[]{fid},
		new ForumRowMapper());
		return forum;
	}
}


13.在source目录中创建配置文件spring-datasource.xml,如图所示

wKioL1jR3oizBr_iAAA4rVbsG0Y252.png-wh_50


14.配置文件spring-datasource.xml的内容如下

<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-3.0.xsd">

	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">

		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/b_shequ_two" />
		<property name="username" value="root" />
		<property name="password" value="" />
	</bean>

</beans>


15.在source目录中创建配置文件applicationContext.xml,如图所示

wKiom1jR3tDgHnH6AAA5HKgfms8755.png-wh_50


16.配置文件applicationContext.xml的内容如下

<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">
	
	<import resource="spring-datasource.xml" />

	<bean id="forumDao" class="com.mycompany.shequ.dao.impl.ForumDaoImpl">
		<property name="dataSource" ref="dataSource" />
	</bean>

</beans>


17.在test目录中创建ForumDaoImplTest测试类,包名(com.mycompany.shequ.dao.impl),如图所示

wKiom1jR3xegyiHPAAA6OVqDHnA478.png-wh_50


18.ForumDaoImplTest测试类的内容如下


package com.mycompany.shequ.dao.impl;

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 testFindByForumId(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		ForumDao forumDao = (ForumDao) context.getBean("forumDao");
		
		Forum forum = forumDao.findByForumId(40);
		System.out.println(forum.getName());
	}
}


19.运行测试类中的testFindByForumId方法,运行结果如图所示

wKioL1jR34LSbvStAAEypKfDw-E574.png-wh_50


20.自定义的RowMapping完成.

wKioL1jUiM7A5WV8AABwCKMO5XE292.png-wh_50