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为我们提供了非常多。可是全部的配置方式都是一致的。
Spring学习笔记之六(数据源的配置)的更多相关文章
- Spring学习笔记(2)——Bean的配置
要使应用程序中的Spring容器成功启动,需要以下三个方面的条件都具备: 1.Spring框架的类包都已经放到应用程序的类路径下 2.应用程序为Spring提供完备的Bean配置信息 3.Bean的类 ...
- spring学习笔记一 入门及配置
Spring是一个开源框架,为了解决企业应用开发的复杂性而创建的.主要优势之一就是其分层架构.Spring的核心是控制反转和面向切面.简单来说,Spring是一个分层的一站式轻量级开源框架. 使用Sp ...
- Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)
在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...
- 不错的Spring学习笔记(转)
Spring学习笔记(1)----简单的实例 --------------------------------- 首先需要准备Spring包,可从官方网站上下载. 下载解压后,必须的两个包是s ...
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- spring学习笔记(一) Spring概述
博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书. 强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...
- Spring学习笔记2——表单数据验证、文件上传
在上一章节Spring学习笔记1——IOC: 尽量使用注解以及java代码中,已经搭建了项目的整体框架,介绍了IOC以及mybatis.第二节主要介绍SpringMVC中的表单数据验证以及文件上传. ...
- 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传
作者:ssslinppp 1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...
- 【Spring学习笔记-MVC-9】SpringMVC数据格式化之日期转换@DateTimeFormat
作者:ssslinppp 1. 摘要 本文主要讲解Spring mvc数据格式化的具体步骤: 并讲解前台日期格式如何转换为java对象: 在之前的文章<[Spring学习笔记-MVC ...
随机推荐
- Flume Interceptors官网剖析(博主推荐)
不多说,直接上干货! Flume Sources官网剖析(博主推荐) Flume Channels官网剖析(博主推荐) Flume Channel Selectors官网剖析(博主推荐) Flume ...
- 【Codeforces Round #450 (Div. 2) A】Find Extra One
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟. 看看Y左边或右边的点个数是否<=1 [代码] #include <bits/stdc++.h> using ...
- JVM route
http://www.linuxidc.com/Linux/2013-06/86446.htm
- js进阶 13-9/10 jquery如何实现三级列表
js进阶 13-9/10 jquery如何实现三级列表 一.总结 一句话总结:用的是定位,父标签相对定位,子标签就可以绝对定位了,绝对定位的孩子还是可以设置绝对定位.用toggle设置子菜单显示和隐藏 ...
- Ten ways to improve the performance of large tables in MySQL--转载
原文地址:http://www.tocker.ca/2013/10/24/improving-the-performance-of-large-tables-in-mysql.html Today I ...
- POJ 2386 Lake Counting DFS水水
http://poj.org/problem?id=2386 题目大意: 有一个大小为N*M的园子,雨后积起了水.八连通的积水被认为是连接在一起的.请求出院子里共有多少水洼? 思路: 水题~直接DFS ...
- IR_drop
IR压降是指出现在集成电路中电源和地网络上电压下降或升高的一种现象.随着半导体工艺的演进金属互连线的宽度越来越窄,导致它的电阻值上升,所以在整个芯片范围内将存在一定的IR压降.IR压降的大小决定于从电 ...
- 【Codeforces Round #443 (Div. 2) A】Borya's Diagnosis
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟 [代码] #include <bits/stdc++.h> using namespace std; const ...
- OC学习篇之---第一个程序HelloWorld
从这篇开始我们就开始学习OC的相关知识了,在学习之前,个人感觉需要了解的其他的两门语言:一个是C/C++,一个是面向对象的语言(当然C++就是面向对象,不过这里最好还是Java).在干活之前,得先找到 ...
- std::的概念与作用
std:: 当中std是名称空间,防止反复.比如说很多人给函数取名可能都叫f1():你使用的时候就可能造成问题.如果各人均把自己的f1()放进自己的名称空间.我们在使用的时候带上名称空间就不会有问题. ...