SpringIOC整合dbUtil做的增删改查以及转账业务的实现

目录

一、xml方式实现

1.介绍lombok插件

2.功能

3.步骤

3.1 idea安装插件(只做一次)

3.2 添加坐标 

3.3 编写注解

4.核心类

4.1 QueryRunner

4.2 query() 查询

4.3 update() 增删改

5.配置文件applicationContext.xml

6.junit测试

6.1使用步骤

6.1.1 坐标

6.1.2 注解(修饰方法)

二、annotation注解方式实现

1.控制层(cotroller)

2.业务层(service)

3.数据访问层(dao)

4.配置文件applicationContext.xml

三、configuration配置类方式实现

1.ApplicationConfig

2.DataConfig 替换applicationContext.xml

3.测试类

四、在xml基础上实现转账业务

1.同一个业务方法的多个dao方法公用一个connection对象

2.ThreadLocal

3.通过连接对象进行事务的统一管理

5.项目总结:


一、xml方式实现

1.介绍lombok插件

dbUtil-阿帕奇提供操作数据库的插件

2.功能

对实体类自动,动态生成getset,无参有参 toString.....

3.步骤
3.1 idea安装插件(只做一次)

3.2 添加坐标 
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
3.3 编写注解
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Account implements Serializable {
    private int aid;
    private String aname;
    private int amoney;

    public Account(String aname, int amoney) {
        this.aname = aname;
        this.amoney = amoney;
    }
}
4.核心类
4.1 QueryRunner
//操作数据库的核心类
    QueryRunner queryRunner;

    public void setQueryRunner(QueryRunner queryRunner) {
        
        this.queryRunner = queryRunner;
    }
4.2 query() 查询
@Override
    public void save(Account account) {
        try {
            queryRunner.update("insert into account(aname,amoney) value(?,?)",account.getAname(),account.getAmoney());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
@Override
public void updateById(Account account) {
    try {
        queryRunner.update("udpate account set aname=?,amoney=? where aid=?",account.getAname(),account.getAmoney(),account.getAid());
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
}

@Override
public void deleteById(int id) {
    try {
        queryRunner.update("delete from account where aid=?",id);
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
}
4.3 update() 增删改
@Override
public Account findByName(String name) {
    try {
        return queryRunner.query("select * from account where aname=?",new BeanHandler<Account>(Account.class),name);
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
    return null;
}

@Override
public List<Account> findAll() {
    try {
        return queryRunner.query("select * from account",new BeanListHandler<Account>(Account.class));
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
    return null;
}
5.配置文件applicationContext.xml
<!--加载资源文件-->
    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>

    <!--注入数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${msg1}"></property>
        <property name="jdbcUrl" value="${msg2}"></property>
        <property name="user" value="${msg3}"></property>
        <property name="password" value="${msg4}"></property>
    </bean>

    <!--注入QueryRunner-->
    <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
        <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>

    <!--注入dao-->
    <bean id="mapperImp" class="com.ztt.dao.AccountMapperImp">
        <property name="queryRunner" ref="queryRunner"></property>
    </bean>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值