一.properties文件方式配置数据源
1.放入database.properties(检查一下里面的信息是否与数据库相匹配)
2.在applicationContext.xml配置参数
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!-- 引入properties文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:database.properties</value>
</property>
</bean>
<!-- 配置DataSource -->
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引用数据源组件 -->
<property name="dataSource" ref="dataSource" />
<!-- 引用MyBatis配置文件中的配置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- 配置DAO -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
<property name="basePackage" value="com.dao" />
</bean>
<context:component-scan base-package="com.servicet" />
<!-- 定义事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="select*" propagation="SUPPORTS" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delect*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 定义切面 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod"
expression="execution(* com.servicet..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
</beans>
二.spring对JNDI对数据源的支持
1.把mysql-connector-java-5.1.0-bin.jar包放在Tomcat-bin目录下
2.修改Tomcat-conf目录下的context.xml文件
3.修改内容如下(注意:更改数据库名字,加油Resource name里面填写的名字后期在spring配置要一致)
4.修改applicationContext.xml配置(注: <value>java:comp/env/这里后面的要跟刚context.xml的名字一样)
5.测试要通过WEB测试,通过通过jsp页面调用servlet访问数据库看看是否返回数据信息,如成功则生效
BUG:Cannot convert value '0000-00-00 00:00:00' from column 1 to TIMESTAMP
解决方案:
1.String url = "jdbc:mysql://localhost:3306/test?relaxAutoCommit=true&zeroDateTimeBehavior=convertToNull";
注意符号"&"如果不成功的话, 需要转换成特定的字符,如下表所示,即把"&"转换为&
https://blog.csdn.net/woshisap/article/details/6558732