Java培训之Spring4.3.14.RELEASE整合Hibernate5.2.10.Final

2018-04-19 13:40:43 557浏览

USerDao层实现类不要继承HibernateDaoSupport,现在Spring不推荐使用这种方式:

会报错:org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;

使用Hibernate5.2.10.Final版本会报错。使用Hibernate5.1.0.Final版本则不会报错,总之推荐使用SessionFactory的方式来获取Session:



代码如下:UserDaoImpl

  @Repository
  publicclassUserDaoImplimplementsUserDao{
  @Autowired
  privateSessionFactorysessionFactory;
  @Override
  publicvoidadd(Stringusername){
  //TODOAuto-generatedmethodstub
  //Stringsql="insertintouser(username)values(?)";
  Useruser=newUser();
  user.setUsername(username);
  Sessionsession=sessionFactory.getCurrentSession();
  session.save(user);
  }
  }

Spring的xml配置文件

  <?xmlversion="1.0"encoding="UTF-8"?>
  <beansxmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop"
  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/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx.xsd">
  <context:component-scanbase-package="com.servlet"></context:component-scan>
  <context:property-placeholderlocation="classpath:jdbc.properties"/>
  <!--配置C3p0连接池对象-->
  <beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <propertyname="user"value="root"></property>
  <propertyname="password"value="root"></property>
  <propertyname="jdbcUrl"value="jdbc:mysql:///spring"></property>
  <propertyname="driverClass"value="com.mysql.jdbc.Driver"></property>
  <propertyname="maxPoolSize"value="200"></property>
  <propertyname="initialPoolSize"value="20"></property>
  </bean>
  <!--配置SessionFactory-->
  <beanid="sessionFactory"class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  <!--引入数据源-->
  <propertyname="dataSource"ref="dataSource"></property>
  <propertyname="hibernateProperties">
  <props>
  <propkey="hibernate.show_sql">true</prop>
  <propkey="hibernate.format_sql">true</prop>
  <propkey="hibernate.hbm2ddl.auto">update</prop>
  <propkey="hibernate.connection.isolation">4</prop>
  <!--<propkey="current_session_context_class">thread</prop>
  --></props>
  </property>
  <propertyname="packagesToScan">
  <list>
  <value>com.servlet</value>
  </list>
  </property>
  </bean>
  <!--1.配置平台事务管理器
  jdbc/Mybatis:DateSourceTransactionManager
  Hibernate:HibernateTransactionManager
  -->
  <beanid="transactionManager"class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  <propertyname="sessionFactory"ref="sessionFactory"></property>
  </bean>
  <!--开启事务注解-->
  <tx:annotation-driventransaction-manager="transactionManager"/>
  </beans>

以上就是关于Java培训之Spring4.3.14.RELEASE整合Hibernate5.2.10.Final的详细介绍,最后想要了解更多关于JavaEE开发问题的小伙伴可以登录扣丁学堂官网咨询。扣丁学堂是专业的JavaEE培训机构,不仅有专业的老师和与时俱进的课程体系,还有大量的Java视频教程供学员观看学习,想要学好JavaEE的小伙伴抓紧时间行动吧。Java技术交流群:670348138。




关注微信公众号获取更多学习资料



关注微信公众号获取更多学习资料



查看更多关于“Java开发资讯的相关文章>>


标签: Java视频教程 Java培训 Java开发工程师 Java在线视频 Java在线学习 Java在线教程

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号