`
joe_zhjiang
  • 浏览: 159135 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

基于struts2.23 + spring2.5.6 + hibernate3.6.4 + hibernate-generic-dao1.0

 
阅读更多
来自:
引用
http://www.oschina.net/code/snippet_147955_5079

这是我为新项目专门搭建的基于全注解方式的SSH基础框架,虽然是老掉牙的的东西,自我感觉很良好,好东西不敢独享,晒晒等拍砖。

概述:基于struts2.23 + spring2.5.6 + hibernate3.6.4 + hibernate-generic-dao1.0(除了spring,我整合的都是最新的GA包,hibernate-generic-dao是google项目库中一个开源的basedao,我灰常喜欢,因为我找不到更好更适合我的)



项目代码是基于eclipse3.6创建的,很简单,大家直接导入则可运行。



1.包结构,源码,测试用例,配置文件一目了然。每个功能模块都在modules包下做开发,配置文件统一在resource管理(基实也没多少配置文件,都用注解嘛)。







2.无论阅读哪个web项目代码,我都是先从web.xml开始,项目有什么东西一清二楚。

我这里将log4j监听放在第一,我想他应该能够从系统启动开启就能记录我的所有日志(求认证)。第二个监听是proxool数据库连接池,听说很高效,所以果断引入(引入步骤搞得复杂吧,我还重写了监听。一切为了稳定,也好扩展我某日喜欢加入动态切换数据源做准备。呵呵)。OpenSessionInView,我想如果你不喜欢可以摘掉,反正我很喜欢。Struts2指定了自定义的struts.xml文件位置,指定扫描注解的action路径。最后是proxool的可视化图形监控,很棒。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>framework</display-name>
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/classes/resources/log4j/log4j.properties</param-value>
	</context-param>
	<context-param>
		<param-name>propertyFile</param-name>
		<param-value>/WEB-INF/classes/resources/hibernate/proxool.properties</param-value>
	</context-param>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:resources/spring/applicationContext.xml</param-value>
	</context-param>
	<!-- log4j Listener -->
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	<!-- Proxool Listener -->
	<listener>
		<listener-class>org.chinasb.framework.core.db.ProxoolListener</listener-class>
	</listener>
	<!-- Open Session In View Filter -->
	<filter>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- Spring Listener -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- Character Encoding Filter -->
	<filter>
        <filter-name>Set Character Encoding</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <!-- 强制进行转码 -->
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
	<!-- 延长action中属性的生命周期, -->
	<filter>
		<filter-name>struts-cleanup</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts-cleanup</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- Struts2 Filter -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>struts-default.xml,struts-plugin.xml,resources/struts/struts.xml</param-value>
		</init-param>
        <init-param>
        	<param-name>actionPackages</param-name>
        	<param-value>org.chinasb.framework.modules</param-value>
        </init-param>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- Proxool Monitoring -->
	<servlet>
		<servlet-name>Admin</servlet-name>
		<servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Admin</servlet-name>
		<url-pattern>/admin.html</url-pattern>
	</servlet-mapping>
	<!-- Welcome List -->
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>


3.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: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/context

http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 使用 annotation -->
	<context:annotation-config />

	<!-- 使用 annotation 自动注册bean,并检查@Controller, @Service, @Repository注解已被注入 -->
	<context:component-scan base-package="org.chinasb.framework.modules" />

	<!-- hibernate属性配置 -->
	<context:property-placeholder location="classpath:resources/hibernate/hibernate.properties"/>

	<!-- Hibernate 配置管理 -->
	<import resource="applicationContext-persistence.xml" />

</beans>


4.hiberante配置所需的一些属性,指定方言,开始hibernate缓存等,后面还有一个c3p0的数据连接池属性。你们下载的代码里面,数据源方面我换成了c3p0,因为proxool我配置的是随web启动的,而我又不想改成随spring加载启动。所以我开发中注释掉proxool,以后上线再打开。

## hibernate
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=false
hibernate.hbm2ddl.auto=update

## hibernate cache
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.use_query_cache=false
hibernate.cache.use_second_level_cache=true

## C3P0 configuration
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/chinasb??useUnicode=true&characterEncoding=utf-8
hibernate.connection.username=root
hibernate.connection.password=root


5.applicationContext-persistence.xml,这里就是数据源及事务的一些配置了。事务我使用了cglib类级别的代理注解配置方式,如果你喜欢用jdk接口方式动态的代理,可以去掉 proxy-target-class="true"。顺便也保留了声名式事务。

<?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: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.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- Proxool 数据源 -->
	<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
		</property>
		<property name="url">
			<value>proxool.default</value>
		</property>
	</bean> -->

	<!-- C3P0 数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${hibernate.connection.driver_class}"/>
        <property name="jdbcUrl" value="${hibernate.connection.url}"/>
        <property name="properties">
            <props>
                <prop key="user">${hibernate.connection.username}</prop>
                <prop key="password">${hibernate.connection.password}</prop>
            </props>
        </property>
    </bean>

    <!-- SessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan" value="org.chinasb.framework.modules.*.model"/>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
				<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
				<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
				<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
			</props>
		</property>
	</bean>

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

	<!-- 配置注解实现管理事务(cglib:proxy-target-class="true") -->
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

	<!-- 指定使用cglib -->
	<!-- <aop:aspectj-autoproxy proxy-target-class="true" />  -->

	<!-- 配置事务的传播特性 -->
	<!--
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="edit*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="batchUpdate" propagation="REQUIRED" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>
	-->

	<!-- 配置事务的切入点 -->
	<!--
	<aop:config>
		<aop:pointcut id="targetMethod" expression="execution(* org.chinasb.framework.modules..service.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="targetMethod" />
	</aop:config>
	-->

</beans>


6.struts.xml,你懂的。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <!-- 开启使用开发模式,详细错误提示 -->
    <constant name="struts.devMode" value="false" />
    <!-- 将对象交给spring管理 -->
    <constant name="struts.objectFactory" value="spring" />
    <!-- 指定资源编码类型 -->
    <constant name="struts.i18n.encoding" value="UTF-8" />
	<!-- 指定每次请求到达,重新加载资源文件 -->
    <constant name="struts.i18n.reload" value="false" />
    <!-- 指定每次配置文件更改后,自动重新加载 -->
    <constant name="struts.configuration.xml.reload" value="false" />
    <!-- 国际化资源文件 -->
    <constant name="struts.custom.i18n.resources" value="resources/content/Language" />
    <!-- 默认后缀名 -->
    <constant name="struts.action.extension" value="do,action,jhtml,," />
	<!-- Struts Annotation -->
    <constant name="actionPackages" value="org.chinasb.framework.modules"/>
</struts>


好了,下面我简单讲一下开发流程。



在modules下建立模块,和相应的包(action,dao,model,service,util),比如我上面包结构的demo模块。



demo.java,model类,映射数据库中的表,每个model一张表,为了适应basedao,每个model还对应每个dao(不要觉得这是麻烦的)。jpa的注解,你们懂的,不解释。

package org.chinasb.framework.modules.demo.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Demo implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String title;
    private String content;
    private Date publishdate;

    public int getId() {
	return id;
    }

    public void setId(int id) {
	this.id = id;
    }

    public String getTitle() {
	return title;
    }

    public void setTitle(String title) {
	this.title = title;
    }

    public String getContent() {
	return content;
    }

    public void setContent(String content) {
	this.content = content;
    }

    public Date getPublishdate() {
	return publishdate;
    }

    public void setPublishdate(Date publishdate) {
	this.publishdate = publishdate;
    }

}


DemoDao,接口类。我想在多数程序员的项目里做的绝大多数事情都是相关数据库的CURD操作,所以基础框架里整合一个强大的BaseDao是必须的,而我选择了开源的GenericDao(觉得不适合你的可以替掉)。直接继承GenericDao接口,指定操作的model,与主键的类型。

package org.chinasb.framework.modules.demo.dao;

import org.chinasb.framework.core.base.dao.hibernate.GenericDAO;
import org.chinasb.framework.modules.demo.model.Demo;

public interface DemoDao extends GenericDAO<Demo, Integer> {

}

DemoDaoImpl,呵,这样写我觉得不麻烦,不多说。@Repository注解交给spring管理。

package org.chinasb.framework.modules.demo.dao.impl;

import org.chinasb.framework.core.base.dao.BaseDAO;
import org.chinasb.framework.modules.demo.dao.DemoDao;
import org.chinasb.framework.modules.demo.model.Demo;
import org.springframework.stereotype.Repository;

@Repository
public class DemoDaoImpl extends BaseDAO<Demo, Integer> implements DemoDao {

}

BaseDAO,主要是注入sessionFactory给hibernate-generic-dao。

package org.chinasb.framework.core.base.dao;

import java.io.Serializable;

import org.chinasb.framework.core.base.dao.hibernate.GenericDAOImpl;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class BaseDAO<T, ID extends Serializable> extends GenericDAOImpl<T, ID> {

    @Autowired
    @Override
    public void setSessionFactory(SessionFactory sessionFactory) {
	super.setSessionFactory(sessionFactory);
    }
}

DemoService,接下来就要定义一些相关数据库操作的业务接口给上层使用了,这里主要列出了baseDao能操作的部分,像分页,字段过滤查询方面的没有列出。

package org.chinasb.framework.modules.demo.service;

import java.util.List;
import org.chinasb.framework.modules.demo.model.Demo;

public interface DemoService {
    /**
     * 增加或更新demo
     *
     * @param demo
     * @return
     */
    public boolean save(Demo demo);

    /**
     * 批量增加或更新demo
     *
     * @param demo
     * @return
     */
    public boolean[] save(Demo[] demos);

    /**
     * 删除demo
     *
     * @param demo
     * @return
     */
    public boolean remove(Demo demo);

    /**
     * 批量删除demo
     *
     * @param demos
     */
    public void remove(Demo[] demos);

    /**
     * 根据主键删除demo
     *
     * @param id
     * @return
     */
    public boolean removeById(Integer id);

    /**
     * 批量根据主键删除demo
     *
     * @param ids
     */
    public void removeByIds(Integer[] ids);

    /**
     * 查询demo数据记录集
     *
     * @return
     */
    public List<Demo> findAll();

    /**
     * 根据主键查询demo
     *
     * @param id
     * @return
     */
    public Demo findById(Integer id);

    /**
     * 批量根据主键查询demo记录集
     *
     * @param ids
     * @return
     */
    public Demo[] findByIds(Integer[] ids);

    /**
     * 持久化session数据到数据库
     */
    public void flush();
}

DemoServiceImpl,接口实现。这里全用注解控制了事务,及演示如何使用baseDao。当然强大的baseDao并不仅仅这些方法,还有分页等强大的查询数据功能。大家可以在http://code.google.com/p/hibernate-generic-dao/ 进行查看。

package org.chinasb.framework.modules.demo.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.chinasb.framework.modules.demo.dao.DemoDao;
import org.chinasb.framework.modules.demo.model.Demo;
import org.chinasb.framework.modules.demo.service.DemoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DemoServiceImpl implements DemoService {
    @Resource
    private DemoDao demoDao;

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public boolean save(Demo demo) {
	return demoDao.save(demo);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public boolean[] save(Demo[] demos) {
	return demoDao.save(demos);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public boolean remove(Demo demo) {
	return demoDao.remove(demo);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void remove(Demo[] demos) {
	demoDao.remove(demos);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public boolean removeById(Integer id) {
	return demoDao.removeById(id);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void removeByIds(Integer[] ids) {
	demoDao.removeByIds(ids);
    }

    @Override
    public List<Demo> findAll() {
	return demoDao.findAll();
    }

    @Override
    public Demo findById(Integer id) {
	return demoDao.find(id);
    }

    @Override
    public Demo[] findByIds(Integer[] ids) {
	return demoDao.find(ids);
    }

    @Override
    public void flush() {
	demoDao.flush();
    }
}


DemoAction,这里演示了查看,增,删功能。也是基于注解的方式,继承了baseAction。

package org.chinasb.framework.modules.demo.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.chinasb.framework.core.base.action.BaseAction;
import org.chinasb.framework.modules.demo.model.Demo;
import org.chinasb.framework.modules.demo.service.DemoService;
import org.springframework.stereotype.Controller;

@Controller
public class DemoAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    @Resource
    private DemoService demoService;

    @Action(value = "/demoAction", results = { @Result(name = SUCCESS, location = "/manager/modules/demo/index.jsp") })
    @Override
    public String execute() {
	List<Demo> demoList = demoService.findAll();
	httpServletRequest.setAttribute("DEMO_LIST", demoList);
	return SUCCESS;
    }

    @Action(value = "/demoAddAction", results = { @Result(name = SUCCESS, location = "/demoAction", type = "redirect") })
    public String add() {
	Demo demo = new Demo();
	demo.setTitle(httpServletRequest.getParameter("title"));
	demo.setContent(httpServletRequest.getParameter("content"));
	demo.setPublishdate(new Date());
	demoService.save(demo);
	return SUCCESS;
    }

    @Action(value = "/demoDeleteAction", results = { @Result(name = SUCCESS, location = "/demoAction", type = "redirect") })
    public String delete() {
	demoService.removeById(Integer.parseInt(httpServletRequest.getParameter("id")));
	return SUCCESS;
    }
}


BaseAction,这里只封装了基本的request,reponse,context,session,大家按需要继续完善。

package org.chinasb.framework.core.base.action;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport implements ServletContextAware,
	ServletResponseAware, ServletRequestAware, SessionAware {

    private static final long serialVersionUID = 1L;

    protected ServletContext servletContext;

    protected HttpServletRequest httpServletRequest;

    protected HttpServletResponse httpServletResponse;

    protected HttpSession httpSession;

    protected Map<String, Object> session;

    @Override
    public void setServletContext(ServletContext context) {
	this.servletContext = context;
    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
	this.httpServletResponse = response;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
	this.httpServletRequest = request;
	this.httpSession = request.getSession();
    }

    @Override
    public void setSession(Map<String, Object> session) {
	this.session = session;
    }

}

最后就是测试用例了,这里使用了经典的junit4.4版本,依然是注解方式。

BaseTestTemplate,大家都不想每个用例都去写一次读取applicationContext吧,把一些公用的东西封装起来吧。

package org.chinasb.framework.core.base.test;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:resources/spring/applicationContext.xml" })
@TransactionConfiguration(defaultRollback = false)
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseTestTemplate {

}


DemoActionTest,简单的测试用例,这里只是为了说明如何在这个框架里进行单元测试。所以我的目的达到了,简单吧。

package org.chinasb.framework.modules.demo.action;

import java.util.Date;

import javax.annotation.Resource;

import org.chinasb.framework.core.base.test.BaseTestTemplate;
import org.chinasb.framework.modules.demo.model.Demo;
import org.chinasb.framework.modules.demo.service.DemoService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;

public class DemoActionTest extends BaseTestTemplate {

    @Resource
    private DemoService demoService;

    @Before
    public void setUp() throws Exception {
	System.out.println("测试开始");
    }

    @After
    public void tearDown() throws Exception {
	System.out.println("测试结束");
    }

    @BeforeTransaction
    public void beforeTransaction() {
	System.out.println("事务开始");
    }

    @AfterTransaction
    public void afterTransaction() {
	System.out.println("事务结束");
    }

    @Test
    public void testInsert() {
	Demo demo = new Demo();
	demo.setTitle("junit-test");
	demo.setContent("junit-content");
	demo.setPublishdate(new Date());
	demoService.save(demo);
    }

    @Test
    public void testInsertMore() {
	Demo[] demos = new Demo[10];
	for (int i = 0; i < 10; i++) {
	    Demo demo = new Demo();
	    demo.setTitle("junit-test" + i);
	    demo.setContent("junit-content" + i);
	    demo.setPublishdate(new Date());
	    demos[i] = demo;
	}
	demoService.save(demos);
    }
}




测试表:

/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50150
Source Host           : localhost:3306
Source Database       : chinasb

Target Server Type    : MYSQL
Target Server Version : 50150
File Encoding         : 65001

Date: 2011-07-07 20:40:57
*/

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `demo`
-- ----------------------------
DROP TABLE IF EXISTS `demo`;
CREATE TABLE `demo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `content` varchar(255) DEFAULT NULL,
  `publishdate` datetime DEFAULT NULL,
  `title` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=133 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of demo
