Spring 框架系列之 JDBC 整合实例
本文内容纲要:
- 扫描关注微信公众号,了解更多
微信公众号:compassblog
欢迎关注、转发,互相学习,共同进步!
有任何问题,请后台留言联系!
1、Spring框架整合 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
2、JdbcTemplate 模板整合 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.Driver
jdbc.user=root
jdbc.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)、配置依赖关系与使用图解:
注:部分知识源于网络,侵权联删除!
扫描关注微信公众号,了解更多
本文内容总结:扫描关注微信公众号,了解更多,
原文链接:https://www.cnblogs.com/compassblog/p/8503280.html
以上是 Spring 框架系列之 JDBC 整合实例 的全部内容, 来源链接: utcz.com/z/362678.html