框架--Spring框架和C3P0,DBCP,DRUID整合

Spring框架和dbcp,c3p0,druid的整合

Spring 和c3p0的整合

  • maven依赖:
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.8</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
      <dependency>
          <groupId>com.mchange</groupId>
          <artifactId>c3p0</artifactId>
          <version>0.9.5.2</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>5.1.5.RELEASE</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.1.5.RELEASE</version>
      </dependency>
  • 使用xml文件中注入的方式,加载外部配置文件,并使用Spring提供的jdbcTemplate操作数据库
  • 数据库表结构
    在这里插入图片描述
  • 代码示例:
  • 实体类:
/*
* 实体类
* 也相当于一个bean
* 有name和password两个属性,
* 对应数据库中的响应的两个字段
*并生成它的set get方法和带参和无参构造器
* */
public class User {
    private String name;
    private String password;

    public User() {
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

  • dao层规范接口
import com.wdhcr.c3p0.entity.User;

/*
* 这是dao层的规范接口
*
* */
public interface Dao {
    //这是添加用户的方法
    public void addUser(User user);
    //这是删除用户的方法
    public void delUser(String name);
    //这是更新用户的方法
    public void updateUser(User user);
    //查询所有的方法
    public  void findAll();
}
  • dao层具体实现类
import com.wdhcr.c3p0.entity.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/*
* 这dao层接口的一个实现类
* 并实现它里边的数据库操作的方法
*
* */
//使用注解的方式,告诉扫描器这是一个bean,并设置对应的值
@Component(value = "daoimpl")
public class DaoImpl implements Dao{
    //是用来装配bean的它是按照名称来装配的,autowried是按照类型装配的,
    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    @Override
    public void addUser(User user) {
        //定义sql语句
        String sql = "insert into user01 values(?,?)";
        int row = jdbcTemplate.update(sql, user.getName(), user.getPassword());
        System.out.println("成功添加"+row+"行数据");

    }

    @Override
    public void delUser(String name) {
        //定义sql语句
        String sql = "delete from user01 where name = ?";
        int row = jdbcTemplate.update(sql, name);
        System.out.println("成功删除"+row+"行数据");
    }

    @Override
    public void updateUser(User user) {
        //定义sql语句
        String sql = "update user01 set password = ? where name = ?";
        int row = jdbcTemplate.update(sql, user.getPassword(), user.getName());
        System.out.println("成功修改"+row+"行数据");
    }

    @Override
    public void findAll() {
        //定义失去了语句
        String sql = "select * from user01";
        List<User> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
        for (User user : query) {
            System.out.println(user.getName()+"\t"+user.getPassword());
        }
    }
}
  • services层的规范接口
import com.wdhcr.c3p0.entity.User;

/*
* 这是service层的规范接口
* */
public interface Servicess {
    //添加用户
    public void addSerUser(User user);
    //删除用户
    public void delSerUser(String name);
    //更新用户
    public void updateSerUser(User user);
    //查询用户
    public void findSerUser();
}

  • serives层的具体实现
import com.wdhcr.c3p0.dao.DaoImpl;
import com.wdhcr.c3p0.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/*
* 这是service层的一个实现类
*
* */
//说明这个类是services层的
@Service
//告诉扫描器这是一个bean
@Component(value = "services")
public class ServiceImpl implements Servicess {
    @Resource(name = "daoimpl")//使用注解的方式注入属性的值
    private DaoImpl dao;

    @Override
    public void addSerUser(User user) {
        //调用dao层相关的方法
        dao.addUser(user);
    }

    @Override
    public void delSerUser(String name) {
        dao.delUser(name);
    }

    @Override
    public void updateSerUser(User user) {
        dao.updateUser(user);
    }

    @Override
    public void findSerUser() {
        dao.findAll();
    }
}

  • 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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--    设置一个扫描器,并指定它的扫描路径-->
    <context:component-scan base-package="com.wdhcr.c3p0"></context:component-scan>
    <!--    加载外部配置文件-->
    <context:property-placeholder location="db.properties"></context:property-placeholder>
    <!--    给c3p0连接池bean注入相关配置-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driver}"></property>
        <property name="jdbcUrl" value="${url}"></property>
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
    </bean>
    <!--    向spring框架为我们提供的jdbctemplate中注入我们的连接-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>
  • 外部数据库连接池的配置文件

在这里插入图片描述

