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 ...
随机推荐
- Android控件:RadioButton(单选button)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- linux又一次编译安装gd,添加freetype支持,解决验证码不显示问题,Fatal error: Call to undefined function imagettftext()
问题: Fatal error: Call to undefined function Think\imagettftext() in /var/www/webreg/ThinkPHP/Library ...
- 学习笔记:Vue——组件和Prop
前言:这一篇是关于组件基础.组件注册.Prop等内容. 1.组件基础 01.组件是可复用的Vue实例 02.组件中的data选项必须是一个函数 03.一个组件默认可以有任意数量的prop 任何值都可以 ...
- Javascript和jquery事件--阻止事件冒泡和阻止默认事件
阻止冒泡和阻止默认事件—js和jq相同,jq的event是一个全局的变量 我们写代码的时候常用的都是事件冒泡,但是有的时候我们并不需要触发父元素的事件,而浏览器也有自己的默认行为(表单提交.超链接跳转 ...
- C# 进制转换 在什么情况下使用16进制,字节数组,字符串
C# 进制转换 Admin2013年9月18日 名人名言:从工作里爱了生命,就是通彻了生命最深的秘密.——纪伯伦 1.请问c#中如何将十进制数的字符串转化成十六进制数的字符串 //十进制转二进制Con ...
- GCC 编译 --sysroot
-sysroot 的作用 如果在编译时指定了-sysroot就是为编译时指定了逻辑目录.编译过程中需要引用的库,头文件,如果要到/usr/include目录下去找的情况下,则会在前面加上逻辑目录. 如 ...
- 动态规划 —— 求解通配符问题(wildcard)
he?p help, heap, √ hellp, × *p*(必须包含 p,左右随意) help, papa, √ hello × *bb*(必须包含连续的两个 bb,左右随意) babbc √ 1 ...
- java学习笔记之基础语法(二)
1.数组: 概念:同一种类型数据的集合,其实,数组就是一个容器 优点:可以方便的对其进行操作,编号从0开始,方便操作这些元素. 2,数组的格式 元素类型[]数组名=new 元素类型[数组元素个数]: ...
- 表单提交数据格式form data
前言: 最近遇到的最多的问题就是表单提交数据格式问题了. 常见的三种表单提交数据格式,分别举例说明:(项目是vue的框架) 1.application/x-www-form-urlencoded 提交 ...
- SpringBoot日志logback-spring.xml分环境(转)
springboot按照profile进行打印日志 log4j logback slf4j区别? 首先谈到日志,我们可能听过log4j logback slf4j这三个名词,那么它们之间的关系是怎么样 ...