<!-- 引入外部属性文件jdbc.properties --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置c3p0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置hibernate相关属性 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 注入连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 配置hibernate属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 加载hibernate中的映射文件 --> <property name="mappingResources"> <list> <value>ssh/domain/Product.hbm.xml</value> </list> </property> </bean> <!-- 配置dao层的类 --> <bean id="productDao" class="ssh.dao.ProductDao"> </bean> <!-- 配置service层的类 --> <bean id="productService" class="ssh.service.ProductService"> <property name="productDao" ref="productDao"></property> </bean> <!-- 配置action层的类 --> <!-- 由于action是多例的,而spring是单例,需要添加scope="prototype" --> <bean id="productAction" class="ssh.action.ProductAction" scope="prototype"> <property name="productService" ref="productService"></property> </bean>
<hibernate-mapping> <class name="ssh.domain.Product" table="product"> <id name="pid" column="pid"> <generator class="native"/> </id> <property name="pname" column="pname" length="20"/> <property name="price" column="price"/> </class> </hibernate-mapping>
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssh
jdbc.username=root
jdbc.password=21henearkrxnrn!@#$
加上slf4j的api和log4j-slf4j,就会报错:
Failed to instantiate SLF4J LoggerFactory
Reported exception:
java.lang.NoClassDefFoundError: org/apache/log4j/Level
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
at com.mchange.v2.log.slf4j.Slf4jMLog.<init>(Slf4jMLog.java:68)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
...
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1275)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1109)
...
严重: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/log4j/Level
...
Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/Level
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
at com.mchange.v2.log.slf4j.Slf4jMLog.<init>(Slf4jMLog.java:68)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
...
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1275)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1109)
...
严重: Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/log4j/Level
...
你的MySQL数据库是不是5.0以上的版本,是的话要把方言的设置改成org.hibernate.dialect.MySQL5Dialect,就是在原来的那个上面加个"5"。
第二个问题解决了,slf4j-api和log4j-slf4j是冲突的!