初步认识了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. Java序列化对象为字符串并将字符串反序列化为对象

    对象的序列化与反序列化其实就是将对象的状态保存下来,一般是保存到文件中,但是其实更常用的是将对象序列化为字符串保存到数据库中,然后在需要读取对象的情况下将字符串反序列化为对象.   可以序列化的类必须 ...

  2. 109.关路灯(区间dp)

    1258 关路灯  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 多瑞卡得到了一份有趣而高薪的工作.每 ...

  3. ios-真机调试出错信息

    更新证书错误Code Sign error: Provisioning profile ‘XXXX'can't be found                   在Xcode中当你在更新了你得证书 ...

  4. jquery获取select下拉框的前一个,后一个,第一个,最后一个option对象

    $("select option:selected").next(); <select> <option value="1" selected ...

  5. What's New In DevTools (Chrome 59)来看看最新Chrome 59的开发者工具又有哪些新功能

    原文:https://developers.google.com/web/updates/2017/04/devtools-release-notes#command-menu 参考:https:// ...

  6. [GitHub开源]基于HTML5实现的轻量级Google Earth三维地图引擎,带你畅游世界 【转】

    http://blog.csdn.net/iispring/article/details/52679185 WebGlobe HTML5基于原生WebGL实现的轻量级Google Earth三维地图 ...

  7. win10下将spark的程序提交给远程集群中运行

    一,开发环境: 操作系统:win19 64位 IDE:IntelliJ IDEA JDK:1.8 scala:scala-2.10.6 集群:linux上cdh集群,其中spark为1.5.2,had ...

  8. ILockBytes Windows Mobile 6.5

    ILockBytes Windows Mobile 6.5  https://msdn.microsoft.com/zh-cn/library/aa911496(en-us,MSDN.10).aspx ...

  9. diamond淘宝框架使用

    转载:http://blog.csdn.net/coolyqq/article/details/50435634 一.概况 diamond是淘宝内部使用的一个管理持久配置的系统,它的特点是简单.可靠. ...

  10. autoconfig.xml与antx.properties一级application.properties之间的关系

    Java web项目中一般都有配置文件,文件中包含一些配置信息供Java工程启动和运行时使用,这些常见的配置文件大都是一些以.properties后缀的文件,比如常见的antx.properties以 ...