`
txazo
  • 浏览: 78323 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

SSH2架构

    博客分类:
  • SSH2
阅读更多
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
分享到:
评论
3 楼 janery1290 2012-09-06  
2 楼 txazo 2012-09-05  
不会吧,哪里出错了
1 楼 kugezhouwei 2012-09-05  
好像运行起来有错误呢?为什么实体类没有写出啊

相关推荐

Global site tag (gtag.js) - Google Analytics