1. 程式人生 > >Spring+hibernate的配置和log4j的使用

Spring+hibernate的配置和log4j的使用

今日工作內容:上學校課程,學Spring+hibernate

1、spring和hibernate的配置:

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">


	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="oracle.jdbc.OracleDriver">
		</property>
		<property name="url"
			value="jdbc:oracle:thin:@localhost:1521:orcl">
		</property>
		<property name="username" value="warehouse"></property>
		<property name="password" value="tiger"></property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.Oracle9Dialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean></beans>
上面有一點和hibernate的不一樣,就是hibernate對的方言、一些配置的語法和hibernate不一樣前面要加hibernate。
<prop key="hibernate.dialect">
2、錯誤:Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.orm.hibernate3.LocalSessionFactoryBean] is defined: expected single bean but found 0: 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:271)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1101)
at Test.main(Test.java:9)

解決:import導包的時候導錯了。import org.springframework.orm.hibernate3.LocalSessionFactoryBean;我導成了3,而我的hibernate用的是4。所以出現了錯誤。

3、log4j的使用和配置:

log4j.rootLogger=ERROR,stdout,fileout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %c - %m%n


log4j.appender.fileout=org.apache.log4j.RollingFileAppender
log4j.appender.fileout.File=logtest.txt
log4j.appender.fileout.File.DatePattern = '_'yyyyMMdd
log4j.appender.fileout.MaxFileSize=10000KB 
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.ConversionPattern=%5p %d %c - %m%n

共有5個級別:debug,info,warn,error,fatal。

可以在控制檯輸出,以檔案的形式存在,可以每天產生一個日誌檔案,檔案到指定大小之後生成新的檔案(即RollingFileAppender)。