Hibernate 登陆实例的基本操作

来源:互联网 发布:图片打印编辑软件 编辑:程序博客网 时间:2024/05/21 19:26
  1. Hibernate 登陆实例的基本操作
  2. MySessionFactory.java
  3. public class MySessionFactory {
  4.     private static Session session;
  5.     private static Configuration config = new Configuration();
  6.     private static SessionFactory sessionFactory;
  7.     
  8.     public static Session getSession() throws HibernateException{
  9.         if(session==null || !session.isOpen()){
  10.             if (sessionFactory == null){
  11.                 rebuildSessionFactory();
  12.             }
  13.             session=sessionFactory.openSession();
  14.         }
  15.         return session;
  16.     }
  17.     
  18.     private static void rebuildSessionFactory() {
  19.         try {
  20.             config.configure();
  21.             sessionFactory = config.buildSessionFactory();
  22.         } catch (Exception e) {
  23.             e.printStackTrace();
  24.         }
  25.     }
  26.     
  27. }
  28. MyPlugIn.java
  29. import javax.servlet.ServletException;
  30. import org.apache.struts.action.ActionServlet;
  31. import org.apache.struts.action.PlugIn;
  32. import org.apache.struts.config.ModuleConfig;
  33. public class MyPlugIn implements PlugIn {
  34.     public void destroy() {
  35.         // TODO 自动生成方法存根
  36.     }
  37.     public void init(ActionServlet servlet, ModuleConfig conf)
  38.             throws ServletException {
  39.         // TODO 自动生成方法存根
  40.         
  41.         servlet.getServletContext().setAttribute("DBSession"
  42. MySessionFactory.getSession());
  43.     }
  44. }
  45. 在Struts-config中配置
  46. <plug-in className="my.MyPlugIn"></plug-in>
  47. UserOpertor.java:
  48. import java.util.List;
  49. import org.hibernate.Query;
  50. import org.hibernate.Session;
  51. public class UserOpertor {
  52.     
  53.     private Session se=null;
  54.     
  55.     public UserOpertor(){
  56. //      Configuration config=new Configuration().configure();
  57. //      SessionFactory sf=config.buildSessionFactory();
  58. //      se= sf.openSession();
  59.     }
  60.     
  61.     public UserOpertor(Object sessionObj){
  62.         se=(Session)sessionObj;     
  63.     }
  64.     
  65.     
  66.     
  67.     public List getAllUser(){
  68.         Query query= se.createQuery("from User");
  69.         List rtn=query.list();
  70.         return rtn;
  71.     }
  72.     
  73.     public void addUser(User user){
  74.         se.save(user);
  75.         se.beginTransaction().commit();
  76.     }
  77.     
  78.     public void updUser(User user){
  79.         se.update(user);
  80.         se.beginTransaction().commit();
  81.     }
  82.     
  83.     public void delUser(User user){
  84.         se.delete(user);
  85.         se.beginTransaction().commit();
  86.     }
  87.     
  88. }
  89. Action:
  90. public class LoginAction extends Action {
  91.     public ActionForward execute(ActionMapping mapping, ActionForm form,
  92.             HttpServletRequest request, HttpServletResponse response) {
  93.         //1:
  94.         LoginForm loginForm = (LoginForm) form;
  95.         Object obj= this.servlet.getServletContext().getAttribute("DBSession");
  96.         boolean flag=false;
  97.         //2:
  98.         UserOpertor op=new UserOpertor(obj);
  99.         ArrayList users=(ArrayList)op.getAllUser();
  100.         for(int i=0;i<users.size();i++){
  101.             User temp=(User) users.get(i);
  102.             if(temp.getUid().equals(loginForm.getUid())&&temp.getPwd().equals
  103. (loginForm.getPwd())){
  104.                 flag=true;
  105.                 break;
  106.             }           
  107.         }
  108.         //3:
  109.         if(flag){
  110.             return mapping.findForward("aaa");
  111.         }else{
  112.             return mapping.findForward("bbb");
  113.         }
  114.     }
  115. }
  116. user.java
  117. public class User {
  118.     private String uid;
  119.     private String uname;
  120.     private String pwd;
  121.     public String getPwd() {
  122.         return pwd;
  123.     }
  124.     public void setPwd(String pwd) {
  125.         this.pwd = pwd;
  126.     }
  127.     public String getUid() {
  128.         return uid;
  129.     }
  130.     public void setUid(String uid) {
  131.         this.uid = uid;
  132.     }
  133.     public String getUname() {
  134.         return uname;
  135.     }
  136.     public void setUname(String uname) {
  137.         this.uname = uname;
  138.     }
  139.     
  140. }
  141. modle.xml:
  142. <?xml version="1.0" encoding="UTF-8"?>
  143. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  144. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  145. <hibernate-mapping>
  146.     <class  name="my.User"  table="UsersInfo" >
  147.         <id name="uid" column="uid" type="string" >
  148.             <generator class="assigned"></generator>
  149.         </id>
  150.         <property name="uname" column="uname" type="string" />
  151.         <property name="pwd" column="pwd" type="string" />
  152.     </class>
  153. </hibernate-mapping>
  154. hibernate.cfg.xml:
  155. <?xml version='1.0' encoding='UTF-8'?>
  156. <!DOCTYPE hibernate-configuration PUBLIC
  157.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  158.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  159. <!-- Generated by MyEclipse Hibernate Tools.                   -->
  160. <hibernate-configuration>
  161. <session-factory>
  162.     <property name="connection.username">sa</property>
  163.     <property name="connection.url">jdbc:odbc:Test</property>
  164.     <property name="dialect">
  165.         org.hibernate.dialect.SQLServerDialect
  166.     </property>
  167.     <property name="connection.password">111</property>
  168.     <property name="connection.driver_class">
  169.         sun.jdbc.odbc.JdbcOdbcDriver
  170.     </property>
  171.     <property name="show_sql">true</property>
  172.     <mapping resource="model.xml" />
  173. </session-factory>
  174. </hibernate-configuration>
原创粉丝点击