Hibernate 登陆实例的基本操作
来源:互联网 发布:图片打印编辑软件 编辑:程序博客网 时间:2024/05/21 19:26
- Hibernate 登陆实例的基本操作
- MySessionFactory.java
- public class MySessionFactory {
- private static Session session;
- private static Configuration config = new Configuration();
- private static SessionFactory sessionFactory;
- public static Session getSession() throws HibernateException{
- if(session==null || !session.isOpen()){
- if (sessionFactory == null){
- rebuildSessionFactory();
- }
- session=sessionFactory.openSession();
- }
- return session;
- }
- private static void rebuildSessionFactory() {
- try {
- config.configure();
- sessionFactory = config.buildSessionFactory();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- MyPlugIn.java
- import javax.servlet.ServletException;
- import org.apache.struts.action.ActionServlet;
- import org.apache.struts.action.PlugIn;
- import org.apache.struts.config.ModuleConfig;
- public class MyPlugIn implements PlugIn {
- public void destroy() {
- // TODO 自动生成方法存根
- }
- public void init(ActionServlet servlet, ModuleConfig conf)
- throws ServletException {
- // TODO 自动生成方法存根
- servlet.getServletContext().setAttribute("DBSession",
- MySessionFactory.getSession());
- }
- }
- 在Struts-config中配置
- <plug-in className="my.MyPlugIn"></plug-in>
- UserOpertor.java:
- import java.util.List;
- import org.hibernate.Query;
- import org.hibernate.Session;
- public class UserOpertor {
- private Session se=null;
- public UserOpertor(){
- // Configuration config=new Configuration().configure();
- // SessionFactory sf=config.buildSessionFactory();
- // se= sf.openSession();
- }
- public UserOpertor(Object sessionObj){
- se=(Session)sessionObj;
- }
- public List getAllUser(){
- Query query= se.createQuery("from User");
- List rtn=query.list();
- return rtn;
- }
- public void addUser(User user){
- se.save(user);
- se.beginTransaction().commit();
- }
- public void updUser(User user){
- se.update(user);
- se.beginTransaction().commit();
- }
- public void delUser(User user){
- se.delete(user);
- se.beginTransaction().commit();
- }
- }
- Action:
- public class LoginAction extends Action {
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- //1:
- LoginForm loginForm = (LoginForm) form;
- Object obj= this.servlet.getServletContext().getAttribute("DBSession");
- boolean flag=false;
- //2:
- UserOpertor op=new UserOpertor(obj);
- ArrayList users=(ArrayList)op.getAllUser();
- for(int i=0;i<users.size();i++){
- User temp=(User) users.get(i);
- if(temp.getUid().equals(loginForm.getUid())&&temp.getPwd().equals
- (loginForm.getPwd())){
- flag=true;
- break;
- }
- }
- //3:
- if(flag){
- return mapping.findForward("aaa");
- }else{
- return mapping.findForward("bbb");
- }
- }
- }
- user.java
- public class User {
- private String uid;
- private String uname;
- private String pwd;
- public String getPwd() {
- return pwd;
- }
- public void setPwd(String pwd) {
- this.pwd = pwd;
- }
- public String getUid() {
- return uid;
- }
- public void setUid(String uid) {
- this.uid = uid;
- }
- public String getUname() {
- return uname;
- }
- public void setUname(String uname) {
- this.uname = uname;
- }
- }
- modle.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="my.User" table="UsersInfo" >
- <id name="uid" column="uid" type="string" >
- <generator class="assigned"></generator>
- </id>
- <property name="uname" column="uname" type="string" />
- <property name="pwd" column="pwd" type="string" />
- </class>
- </hibernate-mapping>
- hibernate.cfg.xml:
- <?xml version='1.0' encoding='UTF-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <!-- Generated by MyEclipse Hibernate Tools. -->
- <hibernate-configuration>
- <session-factory>
- <property name="connection.username">sa</property>
- <property name="connection.url">jdbc:odbc:Test</property>
- <property name="dialect">
- org.hibernate.dialect.SQLServerDialect
- </property>
- <property name="connection.password">111</property>
- <property name="connection.driver_class">
- sun.jdbc.odbc.JdbcOdbcDriver
- </property>
- <property name="show_sql">true</property>
- <mapping resource="model.xml" />
- </session-factory>
- </hibernate-configuration>
- Hibernate 登陆实例的基本操作
- Hibernate数据库操作基本实例
- Hibernate映射的基本操作
- Hibernate映射的基本操作
- Hibernate映射的基本操作
- Hibernate映射的基本操作
- Hibernate映射的基本操作
- hibernate 映射的基本操作
- Hibernate映射的基本操作
- hibernate的基本保存操作
- Hibernate映射的基本操作
- Hibernate映射的基本操作的笔记
- Hibernate映射的基本操作(转载)
- Hibernate框架中Session的基本操作
- mysql和hibernate的基本操作
- "Hibernate"对数据库操作的实例代码
- Jrtplib的最基本的操作实例
- java File的基本操作实例
- P1132已知先后序和中序求先序排列解题报告
- 第一次使用URL编码
- MyEclipse 5.5.1 GA 为什么总是提示 轰炸机 开始? 是什么错误,,
- Suse Linux的防火墙相关
- 设计高性能网站架构-LLMP
- Hibernate 登陆实例的基本操作
- IronPython 2.0 released
- virtualbox 虚拟ubuntu设置分辨率
- enctype 属性取值
- 抓取UPS快递运单页面的方法
- C++高精度定时器
- 几种开源SIP协议栈对比
- 给XMLHttpRequest设置超时时间
- JS断点调试