JdbcTemplate经典案例

Posted 关耳er

tags:

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

一、JdbcTemplate案例配置式

1)导入依赖

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>5.1.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.32</version>
</dependency>
View Code

(2)jdbc.properties连接数据库

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/account?useUniCode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123
View Code

(3)Accounts实体类

package cn.spring.accountstest.entity;

public class Accounts {
    private Integer accountid;
    private String accountname;
    private double balance;

    public Integer getAccountid() {
        return accountid;
    }

    public void setAccountid(Integer accountid) {
        this.accountid = accountid;
    }

    public String getAccountname() {
        return accountname;
    }

    public void setAccountname(String accountname) {
        this.accountname = accountname;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}
View Code

(4)Accountsdao 

package cn.spring.accountstest.dao;

import cn.spring.accountstest.entity.Accounts;

import java.util.List;

public interface AccountsDao {
    //查询所有的账户
    List<Accounts> getList();
}
View Code

(5)AccountdaoImpl实现类

package cn.spring.accountstest.dao.impl;

import cn.spring.accountstest.dao.AccountsDao;
import cn.spring.accountstest.entity.Accounts;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountsDao {
    @Override
    public List<Accounts> getList() {
        JdbcTemplate jdbcTemplate = this.getJdbcTemplate();
        String sql="select * from accounts";
        List<Accounts> lists = jdbcTemplate.query(sql, new RowMapper<Accounts>() {
            /**
             *
             * @param rs   普通的rs  系统给的   读取器
             * @param i    读取器读取的第几条数据
             * @return accounts  单个对象
             * @throws SQLException
             */
            @Override
            public Accounts mapRow(ResultSet rs, int i) throws SQLException {
                Accounts accounts = new Accounts();
                accounts.setAccountid(rs.getInt("accountid"));
                accounts.setAccountname(rs.getString("accountname"));
                accounts.setBalance(rs.getDouble("balance"));
                return accounts;
            }
        });
        return lists;
    }
}
View Code

(6)AccountService

package cn.spring.accountstest.service;

import cn.spring.accountstest.entity.Accounts;

import java.util.List;

public interface AccountsService {
    //查询所有的账户
    List<Accounts> getList();
}
View Code

(7)AccountServiceImpl实现类

package cn.spring.accountstest.service.impl;

import cn.spring.accountstest.dao.AccountsDao;
import cn.spring.accountstest.entity.Accounts;
import cn.spring.accountstest.service.AccountsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
public class AccountsServiceImpl implements AccountsService{
    //使用Spring IOC 将AccountDao对象注入
    AccountsDao dao;
    @Override
    public List<Accounts> getList() {
        return dao.getList();
    }


    public AccountsDao getDao() {
        return dao;
    }

    public void setDao(AccountsDao dao) {
        this.dao = dao;
    }
}
View Code

(8)applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--1.配置数据源   spring内置的数据源-->
    <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.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--2.引入属性文件-->
    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>

    <!--3.构建jdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--4.dao-->
    <bean id="accountsDao" class="cn.spring.accountstest.dao.impl.AccountDaoImpl">
        <!--为jdbcTemplate配置数据源-->
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <!--5.service-->
    <bean id="accountsService" class="cn.spring.accountstest.service.impl.AccountsServiceImpl">
        <property name="dao" ref="accountsDao"></property>
    </bean>



    <!--扫描注解:包扫描器-->
    <context:component-scan base-package="cn.spring"></context:component-scan>

    <!--开启AOP注解支持-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
View Code

(9)测试类

package cn.spring;

import cn.spring.accountstest.entity.Accounts;
import cn.spring.accountstest.service.AccountsService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class AccountsTest {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Spring容器中获取Service对象
        AccountsService accountsService=(AccountsService) context.getBean(AccountsService.class);
        List<Accounts> list = accountsService.getList();
        //输出数据
        for (Accounts accounts:list) {
            System.out.println("账户名:"+accounts.getAccountname()+",余额为:"+accounts.getBalance());
        }
    }
}
View Code

(10)控制台

  

 

以上是关于JdbcTemplate经典案例的主要内容,如果未能解决你的问题,请参考以下文章

springboot使用jdbcTemplate案例

jdbctemplate入门案例

JdbcTemplate入门案例2

SpringSpring JDBCTemplate

spring boot与jdbcTemplate的整合案例2

Spring之JdbcTemplate使用