java后台开发Hibernate例子--登录查询

来源:互联网 发布:学历 知乎 编辑:程序博客网 时间:2024/06/11 03:40

java后台开发Hibernate例子–登录查询

  • java后台开发Hibernate例子登录查询
    • 配置
    • 测试

环境:

win7;jdk1.8_121;tomact8.0;Mysql,
hibernate-release-5.2.10;
需要安装及配置好jdk,mysql。

记录一下。完整代码下载(需要的jar包已放在lib下):
http://download.csdn.net/download/yhhyhhyhhyhh/9921594

1.配置

Servlet3.0以后不需要再配置web.xml中的servlet属性(其他如utf-8中文设置,监视器等属性根据需要设置)

连接数据库需要:mysql-connector-java-5.0.8-bin.jar

服务器上运行servlet需要:servlet-api.jar

hibernate:需要下载jia包,代码中已有

数据库信息:

这里写图片描述

配置文件:(当然也可使用注解进行数据表和实体类的映射)

需要配置好:
web.xml;-servlet..xml;hibernate.cfg.xml(主要是配置数据库映射信息及.hbm.xml路径);Employee.hbm.xml(配置实体类和数据表字段的映射及一对一(默认),多对多,一对等映射关系)

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory>  <!-- Database connection settings -->  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  <property name="connection.url">jdbc:mysql://localhost:3306/companyinfoitem</property>  <property name="connection.username">root</property>  <property name="connection.password">root</property>  <!-- JDBC connection pool (use the built-in) -->  <property name="connection.pool_size">1</property>  <!-- SQL dialect -->  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  <!-- Enable Hibernate's automatic session context management -->  <property name="current_session_context_class">thread</property>  <!-- Disable the second-level cache -->  <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>  <!-- Echo all executed SQL to stdout -->  <property name="show_sql">false</property>  <!-- Drop and re-create the database schema on startup -->  <!--  <property name="hbm2ddl.auto">update</property>  -->  <mapping resource="Employee.hbm.xml"/> </session-factory></hibernate-configuration>

Employee.hbm.xml

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC  "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- 把定义的DAO和数据库中的表映射 --><hibernate-mapping package="com.spring.dao">   <class name="Employee" table="employee">      <id name="id" type="int" column="id">         <generator class="native"/>      </id>       <property name="isadmin" column="isadmin" type="int"/>      <property name="name" column="name" type="string"/>      <property name="password" column="password" type="string"/>      <property name="address" column="address" type="string"/>      <property name="telephone" column="telephone" type="string"/>      <property name="email" column="email" type="string"/>   </class></hibernate-mapping>

整个工程目录:
这里写图片描述
对实体类的操作:

package com.spring.dao;import java.util.ArrayList;import java.util.List;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;public class EmployeeControl {public static SessionFactory factory;    /*     * 登录     */    public boolean loginAccount(String name, String password)    {          boolean loginFlag=false;          Session session = factory.openSession();          List<Employee> employees =new ArrayList<Employee>();            Transaction tx = null;              try{                 tx = session.beginTransaction();            String hql = "FROM Employee WHERE name = ? AND password = ?";             employees = session.createQuery(hql).setString(0, name).setString(1, password).list();             if(!employees.isEmpty())             {                 loginFlag=true;             }              tx.commit();              }catch (HibernateException e) {                 if (tx!=null) tx.rollback();                 e.printStackTrace();               }finally {                 session.close();               }        return loginFlag;    }      /*       * 添加       */       public void addEmployee(int isadmin,String name, String password, String address,String telephone,String email){          Session session = factory.openSession();          Transaction tx = null;          try{             tx = session.beginTransaction();             Employee employee = new Employee(isadmin,name,password,address,telephone,email);           int  employeeID = (int) session.save(employee);              tx.commit();          }catch (HibernateException e) {             e.printStackTrace();           }finally {             session.close();           }       }       /*        * 根据id查找        */       public Employee findEmployeeById(int EmployeeID )       {           Session session = factory.openSession();           Employee employee = null;              Transaction tx = null;              try{                 tx = session.beginTransaction();                employee =                            (Employee)session.get(Employee.class, EmployeeID);                  tx.commit();              }catch (HibernateException e) {                 if (tx!=null) tx.rollback();                 e.printStackTrace();               }finally {                 session.close();               }           return employee;       }        /*         * 查找所有         */       public List<Employee> findEmployeeByHQL( ){           List<Employee> employees =new ArrayList<Employee>();        Session session = factory.openSession();        Transaction tx = null;          try{             tx = session.beginTransaction();        employees = session.createQuery("FROM Employee").list();              tx.commit();          }catch (HibernateException e) {             if (tx!=null) tx.rollback();             e.printStackTrace();           }finally {             session.close();           }          return employees;       }        /*         * 根据id修改         */       public void updateEmployee(int EmployeeID, String password ){          Session session = factory.openSession();          Transaction tx = null;          try{             tx = session.beginTransaction();             Employee employee =                         (Employee)session.get(Employee.class, EmployeeID);              employee.setPassword(password);               session.update(employee);              tx.commit();      }catch (HibernateException e) {             if (tx!=null) tx.rollback();             e.printStackTrace();           }finally {             session.close();             }       }       /*        * 根据id删除        */       public void deleteEmployee(int EmployeeID){          Session session = factory.openSession();          Transaction tx = null;          try{             tx = session.beginTransaction();             Employee employee =                        (Employee)session.get(Employee.class, EmployeeID);              session.delete(employee);              tx.commit();          }catch (HibernateException e) {             if (tx!=null) tx.rollback();             e.printStackTrace();           }finally {             session.close();           }    }}

2.测试

实体类实现了对数据表的:按id查询,按用户名和密码查询,增删查改。

​ 测试结果:
这里写图片描述

这里写图片描述

这里写图片描述

原创粉丝点击