一、整合原理
二、导包(41个)
1.hibernate
(1)hibernate/lib/required
(2)hibernate/lib/jpa | java persist api java的持久化规范(接口)
(3)数据库驱动
2.struts2
(1)struts-blank.war/WEB-INF/lib/*
注意:javassist-3.18.1-GA.jar包与hibernate中的重复(只保留高版本即可)
(2)struts整合spring插件包
注意:这个包一旦导入,那么struts2在启动时就会寻找spring容器.找不到将会抛出异常
3.spring
(1)基本:4+2
core | beans | context | expression | logging | log4j
(2)整合web:web包
spring-web
(3)整合aop:4个
spring-aop | spring-aspect | aop联盟 | aopweaving
(4)整合Hibernate和事务:4个
spring-jdbc | spring-tx | c3p0 | spring-orm
(5)整合junit4测试:test包
spring-test
4.标签库
standard.jar | jstl-1.2.jar
三、单独配置spring容器
1.创建applicationContext.xml,并导入约束(4个) beans | context | aop | tx
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "> <bean name="userAction" class="cn.xyp.web.action.UserAction"></bean></beans>
2.配置spring随项目启动(web.xml)
<!-- 让spring随web启动而创建的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置spring配置文件位置参数 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
四、单独配置struts2
1.配置struts2主配置文件(struts.xml)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="crm" namespace="/" extends="struts-default"> <action name="UserAction_*" class="cn.xyp.web.action.UserAction" method="{1}"> <result name="success">/success.jsp</result> </action> </package></struts>
2.配置struts2核心过滤器到web.xml
<!-- struts2核心过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
五、struts2与spring整合
1.导包(已经导入)
struts2-spring-plugin-2.3.24.jar
2.配置常量
查看默认配置文件从31行开始找到要配置的变量。
### if specified, the default object factory can be overridden here ### Note: short-hand notation is supported in some cases, such as "spring" ### Alternatively, you can provide a com.opensymphony.xwork2.ObjectFactory subclass name here # struts.objectFactory = spring ### specifies the autoWiring logic when using the SpringObjectFactory. ### valid values are: name, type, auto, and constructor (name is the default) struts.objectFactory.spring.autoWire = name
添加常量到struts.xml
<!-- # struts.objectFactory = spring 将action的创建交给spring容器 struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性 --> <constant name="struts.objectFactory" value="spring"></constant>
3.整合方案1:struts2自己创建action,spring负责组装依赖属性(了解)
<!-- 整合方案1:class属性上仍然配置action的完整类名 struts2仍然创建action,由spring负责组装Action中的依赖属性 --> <action name="UserAction_*" class="cn.xyp.web.action.UserAction" method="{1}" > <result name="toHome" type="redirect" >/index.htm</result> <result name="error" >/login.jsp</result> </action>
不推荐理由:最好由spring完整管理action的生命周期.spring中功能才应用到Action上.
4.整合方案2:spring负责创建action以及组装.(推荐)
applicationContext.xml:
<!-- action --> <!-- 注意:Action对象作用范围一定是多例的.这样才符合struts2架构 --> <bean name="userAction" class="cn.itcast.web.action.UserAction" scope="prototype" > <property name="userService" ref="userService" ></property> </bean>
struts.xml:
<!-- 整合方案2:class属性上填写spring中action对象的BeanName 完全由spring管理action生命周期,包括Action的创建 注意:需要手动组装依赖属性 --> <action name="UserAction_*" class="userAction" method="{1}" > <result name="toHome" type="redirect" >/index.htm</result> <result name="error" >/login.jsp</result> </action>
六、单独配置hibernate
1.导入实体类&orm元数据
举例:User.java
package cn.xyp.web.domain;import java.util.HashSet;import java.util.Set;public class User { private Long user_id; private String user_code; private String user_name; private String user_password; private Character user_state; public Long getUser_id() { return user_id; } public void setUser_id(Long user_id) { this.user_id = user_id; } public String getUser_code() { return user_code; } public void setUser_code(String user_code) { this.user_code = user_code; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_password() { return user_password; } public void setUser_password(String user_password) { this.user_password = user_password; } public Character getUser_state() { return user_state; } public void setUser_state(Character user_state) { this.user_state = user_state; } @Override public String toString() { return "User [user_id=" + user_id + ", user_code=" + user_code + ", user_name=" + user_name + ", user_password=" + user_password + "]"; } }
User.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.xyp.domain" > <class name="User" table="sys_user" > <id name="user_id" > <generator class="native"></generator> </id> <property name="user_code" ></property> <property name="user_name" ></property> <property name="user_password" ></property> <property name="user_state" ></property> </class></hibernate-mapping>
2.配置主配置文件(hibernate.xml)
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!-- 数据库驱动 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 数据库url --> <property name="hibernate.connection.url">jdbc:mysql:///crm_32</property> <!-- 数据库连接用户名 --> <property name="hibernate.connection.username">root</property> <!-- 数据库连接密码 --> <property name="hibernate.connection.password">1234</property> <!-- 数据库方言 注意: MYSQL在选择方言时,请选择最短的方言. --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 将hibernate生成的sql语句打印到控制台 --> <property name="hibernate.show_sql">true</property> <!-- 将hibernate生成的sql语句格式化(语法缩进) --> <property name="hibernate.format_sql">true</property> <!-- 自动导出表结构. 自动建表 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 引入实体配置文件 --> <mapping resource="cn/xyp/domain/Customer.hbm.xml" /> <mapping resource="cn/xypt/domain/LinkMan.hbm.xml" /> <mapping resource="cn/xyp/domain/User.hbm.xml" /> </session-factory></hibernate-configuration>
七、spring整合hibernate
1.整合原理
将sessionFactory对象交给spring容器管理
2.在spring中配置sessionFactory
(1)配置方案一:(了解)
<!-- 加载配置方案1:仍然使用外部的hibernate.cfg.xml配置信息 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" > <property name="configLocation" value="classpath:hibernate.cfg.xml" ></property> </bean>
(2)配置方案二:(推荐)
<!-- 加载配置方案2:在spring配置中放置hibernate配置信息 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" > <!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 --> <property name="dataSource" ref="dataSource" ></property> <!-- 配置hibernate基本信息 --> <property name="hibernateProperties"> <props> <!-- 必选配置 --> <prop key="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</prop> <prop key="hibernate.connection.url" >jdbc:mysql:///crm_32</prop> <prop key="hibernate.connection.username" >root</prop> <prop key="hibernate.connection.password" >1234</prop> <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop> <!-- 可选配置 --> <prop key="hib
作者: kent鹏
出处: http://www.cnblogs.com/xieyupeng/
关于作者:专注JAVAEE领域,请多多赐教!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
http://www.cnblogs.com/xieyupeng/p/7108141.html