-- ----------------------------
INSERT INTO demo VALUES ('131', 'test', '2011-07-06 01:29:19', 'test');
INSERT INTO demo VALUES ('132', 'test-2', '2011-07-07 20:40:38', 'test-2');

众望所归,出图:





终于写完了,好累啊。下一步跟汪兄商量如何完美整合他那个强大的数据级权限中间件(ralasafe),这样在未来一投入使用即附带有权限控制,多爽。好了,大家看得也累,喜欢的,不喜欢的都出来拍拍砖吧。不对的地方,请各位N人多多指正。



源码:
引用
http://www.chinasb.org/wp-content/uploads/2011/07/framework.zip


Google Code:
引用
http://code.google.com/p/ssh-base-framework/


Google Code Download:
引用
http://code.google.com/p/ssh-base-framework/downloads/list


Google Code SVN:
引用
http://ssh-base-framework.googlecode.com/svn/trunk/

分享到:
评论
1 楼 sielo 2012-05-06  
全部都很好,只是Service为什么不是Generic的?

相关推荐

    Struts2.1.8+Spring2.5.6+Hibernate3.3.2项目整合

    Struts2.1.8+Spring2.5.6+Hibernate3.3.2是经典的Java Web开发框架组合,常被称为SSH。这三个框架协同工作,为开发者提供了强大的模型-视图-控制器(MVC)架构支持,使得企业级应用的开发更加高效和模块化。 Struts...

    Struts2.1.6+Spring2.5.6+Hibernate3.3.2整合包

    综上所述,Struts2.1.6+Spring2.5.6+Hibernate3.3.2的整合提供了全面的Java Web开发解决方案,它简化了开发流程,提高了代码的可维护性和可扩展性。开发者可以通过掌握这三个框架的整合,提升自身在企业级应用开发中...

    Struts2+Spring2.5.6+Hibernate3 用到的jar包

    Struts2+Spring2.5.6+Hibernate3 用到的jar包 antlr-2.7.6.jar asm.jar backport-util-concurrent.jar cglib-nodep-2.1_3.jar commons-collections-3.1.jar commons-dbcp.jar commons-fileupload-1.2.1.jar commons-...

    Struts2.1.6+Spring2.5.6+Hibernate3.3.1框架整合开发

    Struts2.1.6+Spring2.5.6+Hibernate3.3.1框架整合开发 http://www.cnitblog.com/intrl/archive/2009/04/13/56322.aspx 这篇文章的源码由于他的上传的空间用户可能下载失败,作者相当牛而且具有奉献精神

    Struts2.1.6+Spring2.5.6+Hibernate3.3.1全注解

    在深入探讨Struts2.1.6+Spring2.5.6+Hibernate3.3.1全注解的实现细节之前,我们先回顾一下这三个框架的基本概念及其在JavaEE企业级开发中的作用。 ### Struts2框架 Struts2是一个开源的Web应用框架,它遵循MVC...

    Struts2.1.6+Spring2.5.6+Hibernate3.3.1整合

    ### Struts2.1.6 + Spring2.5.6 + Hibernate3.3.1 整合 在本文中,我们将详细介绍如何将Struts2.1.6、Spring2.5.6以及Hibernate3.3.1进行整合,并构建一个简单的MVC应用。通过这个过程,读者将了解到这三种框架的...

    struts2+spring2.5.6+hibernate3.0+ext整合开发

    在"struts2+spring2.5.6+hibernate3.0+ext"的整合开发中,通常会使用Spring来管理Struts2的Action类,实现业务逻辑的解耦。同时,Spring可以作为Hibernate的事务管理器,处理数据库操作。Ext则用于创建前端用户界面...

    struts2.2.3+spring2.5.6+hibernate3.2

    struts2.2.3+spring2.5.6+hibernate3.2 内部WEB-INF/lib有所用的全部JAR,不用自己另外再找一些JAR webroot/database下有mysql所用的数据库创建脚本和已经存在的一些数据 student_data.sql 已有数据 student.sql...

    struts2.1.8+spring2.5.6+hibernate3.3.2

    struts2.1.8 + spring2.5.6(支持注解开发Annotations) + hibernate3.3.2(支持注解开发Annotations) + mysql-connector-java-5.1.7-bin.jar 精简完整版,测试可用

    struts1+spring2.5.6+jdbc配置

    Struts1、Spring 2.5.6 和 JDBC 是经典的Java Web开发技术组合,它们各自在应用程序架构中扮演着不同的角色。Struts1是MVC(Model-View-Controller)框架,Spring则是一个全面的轻量级应用框架,而JDBC(Java ...

    Struts2.1.8+Spring2.5.6+hibernate3.2.3整合

    Struts2、Spring和Hibernate是Java Web开发中的三大框架,它们各自在应用程序的不同层面发挥着重要作用。Struts2是用于处理MVC(Model-View-Controller)架构的框架,Spring是一个全面的后端管理框架,而Hibernate则...

    Struts2.1.6+Spring2.5.6+Hibernate3.3.1全注解实例详解

    Struts2.1.6+Spring2.5.6+Hibernate3.3.1全注解实例详解,涉及的是JavaEE开发中常见的SSH框架(Struts2、Spring、Hibernate)的整合,尤其强调了注解在开发中的应用,旨在简化配置,提高开发效率。SSH框架的组合在...

    struts2.1.8+spring2.5.6+hibernate3.3.2整合项目开发

    在"struts2.1.8+spring2.5.6+hibernate3.3.2整合项目开发"中,首先我们需要理解这三个框架的基本概念和用法: 1. **Struts2**:Struts2是一个基于MVC设计模式的Web应用框架,用于构建可维护性和可扩展性高的应用...

    struts2.1.8 + spring2.5.6 + hibernate3.3.2整合

    ### Struts2.1.8 + Spring2.5.6 + Hibernate3.3.2整合实践 #### 一、概述 在Java Web开发领域,Struts2、Spring以及Hibernate是三个非常重要的开源框架,它们各自在不同的方面发挥着重要作用:Struts2用于构建MVC...

    Struts2.1.6+Spring2.5.6+Hibernate3.3.1框架整合常见错误

    ### Struts2.1.6+Spring2.5.6+Hibernate3.3.1框架整合常见错误 在使用Struts2.1.6、Spring2.5.6与Hibernate3.3.1进行框架整合时,开发者经常会遇到一些常见的问题。这些问题可能会导致程序无法正常运行或出现异常...

    struts1.3.8+spring2.5.6+hibernate3.3框架集成

    Struts1.3.8、Spring2.5.6和Hibernate3.3是经典的Java Web开发框架组合,常被称为SSH框架。这个集成项目是基于这三个框架的整合,旨在提供一个可快速开发、易于维护的后端系统基础。下面将详细阐述这三个框架的主要...

    Struts2.1.6+Spring2.5.6+Hibernate3.3.1全注解实例详解(含JAR包)

    Struts2.1.6、Spring2.5.6和Hibernate3.3.1是Java Web开发中的三个重要框架,合称为SSH(Struts2、Spring、Hibernate)。这个全注解实例详解旨在帮助开发者理解如何在不使用XML配置的情况下,通过注解实现这三大框架...

    Struts2.1.6 +Spring2.5.6 + Hibernate3.3.2整合包

    Struts2.1.6、Spring2.5.6和Hibernate3.3.2是Java Web开发中的三个重要框架,它们常被组合使用,形成所谓的"S2SH"集成框架,以实现MVC(模型-视图-控制器)架构,提供企业级应用的高效能和可扩展性。这里我们将详细...

Global site tag (gtag.js) - Google Analytics