• spring中JdbcTemplate使用


    1、maven依赖

    <?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>com.ly.spring</groupId>
        <artifactId>spring06</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.0.2.RELEASE</version>
            </dependency>
            <!--JdbcTemplate-->
            <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>
            <!--整合junit-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>5.0.2.RELEASE</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <!--解决IDEA maven变更后自动重置LanguageLevel和JavaCompiler版本的问题-->
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>13</source>
                        <target>13</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    2、实体类

    package com.ly.spring.domain;
    
    import java.io.Serializable;
    
    public class Account implements Serializable {
        private int id;
        private int uid;
        private double money;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public int getUid() {
            return uid;
        }
    
        public void setUid(int uid) {
            this.uid = uid;
        }
    
        public double getMoney() {
            return money;
        }
    
        public void setMoney(double money) {
            this.money = money;
        }
    
        @Override
        public String toString() {
            return "Account{" +
                    "id=" + id +
                    ", uid=" + uid +
                    ", money=" + money +
                    '}';
        }
    }

    3、dao接口

    package com.ly.spring.dao;
    
    import com.ly.spring.domain.Account;
    
    import java.util.List;
    
    public interface IAccountDao {
        public List<Account> findAll();
        public Account findOne(Integer id);
        public int saveAccount(Account account);
        public int updateAccount(Account account);
        public int deleteAccount(Integer id);
        public int findAccount();
    }

    4、dao实现类

    package com.ly.spring.dao.impl;
    
    import com.ly.spring.dao.IAccountDao;
    import com.ly.spring.domain.Account;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.support.JdbcDaoSupport;
    import org.springframework.stereotype.Repository;
    
    import javax.sql.DataSource;
    import java.util.List;
    
    @Repository("accountDao")
    public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
        //给JdbcDaoSupport注入DataSource
        @Autowired
        public AccountDaoImpl(DataSource dataSource) {
            super.setDataSource(dataSource);
        }
    
        @Override
        public List<Account> findAll() {
            return super.getJdbcTemplate().query("select * from account",new BeanPropertyRowMapper<Account>(Account.class));
        }
    
        @Override
        public Account findOne(Integer id) {
            return super.getJdbcTemplate().queryForObject("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),6);
        }
    
        @Override
        public int saveAccount(Account account) {
            return super.getJdbcTemplate().update("insert into account(id,uid,money) values(?,?,?)",account.getId(),account.getUid(),account.getMoney());
        }
    
        @Override
        public int updateAccount(Account account) {
            return super.getJdbcTemplate().update("update account set money = ? where id = ?",account.getMoney(),account.getId());
        }
    
        @Override
        public int deleteAccount(Integer id) {
            return super.getJdbcTemplate().update("delete from account where id = ?",id);
        }
    
        @Override
        public int findAccount() {
            return super.getJdbcTemplate().queryForObject("select count(1) from account",Integer.class);
        }
    
    }

    5、jdbc配置文件

    jdbc.driver = com.mysql.jdbc.Driver
    jdbc.url = jdbc:mysql://localhost:3306/db01
    jdbc.user = root
    jdbc.password = root

    6、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">
        <!--配置注解扫描包-->
        <context:component-scan base-package="com.ly.spring"></context:component-scan>
        <!--配置资源文件-->
        <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
        <!--配置数据源并注入相关属性-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.driver}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="username" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
        </bean>
    </beans>

    7、测试类

    package com.ly.spring.test;
    
    import com.ly.spring.dao.IAccountDao;
    import com.ly.spring.domain.Account;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import java.util.List;
    
    //替换junit的main方法
    @RunWith(SpringJUnit4ClassRunner.class)
    //指定spring的配置文件的位置
    @ContextConfiguration(locations = "classpath:bean.xml")
    public class MainTest {
        @Autowired
        private IAccountDao accountDao;
        @Test
        public void testFindAll() {
            List<Account> accounts = accountDao.findAll();
            System.out.println(accounts);
            System.out.println("------");
        }
        @Test
        public void testFindOne() {
            Account account = accountDao.findOne(52);
            System.out.println(account);
        }
    
        @Test
        public void testSaveAccount() {
            Account account = new Account();
            account.setUid(51);
            account.setMoney(20000);
            accountDao.saveAccount(account);
        }
    
        @Test
        public void testUpdateAccount() {
            Account account = new Account();
            account.setId(7);
            account.setMoney(100000);
            accountDao.updateAccount(account);
        }
        @Test
        public void testDeleteAccount() {
            accountDao.deleteAccount(7);
        }
    
        @Test
        public void testFindAccount() {
            int count = accountDao.findAccount();
            System.out.println("count---"+count);
    }
    }
  • 相关阅读:
    Atitit 软件知识点分类体系 分类 按照书籍的分类 学科分类 体系与基础部分 计算机体系结构 硬件接口技术(usb,agp,pci,div,hdmi) os操作系统 中间件 语言部分
    Atitit spring注解事务的demo与代码说明 目录 1.1. Spring框架中,要如何实现事务?有一个注解,@EnableTransactionManagement 1 1.2. 事务管理
    Atitit springboot mybatis spring 集成 Springboot1.4 mybatis3.4.6 /springbootMybatis 目录 1.1. 设置map
    Atitit 计算机系统结构 计算机系统结构 Cpu 存储 cache 指令系统 目录 Line 56: 第2章指令系统设计 指令格式 寻址方式 1 Line 64: 第3章CPU及其实现
    Atitit 微服务 分布式 区别 微服务的判断标准 目录 1.1. 区别 微服务侧重于微小服务进程隔离级别,分布式侧重于机器隔离 1 2. 微服务是一种架构, 。多微才叫微? 1 2.1. 微服务
    Atitit spirngboot 访问 html文件总结 自设计web服务器原理与实现 Url路由压力,读取url,获得项目更路径绝对路径,拼接为文件路径。读取文建内容输出即可 目录路径 u
    Atitit 现代信息检索 Atitit 重要章节 息检索建模 检索评价 第8章 文本分类 Line 210: 第9章 索引和搜索 第11章 Web检索 第13章 结构化文本检索 目录 L
    Atitit 定时器timer 总结 目录 1.1. Js定时器 window.setInterval 1 2. Java定时器 timer 1 1.1.Js定时器 window.setInter
    Atitit spring5 集成 mybatis 注解班
    Atitit 微服务的一些理论 目录 1. 微服务的4个设计原则和19个解决方案 1 2. 微服务应用4个设计原则 1 2.1. AKF拆分原则 2 2.2. 前后端分离 2 2.3. 无状态服务
  • 原文地址:https://www.cnblogs.com/liuyang-520/p/12347023.html
Copyright © 2020-2023  润新知