1Spring 框架整合 DAO 模板
- JDBC:org.springframework.jdbc.core.JdbcTemplate
- Hibernate3.0:org.springframework.orm.hibernate3.HibernateTemplate
- MyBatis:org.springframework.orm.ibatis.SqlMapClientTemplate
- JPA:org.springframework.orm.jpa.JpaTemplate
2JdbcTemplate 模板整合 JDBC 实例
(1)新建 web 项目, 导入所需要的 jar 包, 如下图所示:
(2)在 MySQL 数据库 JdbcTemplateDB 中新建数据表 t_user, 如下图所示:
(3)新建一个 bean 类, 具体代码如下:
- User.java
- package com.spring.bean;public class User { private Integer id; private String name; 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; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; }}
(4)书写 DAO 接口, 代码如下:
- UserDao.java
- package com.spring.dao;import java.util.List;import com.spring.bean.User;public interface UserDao { // 增 void save(User u); // 删 void delete(Integer id); // 改 void update(User u); // 查 User getById(Integer id); // 查 int getTotalCount(); // 查 List<User> getAll();}
(5)书写一个类实现 DAO 接口, 代码如下:
- UserDaoImpl.java
- package com.spring.dao;import java.sql.ResultSet;import java.sql.SQLException;import java.util.List;import org.springframework.jdbc.core.RowMapper;import org.springframework.jdbc.core.support.JdbcDaoSupport;import com.spring.bean.User;// 使用 JDBC 模板实现增删改查 public class UserDaoImpl extends JdbcDaoSupport implements UserDao { public void save(User u) { String sql = "insert into t_user values(null,?)"; super.getJdbcTemplate().update(sql, u.getName()); } public void delete(Integer id) { String sql = "delete from t_user where id = ?"; super.getJdbcTemplate().update(sql,id); } public void update(User u) { String sql = "update t_user set name = ? where id=?"; super.getJdbcTemplate().update(sql, u.getName(),u.getId()); } public User getById(Integer id) { String sql = "select * from t_user where id = ?"; return super.getJdbcTemplate().queryForObject(sql,new RowMapper<User>(){ public User mapRow(ResultSet rs, int arg1) throws SQLException { User u = new User(); u.setId(rs.getInt("id")); u.setName(rs.getString("name")); return u; }}, id); } public int getTotalCount() { String sql = "select count(*) from t_user"; Integer count = super.getJdbcTemplate().queryForObject(sql, Integer.class); return count; } public List<User> getAll() { String sql = "select * from t_user"; List<User> list = super.getJdbcTemplate().query(sql, new RowMapper<User>(){ public User mapRow(ResultSet rs, int arg1) throws SQLException { User u = new User(); u.setId(rs.getInt("id")); u.setName(rs.getString("name")); return u; }}); return list; }}
(6)新建数据库驱动链接资源文件, 代码如下:
- db.properties
- jdbc.jdbcUrl=jdbc:mysql:///JdbcTemplateDBjdbc.driverClass=com.mysql.jdbc.Driverjdbc.user=rootjdbc.password=root
(7)书写 Spring 配置文件, 代码如下:
- applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"><!-- 指定 spring 读取 db.properties 配置 --><context:property-placeholder location="classpath:db.properties" /><!-- 1. 将数据库连接池放入 spring 容器 --><bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property></bean><!-- 2. 将 JDBCTemplate 放入 spring 容器 --><bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property></bean><!-- 3. 将 UserDao 放入 spring 容器 --><bean name="userDao" class="com.spring.dao.UserDaoImpl"> <property name="dataSource" ref="dataSource"></property></bean></beans>
(8)新建测试类进行测试, 代码如下:
- TestDemo.java
- package com.spring.test;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.mchange.v2.c3p0.ComboPooledDataSource;import com.spring.bean.User;import com.spring.dao.UserDao;// 演示 JDBC 模板 @RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class TestDemo { @Autowired private UserDao ud; @Test public void fun1() throws Exception{ //1 准备连接池 ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql:///JdbcTemplateDB"); dataSource.setUser("root"); dataSource.setPassword("root"); //2 创建 JDBC 模板对象 JdbcTemplate jt = new JdbcTemplate(); jt.setDataSource(dataSource); //3 书写 sql 语句, 并执行 String sql = "insert into t_user values(null,'rose')"; jt.update(sql); } @Test public void fun2() throws Exception{ User u = new User(); u.setName("tom"); ud.save(u); } @Test public void fun3() throws Exception{ User u = new User(); u.setId(2); u.setName("jack"); ud.update(u); } @Test public void fun4() throws Exception{ ud.delete(2); } @Test public void fun5() throws Exception{ System.out.println(ud.getTotalCount()); } @Test public void fun6() throws Exception{ System.out.println(ud.getById(1)); } @Test public void fun7() throws Exception{ System.out.println(ud.getAll()); }}
(9)配置依赖关系与使用图解:
注: 部分知识源于网络, 侵权联删!
您可能还喜欢:
Spring 框架系列之 AOP 思想
Spring 框架系列 (二) 之 Bean 的注解管理
SSH 框架之 Hibernate 系列(一)
来源: https://juejin.im/entry/5ab8ba3e518825555c1da708