SSH2自学逻辑

1、创建java web程序,加入jar文件

2、编写bean.xml文件


       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-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


3、编写配置



   
   
   
   
   







 

  
   

   
     cn/itcast/bean/Person.hbm.xml
   


   
   
       hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
       hibernate.hbm2ddl.auto=update
       hibernate.show_sql=false
       hibernate.format_sql=false
       hibernate.cache.use_second_level_cache=true
               hibernate.cache.use_query_cache=false
           hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
     

   

 


 

 ---事务管理

4、编写普通类Person类。

public class Person {
private Integer id;
private String name;

public Person(){}

public Person(String name) {
this.name = name;
}

       .....省略get,set方法。

5、植入对应的Person.hbm.xml


        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

   
   
       
           
       

       
   


6、编写对应的二级缓存文件ehcache.xml




   
            timeToIdleSeconds="120"
        timeToLiveSeconds="180"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="60"/>
    overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" diskPersistent="false"/>

7、编写struts2的配置文件,载入person对象。


    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

   
   
   
   
   
   
   
   
   
   
     
   
   
    
 
 
  /WEB-INF/page/message.jsp
 


/WEB-INF/page/personlist.jsp
/WEB-INF/page/addperson.jsp

   


8、编写业务类

public interface PersonService {
public void save(Person person);
public void update(Person person);
public Person getPerson(Integer personid);
public void delete(Integer personid);
public List getPersons();
}

9、编写实现类

package cn.itcast.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.itcast.bean.Person;
import cn.itcast.service.PersonService;

@Transactional
public class PersonServiceBean implements PersonService {
@Resource private SessionFactory sessionFactory;

public void save(Person person){
sessionFactory.getCurrentSession().persist(person);
}

public void update(Person person){
sessionFactory.getCurrentSession().merge(person);
}
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
public Person getPerson(Integer personid){
return (Person)sessionFactory.getCurrentSession().get(Person.class, personid);
}

public void delete(Integer personid){
sessionFactory.getCurrentSession().delete(
sessionFactory.getCurrentSession().load(Person.class, personid));
}
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
@SuppressWarnings("unchecked")
public List getPersons(){
return sessionFactory.getCurrentSession().createQuery("from Person").list();
}
}

10、编写web.xml文件


xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
  contextConfigLocation
  classpath:beans.xml



     org.springframework.web.context.ContextLoaderListener


   
        struts2
        org.apache.struts2.dispatcher.FilterDispatcher
   

   
        struts2
        /*
   


       OpenSessionInViewFilter
       org.springframework.orm.hibernate3.support.OpenSessionInViewFilter


       OpenSessionInViewFilter
       /*

 
    index.jsp
 


10、编写PersonAction类

package cn.itcast.web;
import java.util.List;
import javax.annotation.Resource;
import cn.itcast.bean.Person;
import cn.itcast.service.PersonService;

public class PersonAction {
@Resource PersonService personService;
private String message;
private List persons;
private Person person;

public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
/**
* 人员列表显示
*/
public String list(){
this.persons = personService.getPersons();
return "list";
}
/**
* 人员添加界面
*/
public String addUI(){
return "add";
}
/**
* 人员添加
*/
public String add(){
this.personService.save(this.person);
this.message="添加成功";
return "message";
}
public List getPersons() {
return persons;
}

public void setPersons(List persons) {
this.persons = persons;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}

11、编写personlist.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>


 
    人员列表
 
  
 
   
    id=,name=

   

 

12、编写message.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>



 
    My message page
 
  
 
   

 

13、编写addperson.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>


 
    人员添加
 
  
 
   
    名称:
   
   

 















你可能感兴趣的:(SSH2自学逻辑)