标签:j2ee hibrenate4 struts2 spring4 s2sh框架
最后,我们把整个项目的截图,代码发一下,大家不想下载那个项目的话,可以在这里看到所有的代码(因为那个项目需要一个下载积分,真不多= =,我觉得我搞了那么久,收点积分应该不过分吧。。。嘿嘿)
这里,我尽量用截图来搞,免得复制粘贴,怪烦的
package cn.cutter_point.service; import java.util.List; import cn.cutter_point.bean.Person; public interface PersonService { //这个业务bean实现几个方法,保存,更新,删除,获取,获取全部 public abstract void save(Person persnon); public abstract void update(Person person); public abstract void delete(Integer personid); public abstract Person getPerson(Integer personid); public abstract List<Person> getPersons(); }
/** * 功能:实现SSH的整合hibernate4+spring4+struts2,这个是一个实体bean * 时间:2015年3月28日21:13:10 * author:cutter_point * 文件:Person.java */ package cn.cutter_point.service.impl; import java.util.List; import javax.annotation.Resource; import org.hibernate.SessionFactory; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import cn.cutter_point.bean.Person; import cn.cutter_point.service.PersonService; @Transactional public class PersonServiceBean implements PersonService { @Resource //这个就是依赖注入 private SessionFactory sessionFactory; //这个业务bean实现几个方法,保存,更新,删除,获取,获取全部 @Override public void save(Person person) { sessionFactory.getCurrentSession().persist(person); } @Override public void update(Person person) { sessionFactory.getCurrentSession().merge(person); } @Override public void delete(Integer personid) { sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().get(Person.class, personid)); } @Override @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true) public Person getPerson(Integer personid) { return (Person) sessionFactory.getCurrentSession().get(Person.class, personid); } @SuppressWarnings("unchecked") //取消警告 @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true) @Override public List<Person> getPersons() { return sessionFactory.getCurrentSession().createQuery("from Person").list(); } }
/** * 功能:集成ssh框架 * author:cutter_point * 时间:2015年3月29日17:30:07 */ package cn.cutter_point.web.action; import javax.annotation.Resource; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import cn.cutter_point.bean.Person; import cn.cutter_point.service.PersonService; import com.opensymphony.xwork2.ActionSupport; public class PersonAction extends ActionSupport { @Resource private PersonService personService; //先按名字注入,如果找不到的话就按类型注入 private String name; //名字 public String getName() { return name; } public void setName(String name) { this.name = name; } public String add() throws Exception { personService.save(new Person(name)); return "add"; } public String list() throws Exception { /* //获取实例,方法1 ServletContext sc = ServletActionContext.getRequest().getSession().getServletContext(); WebApplicationContext wac = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); //方法2 WebApplicationContext webApplicationContext = WebApplicationContextUtils. getRequiredWebApplicationContext(ServletActionContext.getServletContext()); if(wac == webApplicationContext) { System.out.println("ok!!!"); } PersonService personService = (PersonService) wac.getBean("personServiceBean");*/ HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("persons", personService.getPersons()); return "list"; } }
package junit.test; import static org.junit.Assert.*; import java.util.List; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.cutter_point.bean.Person; import cn.cutter_point.service.PersonService; public class PersonServiceTest { private static PersonService personService; @BeforeClass public static void setUpBeforeClass() throws Exception { try { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); personService = (PersonService) applicationContext.getBean("personServiceBean"); } catch (Exception e) { e.printStackTrace(); } } @Test public void testSave() { // fail("Not yet implemented"); personService.save(new Person("cutter_point")); } @Test public void testUpdate() { // fail("Not yet implemented"); Person person = personService.getPerson(2); //.... person.setName("小丽"); personService.update(person); } @Test public void testDelete() { // fail("Not yet implemented"); personService.delete(9); } @Test public void testGetPerson() { Person person = personService.getPerson(2); System.out.println(person.getName()); System.out.println("关闭数据区=========="); try { Thread.sleep(1000*30);//15秒 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("第二次获取数据"); person = personService.getPerson(2); System.out.println(person.getName()+" ++++++++++"); } @Test public void testGetPersons() { List<Person> persons = personService.getPersons(); for(Person person : persons) { System.out.println(person.getName()); } } }
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:annotation-config /> <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/cutter_point?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="xiaofeng2015" /> </bean> --> <!-- 推荐配置 --> <bean id="myDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <!-- results in a setDriverClassName(String) call --> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/cutter_point"/> <property name="username" value="root"/> <property name="password" value="xiaofeng2015"/> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1"/> <!-- 连接池的最大值 dbcp2里面似乎没有--> <!-- <property name="maxActive" value="500"/> --> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="2"/> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="1"/> </bean> <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> 扫描实体类 <property name="packagesToScan" value="com.tzl"/> hibernate 配置 <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true hibernate.format_sql=true hibernate.hbm2ddl.auto=none 其他取值 create、create-drop、update、validate hibernate.current_session_context_class=thread hibernate.temp.use_jdbc_metadata_defaults=false </value> </property> </bean> --> <!-- hibernate二级缓存的配置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- configuration elided for brevity --> <property name="dataSource" ref="myDataSource" /> <property name="mappingResources"> <list> <!-- 映射文件 --> <value>cn/cutter_point/bean/Person.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <!-- 用来配置hibernate的属性配置 --> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=update <!--其他取值 create、create-drop、update、validate none --> hibernate.show_sql=true hibernate.format_sql=true <!-- 开启二级缓存功能 --> hibernate.cache.use_second_level_cache = true hibernate.cache.use_query_cache = false hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.EhCacheRegionFactory <!-- hibernate3的二级缓存配置 --> <!-- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> --> </value> </property> </bean> <!-- 配置事务管理器,针对hibernate --> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 采用注解的方式管理事务 --> <tx:annotation-driven transaction-manager="txManager"/> <bean id="personServiceBean" class="cn.cutter_point.service.impl.PersonServiceBean" /> <bean id="PersonAction" class="cn.cutter_point.web.action.PersonAction" autowire="byName" /> </beans>
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as ~ indicated by the @author tags or express copyright attribution ~ statements applied by the authors. All third-party contributions are ~ distributed under license by Red Hat Middleware LLC. ~ ~ This copyrighted material is made available to anyone wishing to use, modify, ~ copy, or redistribute it subject to the terms and conditions of the GNU ~ Lesser General Public License, as published by the Free Software Foundation. ~ ~ This program is distributed in the hope that it will be useful, ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License ~ for more details. ~ ~ You should have received a copy of the GNU Lesser General Public License ~ along with this distribution; if not, write to: ~ Free Software Foundation, Inc. ~ 51 Franklin Street, Fifth Floor ~ Boston, MA 02110-1301 USA --> <ehcache> <!-- Sets the path to the directory where cache .data files are created. If the path is a Java System Property it is replaced by its value in the running VM. The following properties are translated: user.home - User's home directory user.dir - User's current working directory java.io.tmpdir - Default temp file path --> <!-- 缓存文件存放到硬盘哪里 --> <diskStore path="G:\Workspaces\MyEclipse Professional 2014\ssh\cacheFile" /> <!--Default Cache configuration. These will applied to caches programmatically created through the CacheManager. The following attributes are required for defaultCache: maxInMemory - Sets the maximum number of objects that will be created in memory eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used if the element is not eternal. Idle time is now - last accessed time timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used if the element is not eternal. TTL is now - creation time overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit. --> <!-- defaultCache节点为缺省的缓存策略 maxElementsInMemory 内存中最大允许存在的对象数量 eternal 设置缓存中的对象是否永远不过期 overflowToDisk 把溢出的对象存放到硬盘上 timeToIdleSeconds 指定缓存对象空闲多长时间就过期,过期的对象会被清除掉 timeToLiveSeconds 指定缓存对象总的存活时间 diskPersistent 当jvm结束是是否持久化对象 diskExpiryThreadIntervalSeconds 指定专门用于清除过期对象的监听线程的轮询时间 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> <!-- 特殊配置项目 --> <cache name="cn.cutter_point.bean.Person" maxElementsInMemory="100" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" /> </ehcache>
好的就这么多了,等会上传项目
连接地址:http://download.csdn.net/detail/cutter_point/8548263
标签:j2ee hibrenate4 struts2 spring4 s2sh框架
原文地址:http://blog.csdn.net/cutter_point/article/details/44755579