strust2 和 hibernate的整合------登录的实现
初步认识了struts2,并与hibernate进行整合,完成了一个登录的案例,下面贴源码
1.实体类User
public class User {private Integer id; private String uname; private String upass;...省略set和get方法}
2.实体类的映射文件
1 <class name="www.change.tm.bean.User" table="USERS"> 2 <id name="id" type="java.lang.Integer"> 3 <column name="ID" /> 4 <generator class="native" /> 5 </id> 6 <property name="uname" type="java.lang.String"> 7 <column name="UNAME" /> 8 </property> 9 <property name="upass" type="java.lang.String"> 10 <column name="UPASS" /> 11 </property> 12 </class>
3.hibernate.cfg.xml
1 <session-factory> 2 <!-- 配置hibernate的基本信息 --> 3 <property name="connection.username">****</property> 4 <property name="connection.password">****</property> 5 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 6 <property name="connection.url">jdbc:mysql:///hibernate3</property> 7 8 <!-- hibernate的基本配置 --> 9 <!-- 数据库方言 --> 10 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 11 12 <!-- 是否打印sql --> 13 <property name="show_sql">true</property> 14 <!-- 是否格式化sql --> 15 <property name="format_sql">true</property> 16 <!-- 生成表的策略 --> 17 <property name="hbm2ddl.auto">update</property> 18 19 <mapping resource="www/change/tm/bean/User.hbm.xml"/> 20 21 </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{@Overridepublic 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{@Overridepublic Session getSession() {// TODO Auto-generated method stubreturn HibernateControl.getSession();}}
6.util包里
引入HiberSessionFactory.java文件
1 package www.change.tm.util; 2 3 import org.hibernate.HibernateException; 4 import org.hibernate.Session; 5 import org.hibernate.cfg.Configuration; 6 import org.hibernate.service.ServiceRegistry; 7 import org.hibernate.service.ServiceRegistryBuilder; 8 9 /** 10 * Configures and provides access to Hibernate sessions, tied to the 11 * current thread of execution. Follows the Thread Local Session 12 * pattern, see {@link http://hibernate.org/42.html }. 13 */ 14 public class HiberSessionFactory { 15 16 /** 17 * Location of hibernate.cfg.xml file. 18 * Location should be on the classpath as Hibernate uses 19 * #resourceAsStream style lookup for its configuration file. 20 * The default classpath location of the hibernate config file is 21 * in the default package. Use #setConfigFile() to update 22 * the location of the configuration file for the current session. 23 */ 24 private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); 25 private static org.hibernate.SessionFactory sessionFactory; 26 27 private static Configuration configuration = new Configuration(); 28 private static ServiceRegistry serviceRegistry; 29 30 static { 31 try { 32 System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@"); 33 configuration.configure(); 34 System.out.println("configuration="+configuration); 35 serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 36 System.out.println("serviceRegistry="+serviceRegistry); 37 sessionFactory = configuration.buildSessionFactory(serviceRegistry); 38 System.out.println(); 39 } catch (Exception e) { 40 System.err.println("%%%% Error Creating SessionFactory %%%%"); 41 e.printStackTrace(); 42 } 43 } 44 private HiberSessionFactory() { 45 } 46 47 /** 48 * Returns the ThreadLocal Session instance. Lazy initialize 49 * the <code>SessionFactory</code> if needed. 50 * 51 * @return Session 52 * @throws HibernateException 53 */ 54 public static Session getSession() throws HibernateException { 55 Session session = (Session) threadLocal.get(); 56 57 if (session == null || !session.isOpen()) { 58 if (sessionFactory == null) { 59 rebuildSessionFactory(); 60 } 61 session = (sessionFactory != null) ? sessionFactory.openSession() 62 : null; 63 threadLocal.set(session); 64 } 65 66 return session; 67 } 68 69 /** 70 * Rebuild hibernate session factory 71 * 72 */ 73 public static void rebuildSessionFactory() { 74 try { 75 configuration.configure(); 76 serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 77 sessionFactory = configuration.buildSessionFactory(serviceRegistry); 78 } catch (Exception e) { 79 System.err.println("%%%% Error Creating SessionFactory %%%%"); 80 e.printStackTrace(); 81 } 82 } 83 84 /** 85 * Close the single hibernate session instance. 86 * 87 * @throws HibernateException 88 */ 89 public static void closeSession() throws HibernateException { 90 Session session = (Session) threadLocal.get(); 91 threadLocal.set(null); 92 93 if (session != null) { 94 session.close(); 95 } 96 } 97 98 /** 99 * return session factory 100 * 101 */ 102 public static org.hibernate.SessionFactory getSessionFactory() { 103 return sessionFactory; 104 } 105 /** 106 * return hibernate configuration 107 * 108 */ 109 public static Configuration getConfiguration() { 110 return configuration; 111 } 112 113 }
7.数据库
转载于:https://www.cnblogs.com/zx-n/p/5277984.html
strust2 和 hibernate的整合------登录的实现相关推荐
- Struts2与Spring、Hibernate三者整合的过程示例
转载地址:http://www.360doc.com/content/09/0416/09/61497_3148602.shtml# 原来spring配置文件自动生成数据源和整合先后有关系,留着做个提 ...
- Struts2+Hibernate+Spring 整合示例
转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...
- Hibernate框架整合
Hibernate框架整合 这里给出整合了Hibernate的留言板的程序框架图,采用接口编程的方式. PS:这可以和前端时间写的Hibernate入门.Struts入门.Web MVC模式实现这三篇 ...
- Struts2和hibernate框架整合实现简单的注册登陆功能
Struts2和hibernate框架整合实现简单的注册登陆功能 项目结构: LoginAction.java package action; import vo.User; import vo.Us ...
- spring和hibernate的整合
阅读目录 一.概述 二.整合步骤 1.大致步骤 2.具体分析 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让H ...
- Struts2+Spring+Hibernate的整合
整体程序结构 1.maven依赖 <!--实现Struts2+Spring+Hibernate的整合 --><dependencies><!--Spring --> ...
- Spring Security 4 整合Hibernate 实现持久化登录验证(带源码)
上一篇文章:Spring Security 4 整合Hibernate Bcrypt密码加密(带源码) 原文地址:http://websystique.com/spring-security/spri ...
- struts2+spring+hibernate框架整合与项目
嗯,其实一两周前都写好了,可一直懒得发,今天终于不懒一会,发一下.内容很清楚,主要是搭建框架的过程还有我写项目中遇到的许多问题.鉴于太多了,所以懒惰的我直接发的我参考的那些作者的链接,大家可以看一看. ...
- Spring + SpringMVC + Hibernate + Shiro整合
以前就一直想学Shiro怎么使用,但一直没动力学,这次因为项目中要用,没办法就去学了.其实Shiro还是挺简单的,而且用着也很方便.例子是一个关于用户角色权限的例子,用户与角色,角色与权限均为多对多的 ...
最新文章
- 漫话:如何给女朋友解释什么是 Git 和 GitHub?
- 生产环境WEB服务管理脚本之日志检测脚本
- CSS教程--CSS背景
- zabbix自动发现主机并加入组绑定模板
- DFF之--(一)神经网络入门之线性回归
- 【暖*墟】#树链剖分# 树链剖分学习与练习
- OpenCasCade默认的小坐标系的构建
- win10 安装IIS
- 弘辽科技:想要利用直通车打造爆款,这个技巧一定要把握
- Centos6普通用户获取最高权限方法
- es自建搜索词库_ES——中文分词以及词库扩展
- 多种文字翻译软件-翻译常用软件
- 华为认证专用模拟器 企业内部业务网络设计
- HTML+CSS静态网页作业:NBA勒布朗詹姆斯篮球明星带js(5页)
- 做了 8 个月的技术经理,我信了……
- 北大计算机本科生如何保研清华,保研北京大学的2018届本科生,都来自哪些高校?...
- 国内oschina Maven公共仓库
- SVN教程 服务端/客户端
- 【ASE+python】实现将poscar格式文件批量转换为xsd格式文件
- 未来机器人会有多“可怕”,这些技术已经有所体现