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查询,按用户名和密码查询,增删查改。
测试结果:
阅读全文
1 0
- java后台开发Hibernate例子--登录查询
- java后台开发servlet例子--登录
- java后台开发Maven例子--整合spring,spingmvc,hibernate
- Hibernate模糊查询后台打印乱码[java]
- java后台开发SpringMVC例子--图片上传下载
- Appcan与后台数据交互,登录例子
- Hibernate中HQL查询例子
- 【java】struts和ajax的小例子①→后台的登录验证
- spring+hibernate+freemarker+jQuery 登录注册例子
- Hibernate实现登录注册小例子
- Struts Hibernate Spring 后台数据查询
- Hibernate+boostrapTable分页查询(——后台)
- Java软件开发基础知识梳理之(7)------Hibernate查询数据
- Hibernate的createSQLQuery查询的小例子
- Hibernate的createSQLQuery查询的小例子
- Hibernate的createSQLQuery查询的小例子
- hibernate的hql模糊查询例子
- hibernate---HQL之引用查询小例子
- 538. Convert BST to Greater Tree
- nginx反向代理配置及优化
- 牛客剑指offer刷题笔记
- Android Telephony分析(五)--- TelephonyRegistry 详解
- javascript异步和单线程
- java后台开发Hibernate例子--登录查询
- hpu暑假训练 E: YK的书架【思维】
- 二分搜索--控制精度--hdu2199 Can you solve this equation
- 老司机手把手教你自己编译jdk
- WIN32 控件显示RGB 图像 / Mat 图像
- ubuntu rebar3 安装
- SSH框架的初步搭建
- js数组的迭代器方法(some、every、forEach、map、filter)
- Common sense for server