spring中连接数据库

来源:互联网 发布:淘宝网地板胶 编辑:程序博客网 时间:2024/05/24 06:35
${username}是个关键字  不同引入方式优先读取的内容不同1、bean来定义读取资源文件优先读取资源文件2、context命名空间  读取资源文件优先读取系统用户第一种1、使用Bean定义 来读取properties文件<bean id="mnmn" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:/lesson02/jdbc/jdbcoracle.properties"></property></bean>2、创建数据库的连接类<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="${password}"></property><property name="driverClassName" value="${driverClass}"></property></bean><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSouce"></property></bean>第二种1、先配置context标签的快捷提示 xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd" 2、context命名空间  读取资源文件 读取properties文件<context:property-placeholder location="classpath:/lesson02/jdbc/jdbcoracle.properties"/>3、创建数据库的连接类<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="${password}"></property><property name="driverClassName" value="${driverClass}"></property></bean><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSouce"></property></bean>第三种(使用注解)1、spring.xml文件<?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-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"><!-- 读取properties文件 --><context:property-placeholder location="classpath:/lesson02/jdbc/jdbcoracle.properties"/><!-- 扫描注解 --><context:component-scan base-package="lesson02"></context:component-scan></beans>2、创建一个类package lesson02.mvc;import javax.sql.DataSource;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.datasource.DriverManagerDataSource;@Configurationpublic class MyConf {@Value("${url}")private String url;@Value("${driverClass}")private String driverClassName;@Value("${username1}")private String userName;@Value("${password}")private String password;@Beanpublic JdbcTemplate jdbcTemplate(DataSource dataSource){return new JdbcTemplate(dataSource);}@Beanpublic DataSource dataSouce(){DriverManagerDataSource dmd=new DriverManagerDataSource();dmd.setUrl(url);dmd.setDriverClassName(driverClassName);dmd.setUsername(userName);dmd.setPassword(password);return dmd;}}

原创粉丝点击