Spring中hibernateTemplate的使用

Wesley13
• 阅读 450
  1. <beans xmlns="http://www.springframework.org/schema/beans"

  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

  3. xmlns:aop="http://www.springframework.org/schema/aop"

  4. xmlns:tx="http://www.springframework.org/schema/tx"

  5. xsi:schemaLocation="http://www.springframework.org/schema/beans

  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

  7. http://www.springframework.org/schema/context

  8. http://www.springframework.org/schema/context/spring-context-2.5.xsd

  9. http://www.springframework.org/schema/tx

  10. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

  11. http://www.springframework.org/schema/aop

  12. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"\>

  13. <context:annotation-config />

  14. <context:component-scan base-package="com.spring" />

  15. <bean id="logInterceptor" class="com.spring.aop.LogInterceptor" />

  16. <bean

  17. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

  18. <property name="locations">

  19. <value>classpath:jdbc.properties</value>

  20. </property>

  21. </bean>

  22. <bean id="dataSource" destroy-method="close"

  23. class="org.apache.commons.dbcp.BasicDataSource">

  24. <property name="driverClassName" value="${jdbc.driverClassName}" />

  25. <property name="url" value="${jdbc.url}" />

  26. <property name="username" value="${jdbc.username}" />

  27. <property name="password" value="${jdbc.password}" />

  28. </bean>

  29. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  30. <property name="dataSource" ref="dataSource"/>

  31. <property name="mappingResources">

  32. <list>

  33. <value>com/spring/model/user.hbm.xml</value>

  34. <value>com/spring/model/userlog.hbm.xml</value>

  35. </list>

  36. </property>

  37. <property name="hibernateProperties">

  38. <value>

  39. hibernate.dialect=org.hibernate.dialect.MySQLDialect

  40. hibernate.show_sql=true

  41. hibernate.hbm2ddl.auto=create

  42. </value>

  43. </property>

  44. </bean>

  45. <tx:advice transaction-manager="transactionManager" id="txManager">

  46. tx:attributes\

  47. <tx:method name="save"/>

  48. </tx:attributes>

  49. </tx:advice>

  50. aop:config\

  51. <aop:pointcut id="entryPointMethod" expression="execution(public * com.spring.service..*.*(..))"/>

  52. <aop:advisor

  53. advice-ref="txManager"

  54. pointcut-ref="entryPointMethod"

  55. />

  56. </aop:config>

  57. <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

  58. <property name="sessionFactory" ref="sessionFactory"></property>

  59. </bean>

  60. </beans>

2、使用@resource 注入,然后调用save方法:

[java] view plain copy

  1. @Component("userDaoImpl")

  2. public class UserDaoImpl implements UserDao{

  3. @Resource

  4. private HibernateTemplate hibernateTemplate;

  5. public HibernateTemplate getHibernateTemplate() {

  6. return hibernateTemplate;

  7. }

  8. public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {

  9. this.hibernateTemplate = hibernateTemplate;

  10. }

  11. @Override

  12. public void save(User u) {

  13. try {

  14. hibernateTemplate.save(u);

  15. } catch (HibernateException e) {

  16. e.printStackTrace();

  17. }

  18. }

  19. }

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
AndroidStudio封装SDK的那些事
<divclass"markdown\_views"<!flowchart箭头图标勿删<svgxmlns"http://www.w3.org/2000/svg"style"display:none;"<pathstrokelinecap"round"d"M5,00,2.55,5z"id"raphael
Stella981 Stella981
2年前
Spring MVC 配置文件dispatcher
1.<?xmlversion\"1.0"encoding\"UTF8"?2.<beansxmlns\"http://www.springframework.org/schema/beans"3.xmlns:mvc\"http://www.springframework.org/schema/mvc"4.xml
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Easter79 Easter79
2年前
Spring之级联属性
新建XML文件<?xmlversion"1.0"encoding"UTF8"?<beansxmlns"http://www.springframework.org/schema/beans"xmlns:xsi"http://www.w3.org/2001/XMLSchemainsta
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这