Spring学习笔记之六(数据源的配置)

 

 1.前言

上一篇博客分析了,Spring中实现AOP的两种动态代理的机制,以下这篇博客。来解说一下Spring中的数据源的配置。


 2.DAO支持的模板类

Spring提供了非常多关于Dao支持的模板类,比如HibernateTemplate、JdbcTemplate等,以下以后者为例。来看一个Demo

<span style="font-family:SimSun;font-size:18px;">package com.test;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class JDBCTest {
	public static void main(String[] args) {
		//创建一个DataSource的对象,封装数据库连接的信息
		DriverManagerDataSource dataSource=new DriverManagerDataSource();
		
		//为其指定连接数据库的信息
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://localhost:3306/test");
		dataSource.setUsername("root");
		dataSource.setPassword("");
		
		//使用模板类,必须先有一个模板类的对象,必须为其提供数据库相关的连接信息
		JdbcTemplate template=new JdbcTemplate(dataSource);
		//运行操作
		template.execute("insert into news_title values('54','54','354','6345')");
	}
}
</span>


 3.数据源配置

上面则仅仅是简单的利用了一个JDBCTemplate。而在Spring中为我们提供了非常多Dao的模板类,例JdbcTemplate、HibernateTemplate、SqlMapClientTemplate(过时)、JpaTemplate (过时)等,以下以JdbcDaoSupport为例,来看一下详细的数据源的配置。

IDAO配置

IDAO是底层的接口类,提供了数据訪问的功能

<span style="font-family:SimSun;font-size:18px;">package cn.itast.tx.account;

public interface AccountDao {
	public void inMoney(String in,Double money);
	public void outMoney(String out,Double money);
}
</span>


DAO实现了IDAO,封装了详细的数据訪问的功能

<span style="font-family:SimSun;font-size:18px;">package cn.itast.template.jdbc;

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

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import cn.itast.template.jdbc.vo.UserModel;

public class UserDaoImpl extends JdbcDaoSupport{
	/*
	//继承了DAO支持抽象类后。将自己主动为其提供注入的方法
	//能够为其注入模板对象也能够为其注入数据源
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	*/
	public void add(UserModel um){
		//String sql = "insert into tbl_user values(null,'"+um.getUserName()+"',"+um.getAge()+")";
		//获取模板对象的方法 this.getJdbcTemplate()
		//this.getJdbcTemplate().execute(sql);
		String sql = "insert into tbl_user values(null,?,?)";
		this.getJdbcTemplate().update(sql, um.getUserName(),um.getAge());
	}
	public void delete(UserModel um){
		String sql = "delete from tbl_user where uuid = "+um.getUuid();
		this.getJdbcTemplate().execute(sql);
	}
	public void update(UserModel um){
		//String sql = "update tbl_user set userName = '"+um.getUserName()+"' ,age = "+um.getAge()+" where uuid = "+um.getUuid();
		//this.getJdbcTemplate().execute(sql);
		String sql = "update tbl_user set userName= ?

, age= ?

where uuid = ?

"; this.getJdbcTemplate().update(sql, um.getUserName(),um.getAge(),um.getUuid()); } public String getNameByUuid(Long uuid){ String sql = "select userName from tbl_user where uuid = ?"; return this.getJdbcTemplate().queryForObject(sql, String.class, uuid); } public Long getCount(){ String sql = "select count(uuid) from tbl_user"; return this.getJdbcTemplate().queryForLong(sql); } public UserModel get(Long uuid){ String sql = "select * from tbl_user where uuid = ?"; RowMapper<UserModel> rm = new RowMapper<UserModel>() { public UserModel mapRow(ResultSet rs, int rowNum)throws SQLException { UserModel um = new UserModel(); um.setUuid(rs.getLong("uuid")); um.setUserName(rs.getString("userName")); um.setAge(rs.getInt("age")); return um; } }; return this.getJdbcTemplate().queryForObject(sql,rm,uuid); } public List<UserModel> getAll(){ String sql = "select * from tbl_user"; RowMapper<UserModel> rm = new RowMapper<UserModel>() { public UserModel mapRow(ResultSet rs, int rowNum)throws SQLException { UserModel um = new UserModel(); um.setUuid(rs.getLong("uuid")); um.setUserName(rs.getString("userName")); um.setAge(rs.getInt("age")); return um; } }; return this.getJdbcTemplate().query(sql, rm); } } </span>



详细的数据源的注入

因为JdbcDaoSupport须要DataSource的注入

<span style="font-family:SimSun;font-size:18px;"><?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">

	<!-- DAO -->
	<bean id="userDao" class="cn.itast.template.jdbc.UserDaoImpl">
		<!-- <property name="jdbcTemplate" ref="jdbcTemplate"/> -->
		<property name="dataSource" ref="dataSource"/>
	</bean>

	<!-- JdbcTemplate -->
	<!-- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"/>
	</bean> -->
	
	<!-- DataSource -->
	<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/springdb"/>
		<property name="username" value="root"/>
		<property name="password" value="root"/>
	</bean>
</beans>




</span>


分析:上述仅仅是一个简单的数据源的注入,Spring为我们提供了非常多。可是全部的配置方式都是一致的。


原文地址:https://www.cnblogs.com/zhchoutai/p/7323307.html