  • 测试类代码
/*
* 这是spring框架和c3p0连接池的整合测试类
* */
public class TestC3p0 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext("c3p0Bean.xml");//加载你的bean的xml文件,注意路径
        //让spring框架为我们生成一个serviceimpl的一个对象
        ServiceImpl services = classPathXmlApplicationContext.getBean("services", ServiceImpl.class);
        //添加一条数据
        //services.addSerUser(new User("张三","123"));
        //删除一个数据
        //services.delSerUser("张三");
        //修改一个数据
        //services.updateSerUser(new User("张三","234"));
        //查询所有数据
        //services.findSerUser();
    }
}

Spring框架和 DBCP的整合

  • maven依赖
<dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.8</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
      <dependency>
          <groupId>commons-dbcp</groupId>
          <artifactId>commons-dbcp</artifactId>
          <version>1.4</version>
      </dependency>
       <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>5.1.5.RELEASE</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.1.5.RELEASE</version>
      </dependency>

  • 使用xml文件中注入的方式,加载外部配置文件,并使用Spring提供的jdbcTemplate操作数据库
  • 数据库表结构
    在这里插入图片描述
  • 实体类User类,dao层的数据库操作类,services层的业务类,和c3p0的一样,这里不再写
  • 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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--    设置扫描器,并设置扫描路径-->
    <context:component-scan base-package="com.wdhcr.dbcp"></context:component-scan>

<!--    加载外部配置文件-->
    <context:property-placeholder location="db.properties"></context:property-placeholder>
<!--    配置dbcp连接池的相关设置-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${user}"></property>
        <property name="password" value="${password}"></property>
    </bean>
<!--    向jdbcTemplate中注入数据库连接-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

  • 测试类代码
import com.wdhcr.dbcp.service.ServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/*
* 这是spring框架和c3p0连接池的整合测试类
* */
public class TestDbcp {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext("dbcpBean.xml");//加载你的bean的xml文件,注意路径
        //让spring框架为我们生成一个serviceimpl的一个对象
        ServiceImpl services = classPathXmlApplicationContext.getBean("services", ServiceImpl.class);
        //添加一条数据
//        services.addSerUser(new User("张三","123"));
        //删除一个数据
//        services.delSerUser("张三");
        //修改一个数据
//        services.updateSerUser(new User("张三","234"));
        //查询所有数据
        services.findSerUser();
    }
}


Spring框架和Druid整合

  • maven依赖
 <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.8</version>
      </dependency>
       <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.1.12</version>
      </dependency>

      <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>5.1.5.RELEASE</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.1.5.RELEASE</version>
      </dependency>
  • 使用xml文件中注入的方式,加载外部配置文件,并使用Spring提供的jdbcTemplate操作数据库
  • 数据库表结构
    在这里插入图片描述
  • 实体类User类,dao层的数据库操作类,services层的业务类,和c3p0的一样,这里不再写
  • 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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--    设置扫描器,并设置扫描路径-->
    <context:component-scan base-package="com.wdhcr.druid"></context:component-scan>
<!--    加载外部配置文件,-->
    <context:property-placeholder location="db.properties"></context:property-placeholder>
<!--    将外部配置文件注入到你的druid连接池中-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${user}"></property>
        <property name="password" value="${password}"></property>
    </bean>
<!--    向jdbcTemplate中注入数据库连接-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>
  • 测试类代码:
import com.wdhcr.druid.service.ServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/*
* 这是spring框架和c3p0连接池的整合测试类
* */
public class TestDruid {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext("druidBean.xml");//加载你的bean的xml文件,注意路径
        //让spring框架为我们生成一个serviceimpl的一个对象
        ServiceImpl services = classPathXmlApplicationContext.getBean("services", ServiceImpl.class);
        //添加一条数据
//        services.addSerUser(new User("张三","123"));
        //删除一个数据
//        services.delSerUser("张三");
        //修改一个数据
//        services.updateSerUser(new User("张三","234"));
        //查询所有数据
        services.findSerUser();
    }
}

  • 以上就是Spring框架和C3P0,DBCP,DRUID的整合
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值