1.导包

struts2包(不需要导入,之前学习struts2时已经导入)

hibernate包(不需要导入,之前学习hibernate时已经导入)

Spring包

整合hibernate的没有hibernate.cfg.xml文件

1.创建表

2.po类

所谓的javabean

数据库映射出来表

3.dao

操作数据库类

4.service

业务处理层

5.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 1 加载hibenrate.cfg.xml 获得SessionFactory
* configLocation确定配置文件位置
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

<!-- 2创建模板
* 底层使用session,session 有sessionFactory获得
-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 3 dao -->
<bean id="userDao" class="test.dao.UserDao">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>

<!-- 4 service -->
<bean id="userService" class="test.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 5 事务管理 -->
<!-- 5.1 事务管理器 :HibernateTransactionManager -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 5.2 事务详情 ,给ABC进行具体事务设置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="register"/>
</tx:attributes>
</tx:advice>
<!-- 5.3 AOP编程,ABCD 筛选 ABC -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* test.service..*.*(..))"/>
</aop:config>

</beans>

测试类:

数据库结果:

2.不使用hibernate.cfg.xml文件

a.我们得知道hibernate的配置文件是干什么用的

a.1连接数据库的配置信息

a.2添加映射文件

a.3设置缓存

等其他的配置信息

所以我们得使用applicationcontext.xml来完成配置

a.1配置连接信息

a.2spring框架整合hibernate框架的工程bean

a.3注册事务管理器<必须>

a.4组件扫描(使用组件扫描会对于开发过程节约很多时间)<使用注释时才用>

a.4.1可以不使用组件扫描

自己在applicationContext.xml中编写

a.5引入注解解析器<必须>

a.6事务注解<必须>

a.7切面类<非必须>

a.8注册service<非必须>

a.1配置连接信息

<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
</bean>

jdbc.properties文件

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///bos19
user=xiaoli
password=123

a.2spring框架整合hibernate框架的工程bean

<!-- spring框架用于整合Hibernate的工厂bean -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 注入Hibernate相关的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 注入Hibernate的映射文件 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/itheima/bos/domain</value>

</list>
</property>
</bean>

a.3注册事务管理器

<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

a.4组件扫描

<!-- 组件扫描 扫描含有注解的类 -->
<context:component-scan base-package="com.itheima.bos"/>

a.5引入注解解析器

<!-- 引入注解解析器 -->
<context:annotation-config/>

a.6事务注解

<!-- 事务注解 -->
<tx:annotation-driven />
<bean id="defaultAdvisorAutoProxyCreator"
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
<!-- 强制使用cglib为Action创建代理对象 -->
<property name="proxyTargetClass" value="true"></property>
</bean>

a.7切面类

<!-- 切面类 -->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"/>

a.8注册service

<!-- 注册Service -->
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService">
</bean>
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService">
</bean>
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService">
</bean>
<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService">
</bean>
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService">
</bean>

a.9其他配置

<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 注入事务管理器对象 -->
<property name="transactionManager" ref="transactionManager"/>
<property name="databaseSchemaUpdate" value="true" />
</bean>

spring整合struts2,hibernate的更多相关文章

  1. Spring整合Struts2,Hibernate的xml方式

    作为一个学习中的码农,一直学习才是我们的常态,所以最近学习了SSH(Spring,Struts2,Hibernate)整合,数据库用的MySQL. 写了一个简单的例子,用的工具是IntelliJ Id ...

  2. spring 整合 struts2 + Hibernate application配置文件(基于注解)

    下面是 application.xml 文件. <?xml version="1.0" encoding="UTF-8"?> <beans x ...

  3. 一 SSH整合:Spring整合Struts2的两种方式,struts.xml管理Action&Bean管理Action

    SSH回顾 1 引入jar包 Struts2的jar包 D:\Struts2\struts-2.3.35\apps\struts2-blank\WEB-INF\lib  开发基本包 Struts2有一 ...

  4. Struts2的使用以及Spring整合Struts2

    一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar ...

  5. spring整合mybatis(hibernate)配置

    一.Spring整合配置Mybatis spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通过spring配置文件一步到位.一般需要具备如下几个基本配置. 1. ...

  6. Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)

    1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...

  7. Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。

    1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...

  8. Spring框架学习(5)spring整合struts2

    内容源自:spring整合struts2 一.spring框架对struts等表现层框架的整合原理 : 使用spring的ioc容器管理struts中用于处理请求的Action 将Action配置成i ...

  9. Spring整合Struts2的方法

    一.基本支持 通常我们整合Spring和struts2的目的是让Spring来管理struts2的控制器.也就是说把Action交由Spring来管理,利用IOC的特性把Action注入到业务逻辑中. ...

随机推荐

  1. composer 的快速安装

    Packagist 镜像 请各位使用本镜像的同学注意: 本镜像已经依照 composer 官方的数据源安全策略完全升级并支持 https 协议!请各位同学 按照下面所示的两个方法将 http://pa ...

  2. Modbus RTU 通信工具设计(转)

    Modbus RTU 通信工具设计 Modbus 是一个工业上常用的通讯协议.一种通讯约定. ModBus 协议是应用层报文传输协议(OSI 模型第7层),它定义了一个与通信层无关的协议数据单元(PD ...

  3. hibernate---一对一单向外键关联--XML

    Student.java: package com.bjsxt.hibernate; public class Student { private int id; private String nam ...

  4. 对于形式参数只能用final修饰符,其它任何修饰符都会引起编译器错误

    在Java中修饰符总共有一下几种: 1.访问控制修饰符    分别有:public private protected,缺省 2.其它修饰符      分别有:abstract,final,stati ...

  5. linux中vi和vim编辑工具

    linux中知名的还有emacs,功能比vim还要强大 vim 如果文件存在vim是打开这个文件,若果不存在,则先新建再打开 命令模式:任何模式都可以通过Esc回到命令模式,命令模式可以通过命令进行选 ...

  6. ios开发获取SIM卡信息

    .加入一个Framework(CoreTelephony.framework). .引入头文件 #import<CoreTelephony/CoreTelephonyDefines.h> ...

  7. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  8. JS利用短路原理简写if语句

    看GoogleDoodle-Dance的源代码,学习到一个小知识——简写if语句. 几乎所有语言中||和&&都遵循“短路”原理,如&&中第一个表达式为假就不会去处理第二 ...

  9. iOS开发——NSDate(待续...)

    1.获取当前系统时间,毫秒级 - (void)viewDidLoad { [super viewDidLoad]; NSString *currentTime = [self getCurrentTi ...

  10. ios UIKit动力 分类: ios技术 2015-07-14 12:55 196人阅读 评论(0) 收藏

    UIkit动力学是UIkit框架中模拟真实世界的一些特性. UIDynamicAnimator 主要有UIDynamicAnimator类,通过这个类中的不同行为来实现一些动态特性. 它一般有两种初始 ...