在此就以查询所有学生信息为例:
实体类
public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
dao层
public interface IStudentDAO {
/**
* 查询所有
* @return
*/
public List<Student> findAll();
}
dao层实现
public class StudentDAOImpl extends JdbcDaoSupport implements IStudentDAO{
public List<Student> findAll() {
final String sql="select *from Student";
List<Student> list= this.getJdbcTemplate().query(sql, new RowMapper<Student>() {
/**
*
* @param resultSet 单条记录读取器
* @param i 索引
* @return
* @throws SQLException
*/
public Student mapRow(ResultSet resultSet, int i) throws SQLException {
Student student=new Student();
student.setId(resultSet.getInt("id"));
student.setName(resultSet.getString("name"));
student.setAge(resultSet.getInt("age"));
return student;
}
});
return list;
}
}
servie层接口
public interface IStudentService {
/**
* 查询所有
* @return
*/
public List<Student> findAll();
}
service层实现
public class StudentServiceImpl implements IStudentService{
//植入dao
private IStudentDAO dao;
public IStudentDAO getDao() {
return dao;
}
public void setDao(IStudentDAO dao) {
this.dao = dao;
}
//查询所有
public List<Student> findAll() {
return dao.findAll();
}
}
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///school
jdbc.username=root
jdbc.password=123156
配置applicationContextJdbdTempLate.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--1.识别jdbc.properties-->
<!--方式一-->
<!-- <context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
<!--方式二-->
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean>
<!--2.数据源 spring内置数据源 DriverManagerDataSource-->
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>-->
<!--数据源 bdcp-->
<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>-->
<!--数据源 c3p0-->
<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>-->
<!--数据源 driud 阿里巴巴-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--jdbctemplate配置-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--dao层-->
<bean id="studentDao" class="cn.happy.jdbctemplate.dao.impl.StudentDAOImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!-- <!–dao层–>
<bean id="studentDao" class="cn.happy.jdbctemplate.dao.impl.StudentDAOImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>-->
<!--service层-->
<bean id="studentService" class="cn.happy.jdbctemplate.service.impl.StudentServiceImpl">
<property name="dao" ref="studentDao"></property>
</bean></beans>
测试类
@Test
public void testJdbc(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextJdbdTempLate.xml");
IStudentService student=(IStudentService)context.getBean("studentService");
List<Student> list = student.findAll();
for(Student item:list){
System.out.println(item.getName() +item.getAge());
}
}