天天看點

關于Spring通路資料層的幾個方法,從spring-jdbc到jdbctemplate到hibernatetemplate

代碼結構

關于Spring通路資料層的幾個方法,從spring-jdbc到jdbctemplate到hibernatetemplate

引入代碼

從入口開始

package spring_jdbc.spring_jdbc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import spring_jdbc.dao.CustomerDAO;
import spring_jdbc.model.Customer;

public class App {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
        CustomerDAO customerDao = (CustomerDAO) context.getBean("customerDAO");
        Customer customer = new Customer(, "can", );
        customerDao.insert(customer);

        Customer findCustomer = customerDao.findByCustomerId();
        System.out.println(findCustomer.getId());
        System.out.println(findCustomer.getName());
        System.out.println(findCustomer.getAge());
    }
}
           

想看源碼的時候遇到問題,ctrl 不能attach source,解決方案

設定java反編譯工具

程式解釋:

ApplicationContext的中文意思是“應用前後關系”,它繼承自BeanFactory接口,代表一個上下文的環境,具體介紹見

ApplicationContext

Spring-Module.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

    <import resource="database/Spring-Datasource.xml" />
    <import resource="customer/Spring-Customer.xml" />

</beans>
           

關于xmlns

Spring-Datasource.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/spring_jdbc" />
        <property name="username" value="root" />
        <property name="password" value="123456" />
    </bean>

</beans>
           

Spring.customer.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

    <bean id="customerDAO" class="org.thinkingingis.dao.impl.JdbcCustomerDAO">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>
           

Customer類

package spring_jdbc.model;

//建立一個與customer表對應的實體類,以便存放 Customer對象

public class Customer {
    private int id;
    private String name;
    private int age;

    public Customer(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
           

CustomerDao

package spring_jdbc.dao;

import spring_jdbc.model.Customer;

public interface CustomerDAO {
    public void insert(Customer customer);
    public Customer findByCustomerId(int custId);
}
           

JdbcCustomerDAO implements CustomerDAO

package spring_jdbc.dao.impl;

import spring_jdbc.dao.CustomerDAO;
import spring_jdbc.model.Customer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

public class JdbcCustomerDAO implements CustomerDAO {

    private DataSource dataSource;
    public void setDataSource(DataSource dataSource){
        this.dataSource = dataSource;
    }


    public void insert(Customer customer) {

        String sql = "INSERT INTO CUSTOMER (CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
        Connection conn = null;

        try {
            conn = dataSource.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(, customer.getId());
            ps.setString(, customer.getName());
            ps.setInt(, customer.getAge());
            ps.executeUpdate();
            ps.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {

                    e.printStackTrace();
                }
            }
        }

    }


    public Customer findByCustomerId(int custId) {
        String sql = "SELECT * FROM CUSTOMER WHERE CUST_ID = ?";
        Connection conn = null;

        try {
            conn = dataSource.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(, custId);
            Customer customer = null;
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                customer = new Customer(
                        rs.getInt("CUST_ID"), 
                        rs.getString("NAME"), 
                        rs.getInt("age")
                );
            }
            rs.close();
            ps.close();
            return customer;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {

                    e.printStackTrace();
                }
            }
        }


    }

}
           

程式解釋:

datasource簡單解釋

配置datasource的三種方法

什麼是JNDI

關于preparedstatement

整合xml,注入,注釋添加請看下集