spring整合JDBC

1.工具类

·JdbcTemplate封装常用JDBC方法

·HibernateTemplate封装常用Hibernate方法

·JdbcDaoSupport一JDBC数据访问对象的基类

·HibernateDaoSupport一Hibernate数据访问对象的基类

JdbcTemplate:直接使用sql语句操作数据库,效率很高,但是编码量大!

         封装了操作数据库的各种方法,该类包含一个dataSource属性(数据源),

         只有在初始化数据源的情况下才能调用JdbcTemplate的方法。

xml文件配置:使用JdbcTemplate前,要先创建数据源对象

1 <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
2         <property name="dataSource" ref="dataSource"></property>
3     </bean>

2.阿里巴巴Druid数据库连接池

官方说明文档:

https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98

properties文件配置基本信息:

 1 username=root 2 password=123123 3 url=jdbc:mysql://localhost:3306/utf8?useUnicode=true&characterEncoding=utf-8 4 driver=com.mysql.jdbc.Driver 

xml文件配置:

 1 <!-- 读取连接配置信息 -->
 2     <util:properties id="props" location="classpath:db.properties"></util:properties>
 3     
 4     <!-- 设置连接池配置 -->
 5     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
 6         <!-- 基本连接信息 -->
 7         <property name="url" value="#{props.url}"></property>
 8         <property name="username" value="#{props.username}"></property>
 9         <property name="password" value="#{props.password}"></property>
10         <property name="driverClassName" value="#{props.driver}"></property>
11         <!-- 配置初始化连接大小 最小连接数   最大连接数 -->
12         <property name="initialSize" value="1"></property>
13         <property name="minIdle" value="1"></property>
14         <property name="maxActive" value="20"></property>
15         <!-- 获取连接的等待超时时间 毫秒单位 -->
16         <property name="maxWait" value="60000"></property>
17         <!-- 隔多久进行一次检测  检测需要关闭的连接 -->
18         <property name="timeBetweenEvictionRunsMillis" value="60000"></property>
19         <!-- 配置一个连接在连接池最小生存时间 -->
20         <property name="minEvictableIdleTimeMillis" value="300000"></property>
21     </bean>
22     <!-- 设置JdbcTemplate -->
23     <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
24         <!-- 设置template的数据源对象 -->
25         <property name="dataSource" ref="dataSource"></property>
26     </bean>
27     <!-- 开启注解扫描 -->
28     <context:component-scan base-package="com.rong.web"></context:component-scan>

3.spring集成jdbc

依赖包:druid-1.0.31、spring-jdbc-4.2.8、spring-tx-4.2.8

* 号会导致数据库生成执行语句计划的时候,先通过一条sql查询查询出该表有哪些字段信息,然后再替换*号,效率下降

代码操作:template需要在xml配置文件中注入一个dataSource属性

    update方法包括增删改操作

    /**实现RowMapper接口的类重写的方法中的参数含义:

   * ResultSet rs, int rowNum

   * rs:结果集对象

  * rowNum:遍历的结果集的第几条数据

   */

 1 package com.web.dao;
 2 
 3 import java.util.List;
 4 
 5 import javax.annotation.Resource;
 6 
 7 import org.springframework.jdbc.core.JdbcTemplate;
 8 import org.springframework.stereotype.Repository;
 9 
10 import com.web.entity.User;
11 import com.web.entity.UserMapper;
12 @Repository
13 public class UserDaoImpl implements UserDao {
14     @Resource
15     JdbcTemplate template;
16 
17     @Override
18     public List<User> findAll() {
      
19 String sql="select id,username,password from user"; 20 List<User> list = template.query(sql, new UserMapper()); 21 return list; 22 } 23 24 @Override 25 public User findById(Integer id) { 26 String sql="select id,username,password from user where id=?"; 27 Object[] objs=new Object[]{id}; 28 User user = template.queryForObject(sql, new UserMapper(), objs); 29 return user; 30 } 31 32 @Override 33 public void update(User user) { 34 String sql="update user set username=?,password=? where id=?"; 35 Object[] objs=new Object[]{user.getUsername(),user.getPassword(),user.getId()}; 36 int num = template.update(sql, objs); 37 if(num>0){ 38 System.out.println("更改成功!"); 39 }else{ 40 System.out.println("更改失败!"); 41 } 42 43 } 44 45 @Override 46 public void delete(Integer id) { 47 String sql="delete from user where id=?"; 48 Object[] objs=new Object[]{id}; 49 int num = template.update(sql, objs); 50 if(num>0){ 51 System.out.println("删除成功!"); 52 }else{ 53 System.out.println("删除失败!"); 54 } 55 56 } 57 58 @Override 59 public void add(User user) { 60 String sql="insert into user values(?,?,?)"; 61 Object[] objs=new Object[]{user.getId(),user.getUsername(),user.getPassword()}; 62 int num = template.update(sql, objs); 63 if(num>0){ 64 System.out.println("添加成功!"); 65 }else{ 66 System.out.println("添加失败!"); 67 } 68 69 } 70 71 }
 1 package com.web.entity;
 2 
 3 import java.sql.ResultSet;
 4 import java.sql.SQLException;
 5 
 6 import org.springframework.jdbc.core.RowMapper;
 7 
 8 public class UserMapper implements RowMapper<User>{
 9 
10     @Override
11     public User mapRow(ResultSet rs, int rowNum) throws SQLException {
12         User user=new User(rs.getInt("id"), rs.getString("username"), rs.getString("password"));
13         return user;
14     }
15 
16 }

 

posted @ 2017-11-08 20:54  57容杰龙  阅读(155)  评论(0编辑  收藏  举报