有前面一节我们知道简答的web页面加入Struts的运行效果和运行机制,现在我们在上面给的基础上加入Hibernate框架,对hibernate最基本的功能与配置进行介绍。为了更好的体现出层次比如Struts的MVC形式等,我们知道当运行到registerAction后会调用后台的execute方法,在execute中会实现数据库的持久化,以及使用JDBC访问数据库,对数据进行操作,当我们加入Hibernate后我们需要实现的ORM的框架机制,因此我们抽象出Service层和DAO层,Service层是我们的控制服务层,而DAO层可以实现跨数据库的具体操作与链接。
加入Hibernate后,web.xml和Struts.xml的配置都不需要任何的改动,我们首先要引入Hibernate的JAR包,然后创建hibernate.cfg.xml文件文件的格式我们可以查看Hibernate的API来点击打开链接。代码如下:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/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:hsqldb:hsql://localhost:3306</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/thridProject.model/user.hbm.xml"/>
</session-factory>
</hibernate-configuration>
从上面的配置中我们可以看到我们在前面只用Struts时的JDBC链接可以在上面的属性中直接配置,所以我们加入hibernate后就不要在自己写JDBC的链接等;Mapping中的resource可以是我们使用xml的配置进行关系标的映射,现在我们在创建user.hbm.xml也可以在hibernate的api中进行查找:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.thridProject.model">
<class name="User" table="T_user">
<id name="username" ></id>
<property name="password"></property>
</class>
</hibernate-mapping>
这里如果没有设置property将会传递不进参数。然后是action层的代码如下:
package com.thridProject.action;
import com.opensymphony.xwork2.ActionSupport;
import com.thridProject.model.User;
import com.thridProject.service.UserService;
public class RegisterAction extends ActionSupport {
/**
* author 张春蕾 2013-11-17
*/
private static final long serialVersionUID = 1L;
private User user;
private UserService userService = new UserService();
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute(){
if(userService.isExist(user)){
return "true";
}else{
return "false";
}
}
}
我们在userAction层用到了userService层代码如下:
package com.thridProject.service;
import com.thridProject.dao.UserDao;
import com.thridProject.impl.UserDaoImpl;
import com.thridProject.model.User;
public class UserService {
private UserDao userDao;
public UserService(){
userDao = new UserDaoImpl();
}
public boolean isExist(User user){
return userDao.isExist(user);
}
}
在这层用到了userDao层的代码:
package com.thridProject.dao;
import com.thridProject.model.User;
public interface UserDao {
public boolean isExist(User user);
}
我们通过userDaoImpl层来实现UserDao,根据不同的UserDaoImpl层可以实现跨数据库平台:
package com.thridProject.impl;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.thridProject.dao.UserDao;
import com.thridProject.model.User;
public class UserDaoImpl implements UserDao{
@SuppressWarnings("unchecked")
public boolean isExist(User user){
boolean exist = false;
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
// List<User> li = session.createSQLQuery("select * from T_USER where username='"+
// user.getUsername()+"'").addEntity(User.class).list();
List<User> li = session.createQuery("from User u where u.username='"+
user.getUsername()+"'").list();
session.getTransaction.commit();
session.close();
sf.close();
if(li.size()!=0){
User user1 = li.get(0);
if(user1.getPassword().equals(user.getPassword())){
exist = true;
}
}
return exist;
}
}
上面注释的代码是使用了createQuery方法和createSQLQuery方法的区别: 点击打开链接,以上都能实现,实现这些以后,就在原来Struts的基础上加入了Hibernate当然,我们这儿的orm是通过xml配置完成,现在我们也可以使用注释来完成ORM配置。