初步认识了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. ThinkPHP模板中JS等带花括号处会被解析错误的解决办法

    如下图,当本人在ThinkPHP框架的模板中写jQuery代码的时候,写了一些注释,并且注重是斜线和换括号{是连着一起的,这层语法上来时是没问题的,但是在ThinkPHP 的模板引擎解析下,会被解析掉 ...

  2. 手把手教你使用FineUI开发一个b/s结构的取送货管理信息系统系列博文索引

    近阶段接到一些b/s类型的软件项目,但是团队成员之前大部分没有这方面的开发经验,于是自己选择了一套目前网上比较容易上手的开发框架(FineUI),计划录制一套视频讲座,来讲解如何利用FineUI快速开 ...

  3. ActiveX控件在项目中的应用

  4. JAVA常见算法题(二十六)

    package com.xiaowu.demo; import java.util.Scanner; /** * Java实现将阿拉伯数字转为汉字 * * @author WQ * */ public ...

  5. ylbtech-dbs-m-YinTai(银泰网)

    ylbtech-dbs:ylbtech-dbs-m-YinTai(银泰网) -- =============================================-- DatabaseNam ...

  6. Gitlab系列八之重置管理员密码

    gitlab web登入密码忘记以后可以用如下方式修改密码 [root@gitlat-test gitlab]# gitlab-rails console production Loading pro ...

  7. Kubernetes概念介绍和v1版本部署过程

    简介: k8s一个开源的,跨主机管理容器应用集群的编排系统,为应用提供了基础的部署.维护和扩缩容机制. 编排:跨Docker主机同一管理容器集群. 目的 简化开发和运维容器集群的工作. 让开发和运维能 ...

  8. Solr json,xml等文件数据导入(添加索引)linux下操作

    使用solr-5.3.1\example\exampledocs下的post.jar来完成数据导入 1.将想要导入的文件放在solr-5.3.1\example\exampledocs中,如aaa.x ...

  9. webmagic的多线程及线程池的应用

  10. Appium Python 一:环境搭建

    安装Android SDK以及模拟器 由于Appium依赖于Android SDK,所以需要先安装SDK. 这里由于需要在Android模拟器上跑测试用例,所以同时需要安装Android 模拟器. 1 ...