Spring配置文件配置c3p0连接MySQL数据库简单实现(xml版)

导入Maven依赖

<!-- 报错java不支持发型版本加上 properties-->
<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring-version>5.0.2.RELEASE</spring-version>
</properties>

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-version}</version>
        </dependency>

    </dependencies>

编写实体类

public class Person {
	
    private String pId;
	
    private String pName;
    
    private String page;

    public Person() {
    }

    public Person(String pId, String pName, String page) {
        this.pId = pId;
        this.pName = pName;
        this.page = page;
    }
}

编写dao

import com.wlp.pojo.Person;

/**
 * 功能:Person接口
 * @author 花子June
 * @modifier 花子June
 * @date 2021-03-16 8:59
 * @Version V1.0
 */
public interface PersonDao {
	// 插入一条数据
    int insertPerson(Person p) throws Exception;
}

package com.wlp.dao.impl;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.wlp.dao.PersonDao;
import com.wlp.pojo.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * 功能:Person接口实现类
 * @author 花子June
 * @modifier 花子June
 * @date 2021-03-16 9:00
 * @Version V1.0
 */
public class PersonDaoImpl implements PersonDao {
	
    private  ComboPooledDataSource dataSource;

    public void setDataSource(ComboPooledDataSource dataSource) {
        this.dataSource = dataSource;
    }

    public int insertPerson(Person p) throws Exception{

        // 获取连接
        Connection connection = dataSource.getConnection();

        // 预编译sql语句
        PreparedStatement ps = connection.prepareStatement("insert into person values(?,?,?)");

		// 依次设置?的值
        ps.setString(1,p.getpId());
        ps.setString(2,p.getpName());
        ps.setString(3,p.getPage());

		// 执行sql语句并返回结果(省略关闭资源)
        return ps.executeUpdate();
    }
}

编写Service

/**
 * 功能:Person服务类接口
 * @author 花子June
 * @modifier 花子June
 * @date 2021-03-16 9:07
 * @Version V1.0
 */
public interface PersonService {

    int save(Person p);
}
/**
 * 功能:Person服务类接口实现类
 * @author 花子June
 * @modifier 花子June
 * @date 2021-03-16 9:07
 * @Version V1.0
 */
public class PersonServiceImpl implements PersonService {

    private PersonDao personDao;

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

	// 插入一条数据
    public int save(Person p) {

        try {
            return personDao.insertPerson(p);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }
}

编写properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_wlp?serverTimezone=UTC
jdbc.user=root
jdbc.password=xxxx

编写Spring配置文件

<?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"
       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">

	<!-- 注册Person类到Spring中 -->
    <bean id="person" class="com.wlp.pojo.Person"/>
    
	<!-- 注册personDaoImpl实现类到Spring容器中 -->
    <bean id="personDaoImpl" class="com.wlp.dao.impl.PersonDaoImpl">
    		<!-- 使用Set注入方式注入DataSource -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
	<!-- 注册personServiceImpl实现类到Spring容器中 -->
    <bean id="personServiceImpl" class="com.wlp.service.impl.PersonServiceImpl">
    	<!-- 使用Set注入方法注入PersonDao实现类 -->
        <property name="personDao" ref="personDaoImpl"/>
    </bean>

    <!-- 导入properties配置文件 -->
    <context:property-placeholder location="jdbc.properties"/>

    <!-- 配置c3p0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    	<!-- Mysql驱动 -->	
        <property name="driverClass" value="${jdbc.driver}"/>
        <!-- MySQL Url -->
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <!-- MySQL User用户名 -->
        <property name="user" value="${jdbc.user}"/>
		<!-- MySQL password 用户密码 -->
        <property name="password" value="${jdbc.password}"/>
    </bean>

</beans>

测试

  @Test
    public void personTest(){

		// 通过xml文件获取Spring容器对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

		// 通过getBean获取对象
        PersonServiceImpl ps = context.getBean("personServiceImpl", PersonServiceImpl.class);

		// 执行方法并输出返回值
        System.out.println(ps.save(new Person()));
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值