Spring框架簡介⑨

Milky-way發表於2018-08-08

JDBCTemplate框架的簡單使用(實際工作中使用較少)

引包

連結:https://pan.baidu.com/s/1lyoSeLmkkFJurBF_4GFH4g 密碼:hyz8

先上第一個例子, 建立資料庫表我省略了...

配置檔案:

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

    <!-- 配置資料來源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/spring_10"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
        <!-- 初始化連線數 -->
        <property name="initialSize" value="1"></property>
        <!-- 最大連線數 -->
        <property name="maxActive" value="5"></property>
        <!-- 最大空閒連線數 -->
        <property name="maxIdle" value="2"></property>
        <!-- 最小空閒連線數 -->
        <property name="minIdle" value="1"></property>
    </bean>
    
    <bean id="userDao" class="com.rl.spring.dao.impl.UserDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="userService" class="com.rl.spring.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>
</beans>

UserDaoImpl類:

package com.rl.spring.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.rl.spring.dao.UserDao;
import com.rl.spring.model.User;

public class UserDaoImpl implements UserDao {
    
    //需要引入資料來源
    private DataSource dataSource;
    
    private JdbcTemplate jt;
    
    /**
     * 使用配置檔案的方式注入必須提供set方法
     */
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jt = new JdbcTemplate(dataSource);
    }

    /**
     * 對於save跟update的方法 都是採用jt.update()
     * userId自增
     */
    @Override
    public void saveUser(User user) {
        String sql = "insert into t_user values(null, ?, ?)";
        jt.update(sql, new Object[] {user.getUsername(), user.getPassword()});
    }

   
}

UserServiceImpl類:

package com.rl.spring.service.impl;

import java.util.List;

import com.rl.spring.dao.UserDao;
import com.rl.spring.model.User;
import com.rl.spring.service.UserService;
public class UserServiceImpl implements UserService {

    private UserDao userDao;
    
    /**
     * 使用配置檔案的方式注入, 必須提供set方法
     * @param userDao
     */
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void saveUser(User user) {
        userDao.saveUser(user);
    }

    
}

測試程式碼:

package com.rl.spring.test;

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 com.rl.spring.model.User;
import com.rl.spring.service.UserService;

@RunWith(value=SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:ApplicationContext.xml"})
public class TestSpring {
	
    @Autowired
    UserService userService;
    
    @Test
    public void test() {
        User user = new User();
        user.setUsername("lijialin");
        user.setPassword("123");
        userService.saveUser(user);
    }
    
}

資料庫展示結果:

接下來:

修改:

UserDaoImpl類:

package com.rl.spring.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.rl.spring.dao.UserDao;
import com.rl.spring.model.User;

public class UserDaoImpl implements UserDao {
    
    //需要引入資料來源
    private DataSource dataSource;
    
    private JdbcTemplate jt;
    
    /**
     * 使用配置檔案的方式注入必須提供set方法
     */
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jt = new JdbcTemplate(dataSource);
    }

    

    @Override
    public void updateUser(User user) {
        String sql = "update t_user t set t.username = ?, t.password=? where t.user_id=?";
        jt.update(sql, new Object[] {user.getUsername(), user.getPassword(), user.getUserId()});
    }

}

UserServiceImpl類我就不放上去了

測試程式碼:

package com.rl.spring.test;

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 com.rl.spring.model.User;
import com.rl.spring.service.UserService;

@RunWith(value=SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:ApplicationContext.xml"})
public class TestSpring {
	
    @Autowired
    UserService userService;
    
    
    @Test
    public void test1() {
        User user = new User();
        user.setUserId(1);
        user.setUsername("zhangsan");
        user.setPassword("666");
        userService.updateUser(user);
    }
    
}

資料庫結果:

接下來:

查詢:

UserDaoImpl類:

package com.rl.spring.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.rl.spring.dao.UserDao;
import com.rl.spring.model.User;
import com.rl.spring.model.UserRowMapper;

public class UserDaoImpl implements UserDao {
    
    //需要引入資料來源
    private DataSource dataSource;
    
    private JdbcTemplate jt;
    
    /**
     * 使用配置檔案的方式注入必須提供set方法
     */
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jt = new JdbcTemplate(dataSource);
    }

   

    @Override
    public User queryUserById(int userId) {
        String sql = "select * from t_user where user_id = ?";
        User user = jt.queryForObject(sql, new Object[] {userId}, new UserRowMapper());
        return user;
    }

    @Override
    public List<User> queryUserAll() {
        String sql = "select * from t_user";
        List<User> userList = jt.query(sql, new UserRowMapper());
        return userList;
    }
}

測試程式碼:

package com.rl.spring.test;

import java.util.List;

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 com.rl.spring.model.User;
import com.rl.spring.service.UserService;

@RunWith(value=SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:ApplicationContext.xml"})
public class TestSpring {
	
    @Autowired
    UserService userService;
    
   
    
    @Test
    public void test2() {
        User user = userService.queryUserById(1);
        System.out.println(user);
    }
    
    @Test
    public void test3() {
        List<User> userList = userService.queryUserAll();
        System.out.println(userList);
    }
    
}

結果就不截圖了...

 

Spring對屬性檔案的讀取

屬性檔案:

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/spring_10
uname=root
pword=root
initialSize=1
maxActive=5
maxIdle=2
minIdle=1

最終的配置檔案

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

    <!-- 配置屬性檔案的位置 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <!-- 配置資料來源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    
        <!-- 採用${xxx}的形式讀取屬性檔案中對應key的內容 -->
        <property name="driverClassName" value="${driverClassName}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${uname}"></property>
        <property name="password" value="${pword}"></property>
        <!-- 初始化連線數 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 最大連線數 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 最大空閒連線數 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 最小空閒連線數 -->
        <property name="minIdle" value="${minIdle}"></property>
    </bean>
    
    <bean id="userDao" class="com.rl.spring.dao.impl.UserDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="userService" class="com.rl.spring.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>
</beans>

 

相關文章