jdbcTemplate 在spring 中Dao层,实现CRUD操作

本文介绍了如何在Spring项目中利用jdbcTemplate进行数据库的CRUD操作,包括下载相关jar,创建实体类和Dao接口及实现类,配置bean.xml以通过IOC注入jdbcTemplate。在面对多个实现类时,可以采用让实现类继承JdbcDaoSupport的方式,减少代码冗余,只需配置数据源即可。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.mave 下载相关jar
在这里插入图片描述

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
</dependencies>

2.写 实体类,Dao接口,Dao的实现类
在这里插入图片描述在这里插入图片描述在这里插入图片描述

//账户持久层实现类
public class Account1DaoImpl implements IAccount1Dao {
    //JdbcTemplate 数据库操作类对象,作为属性。通过xml配置注入
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    //id单查
    public Account1 findById(int id) {
         List<Account1> accs = jdbcTemplate.query("select * from account1  where id = ?",
                new BeanPropertyRowMapper<Account1>(Account1.class),id);
        return accs.isEmpty()?null:accs.get(0);
    }
    //名字查询操作
    public Account1 findById(String name) {
        List<Account1> accs = jdbcTemplate.query("select * from account1  where name = ?",
                new BeanPropertyRowMapper<Account1>(Account1.class),name);
        if(accs.isEmpty()){
            return null;
        }
        if(accs.size()>1){
            throw new RuntimeException("结果不唯一");
        }
        return accs.get(0);
    }
    //修改操作
    public void modify(Account1 acc) {
        jdbcTemplate.update("update account1 set name = ?,money = ? where id = ?;",
                acc.getName(),acc.getMoney(),acc.getId());
    }
}

3.写bean.xml 配置文件,把实现类中的jdbcTemplate属性,通过IOC容器注入
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置Dao实现类中的 jdbcTemplate 做(增删改查) 操作对象属性 添加进IOC容器-->
    <bean id = "account1Dao" class="wuwei.dao.impl.Account1DaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    <!--把数据源的4个属性,添加进jdbcTemplate对象中-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置spring内置数据源-->
    <bean id ="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- 配置spring内置连接数据库的4个基本信息 ,注意spring内置的 name 名是固定的字符。-->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123"></property>
    </bean>
</beans>

4.测试操作
在这里插入图片描述

public class Test {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccount1Dao account1Dao = ac.getBean("account1Dao",IAccount1Dao.class);
        //增加测试
        Account1 as = account1Dao.findById(1);
        System.out.println(as);
        //修改操作
        account1Dao.modify(new Account1(2,"孜然",500));
        System.out.println("修改成功");
    }
}

知识拓展:
当拥有多个实现类的时候,为解决代码冗余,实现类中的jdbcTemplate 可以不写,直接使用spring内部封装的,让实现类继承 JdbcDaoSupport 就可以直接实现。之后把bean.xml 配置文件改为 数据源就可以,也就是不用再IOC注入 jdbcTemplate
在这里插入图片描述

//账户持久层实现类
public class Account1DaoImpl extends JdbcDaoSupport implements IAccount1Dao {
//    //JdbcTemplate 数据库操作类对象,作为属性。通过xml配置注入
//    private JdbcTemplate jdbcTemplate;
//    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
//        this.jdbcTemplate = jdbcTemplate;
//    }
    //id单查
    public Account1 findById(int id) {
         List<Account1> accs = super.getJdbcTemplate().query("select * from account1  where id = ?",
                new BeanPropertyRowMapper<Account1>(Account1.class),id);
        return accs.isEmpty()?null:accs.get(0);
    }
    //名字查询操作
    public Account1 findById(String name) {
        List<Account1> accs = super.getJdbcTemplate().query("select * from account1  where name = ?",
                new BeanPropertyRowMapper<Account1>(Account1.class),name);
        if(accs.isEmpty()){
            return null;
        }
        if(accs.size()>1){
            throw new RuntimeException("结果不唯一");
        }
        return accs.get(0);
    }
    //修改操作
    public void modify(Account1 acc) {
        super.getJdbcTemplate().update("update account1 set name = ?,money = ? where id = ?;",
                acc.getName(),acc.getMoney(),acc.getId());
    }
}

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置Dao实现类中的 jdbcTemplate 做(增删改查) 操作对象属性 添加进IOC容器-->
    <bean id = "account1Dao" class="wuwei.dao.impl.Account1DaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--&lt;!&ndash;把数据源的4个属性,添加进jdbcTemplate对象中&ndash;&gt;-->
    <!--<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">-->
        <!--<property name="dataSource" ref="dataSource"></property>-->
    <!--</bean>-->
    <!--配置spring内置数据源-->
    <bean id ="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- 配置spring内置连接数据库的4个基本信息 ,注意spring内置的 name 名是固定的字符。-->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123"></property>
    </bean>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值