实验六:hibernate的应用
一.内容:在数据库里建立表格,制作一个登陆页面,输入账号和密码,如果匹配,则显示“登录成功”,否则显示“登录失败”,用hibernate框架来实现
- 目的:掌握hibernate框架的原理和基本使用方法
- 这里使用hibernate + struts2框架(既是对struts2框架的复习,,又是hibernate框架掌握)(本实验使用MySQL数据库)
![f17df192afb354184ea90a1060b4e3e5.png](https://i-blog.csdnimg.cn/blog_migrate/66212a08890e9807fd4ee6d60ba1adde.jpeg)
![a50b0ceb062cdefd71b8cbe065a4b18d.png](https://i-blog.csdnimg.cn/blog_migrate/d28ba925b6a86cc739267c52dd45fae1.jpeg)
![d329e3cad6a6a21c1f1e92f6b8fae275.png](https://i-blog.csdnimg.cn/blog_migrate/0300cf8ac145ca0ae56d0262f94c5d85.jpeg)
(一)Struts2框架 相关
- Action包下面的 LoginAction类(负责业务逻辑层)
(/src/action/LoginAction.java)
package action;
import dao.CustomerDao;
import po.Customer;
public class LoginAction {
private String account;
private String password;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute(){
CustomerDao cdao=new CustomerDao();
Customer cus=cdao.getCustomerByAccount(account);
if(cus!=null&&cus.getPassword().equals(password)){
return "success";
}else{
return "fail";
}
}
}
- 配置文件struts.xml
(配置Action及其 相关的页面)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="login" class="action.LoginAction">
<result name="success">/success.jsp</result>
<result name="fail">/fail.jsp</result>
</action>
</package>
</struts>
- 相关的页面 (表示层)
login.jsp success.jsp fail.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action=h习题/login.action method="post">
请输入账号:<input name="account" type="text"><br>
请输入密码:<input name="password" type="password">
<input type="submit" value="提交">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
成功
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
失败
</body>
</html>
- 配置过滤器 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
(二)hibernate相关部分
1..配置文件 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">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/dbcustomer</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="po/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
- 编写持久化类 Custtomer.java
package po;
public class Customer {
private String account;
private String password;
private String cname;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
}
3.。映射文件Customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="po.Customer" table="T_CUSTOMER">
<id name="account" column="ACCOUNT">
<generator class="assigned" />
</id>
<property name="password" column="PASSWORD" />
<property name="cname" column="CNAME" />
</class>
</hibernate-mapping>
- dao层(对hibernate进行测试)
package dao;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import po.Customer;
public class CustomerDao {
public Customer getCustomerByAccount(String account){
Customer cus = null;
Session session = new Configuration().configure().buildSessionFactory().openSession();
cus = (Customer)session.get(Customer.class, account);
session.close();
return cus;
}
}