1. 服务器
2. 数据库
3. 数据源
4. Web.xml配置
5. Hibernate实体映射
6. Hibernate属性配置
7. Spring SessionFactory
8. SessionFactory注入Dao
9. Spring Transaction
10.Spring整合Struts2
11.Spring整合三层
12.Spring JMS
13.Spring JavaMail
14.Spring AOP
15.Log4j日志
1. 服务器
Java开发常用的服务器有Tomcat、JBoss和WebLogic。服务器可以支持JNDI、数据源、JTA、JMS、JavaMail、JAAS等等。SSH2开发中比较常用的有数据源和JMS,使用时需要进行相关的配置。
参考:
http://txazo.iteye.com/admin/blogs/1669205
2. 数据库
Java开发最常用的数据库有MySQL和Oracle。
参考:
http://txazo.iteye.com/admin/blogs/1669630
3. 数据源
数据源可以在hibernate.cfg.xml中配置,也可以在applicationContext.xml中配置。
1)hibernate.cfg.xml中配置数据源
参考:
http://txazo.iteye.com/admin/blogs/1669444
2)applicationContext.xml中配置数据源
参考:
http://txazo.iteye.com/admin/blogs/1669438
4. Web.xml配置
1)Struts2核心过滤器
<!-- Struts2 Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.
StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<!-- Struts2 Filter Mapping -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2)Spring监听器
<!-- Spring ConfigurationFile Location -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Spring Context Listener -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
3)Spring编码过滤器
<!-- Spring Encoding Filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!-- Spring Encoding Filter Mapping -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4)Spring Log4j监听器
<!-- Log4j ConfigurationFile Location -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- Spring Log4j Listener -->
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
5)Spring Request监听器
<!-- Spring Web Request Listener -->
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
6)Spring防内存泄漏监听器
<!-- Spring Introspector Cleanup Listener -->
<listener>
<listener-class>
org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>
5. Hibernate实体映射
Hibernate实体映射有2种方式,一种是XML的方式,一种是Annotation的方式。
1)XML实体映射
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.txazo.domain.User">
<id name="id">
<generator class="identity" />
</id>
<property name="username" />
<property name="password" />
</class>
</hibernate-mapping>
2)Annotation实体映射
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
6. Hibernate属性配置
Hibernate属性可以配置在hibernate.cfg.xml中,也可以配置在applicationContext.xml中,此时可以不需要hibernate.cfg.xml配置文件。
1)hibernate.cfg.xml中配置Hibernate属性
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- XML Entity Mapping -->
<mapping resource="com/txazo/domain/User.hbm.xml" />
<!-- Annotation Entity Mapping -->
<!--
<mapping class="com.txazo.domain.User" />
-->
</session-factory>
</hibernate-configuration>
2)applicationContext.xml中配置Hibernate属性
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- XML Entity Mapping -->
<property name="mappingResources">
<list>
<value>com/txazo/domain/User.hbm.xml</value>
</list>
</property>
<!-- Annotation Entity Mapping -->
<!--
<property name="packagesToScan">
<list>
<value>com.txazo.domain</value>
</list>
</property>
-->
<!-- Annotation Entity Mapping -->
<!--
<property name="annotatedClasses">
<list>
<value>com.txazo.domain.User</value>
</list>
</property>
-->
</bean>
7. Spring SessionFactory
Hibernate3提供2种SessionFactory,LocalSessionFactoryBean和AnnotationSessionFactoryBean。
Hibernate4只提供一种SessionFactory,LocalSessionFactoryBean。
1)Hibernate3的LocalSessionFactoryBean
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="classpath:hibernate.cfg.xml" />
2)Hibernate3的AnnotationSessionFactoryBean
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.
AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="classpath:hibernate.cfg.xml" />
3)Hibernate4的LocalSessionFactoryBean
Hibernate4只能使用org.springframework.orm.hibernate4.LocalSessionFactoryBean。
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="classpath:hibernate.cfg.xml" />
4) SessionFactory中关于DataSource的配置
若数据源配置在hibernate.cfg.xml中,则无需在SessionFactory中配置DataSource。
若数据源配置在applicationContext.xml中,则需要在SessionFactory中配置DataSource。
5)SessionFactory整合Hibernate
可以直接引用hibernate.cfg.xml配置文件的路径。
也可以在SessionFactory下单独配置Hibernate属性。
8. SessionFactory注入Dao
Hibernate3可以使用Spring提供的HibernateTemplate类来操作数据库。
Hibernate4则只能使用原生的Hibernate API操作数据库。
1)Hibernate3方式
<!-- HibernateTemplate -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate"
p:sessionFactory-ref="sessionFactory" />
<bean id="baseDao" class="com.txazo.dao.impl.BaseDaoImpl">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
public class BaseDaoImpl implements BaseDao {
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate
hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public <T> void save(T t) {
hibernateTemplate.save(t);
}
}
2)Hibernate4方式
<bean id="baseDao" class="com.txazo.dao.impl.BaseDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
public class BaseDaoImpl implements BaseDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Session getSession() {
return sessionFactory.getCurrentSession();
}
@Override
public <T> void save(T t) {
getSession().save(t);
}
}
9. Spring Transaction
Spring中配置事务有2种方式,一种是基于AOP的配置,一种是声明式的配置。
1)AOP事务
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.
HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"></tx:method>
<tx:method name="*"></tx:method>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pointcut"
expression="execution(*
com.txazo.service.impl.*Impl.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>
2)声明式事务
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.
HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<tx:annotation-driven transaction-manager="transactionManager" />
@Transactional
public class UserServiceImpl implements UserService {
}
3)Hibernate3和Hibernate4的区别
Spring对Hibernate3提供org.springframework.orm.hibernate3.HibernateTransactionManager事务管理器。
Spring对Hibernate4提供org.springframework.orm.hibernate4.HibernateTransactionManager事务管理器。
10. Spring整合Struts2
Spring整合Struts2有多种方式。
1)Struts2 Spring Plugin插件自动注入
struts.xml中配置Action,applicationContext.xml中无需任何配置,Struts2 Spring Plugin插件自动为Action注入Service。
<action name="login" method="login"
class="com.txazo.action.LoginAction">
<result name="input">/login.html</result>
<result name="success">/home.jsp</result>
</action>
2)Spring中配置Action
applicationContext.xml中配置Bean,struts.xml中的Action引用applicationContext.xml中配置的Bean。
<bean id="loginAction" class="com.txazo.action.LoginAction"
scope="session">
<property name="userService">
<ref bean="userService" />
</property>
</bean>
<action name="login" method="login" class="loginAction">
<result name="input">/login.html</result>
<result name="success">/home.jsp</result>
</action>
3)Spring Annotation配置
@Controller
@Scope("session")
public class LoginAction extends ActionSupport {
@Autowired
private UserService userService;
}
11.Spring整合三层
三层架构,Dao注入Service,Service注入Action,注入的对象需要提供相应的set方法。
1)XML的配置
<!-- Dao -->
<bean id="baseDao" class="com.txazo.dao.impl.BaseDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- Service -->
<bean id="userService" class="com.txazo.service.impl.UserServiceImpl">
<property name="baseDao">
<ref bean="baseDao" />
</property>
</bean>
<!-- Action -->
<bean id="loginAction" class="com.txazo.action.LoginAction"
scope="session">
<property name="userService">
<ref bean="userService" />
</property>
</bean>
2)Annotation的配置
基于Annotation的配置需要启动Spring的自动扫描组件。
<context:component-scan base-package="com.txazo" />
@Repository("baseDao")
public class BaseDaoImpl implements BaseDao {
@Autowired
private HibernateTemplate hibernateTemplate;
}
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private BaseDao baseDao;
}
@Controller
@Scope("session")
public class LoginAction extends ActionBase {
@Autowired
private UserService userService;
}
12.Spring JMS
Spring提供对JMS的支持。
参考:
http://txazo.iteye.com/admin/blogs/1670756
13.Spring JavaMail
Spring提供对JavaMail的支持。
参考:
http://txazo.iteye.com/admin/blogs/1671087
14.Spring AOP
Spring提供对AOP的支持,使用AOP可以实现通用日志事务权限处理。
参考:
http://txazo.iteye.com/admin/blogs/1671799
15.Log4j日志
# Set The RootLogger
log4j.rootLogger=warn, console
# Direct Log Messages To Console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %c:%L - %m%n
# Log Hibernate
log4j.logger.org.hibernate=error
# Log Just The SQL
log4j.logger.org.hibernate.SQL=debug
# Log Schema Export Update
log4j.logger.org.hibernate.tool.hbm2ddl=debug
分享到:
相关推荐
J2EE SSH2架构设计 Struts2.1.8+Spring2.5.6+Hibernate3.3整合,采用AOP事务管理方式
J2EE SSH2架构 Spring2.5.6+Struts2.1.8+hibernate3.3 采用struts2、spring2.5、hibernate3设计的demo集成
已经搭建好的ssh2架构,有自动建表功能,spring和hibernate的全注解,只要修改spring里的数据库连接密码,然后再在mysql命名一个first库,就可以用,所有表格根据类自动生成.开发方便
IntelliJ IDEA 12.0搭建Maven Web SSH2架构项目示例
J2EE SSH2架构设计 Struts2.1.8+Spring2.5.6+Hibernate3.3整合
J2EE SSH2架构设计 Struts2.1.8+Spring2.5.6+Hibernate3.3整合
J2EE SSH2架构设计 Struts2.1.8+Spring2.5.6+Hibernate3.3整合
基于SSH2架构设计,实现基本拍卖流程和业务,数据库采用MySQl
最简单的ssh2架构实例源代码,完整的myeclipse项目文件 新学的人可以看看,或许有些帮助
SSH2架构的网络硬盘源代码,tomcat数据库,有数据库建表sql语句,可运行。
采用ssh2架构的java就业项目-博客系统V2.0,项目导入eclipse即可,内含sql脚本和war文件,非常适合想提高自己ssh2 web开发技术的同学学习与借鉴
非常实用的ssh2架构的仿淘宝Shopping网上购物完整项目源码,内含sql,可以作为毕业设计使用,导入eclipse即可。mysql数据库
权限管理是java Web应用程序开发中的重要一部分,是...本文设计和实现了一个适用于SSH2架构的通用权限管理设计框架,设计中对用户权限的控制分成三类,结合了基于角色的权限控制模型的优点,通过角色将用户和权限进行关...
基于SSH2资源的开发和使用Hibernate synchronizer 开发的Hibernate应用。以及多对多的双向关联
2. 对上传文件进行增加、删除、删除所有等操作 3. 对上传文件前检测同名文件,以及上传文件的大小限制 4. 包含中文名称的文件上传、附件上传成功后,在数据库表中插入一条记录,记录信息包括一个随机字符的字段,在...
详细通过对Struts2框架、Spring框架、Hibernate框架进行分析,提出了基于这三大开源框架的开发策略,设计并实现职工管理系统 系统的一大新颖之处是调用了Google Calendar API,能成功的显示中国节假日,并且能对事件...
SSH架构搭建,SSH架构
NULL 博文链接:https://free0007.iteye.com/blog/1759277
实例讲解SSH技术架构
SSH2框架搭建详细图解struts2.1、spring2.5、hibernate3.2,一个简单的注册!