工欲善其事,必先利其器
Struts2依赖包(struts-2.1.8.1)
1 struts2-core-2.1.8.1.jar struts2框架的核心类库
2 xwork-core-2.1.6.jar xwork类库,struts2在其上构建
3 ognl-2.7.3.jar 对象图导航语言(Object Graph Navigation Languge),struts2框架通过其读写对象的属性
4 freemarker-2.3.15.jar struts2的UI标签的模板使用FreeMarker编写
5 commons-logging-1.0.4.jar ASF出品的日志包,Struts2框架使用这个日志包来支持Log4J和JDK1.4+的日志记录
6 commons-fileupload-1.2.1.jar 文件上传组件,2.1.6版本后必须加入此文件
Struts2 默认的配置文件为struts.xml,该文件需要存放在WEB-INF/classes下、 文件模板:
<?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>
</struts>
模板还可在struts包中的例子中查看
struts-2.1.8.1-all\struts-2.1.8.1\apps\struts2-blank-2.1.8.1\WEB-INF\classes\struts.xml
Struts2 框架是通过Filter启动的,在web.xml中配置:
<filter> <filter-name>struts2</filter-name>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
<!—自从Strut 2.1.3以后,下面的FileterDispatcher已经标注为过时
<filter-class>org.apache.struts2.dispatcher.ng. FilterDispatcher</filter-class>--> </filter>
struts2 /*
</filter-mapping>
在StrutsPrepareAndExecuteFilter的init()方法中将会读取类路径下默认的配置文件struts.xml完成初始化操作。
Struts2读取到struts.xml的内容后,以javabean形式存放在内存中,以后struts2对用户的每次请求处理将使用内存中的数据,而不是每次都读取struts2.xml文件。
Hibernate 依赖包 3.5.3
hibernate3.jar
antlr-2.7.6.jar
cglib-2.2.jar
commons-collections-3.1.jar
commons-logging-1.0.4.jar
jta-1.1.jar
dom4j-1.6.1.jar
javassist-3.9.0.GA.jar
slf4j-api-1.5.8.jar
slf4j-nop-1.6.1.jar
backport-util-concurrent-3.1.jar
ehcache-1.5.0.jar
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
root jdbc:mysql://localhost:3306/fuxi org.hibernate.dialect.MySQLDialect mysql zhao com.mysql.jdbc.Driver update true org.hibernate.cache.EhCacheProvider
</session-factory> </hibernate-configuration>
Dpojo.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.pojo"> <!-- 配置类与表映射 --> <class name="Dpojo" table="dept"> <!-- 二级缓存 --> <!-- <cache usage="read-write"/>--> <!-- 映射主键与对象表示符 --> <id name="id" column="id"> <generator class="increment"></generator> </id> <property name="name" column="dname"></property> <property name="loc" column="dloc"></property> </class> </hibernate-mapping>
HibernateUtil.java
package com.util;
import java.io.Serializable;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public final class HibernateUtil { private static SessionFactory sessionFactory;
private HibernateUtil(){ }static{ Configuration cfg=new Configuration(); cfg.configure(); sessionFactory=cfg.buildSessionFactory();}//SessionFactory
public static SessionFactory getSessionFactory(){ return sessionFactory; }
//Session
public static Session getSession(){ return sessionFactory.openSession(); }
//添加
public static void add(Object entity){ Session s=null; Transaction ts=null; try{ s=HibernateUtil.getSession(); ts=s.beginTransaction(); s.save(entity);//没提交事务的前提下,save方法执行插入语句,再回滚删除 ts.commit(); }catch(HibernateException e){ if(ts!=null){ ts.rollback(); throw e; } }finally{ if(s!=null){ s.close(); } } } //修改 public static void update(Object entity){ Session s=null; Transaction ts=null; try{ s=HibernateUtil.getSession(); ts=s.beginTransaction(); s.update(entity);//没提交事务的前提下,save方法执行插入语句,再回滚删除 ts.commit(); }catch(HibernateException e){ if(ts!=null){ ts.rollback(); throw e; } }finally{ if(s!=null){ s.close(); } } }
//删除
public static void delete(Object entity){ Session s=null; Transaction ts=null; try{ s=HibernateUtil.getSession(); ts=s.beginTransaction(); s.delete(entity);//没提交事务的前提下,save方法执行插入语句,再回滚删除 ts.commit(); }catch(HibernateException e){ if(ts!=null){ ts.rollback(); throw e; } }finally{ if(s!=null){ s.close(); } } }
//查
public static Object get(Class clazz, Serializable id){ Session s=null; try{ s=HibernateUtil.getSession(); Object obj=s.get(clazz, id); return obj; }finally{ if(s!=null){ s.close(); } } }
}
Hibernate对应关系
一对多,
一个部门多个员工,多个员工在一个部门
class Dept{ private int id; private Set<Emp> emp = new HashSet<Emp>(); }
class Emp{ private int id; private int deptid; private Dept dept; }
Dept.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.pojo"> <!-- 配置类与表映射 --> <class name="Dept" table="dept"> <!-- 映射主键与对象表示符 --> <id name="id" column="id"> <!-- <generator class="assigned"></generator> 指定表的主键的管理方式 --> <generator class="sequence"> <param name="sequence">seq_release</param> </generator> </id> <set name="emp" cascade="all" inverse="true"> <key> <column name="deptid"></column> </key> <!-- 一对多 --> <one-to-many class="Emp"/> </set> </class> </hibernate-mapping>
Emp.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.pojo"> <class name="Emp" table="emp"> <id name="id" column="id"> <generator class="sequence"> <param name="sequence">seq_book</param> </generator> </id> <many-to-one name="dept" class="Dept" cascade="all"> <column name="deptid"></column> </many-to-one> </class> </hibernate-mapping>
多对多,拆成两个一对多。
Cour.hbm.xml <set name="student" table=" student_course_link " lazy="false" cascade="all" inverse="true"> <key column="cid"></key> <many-to-many class="Student" column="sid"></many-to-many> </set>
Stu.hbm.xml
<!-- 配置多对多 -->
一对一 也可以 一对多表示。
<one-to-one name="address" class="AddressPojo" cascade="all"></one-to-one>
<one-to-one name="student" class="StudentPojo"></one-to-one>
Spring2.5依赖包
dist\spring.jar
lib\jakarta-commons\commons-logging.jar
lib\aspectj\aspectjweaver.jar 和 aspectjrt.jar 切面编程AOP
lib\cglib\cglib-nodep-2.1.3.jar
lib\j2ee\common-annotations.jar 注解
web.xml需加载
<!-- 配置上下文参数,加载spring配置文件 -->
contextConfigLocation classpath:application*.xml org.springframework.web.context.ContextLoaderListener
获取bean
public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext( new String[]{"applicationContext.xml"}); DBService service=(DBService)context.getBean("dbService"); service.query();
}
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" default-lazy-init="true">
${hibernate.dialect} false false false 100 30 true true org.hibernate.cache.EhCacheProvider
cn/net/Employeelist.hbm.xml PROPAGATION_REQUIRED,-Exception,-DataAccessException