Spring——JDBC模版编程
2016-01-07 21:06
399 查看
本篇参照他人思路写成,而且摘抄不少内容。
原文点击这里
首先来看如何配置数据源
我们可以通过3种方式配置数据源:使用JDNI配置数据源
这种做法我没有使用过,但是网上有很多详细的教程,想知道如何做可以点击这个链接JNDI学习总结
使用连接池配置数据源
一般情况下都是采用这种方式,对于连接池的实现,也有很多种,比如DBCP,c3p0等等。
用户可以针对连接池进行自己的配置,有助于数据库端的调优。如果想对数据源连接池多谢了解,可以点击这个链接几个主流的Java连接池。
相对来说,最常使用的就是dbcp和c3p0了。
使用JDBC驱动类配置数据源
这种是最基本的通过驱动程序管理数据源,但是没有连接池的概念。
有两种实现方式:
DriverManagerDataSource:一般都是使用这种,这种方式每次请求都会返回一个新的连接。
SingleConnectionDataSource:这种每次都是使用的一个连接。
本篇使用第二种方式
Spring中的模版以及提供的基类
JdbcTemplate提供最简单的数据访问功能
NamedParameterJdbcTemplate
这个模版与JdbcTemplate不同的地方在于,sql语句中不使用?作为参数替代符。而是使用:加变量名的方式作为参数替代符(null, :email, :password, :firstName, :lastName)。然后在通过Map将变量名与实际值绑定起来,传递给crud方法。使用这种方式最大的好处就是,如果参数比较多,并且参数位置或顺序可能变化的情况下,使用NamedParameterJdbcTemplate是非常方便的!
SimpleJdbcTemplate
结合了一些自动装箱等功能。
为了避免每次都要把jdbctemplate的bean注入到我们的DAO里面,Spring为我们实现了三种对应的基类,我们的DAO实现类需要继承这些基类,就可以直接使用模板了。
对应的分别是:JdbcDapSupport、SimpleJdbcDaoSupport、NamedParameterJdbcDaoSupport。
最后用代码说明如何使用这些类
首先看一下全局配置文件applicationContext.xml<?xml version="1.0" encoding="UTF-8"?> <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.xsd"> <!-- 加载数据库配置文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:db.properties</value> </property> </bean> <!-- 配置数据源 --> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${db.driverClassName}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> </bean> <!-- 配置JdbcTemplate类 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <!-- 配置其它类 --> <bean id="studentService" class="service.StudentService"> <property name="studentDao"> <ref bean="studentDao"/> </property> </bean> <bean id="studentDao" class="dao.StudentDao"> <!-- StudentDao持有JdbcTemplate对象的引用 --> <property name="jdbcTemplate"> <ref bean="jdbcTemplate"/> </property> </bean> </beans>
数据库配置文件
db.driverClassName=com.mysql.jdbc.Driver db.url=jdbc\:mysql\://localhost\:3306/student db.username=root db.password=root
dao层代码
public class StudentDao { private JdbcTemplate jdbcTemplate; public StudentDao() { } public StudentDao(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void add() { String sql = "insert into t_student values(null,?,?)"; jdbcTemplate.update(sql, new Object[] {"jack", new Integer(23)}); } public Student query_one() { String sql = "select * from t_student where id=?"; @SuppressWarnings("unchecked") Student s = (Student) jdbcTemplate.queryForObject(sql, new Object[] {new Integer(1)}, new PersonMapper()); return s; } public List<Student> query_all() { String sql = "select * from t_student"; @SuppressWarnings("unchecked") List<Student> list = jdbcTemplate.query(sql, new PersonMapper()); return list; } @SuppressWarnings("rawtypes") private static final class PersonMapper implements RowMapper { @Override public Student mapRow(ResultSet rs, int rowNum) throws SQLException { Student s = new Student(); s.setId(rs.getInt("id")); s.setAge(rs.getInt("age")); s.setName(rs.getString("name")); return s; } } }
service层代码
public class StudentService { private StudentDao studentDao; public void setStudentDao(StudentDao studentDao) { this.studentDao = studentDao; } public void add() { studentDao.add();; } public Student query_one() { return studentDao.query_one(); } public List<Student> query_all() { return studentDao.query_all(); } }
测试代码
public class StudentServiceTest { private ClassPathXmlApplicationContext cpxac; private StudentService studentService; @Before public void init() { cpxac = new ClassPathXmlApplicationContext("applicationContext.xml"); studentService = (StudentService) cpxac.getBean("studentService"); } @Test public void testAdd() { studentService.add(); } @Test public void testQuery_one() { Student s = studentService.query_one(); System.out.println(s); } @Test public void testQuery_all() { List<Student> list = studentService.query_all(); System.out.println(list); } }
学生POJO类
public class Student { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
以上就是Spring中JDBC模版使用了。
相关文章推荐
- jdbc中的Statement和PreparedStatement接口对象
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- JDBC 数据库常用连接 链接字符串
- 模拟Spring的简单实现
- JDBC连接Access数据库的几种方式介绍
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- JDBC程序更新数据库中记录的方法
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- JDBC 程序的常见错误及调试方法
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- 详解Java的Spring框架中的事务管理方式
- 在Java的JDBC使用中设置事务回滚的保存点的方法
- Java中使用JDBC操作数据库简单实例