spring的JDBC的连接

来源:互联网 发布:芒果tv mac版 编辑:程序博客网 时间:2024/06/05 19:31

1.spring配置文件

<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- bean来定义读取资源文件 --><!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:/cn/et/lesson02/jdbc/jdbc.properties"></property></bean> --><!--context命名空间 读取资源文件  --><context:property-placeholder location="classpath:/cn/et/lesson02/jdbc/jdbc.properties"/><!-- 数据源只是为了获取连接 --><bean id="dataSouce"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="url" value="${url}"></property><property name="username" value="${username}"></property><property name="password" value="${password}"></property><property name="driverClassName" value="${dirverClass}"></property></bean><!-- 封装一些操作的方法 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSouce"></property></bean></beans>

2.使用web连接maven项目

1.在webxml中配置监听
  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>
2.使用这个对象就可以在serlvet中获取注解对象
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());GoodsService ns = (GoodsService) ac.getBean("goodsServiceImpl");

3.在jar项目中获取连接

ApplicationContext context = new GenericXmlApplicationContext("classpath:/cn/et/lesson02/mvc/spring.xml");context=(MyController)context.getBean("myController");

ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");





原创粉丝点击