JdbcTemplate Classic Case

A, JdbcTemplate Case Profiles

( 1 ) introducing dependent

<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) the jdbc.properties connection database

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) the Accounts entity class

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 layer 

Package cn.spring.accountstest.dao; 

Import cn.spring.accountstest.entity.Accounts; 

Import java.util.List; 

public  interface AccountsDao {
     // query all accounts 
    List <the Accounts> the getList (); 
}
View Code

(. 5) AccountdaoImpl implementation class

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>the getList () { 
        the JdbcTemplate the jdbcTemplate = the this .getJdbcTemplate (); 
        String SQL = "SELECT * from Accounts" ; 
        List <the Accounts> = jdbcTemplate.query Lists (SQL, new new the RowMapper <the Accounts> () {
             / ** 
             * 
             * @param Common to the system rs rs reader 
             * @param the first few data read by the reader i 
             * @return Accounts single object 
             * @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) the AccountService layer

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 implementation class

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) {
         the 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 > 
    </ the bean > 



    <-! Scan Notes: scanner package -> 
    < context: the Component-Scan Base-Package Penalty for = "cn.spring" > </ context: the Component-Scan > 

    <-! open AOP annotation support -> 
    < AOP: AspectJ-autoproxy > </ AOP: AspectJ-autoproxy > 
< / Beans >
View Code

(9) Test Class

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 <the Accounts> List = accountsService.getList ();
         // output data 
        for (the Accounts Accounts: List) { 
            System.out.println ( "account name:" + accounts.getAccountname () + ", balance is:" + accounts.getBalance ()); 
        } 
    } 
}
View Code

(10) Console

  

 

Guess you like

Origin www.cnblogs.com/Zzzzn/p/11781261.html