Spring-JDBC简介

Spring连接数据库的三种方式

 

1.通过DriverManagerDataSource获取连接对象

 

看简介可以得知这是个获取专门生产Connection对象的工厂类,基本上所有框架用来创建数据库连接都会实现这个接口,Spring的jdbc包就实现了这个接口,实现类就是DriverManagerDataSource,在它之前还有几个抽象类,这里不多BB,知道来历就好。

 

在spring.xml中配置

   <bean id="dataSouce"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     <property name="url" value="jdbc:mysql://localhost:3306/test" ></property>
     <property name="driverClassName" value="com.mysql.jdbc.Driver" ></property>

     <property name="username" value="root" ></property>
     <property name="password" value="123456" ></property>
   </bean>
   

 

 返回的是个DataSource对象,再通过DataSource的getConnection方法即可获取:

ApplicationContext context = new GenericXmlApplicationContext("classpath:/cn/et/day2/jdbc/spring.xml");
		DataSource dataSource =(DataSource)context.getBean("dataSource");
		try {
			System.out.println(dataSource.getConnection());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

这么做还不够简便,获取个连接对象,预编译SQL、执行、获取结果集的逻辑还是要自己动手写,Spring早就预料到这点了, 看下面第二种连接方式。

 

 

2.通过JdbcTemplate(JDBC模板)连接数据库

    SpringJDBC有个模板类,就叫做JdbcTemplate,这里面封装了一系列操作数据的方法,

query、 execute等等,这个类能够使我们开发时只关注SQL语句的编写以及结果的获取,其余的工作交给它就行。

 

    spring.xml配置


   <bean id="dataSouce"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     <property name="url" value="jdbc:mysql://localhost:3306/test" ></property>
     <property name="driverClassName" value="com.mysql.jdbc.Driver" ></property>
     <property name="username" value="root" ></property>
     <property name="password" value="123456" ></property>
   </bean>
   
   <!--  jdbcTemplate 是jdbc的一个模板 封装一些简单的增删改查方法 只要传入sql语句就可以了 -->
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      <property name="dataSource" ref="dataSouce"></property>
   </bean>

   

测试类:

public class TestCotainer {
	static JdbcTemplate jt; //创建一个工具类
	static{
		ApplicationContext context = new GenericXmlApplicationContext("classpath:/cn/et/day2/jdbc/spring.xml");
		jt=(JdbcTemplate)context.getBean("jdbcTemplate");  //实例化工具类
	}

	public static void main(String[] args) throws SQLException {
		//容器对象 传入一个sql语句
		query("select * from student");
		
	}
	
	/**
	 * 封装查看的方法
	 */
	public static void query(String sql){
		List<Map<String, Object>> list=jt.queryForList(sql);
			System.out.println(list);
	}
	
	/**
	 * 封装增删改的方法
	 */
	public static void update(){
		String sql="delete from student where s=1";
		jt.execute(sql);
	}
}

是的,从创建数据库连接到获取查询结果,只需要这么几句Java代码,其他繁琐的步骤全部交给Spring处理,屌的不行。

 

 

3.使用外部属性文件

这个方法呢不仅仅是适用于JDBC,在配置其他Bean的时候一样管用。Spring 提供了一个叫 PropertyPlaceholderConfigurer 的类,这个类可以读取properties文件,然后配置文件里使用形式为 ${键} 的形式,获取属性文件中的数据。

 

    a) Spring 2.0时的玩法:

既然是个类,那么还是要用到bean标签去声明的,这个类的全路径有点长:

   <!-- 扫描配置文件的路径 -->
    <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="location" value="classpath:/demo02/jdbc/jdbc.properties"></property>
   </bean>
   <!-- 数据源只是为了获取连接 ${username}是个关键字 默认获取操作系统的用户名 Administrator -->
   <bean id="dataSouce"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     <property name="url" value="${url}" ></property>
     <property name="username" value="${username1}" ></property>
     <property name="password" value="${password1}" ></property>
     <property name="driverClassName" value="${driverClass}" ></property>
   </bean>
   
   <!-- 封装一些 操作的方法 -->
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      <property name="dataSource" ref="dataSouce"></property>
   </bean>

name=location意思是调用这个类中的setLocation方法,value值则是properties文件所在的路径,classpath前缀,用类路径(相对路径),file前缀,用绝对路径。 

 

注意:使用这种方式后就不能再用username作为键了,这种方式优先填充的是系统操作系统的用户名,如果数据库的用户名和操作系统用户名一致,可以用,如果不一致,就改成别的吧。

 

资源文件:

url=jdbc:mysql://localhost:3306/test
driverClass=com.mysql.jdbc.Driver
username1=root
password1=88888888

 

b) Spring2.5 之后的玩法:

Spring 2.5 之后: 可通过 <context:property-placeholder> 元素简化、

<beans> 中添加 context Schema 定义,添加context命名空间:

 

在配置文件中加入如下配置:

   <!-- 扫描配置文件的路径 -->
   <context:property-placeholder location="classpath:/demo02/jdbc/jdbc.properties"/>

效果等价。。。。。。。。。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值