一、在myeclipse上配置hibernate环境:
1.把hibernate必要的包导入myeclipse中(导入方法如同上一章导入struts2一样)
hibernate主要的包主要有下面几个
还有一个包是连接数据库和hibernate的,因为我用的是mysql数据库所以我这个包是
.
2.给项目添加hibernate的配置文件
右键项目名称>myeclipse>projectFacets>install hibernate Facets
二、我做了一个模拟签到的功能,点击签到,把当天的时间存放到数据库中代码结构如下:
1、其中time.java是作为pojo类,代码如下:
public class Time {
int id;
String time;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
2、编写Time.hbm.xml,每有一个pojo类就需要一个hbm.xml对应映射。代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 指定test表会映射到dps包下的Time.java,然后对所有成员变量进行罗列,在数据库中自动生成的表名为time,对应table属性 -->
<class name="com.qd.po.Time" table="time">
<!-- 自增的主键ID注意这样写,其余的变量类型必须写全java.lang.*否则可能读不到这个变量类型 -->
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<!-- 列名为time 映射time.java中的time属性 -->
<property name="time" type="java.lang.String">
<column name="time" />
</property>
</class>
</hibernate-mapping>
3.编写hibernate.cfg.xml文件,这个文件里面主要配置了一些数据库的属性和数据库的连接。代码如下:
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!--dialect 告知hibernate你用的是哪一个数据库,便于hibernate翻译HQL语句等-->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--connection.url 说明连接数据库的url,其中mysql的语句如下,characterEncoding=UTF8表示指定解码格式为UTF8-->
<property name="connection.url">jdbc:mysql:///mytime?useUnicode=true&characterEncoding=UTF8</property>
<!--说明数据库的用户名和密码-->
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<!--说明数据库的驱动-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--看Hibernate生成的SQL语句-->
<property name="hibernate.format_sql">true</property>
<!--如果是update表明Hibernate将保留原来的数据记录,插入时把新记录添加到已有的表,-->
<!--如果是create,则总是创建新的表,如果原来数据库已有的这个表,则这个表的记录会被全部清洗 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--罗列表与Java文件有多少个映射文件,这里仅有Time.java与Time.hbm.xml的一对,所以就写一个 -->
<mapping resource="com/qd/po/Time.hbm.xml" />
</session-factory>
</hibernate-configuration>
4. 编写拦截器类AuthInterceptor.java通过拦截器实现验证如果当天已经签过到了将会返回一个input。代码如下:
public class AuthInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation arg0) throws Exception {
// TODO Auto-generated method stub
System.out.println("权限拦截器开始执行");
boolean flage = true;
Session sess = HibernateSessionFactory.getSession();
//查询数据库中time表 因为是映射关系所以这里是要打pojo类名字。查询后存到timelist中
String hql="from Time";
List<Time>timeList=sess.createQuery(hql).list();
//转换时间格式为xxxx年xx月xx日 星期x
String formatDate = DateFormat.getDateInstance(DateFormat.FULL,new Locale("zh","CN")).format(new Date());
//循环timelist列表每一个参数,把列表值赋值给time对象
for (Time time : timeList) {
if (time.getTime().equals(formatDate)) {
//System.out.println(time.getTime());
//System.out.println(formatDate);
flage = false;
break;
}
else {
flage = true;
}
}
HibernateSessionFactory.closeSession();
if(flage){
//System.out.println("权限拦截器验证通过结束");
return arg0.invoke();
}else{
//System.out.println("权限拦截器验证没通过");
return "input";
}
}
}
5、编写action类,代码如下:
public class QiandaoAction extends ActionSupport {
//保持访问用户状态从用户请求中获得用户的session
HttpSession session = ServletActionContext.getRequest().getSession();
private String username;
private String userpass;
public String qiandao(){
//数据库连接
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session sess = sf.openSession();
Transaction tx = sess.beginTransaction();
String formatDate = DateFormat.getDateInstance(DateFormat.FULL,new Locale("zh","CN")).format(new Date());
Time t = new Time();
t.setTime(formatDate);
sess.save(t);
tx.commit();
sess.close();
sf.close();
Session selectsess = HibernateSessionFactory.getSession();
String hql="from Time";
List<Time>timeList=selectsess.createQuery(hql).list();
session.setAttribute("timeList", timeList);
return SUCCESS;
}
}
6、编写struts.xml文件。代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 设置Struts对web页面的解码方式 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- dmc模式 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<package name="myPackage" extends="struts-default,json-default">
<!-- 定义登录的action 和自定义拦截器-->
<interceptors>
<interceptor name="authInter" class="com.qd.action.AuthInterceptor"></interceptor>
</interceptors>
<action name="qiandao" class="com.qd.action.QiandaoAction" method="qiandao">
<!-- 使用自定义拦截器 -->
<interceptor-ref name="authInter"></interceptor-ref>
<!-- 默认拦截器栈 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">/qiandaosuccess.jsp</result>
<result name="input">/error.jsp</result>
</action>
</package>
</struts>
7、最后是jsp页面的编写。代码如下:
<!-- index.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<title>签到</title>
</head>
<center>
<body>
<form action="qiandao.action" method="post">
<input type="submit" value="签到">
</form>
</body>
</center>
</html>
<!-- error.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<title>签到失败</title>
</head>
<body>
今天您已经签过到了请明天再来
</body>
</html>
<!-- qiandaosuccess.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>签到成功</title>
</head>
<body>
<center>
已签到的日子
<table border="1" width="50%" cellpadding="0" cellspacing="0">
<tr style="background-color: #0033FF">
<td align="center">id</td>
<td align="center">日期</td>
</tr>
<!-- 这里使用了struts的标签遍历timelist列表 -->
<s:iterator value="#session.timeList">
<tr>
<td align="center"><s:property value="id" /></td>
<td align="center"><s:property value="time"/></td>
</tr>
</s:iterator>
</table>
</center>
</body>
</html>