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 ...
随机推荐
- diff命令具体解释
diff命令參数: diff - 找出两个文件的不同点 总览 diff [选项] 源文件 目标文件 以下是 GNU所接受的 diff 的全部选项的概要. 大多数的选项有两个同样的名字,一个是单个的跟在 ...
- OpenShift 自定义 OPENSHIFT_DOCUMENT_ROOT 变量,替换网站根目录路径!
OpenShift 自定义 OPENSHIFT_DOCUMENT_ROOT 变量,替换网站根目录路径! 预先定义的子目录 :) DIY: DocumentRoot=${OPENSHIFT_RE ...
- Android车载导航的一些困境
车载导航从最初的用解码芯片,过渡到用WinCE系统,已经形成了一个较大的产业.车载导航使用上的一些大原则,基本上被固定了下来.如今WinCE走到了尽头,Android车载导航開始发力,但由于Andro ...
- 逻辑与和逻辑或:&& 、||
逻辑或:首先看左边是真还是假(除了那5个都是真),如果为真,返回左边值,如果为假,返回右边的值 逻辑与:和逻辑或相同,先看左边的值是真是假,如果左边为真返回右边的值,左边为假返回左边的值 在两者同时出 ...
- Perl——正则表达式(四) 查找替换s///
转自http://blog.csdn.net/blog_abel/article/details/40589227 侵删 一. 介绍 使用 s/regex/replacement/modifiers ...
- TensorFlow 学习(十五)—— tensorflow.python.platform
tensorflow.python.platform 下的常用工具类和工具函数:tensorflow/tensorflow/python/platform at master · tensorflow ...
- 大战C100K之-Linux内核调优篇--转载
原文地址:http://joyexpr.com/2013/11/22/c100k-4-kernel-tuning/ 早期的系统,系统资源包括CPU.内存等都是非常有限的,系统为了保持公平,默认要限制进 ...
- FZU 2020 组合
组合数求模要用逆元,用到了扩展的欧几里得算法. #include<cstdio> int mod; typedef long long LL; void gcd(LL a,LL b,LL ...
- [CSS] Use CSS Counters to Create Pure CSS Dynamic Lists
CSS counters let you create dynamic lists without JavaScript. In this lesson, we will create a multi ...
- hdu 4406 费用流
这题问题就是当前时刻究竟选择哪门课程,易知选择是和分数有关的,而且是一个变化的权值,所以能够用拆点的方式,把从基础分到100分都拆成点.但若这样拆点的话,跑费用流时就必须保证顺序.这样就麻烦了..观察 ...