04第四章Spring持久化和事务管理(c3p0连接池 jadbcTemplate增删改查)

一、配置数据源资源

1.所需依赖包(maven):

<!-- c3p0 jdbc操作  -->
<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>

2.在java类中通过ComboPooledDataSource创建数据源

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DriverManagerDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

import java.beans.PropertyVetoException;
/*
第一种配置数据池
 */
public class D1 {
    public static void main(String[] args) throws PropertyVetoException {
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
        comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mybatis_test?						       useUnicode=true&characterEncoding=utf8&useSSL=false");
        comboPooledDataSource.setJdbcUrl("root");
        comboPooledDataSource.setPassword("root123");



        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(comboPooledDataSource);

        System.out.println("afdsa");
        String sql = "insert into tb_adm values(null,'xqh123','113')";
        int update = jdbcTemplate.update(sql);  
        System.out.println("aaaa");


        if(update>0){
            System.out.println("添加成功!");
        }else{
            System.out.println("添加失败!");
        }

    }
}

3.在spring-xml中配置JdbcTemplate的数据源

<!-- spring-xml -->   
<!-- 开启扫描包 注解-->
<context:component-scan base-package="com.xqh.*"/>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass"  value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl"    value="jdbc:mysql://localhost:3306/mybatis_test"/>
        <property name="user"   value="root"/>
        <property name="password" value="root123"/>

    </bean>

<!-- 要记住id 方便JdbcTemplate的调用 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

4.JdbcTemplate表单操作(增删改查)

//dao层实现类
package com.xqh.dao;

import com.xqh.pojo.Adm;
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;

@Component("admDao")
public class AdmDaoImp implements AdmDao{
    @Resource(name = "jdbcTemplate") //调用bean 中jdbcTemplate模板
    private JdbcTemplate template;

    @Override
    public int insertAdm(Adm adm) {
        String sql = "insert into tb_adm values(null,?,?)";
        int update = template.update(sql,adm.getAdm_name(),adm.getAdm_post());
        return update;
    }

    @Override
    public int updateAdm(Adm adm) {
        String sql = "update tb_adm set adm_name=?,adm_post=? where adm_id=?";
        int update = template.update(sql, adm.getAdm_name(), adm.getAdm_post(), adm.getAdm_id());
        return update;
    }

    @Override
    public int delteAdm(Adm adm) {
        String sql = "delete from tb_adm where adm_id=?";
        int update = template.update(sql, adm.getAdm_id());
        return update;
    }

    @Override
    public List<Adm> select() {
        String sql = "select * from tb_adm";
        List<Adm> adm = template.query(sql, new BeanPropertyRowMapper<Adm>(Adm.class));
        return adm;

    }

    @Override
    public List<Adm> selectByName(String adm_name) {
        String sql = "select * from tb_adm where adm_name like concat('%',?,'%')";
        List<Adm> query = template.query(sql, new BeanPropertyRowMapper<Adm>(Adm.class), adm_name);
        return query;
    }
}
//service实现类
package com.xqh.service;

import com.xqh.dao.AdmDao;
import com.xqh.pojo.Adm;
import org.springframework.stereotype.Component;

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

@Component("admService")//注册bean容器 id
public class AdmServiceImp implements AdmService{

    @Resource(name = "admDao")//调用 dao层 注入数据
    private AdmDao admDao;

    @Override
    public int insertAdm(Adm adm) {
        int i = admDao.insertAdm(adm);
        return i;
    }

    @Override
    public int updateAdm(Adm adm) {
        int i = admDao.updateAdm(adm);
        return i;
    }

    @Override
    public int delteAdm(Adm adm) {
        int row = admDao.delteAdm(adm);
        return row;
    }

    @Override
    public List<Adm> select() {
        List<Adm> select = admDao.select();
        return select;
    }

    @Override
    public List<Adm> selectByName(String adm_name) {
        List<Adm> adms = admDao.selectByName(adm_name);
        return adms;
    }
}
//测试类
@Test
    public void insertAdm(){
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        AdmService admService = app.getBean("admService", AdmService.class);
        Adm adm = new Adm(); //添加
        adm.setAdm_name("xqh112");
        adm.setAdm_post("032");

        int i = admService.insertAdm(adm);
        if (i >0){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }

5.读取外部properties文件配置创建数据源

//文件后缀名是 properties的文件
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/mybatis_test
jdbc.user=root
jdbc.password=root123
<!-- spring-xml -->    

	<!-- 引入properti文件 -->
	<context:property-placeholder location="db.properties"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="driverClass"    value="${jdbc.driverClass}"/>
        <property name="jdbcUrl"    value="${jdbc.jdbcUrl}"/><!-- spel表达式引入 properties文件值 -->
        <property name="user"    value="${jdbc.user}"/>
        <property name="password"    value="${jdbc.password}"/>
    </bean>

	<!-- 连接池注入到 JdbcTemplate中 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

二、Spring事务管理

https://www.cnblogs.com/myseries/p/10834172.htm

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值