Java中的Hibernate、Struts2、Spring基礎DAO調試過程



一、配置好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()  );	
		
	}

}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章