一、配置好SSH的基础环境后,调试
1.配置db.properties数据库的连接信息,并配置applicationCoontext.xml
db.properties:
jdbc.user=root
jdbc.password=administrator
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///ejbdatabase
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
#...
applicationCoontext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置 C3P0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
2.DataSource调试连接:
package com.test.ssh.test;
import java.sql.SQLException;
import javax.activation.DataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDAO {
@Test
public void testDataSource() throws SQLException{
ApplicationContext ctx = null ;
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
javax.sql.DataSource dataSource = ctx.getBean(javax.sql.DataSource.class);
System.out.println( dataSource.getConnection() );
}
}
本文出自 “知识可以改变命运” 博客,请务必保留此出处http://iqdutao.blog.51cto.com/2597934/1745553
Java中的Hibernate、Struts2、Spring基础DAO调试过程
原文:http://iqdutao.blog.51cto.com/2597934/1745553