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 ...
随机推荐
- php 查看接口运行时间
代码如何: <?php $start_time = microtime(true); for ($i=0;$i<100000000;$i++){}; $end_time = microti ...
- Android通过包名打开第三方应用
import android.content.ComponentName; import android.content.Context; import android.content.Intent; ...
- UML-如何画SSD?
1.SSD来自哪里?答:用例文本 2.如何为系统事件和操作命名? 3.SSD中的哪些需要放到词汇表中? SSD元素包含 1).操作名称 2).参数 3).返回数据 这些元素,必须要简洁.但别人可能不太 ...
- 单个body|简单解释|复杂解释|反面解释
单个body有三种方法简单解释.复杂解释和反面解释 ========================================================================== ...
- Python 进行 OCR识别 -- pytesseract库
pip install pytesseract 报错:tesseract is not installed or it's not in your path 下载安装 Tesseract-OCR ht ...
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:windows操作系统删除tensorflow
输入:pip uninstall tensorflow Proceed(y/n):y
- BTree非递归
preorder void PreOrder(BTNode* b) { BTNode* p = b; SqStack* st; InitStack(st); if (b != NULL) { Push ...
- Flask pythn Web 框架总结
Flask pythn Web 框架总结 一, Flask 介绍 Flask 是一个基于Python 实现的web 开发的'小型轻框架' 1. flask介绍 Flask是一个基于Python实现的w ...
- (转)Navicat Premium 12.1.8.0安装与激活
http://www.mamicode.com/info-detail-2493067.html
- 二十三、NFS企业级优化
nfs内核优化:(对于本地文件系统也是有效的) [root@nfsserve ~]# cat /proc/sys/net/core/rmem_default(该文件指定了接收套接字缓冲区大小的缺省值) ...