案例:单测查询全部学生

项目结构:

1.导入部署jar包:spring-jdbc

<!--spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>

2.创建实体类:Student

public class Student {
private Integer id; //编号
private String name; //姓名
private Integer age; //年龄
private Date birthday; //出生日期
}

3.创建dao层的接口和实现类:

IStudentDAO:

public interface IStudentDAO {
//查询全部
public List<Student> findAll();
}

StudentDAOImpl:继承JdbcDaoSupport  实现IStudentDAO

public class StudentDAOImpl extends JdbcDaoSupport implements IStudentDAO{

    public List<Student> findAll() {
String sql="select * from student";
List<Student> list=this.getJdbcTemplate().query(sql, new RowMapper<Student>() {
/**
*
* @param rs
* @param i
* @return
* @throws SQLException
*/
public Student mapRow(ResultSet rs, int i) throws SQLException {
Student student=new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setBirthday(rs.getDate("birthday"));
return student;
}
});
return list;
}
}

4.创建service层的接口和实现类:

IStudentService:

public interface IStudentService {
//查询全部
public List<Student> findAll();
}

StudentServiceImpl:

public class StudentServiceImpl implements IStudentService {
private IStudentDAO dao;
public List<Student> findAll() {
return dao.findAll();
} public IStudentDAO getDao() {
return dao;
} public void setDao(IStudentDAO dao) {
this.dao = dao;
}
}

在applicationContextSpring15jdbctemplate.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: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
">
<!--00.识别jdbc.properties-->
<!--方式一-->
<!-- <context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
<!--方式二-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="jdbc.properties"></property>
</bean>
<!--01.建立数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.uname}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--03.dao的配置-->
<bean id="studentDao" class="cn.happy.spring22jdbctemplate.dao.impl.StudentDAOImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--04.service的配置-->
<bean id="studentService" class="cn.happy.spring22jdbctemplate.service.impl.StudentServiceImpl">
<property name="dao" ref="studentDao"></property>
</bean>
</beans>

其他三种数据源的方案:

 <!--02建立数据源 dbcp-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.uname}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--03建立数据源 c3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.uname}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--03建立数据源 alibaba-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.uname}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>

单元测试:

  //jdbcTemplate
@Test
public void test01(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextSpring15jdbctemplate.xml");
IStudentService dao=(IStudentService) context.getBean("studentService");
List<Student> list = dao.findAll();
for (Student item:list){
System.out.println(item.getName());
}
}

执行结果:

最新文章

  1. javascript判断数组中是否包含某个元素
  2. java 实现文件下载
  3. 基于Metronic的Bootstrap开发框架经验总结(4)--Bootstrap图标的提取和利用
  4. datagrid实现单行的选择、取消
  5. Linux学习-0626
  6. oracle中decode()函数
  7. IO 流—&gt;&gt;&gt;补充
  8. iOS更新之DFU模式和恢复模式
  9. b端商家赋值权限
  10. JS代码中加上alert才能正常显示效果
  11. 终极版:Mybatis整合Spring配置
  12. CSS之分组选择器和嵌套选择器
  13. 区间dp之四边形不等式优化详解及证明
  14. C#与C++数据类型比较及结构体转换[整理]
  15. 51 Nod 1256 乘法逆元(数论:拓展欧几里得)
  16. VMware安装步骤既常见问题
  17. tp框架中的一些疑点知识-5
  18. FB01与F-02的区别(转载)
  19. MyCAT 源码解析 —— 分片结果合并(使用unsaferow)
  20. tcp网络通信的三次握手与三次挥手

热门文章

  1. Windows Internals 笔记——错误处理
  2. Moco使用简单指导
  3. azkaban安装使用
  4. SQLSERVER 数据量太大,重启服务器后,数据库显示正在恢复
  5. std::string 是什么
  6. WPF在XAML中实现持续动画的暂停、恢复、停止
  7. 分享微信h5支付源码
  8. 自建yum仓库yum源
  9. 【转】通过 INotifyPropertyChanged 实现观察者模式
  10. Vue2.0学习——axios用法详解