1.搭建struts2的工作环境
1)在lib目录下需要导入的包
commons-fileupload-1.2.2.jar,commons-io-2.0.1.jar,commons-lang3-3.1.jar,freemarker-2.3.19.jar,javassist-3.11.0.GA.jar
ognl-3.0.5.jar,struts2-core-2.3.4.1.jar,xwork-core-2.3.4.1.jar
2)配置web.xml文件
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3)配置struts.xml
<struts>
<package name="struts2" extends="struts-defaut">
<action name="action1" class="com.shengsiyuan.LoginAction">
<result name="success"></result>
</action>
</package>
</struts>
2.搭建Hibernate工作环境
1)在lib目录下导入Hibernate所需要的包,可以通过eclipse工具直接帮我们导入到项目。
2)在src目录下配置hibernate.cfg.xml文件,以MySql Server数据库为例,具体配置如下所示:
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<mapping resource="Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
该配置文件包含了连接数据库所需要的信息,如用户名,密码,连接数据库的URL地址,驱动,以及数据库方言。
3)编写好需要操作的持久化类,例如Person类:
package com.panlei.model;
import java.sql.Date;
public class Person
{
private int id;
private String username;
private String password;
private int age;
private Date registerDate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getRegisterDate() {
return registerDate;
}
public void setRegisterDate(Date registerDate) {
this.registerDate = registerDate;
}
public String toString(){
return username+" "+password+" "+age+" "+registerDate;
}
}
4)在src目录下编写Person类对应的配置文件Person.hbm.xml
<hibernate-mapping>
<class name="com.panlei.model.Person" table="person">
<id name="id" column="id">
<generator class="increment"></generator>
</id>
<property name="username" column="username"></property>
<property name="password" column="password"></property>
<property name="age" column="age"></property>
<property name="registerDate" type="date" column="registerDate"></property>
</class>
</hibernate-mapping>
4)Person.hbm.xml配置文件处理好以后,需要到数据库中创建所需要的表person,并需要将该配置文件加到主配置文件
hibernate.cfg.xml中,如上所示,通过mapping元素下的resource属性可以指定,这样就实现了类与数据库中表的映射。
5)编写一个辅助类,用于生成SessionFactory和Session对象
package com.panlei.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil
{
private static Configuration conf;
private static SessionFactory sessionFactory;
private static ThreadLocal<Session> thread;
static{
conf=new Configuration();
conf.configure();
sessionFactory=conf.buildSessionFactory();
thread=new ThreadLocal<Session>();
}
public static Session openSession(){
Session session=thread.get();
if(session==null){
session=sessionFactory.openSession();
thread.set(session);
}
return session;
}
public static void closeSession(){
Session session=thread.get();
if(session!=null&&session.isOpen()){
session.close();
}
thread.set(null);
}
要想与数据库交互只需打开Session,通过Session的各种方法即可实现对数据的增删改查。
struts2只需调用Hibernate提供的底层服务,Hibernate只需与底层的数据库交互。