初步认识了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的整合------登录的实现的更多相关文章

  1. SpringMVC+spring-security+sitemesh+hibernate+freemarker整合-转

    http://www.oschina.net/code/snippet_170632_46774 代码分享 当前位置: 代码分享 » Java  » Web编程 搜 索   SpringMVC+spr ...

  2. SpringMVC+Apache Shiro+JPA(hibernate)整合配置

    序: 关于标题: 说是教学,实在愧不敢当,但苦与本人文笔有限,实在找不到更合理,谦逊的词语表达,只能先这样定义了. 其实最真实的想法,只是希望这个关键词能让更多的人浏览到这篇文章,也算是对于自己写文章 ...

  3. Struts 2 + Hibernate + Spring 整合要点

    Struts 2 和 Spring 的功能有重合,因此有必要说明下,整合中分别使用了两种框架的哪些技术. Struts 2 使用功能点: 1.拦截器.一处是对非登录用户购物进行拦截,一处是对文件上传的 ...

  4. spring和hibernate的整合

    阅读目录 一.概述 二.整合步骤 1.大致步骤 2.具体分析 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让H ...

  5. SSH(Spring SpringMVC Hibernate)框架整合

    项目说明: 使用SSH(Spring SpringMVC Hibernate)框架整合添加部门功能 项目结构   1.导入依赖jar包 <!--单测--> <dependency&g ...

  6. Struts2+Hibernate+Spring 整合示例

    转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...

  7. Hibernate+Spring整合开发步骤

    Hibernate是一款ORM关系映射框架+Spring是结合第三方插件的大杂烩,Hibernate+Spring整合开发效率大大提升. 整合开发步骤如下: 第一步:导入架包: 1.Hibernate ...

  8. Spring+Struts2+Hibernate的整合

    这篇主要采用Maven搭建Spring+Struts2+Hibernate的整合项目,复习一下SSH框架,虽然spring提供自己的MVC框架, 但是Spring也提供和其他框架的无缝整合,采用组件形 ...

  9. Spring+Hibernate+Struts2整合之实现登录功能

    前端代码: <form id="loginForm" action="${ pageContext.request.contextPath }/user_login ...

随机推荐

  1. Problem T: 零起点学算法15——交换变量

    #include<stdio.h> int main() { int a,b,c; scanf("%d %d",&a,&b); c=a; a=b; b= ...

  2. Ionic2 常见问题及解决方案

    前言 Ionic是目前较为流行的Hybird App解决方案,在Ionic开发过程中会遇到很多常见的开发问题,本文尝试对这些问题给出解决方案. 一些常识与技巧 list 有延迟,可以在ion-cont ...

  3. linux内核3.6版本及以下的bug引发的故障--cpu使用率100%

    现象:         旗舰店运价库cpu使用率100%,load升高,导致后续的请求失败.         重启服务器,cpu.load恢复正常. 触发条件:        (1)linux内核3. ...

  4. PHP empty()函数:Can't use method return value in write context

    <?php if (!empty (get_gpc('userId'))) { $userId = get_gpc('userId'); } else { $error = "ID d ...

  5. 电脑(台式机||笔记本)开机password忘记通用解决方法

    方法:直接制作一个老毛桃装机版u盘启动盘 网址:老毛桃官网 步骤:依照网址的解说,将制作好的U盘插入到电脑的usb插口.执行Windows 登入password破解菜单,搜索password所在的盘符 ...

  6. HTML5 canvas图形库 RGraph【转】

    RGraph是一个使用HTML5 Canvas标签实现的图表制作Library.利用该Library生成的Chart具有可交互性,当鼠标点击或移过时会显示相应的信息,可以动态加载Chart或对特殊点进 ...

  7. [Python爬虫] 之二十五:Selenium +phantomjs 利用 pyquery抓取今日头条网数据

    一.介绍 本例子用Selenium +phantomjs爬取今日头条(http://www.toutiao.com/search/?keyword=电视)的资讯信息,输入给定关键字抓取资讯信息. 给定 ...

  8. PhantomJS用法示例

    收录待用,修改转载已取得腾讯云授权 前言 大家有没有发现之前我们写的爬虫都有一个共性,就是只能爬取单纯的html代码,如果页面是JS渲染的该怎么办呢?如果我们单纯去分析一个个后台的请求,手动去摸索JS ...

  9. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-人机界面如何让文本框可以输入,文本框可以编辑

    选中一个文本框,然后在属性中双击输入配置的OnMouseDown事件(也可以是别的事件,但都是通过这种方法)   在左侧点击写变量,然后输入类型改成VisuDialos.Numpad(数字键盘方式), ...

  10. git 基于某个分支创建分支

    1.拷贝源代码 git clone git@git地址 cd 项目目录 2.根据已有分支创建新的分支 git checkout -b yourbranchname origin/oldbranchna ...