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为我们提供了非常多。可是全部的配置方式都是一致的。

Spring学习笔记之六(数据源的配置)的更多相关文章

  1. Spring学习笔记(2)——Bean的配置

    要使应用程序中的Spring容器成功启动,需要以下三个方面的条件都具备: 1.Spring框架的类包都已经放到应用程序的类路径下 2.应用程序为Spring提供完备的Bean配置信息 3.Bean的类 ...

  2. spring学习笔记一 入门及配置

    Spring是一个开源框架,为了解决企业应用开发的复杂性而创建的.主要优势之一就是其分层架构.Spring的核心是控制反转和面向切面.简单来说,Spring是一个分层的一站式轻量级开源框架. 使用Sp ...

  3. Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)

    在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...

  4. 不错的Spring学习笔记(转)

    Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是s ...

  5. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  6. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

  7. Spring学习笔记2——表单数据验证、文件上传

    在上一章节Spring学习笔记1——IOC: 尽量使用注解以及java代码中,已经搭建了项目的整体框架,介绍了IOC以及mybatis.第二节主要介绍SpringMVC中的表单数据验证以及文件上传. ...

  8. 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传

    作者:ssslinppp       1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...

  9. 【Spring学习笔记-MVC-9】SpringMVC数据格式化之日期转换@DateTimeFormat

    作者:ssslinppp       1. 摘要 本文主要讲解Spring mvc数据格式化的具体步骤: 并讲解前台日期格式如何转换为java对象: 在之前的文章<[Spring学习笔记-MVC ...

随机推荐

  1. secureCRT The remote system refused the connection问题解决

    问题: Ubuntu系统必须开启ssh服务后,XP或者其它的主机才干够远程登陆到Ubuntu系统. 1,安装软件包,运行sudo apt-get install openssh-server Ubun ...

  2. curl 使用 ~/.netrc ( Windows 上是 _netrc ) 问题

    curl 使用  --netrc-file <filemame> , -n, --netrc, --netrc-optional 等选项做登录认证时, 默认文件位于 ~/.netrc ( ...

  3. elasticsearch 源码概述

    从功能上说,可以分为两部分,分布式功能和数据功能.分布式功能主要是节点集群及集群附属功能如restful借口.集群性能检测功能等,数据功能主要是索引和搜索.代码上这些功能并不是完全独立,而是由相互交叉 ...

  4. setInterval()第一个参数带引号和不带引号的区别

    setInterval()第一个参数带引号和不带引号的区别:关于定时函数setInterval()的基本用法这里就不做介绍了,查阅相关教程即可,这里主要介绍一下setInterval()函数的第一个参 ...

  5. AndroidStudio MAT LeakCanary 内存分析之 LeakCanary

    现在我们换一种更清晰方便的方式:LeakCanary https://github.com/square/leakcanary 首先将LeakCanary绑在我们的app上 build.gradle ...

  6. 1. vue环境搭建和配置

      const 相对于 var # 全局安装 vue-cli install可以简写成i 1.$ npm install --global vue-cli # 创建一个基于 webpack 模板的新项 ...

  7. JS学习笔记 - fgm练习 - 网页换肤

    总结: 1. 点击按钮,div内部变色,边框保持颜色不变. 实现原理:其实本来就把background 和 border 分别设置了同一个颜色,看似是一个整体,其实本来就是分开的. 那么点击的时候,只 ...

  8. 【CS Round #48 (Div. 2 only)】Game of Chance

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 在这里写题解 [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h> using n ...

  9. 判断Bigdecimal类型是否等于0的方法

    1.我之前用来判断Bigdecimal类型是否等于0的方法 b.equals(BigDecimal.ZERO); 用equals方法和BigDecimal.ZERO进行比较. 2.上面方法存在的问题 ...

  10. numpy 高阶函数 —— np.histogram

    np.diff(a, n=1, axis=-1):n 表示差分的阶数: >> x = np.array([1, 2, 4, 7, 0]) >> np.diff(x) array ...