Hibernate
是一个开放源代码的
对象关系映射框架
,
它
对
JDBC
进行了轻量级的对象封装
。
它
不仅提供了
从
Java
类到数据表之间的映射
,也提供了
查询和事务机制
。
采用
ORM
映射机制
,实现
Java
对象和关系数据库之间的映射,把
sql
语句传给数据库,并且把数据库返回的结果封装成
对象。
利用struts+hibernate实现注册和登录功能案例:
工程框架结构:
1.pom.xml中引入依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sdau.hibernate03</groupId>
<artifactId>hibernate04</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>6.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.12.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.6.12.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
</dependencies>
</project>
2.实体类创建在model包下 UserInf.java
package model;
public class UserInf {
private String userid;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
private String userpwd;
private String realname;
}
3.Users.hbm.xml:
将实体类中的属性与数据库中的列名对应,形成映射
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<!--其中model.Users是po类的全路径名 table指数据库名-->
<class name="model.UserInf" table="test">
<!--其中id是数据库表主键名-->
<id name="userid" column="userid">
</id>
<!--property代表其它字段映射-->
<property name="userpwd" column="userpwd"/>
<property name="realname" column="realname"/>
</class>
</hibernate-mapping>
4.核心配置文件:hibernate.cfg.xml
连接数据库必备,指明第三步的映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory>
<!-- 四要素 -->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- c3p0数据库连接池 -->
<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
<!-- 方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<!--注册当前session上下文:保证同一线程中Session是同一个session -->
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping resource="Users.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5.Dao包下的UserInfDao.java:
写添加和查询的方法:void add(UserInf ui); int selectByIdPwd(UserInf ui);
package dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.junit.Test;
import model.UserInf;
public class UserInfDao {
@Test
public void add(UserInf ui)
{
//加载核心配置文件
Configuration config=new Configuration().configure();
SessionFactory sessionFactory=config.buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
try
{
session.beginTransaction();
session.save(ui);
session.getTransaction().commit();
}
catch(Exception ex)
{
session.getTransaction().rollback();
}
finally{
session.close();
sessionFactory.close();
}
}
@Test
public int selectByIdPwd(UserInf ui)
{
Configuration config=new Configuration().configure();
SessionFactory sessionFactory=config.buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
int n=0;
try
{
session.beginTransaction();
String sql="select * from test where userid=? and userpwd=?";
NativeQuery<UserInf> query=session.createNativeQuery(sql,UserInf.class);
query.setParameter(1, ui.getUserid());
query.setParameter(2,ui.getUserpwd());
List<UserInf> list=query.getResultList();
if(!list.isEmpty()) n=1;
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
session.close();
sessionFactory.close();
}
return n;
}
}
6.Action包下的LoginAction.java:
作用是:通过调用方法的成功与否,使其返回值与struts.xml中的返回值对应,显示相应的jsp页面。
package action;
import dao.UserInfDao;
import model.UserInf;
public class LoginAction {
private UserInf ui;
public UserInf getUi() {
return ui;
}
public void setUi(UserInf ui) {
this.ui = ui;
}
public String execute()
{
UserInfDao uid=new UserInfDao();
if(uid.selectByIdPwd(ui)==1)
return "success";
else
return "fail";
}
public String addexecute()
{
UserInfDao uid=new UserInfDao();
if((ui!=null)&&(uid.selectByIdPwd(ui)!=1))
{uid.add(ui);
return "success";}
if(uid.selectByIdPwd(ui)==1)
{return "exit";}
else return "fail";
}
}
7.struts过滤器:
连接jsp与java的桥梁。
method默认值为“execute”。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 6.0//EN"
"http://struts.apache.org/dtds/struts-6.0.dtd">
<struts>
<constant name="struts.i18n.reload" value="true"></constant>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.configuration.xml.reload" value="true"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="action.LoginAction">
<result name="success">login_success.jsp</result>
<result name="fail">login_failure.jsp</result>
</action>
<action name="register" class="action.LoginAction" method="addexecute">
<result name="success">register_success.jsp</result>
<result name="fail">register_failure.jsp</result>
<result name="exit">register_failure_user.jsp</result>
</action>
</package>
</struts>
8.前台jsp页面:
省略。
以上就是利用struts+hibernate实现注册和登录功能的全过程。
ssh框架是指:spring+struts+hibernate