1、创建java web程序,加入jar文件
2、编写bean.xml文件
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、编写配置
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
timeToLiveSeconds="180"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="60"/>
7、编写struts2的配置文件,载入person对象。
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
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
}
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
return sessionFactory.getCurrentSession().createQuery("from Person").list();
}
}
10、编写web.xml文件
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">
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
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
return persons;
}
public void setPersons(List
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=
12、编写message.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
13、编写addperson.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
名称: