jdbcTemplate-持久层CRUD

Posted 阿布都日

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdbcTemplate-持久层CRUD相关的知识,希望对你有一定的参考价值。

 

 POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>jdbcTemplate</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.200</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>14</java.version>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
        <encoding>UTF-8</encoding>
    </properties>
</project>

配置:

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="accountDao" class="com.example.dao.impl.AccountDaoImpl">
        <property name="jt" ref="jdbcTemplate"></property>
    </bean>
<!--配置JdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="ds"></property>
    </bean>

    <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.h2.Driver"></property>
        <property name="url" value="jdbc:h2:file:~/.h2/h2"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
</beans>
package com.example.dao;

import com.example.domain.Account;

/**
 * 账户的持久层接口
 */
public interface IAccountDao {

    /**
     * 根据Id查询账户
     * @param id
     * @return
     */
    Account findAccountById(int id);

    /**
     * 根据名称查询账户
     * @param name
     * @return
     */
    Account findAccountByName(String name);

    /**
     * 更新账户
     * @param account
     */
    void updateAccount(Account account);

}
package com.example.dao.impl;

import com.example.dao.IAccountDao;
import com.example.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * 账户的持久层实现类
 */
public class AccountDaoImpl implements IAccountDao {

    public void setJt(JdbcTemplate jt) {
        this.jt = jt;
    }

    private JdbcTemplate jt;


    @Override
    public Account findAccountById(int id) {
        List<Account> accounts = jt.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),id );
        if(accounts.size()==0){
            return null;
        }else {
            return accounts.get(0);
        }
    }

    @Override
    public Account findAccountByName(String name) {
        List<Account> accounts = jt.query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),name);
        if(accounts.size()==0){
            return null;
        }else if(accounts.size()==1) {
            return accounts.get(0);
        }else {
            throw new RuntimeException("结果集不唯一");
        }
    }

    @Override
    public void updateAccount(Account account) {
        jt.update("update account set name = ?, money = ? where id = ?",account.getName(),account.getMoney(),account.getId());
    }
}
package com.example.domain;


import java.io.Serializable;

public class Account implements Serializable {

    public Account(){}

    private int id;
    private String name;
    private float money;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public float getMoney() {
        return money;
    }

    public void setMoney(float money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name=\'" + name + \'\\\'\' +
                ", money=" + money +
                \'}\';
    }
}
package com.example.jdbctemplate;


import com.example.dao.IAccountDao;
import com.example.domain.Account;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * JdbcTemplate的CRUD操作
 */
public class JdbcTemplateDemo {


    public static void main(String[] args) {


        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

        IAccountDao accountDao = ac.getBean("accountDao",IAccountDao.class);

        Account a = accountDao.findAccountById(10);
        System.out.println(a);

        Account b = accountDao.findAccountByName("account3");
        System.out.println(b);

        Account ca = new Account();
        ca.setId(15);
        ca.setName("hello");
        ca.setMoney(10000);
        accountDao.updateAccount(ca);



    }
}

 

以上是关于jdbcTemplate-持久层CRUD的主要内容,如果未能解决你的问题,请参考以下文章

Spring对持久层技术的整合

spring学习spring的jdbcTemplate(增删改查封装)

Spring的JDBC模板

SpringJdbc持久层封装,Spring jdbcTemplate封装,springJdbc泛型Dao,Spring baseDao封装

spring jdbcTemplate的CRUD操作

学习笔记——定义切面优先级 ;Spring中的JdbcTemplate;JdbcTemplate的常用API