spring和hibernate的集成
集成关系图:

项目目录树:

User.java
package com.donghai.bean;
public class User {
private String id;
private String name;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserManager.java
package com.donghai.bean;
public interface UserManager {
public void addUser(String name, String password) throws Exception;
}
UserManagerImpl.java
package com.donghai.bean;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class UserManagerImpl extends HibernateDaoSupport implements UserManager {
@Override
public void addUser(String name, String password) throws Exception {
User user = new User();
user.setName(name);
user.setPassword(password);
this.getHibernateTemplate().save(user);
System.out.println("UserManagerImpl.addUser()---->name: " + name + " password: " + password);
}
}
User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="com.donghai.bean.User" table="tbl_user">
<id name="id">
<!--
<generator class="native" />
-->
<generator class="uuid" />
</id>
<property name="name" />
<property name="password" />
</class>
</hibernate-mapping>
ExportDB.java(数据库导出)
package com.donghai.Client; import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport; public class ExportDB { public static void main(String[] args){
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
Client.java
package com.donghai.Client; import org.hibernate.cfg.Configuration;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.donghai.bean.User;
import com.donghai.bean.UserManager; public class Client { public static void main(String[] args){ // 当struts集成的时候,就把该类也注入到spring中,就不用再new 了
ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager = (UserManager)factory.getBean("userManager");
try {
userManager.addUser("ddd", "aaa");
} catch (Exception e) {
e.printStackTrace();
} }
}
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://172.16.100.10:3306/hibernate_first?useUnicode=true&characterEncoding=GBK</property>
<property name="hibernate.connection.username">hibernate</property>
<property name="hibernate.connection.password">hibernate</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property> <mapping resource="com/donghai/bean/User.hbm.xml"/> </session-factory>
</hibernate-configuration>
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
" > <bean id="userManager" class="com.donghai.bean.UserManagerImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean> <!-- 事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut expression="execution(* com.donghai.bean.*.*(..))" id="allManagerMethod"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>
</beans>
spring和hibernate的集成的更多相关文章
- 菜鸟学习Spring——60s学会Spring与Hibernate的集成
一.概述. Spring与Hibernate的集成在企业应用中是很常用的做法通过Spring和Hibernate的结合能提高我们代码的灵活性和开发效率,下面我就一步一步的给大家讲述Spring如何和H ...
- Java Hour 56 Spring 和 Hibernate 的集成
上一章节我们完成了一个简单的Spring 的试验品,这章要让Spring 上战场了,不要慌,步骤都是一样的. Spring 对 Hibernate 的支持是很多方面的,第一个战场是SessionFac ...
- Spring与Hibernate集成中的Session问题
主要讨论Spring与Hibernate集成中的session问题 1.通过getSession()方法获得session进行操作 public class Test extends Hibernat ...
- Spring+Struts+Hibernate 简介(转)
http://blog.csdn.net/slnqnd/article/details/1772910/ Struts2.0 +Hibernate 3.2 +Spring 2.0 一. ...
- Spring3.0+Hibernate+Atomikos集成构建JTA的分布式事务--解决多数据源跨库事务
一.概念 分布式事务分布式事务是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上.简言之,同时操作多个数据库保持事务的统一,达到跨库事务的效果. JTA ...
- 【译】Spring 4 + Hibernate 4 + Mysql + Maven集成例子(注解 + XML)
前言 译文链接:http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annot ...
- Spring第12篇—— Spring对Hibernate的SessionFactory的集成功能
由于Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心 ...
- spring与hibernate注解及XML方式集成
spring与hibernate注解及XML方式集成 Hibernate Xml方式 该种方式需要在sessionFactory中引入对应的hbm.xml文件,样例如下: <!-- spring ...
- Spring MVC Hibernate MySQL Integration(集成) CRUD Example Tutorial【摘】
Spring MVC Hibernate MySQL Integration(集成) CRUD Example Tutorial We learned how to integrate Spring ...
随机推荐
- javaweb学习——JDBC(五)
管理结果集 JDBC使用ResultSet来封装查询到的结果集,然后移动记录指针来取出结果集的内容,除此之外,JDBC还允许通过ResultSet来更新记录,并提供了ResultSetMetaData ...
- Linux 系统查看服务器SN序列号以及服务器型号
1.单独查看服务器的序列号 [root@localhost ~]# dmidecode -t system | grep 'Serial Number' Serial Number: 2102310Y ...
- dotnet core 禁用https
1,在 Properties\launchSettings.json 中的applicationUrl删除 https的选项 2,在Startup.cs中注释掉 app.UseHttpsRedi ...
- 01 语言基础+高级:1-8 File类与IO流_day09【字节流、字符流】
day09[字节流.字符流] 主要内容 IO流 字节流 字符流 异常处理 Properties 教学目标 能够说出IO流的分类和功能 能够使用字节输出流写出数据到文件 能够使用字节输入流读取数据到程序 ...
- springmvc中那些易被忽略的小知识点
1.springmvc会为没有view的modelandview指定默认view 知道这个的时候我都惊呆了. 我从来都是手动指定view名字,今天看到别人写的代码竟然直接返回了个mav,貌似是在dis ...
- Linux中的各种文件类型
Linux中有一句话:一切皆是文件 1.普通文件( - regular file ) (1).文本文件 文件中的内容是由文本构成的,文本指的是ASCII码字符.文件里的内容本质上都是数字( ...
- PCB绘制——培训内容
1.创建PCB_Project 创建下面并保存 2.画原理图库 需要了解,画框,加引脚(该标注),改网格间距,引脚对齐对格,框选问题(从左至右还是从右至左,shift加选),给库加PCB封装 示例:画 ...
- tensorflow用法记录
使用 embedding 变量 import tensorflow as tf import numpy as np sess = tf.InteractiveSession() M = list(' ...
- ruoyi HttpUtils
package com.ruoyi.common.utils.http; import java.io.BufferedReader; import java.io.IOException; impo ...
- Opencv笔记(十六)——认识轮廓
什么是轮廓? 轮廓可以简单认为成连续的点(连着边界)连在一起的曲线,具有相同的颜色或者灰度.轮廓在形状分析和物体的检测和识别中很有用.谈起轮廓不免想到边缘,它们确实很像.简单的说,轮廓是连续的,边缘并 ...