参考部分《开发者突击:Struts 2核心技术与Java EE框架整合开发实战》第15章、《struts2权威指南》和网上资料
1、建立数据库(mysql),例子为求简单,仅使用一个用户表,建表语句如下:
create table tb_user(
user_id INT NOT NULL AUTO_INCREMENT,
user_name VARCHAR(30),
user_password VARCHAR(30),
user_type int,
PRIMARY KEY (user_id)
);
2、创建工程并将所需运行库文件导入工程,所有需要的运行库文件如下所示。图直接从书上截的,实际版本可能略有区别。
3、在src目录下加入hibernate的配置文件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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/dbFirstJBPM</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">create</property> -->
</session-factory>
</hibernate-configuration>
4、在src下添加struts.xml配置文件,并在web.xml中配置struts2的过滤器,如整合strut2所讲。
5、Struts2无法直接和hibernate交互,如果要整合,必须有中间层组件,如下图所示。
6、为数据库中的表建立持久化对象(PO,Persistent Object)和映射文件。前面表中已经给出了tb_user的建表语句,对应的持久化对象为User,映射文件为User.hbm.xml,在src下建立包dbTable,所有的持久化对象和映射文件都放在里面。持久化对象的作用是完成持久化操作,即通过该对象执行对数据的增删改,以面向对象的方式操作数据库。
User.java内容为:
package dbTable;
import java.io.Serializable;
public class User implements Serializable {
private int user_id;
private String user_name;
private String user_password;
private int user_type;
public User(){
}
public User(int user_id,String user_name, String user_password, int user_type){
this.user_id=user_id;
this.user_name=user_name;
this.user_password=user_password;
this.user_type=user_type;
}
public int getUser_id(){
return this.user_id;
}
public void setUser_id(int user_id){
this.user_id=user_id;
}
public String getUser_name(){
return this.user_name;
}
public void setUser_name(String user_name){
this.user_name=user_name;
}
public String getUser_password(){
return this.user_password;
}
public void setUser_password(String user_password){
this.user_password=user_password;
}
public int getUser_type(){
return this.user_type;
}
public void setUser_type(int user_type){
this.user_type=user_type;
}
}
对应的映射文件内容为:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="dbTable.User" table="tb_user">
<id name="user_id" column="user_id" type="int">
<generator class="increment"/>
</id>
<property name="user_name" type="java.lang.String" column="user_name"/>
<property name="user_password" type="java.lang.String" column="user_password"/>
<property name="user_type" type="int" column="user_type"/>
</class>
</hibernate-mapping>
7、在hibernate.cfg.xml中加入对应表的映射信息,如下:
<mapping resource="dbTable/User.hbm.xml"/>
8、编写数据对应的服务层(理解不够,这样叫未必合适)。建立一个数据interface UserDao和对应的实现类UserDaoImpl(此处只给出用到的根据用户名到数据库中查询是否有此用户的部分)。
public class UserDaoImpl implements UserDao{
public UserDaoImpl(){
}
public User getUserByUsername(String user_name){
User user=null;
Configuration conf=null;
SessionFactory sessionFactory=null;
// File file=new File("E://java//Struts2Hibernate//src//hibernate.cfg.xml");
try{
conf=new Configuration().configure();
sessionFactory=conf.buildSessionFactory();
}catch (Throwable ex){
System.err.println("Initialize SessionFactory failed"+ex);
throw new ExceptionInInitializerError(ex);
}
Session sess=sessionFactory.openSession();
Transaction tx=sess.beginTransaction();
String hql="from User where user_name='"+user_name+"'";
Query userList=sess.createQuery(hql);
List list=userList.list();
user=(User)list.get(0);
// user=(User)sess.get(User.class, new Integer("1"));
tx.commit();
sess.close();
return user;
}
}
9 、修改LoginAction的内容(原来的内容可以从Struts2的示例中得到),如下:
public String execute() throws Exception {
UserDaoImpl userDaoImpl=new UserDaoImpl();
User user=null;
user=userDaoImpl.getUserByUsername(getUsername());
if ((user!=null)&&(user.getUser_password().equals(getPassword()))) {
return "success";
}
else return "error";
}
10、之后部署到Tomcat中即可。
几个需要注意的问题:
1、.hbm.xml中,属性映射中的type是Hibernate的类型,所以如果用Java中的类型,例如String,需要写全java.lang.String。
2、想在Myeclipse中调试程序,要在Debug perspective下,启动Tomcat时选择debug server才能使用断点。
3、需要使用Hibernate中的类时,把对应的jar路径添加到工程依赖的库中。