发表于: 2017-12-25 20:30:48
1 718
今天完成的事情:
1. 方案评审完成
2. 学习Hibernate
明天计划的事情
1. 拆分禅道
2. 学习Hibernate
遇到的问题:
无
收获:
1. 学习Hibernate
spring+ hibernate的整合
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据连接信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<!-- 其他配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 指定hibernate的配置文件位置 -->
<!--<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>-->
<!-- 配置c3p0数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- hibernate常用配置 -->
<property name="hibernateProperties">
<props>
<!-- 指定数据库方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<!-- 显示Hibernate持久化操作所生成的SQL -->
<prop key="hibernate.show_sql">true</prop>
<!--<!– 根据需要自动创建数据表 –>-->
<!--<prop key="hibernate.hbm2ddl.auto">update</prop>-->
<!-- 将SQL脚本进行格式化后再输出 -->
<prop key="hibernate.format_sql">true</prop>
<!-- 避免这个错误信息Disabling contextual LOB creation as createClob() method threw error :java.lang.reflect.InvocationTargetException -->
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
</props>
</property>
<!--配置持久层注解扫描 @Entity-->
<property name="packagesToScan" value="com.hibernate.*"/>
</bean>
<!-- 设定transactionManager -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- b. 配置事务增强(拦截到方法后如果管理事务?) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- c. Aop配置 -->
<aop:config>
<aop:pointcut expression="execution(* com.hibernate.dao.impl.*.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
问题在于获取SessionFactay的只为空,原因IMPL没有给Spring 来实现
2. hibernate成功运行:
- 获取不到bean
如果用注解的形式来生成和获取bean , 会显示没有符合的bean ,
通过getBean("sessionFactay")会显示配置文件里没有这个bean但是,可以通过IDEA跳转打spring.xml
只有通过以下代码才能得到真正原因
@Test
public void testStudentSelect(){
Student student = new Student();
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:spring-hibernate.xml");
applicationContext.getBean("sessionFactory");
// student = studentDao.getStudentUser("qwe");
System.out.println(student);
}
}
报错:
org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 45; SchemaLocation: schemaLocation value = 'http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/sca'; must have even number of URI's.
原因:

- sessionFactory被slf4j阻碍init问题
在解决上面问题后出现以下问题(只要第二个问题被解决,就不会出现第一个问题)
报错信息:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [spring-hibernate.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
原因:
hibernate 不知道哪里有了JAR冲突,在pom配置以下:
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.2</version>
</dependency>
问题解决。
任务进度:拆分禅道
任务开始时间:2017-12-25
评审时间:2017-12-25
评论