strust2 和 hibernate的整合------登录的实现
初步认识了struts2,并与hibernate进行整合,完成了一个登录的案例,下面贴源码
1.实体类User
public class User {
private Integer id;
private String uname;
private String upass;
...省略set和get方法
}
2.实体类的映射文件
<class name="www.change.tm.bean.User" table="USERS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="uname" type="java.lang.String">
<column name="UNAME" />
</property>
<property name="upass" type="java.lang.String">
<column name="UPASS" />
</property>
</class>
3.hibernate.cfg.xml
<session-factory>
<!-- 配置hibernate的基本信息 -->
<property name="connection.username">****</property>
<property name="connection.password">****</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate3</property> <!-- hibernate的基本配置 -->
<!-- 数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 是否打印sql -->
<property name="show_sql">true</property>
<!-- 是否格式化sql -->
<property name="format_sql">true</property>
<!-- 生成表的策略 -->
<property name="hbm2ddl.auto">update</property> <mapping resource="www/change/tm/bean/User.hbm.xml"/> </session-factory>
4.action类
package www.change.tm.action; import java.util.Map; import org.apache.struts2.ServletActionContext; import www.change.tm.bean.User;
import www.change.tm.dao.UserDao;
import www.change.tm.dao.UserDaoImpl; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class Login extends ActionSupport{ //声明dao对象
UserDao userdao = new UserDaoImpl(); private String uname;
private String upass; public String login(){ User user = userdao.login(uname, upass); if (user != null ) {
// 存入到session会话中
ServletActionContext.getRequest().getSession()
.setAttribute("user", user); return SUCCESS; } else {
ServletActionContext.getRequest().setAttribute("msg", "用户名或者密码");
return ERROR;
}
} public void setUname(String uname) {
this.uname = uname;
} public void setUpass(String upass) {
this.upass = upass;
} }
5.dao层
5.1 BaseDao
public interface BaseDao {
public Session getSession();
}
5.2 UserDao
public interface UserDao {
/*
* 登录验证处理
*/
public User login(String uname,String upass);
}
5.3 UserDaoImpl
public class UserDaoImpl extends BaseDaoImpl implements UserDao{
@Override
public User login(String uname, String upass) {
//1.获取session对象
Session session = getSession();
//2.执行查询
Query createQuery = session.createQuery("from User u where u.uname=? and u.upass=?");
User user = (User)createQuery.setString(0, uname).setString(1, upass).uniqueResult();
/*总的写
User user =(User) getSession().createQuery("").setString(0, uname).setString(1, upass).uniqueResult();
*/
//3.session关闭
HiberSessionFactory.closeSession();
return user;
}
}
5.4 BaseDaoImpl
public class BaseDaoImpl implements BaseDao{
@Override
public Session getSession() {
// TODO Auto-generated method stub
return HibernateControl.getSession();
}
}
6.util包里
引入HiberSessionFactory.java文件
package www.change.tm.util; import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder; /**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HiberSessionFactory { /**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static org.hibernate.SessionFactory sessionFactory; private static Configuration configuration = new Configuration();
private static ServiceRegistry serviceRegistry; static {
try {
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@");
configuration.configure();
System.out.println("configuration="+configuration);
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
System.out.println("serviceRegistry="+serviceRegistry);
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
System.out.println();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HiberSessionFactory() {
} /**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
} return session;
} /**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
} /**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
} }
7.数据库

strust2 和 hibernate的整合------登录的实现的更多相关文章
- SpringMVC+spring-security+sitemesh+hibernate+freemarker整合-转
http://www.oschina.net/code/snippet_170632_46774 代码分享 当前位置: 代码分享 » Java » Web编程 搜 索 SpringMVC+spr ...
- SpringMVC+Apache Shiro+JPA(hibernate)整合配置
序: 关于标题: 说是教学,实在愧不敢当,但苦与本人文笔有限,实在找不到更合理,谦逊的词语表达,只能先这样定义了. 其实最真实的想法,只是希望这个关键词能让更多的人浏览到这篇文章,也算是对于自己写文章 ...
- Struts 2 + Hibernate + Spring 整合要点
Struts 2 和 Spring 的功能有重合,因此有必要说明下,整合中分别使用了两种框架的哪些技术. Struts 2 使用功能点: 1.拦截器.一处是对非登录用户购物进行拦截,一处是对文件上传的 ...
- spring和hibernate的整合
阅读目录 一.概述 二.整合步骤 1.大致步骤 2.具体分析 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让H ...
- SSH(Spring SpringMVC Hibernate)框架整合
项目说明: 使用SSH(Spring SpringMVC Hibernate)框架整合添加部门功能 项目结构 1.导入依赖jar包 <!--单测--> <dependency&g ...
- Struts2+Hibernate+Spring 整合示例
转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...
- Hibernate+Spring整合开发步骤
Hibernate是一款ORM关系映射框架+Spring是结合第三方插件的大杂烩,Hibernate+Spring整合开发效率大大提升. 整合开发步骤如下: 第一步:导入架包: 1.Hibernate ...
- Spring+Struts2+Hibernate的整合
这篇主要采用Maven搭建Spring+Struts2+Hibernate的整合项目,复习一下SSH框架,虽然spring提供自己的MVC框架, 但是Spring也提供和其他框架的无缝整合,采用组件形 ...
- Spring+Hibernate+Struts2整合之实现登录功能
前端代码: <form id="loginForm" action="${ pageContext.request.contextPath }/user_login ...
随机推荐
- 十. 图形界面(GUI)设计8.选择框和单选按钮
选择框.单选框和单选按钮都是选择组件,选择组件有两种状态,一种是选中(on),另一种是未选中(off),它们提供一种简单的 “on/off”选择功能,让用户在一组选择项目中作选择. 选择框 选择框(J ...
- 在WinRT程序中使用MEF
今天试了一下在WinRT中使用MEF,这里简单的介绍一下步骤. 首先,使用NuGet安装MEF 然后,就可以使用MEF组装插件了,简单的示例如下: interface ILogger { ...
- ethtool 命令输出的注意点--网卡参数
http://blog.csdn.net/msdnchina/article/details/70339689
- Delphi 使窗体Showmodal后可以操作其他窗体
对话框ShowModal之后不能操作其它窗口,实际上是因为Windows Disable了其它窗口.所以当你需要在模态对话框中访问其它已经可见的窗口时,需要用EnableWindow API来激活对应 ...
- JS向后台传递json数组对象
var Obj = []; //一下代码可以循环插入 var returnObj = new Object();//创建一个对象 returnObj.id = “123”: returnObj.mon ...
- 十个Chatbot框架介绍
十个Chatbot框架介绍 原创 2016年12月13日 16:01:23 4616 Chatbot列表 1. Artificial Intelligence Markup Language ...
- iOS:APNS推送主要代码
首先,在AppDelegate.m 中: 1,注册通知 //[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片 - (BOOL)application:(UI ...
- IOS Vsync
vsync count Don't Sync Application.targetFrameRate 设置FPS上限 Every Second VBlank 30 Every VBlank 60 An ...
- Java solr 分词
代码如下: import java.io.IOException; import java.util.*; import org.apache.solr.client.solrj.SolrClient ...
- 依据出生日期Date 计算年龄
依据出生日期计算年龄 public class DateGetAge { public static int getAge(Date birthDay) throws Exception { Cale ...