hibernate no session 原理_P210课后 实验六:hibernate的应用

实验六:hibernate的应用

一.内容:在数据库里建立表格,制作一个登陆页面,输入账号和密码,如果匹配,则显示“登录成功”,否则显示“登录失败”,用hibernate框架来实现

  1. 目的:掌握hibernate框架的原理和基本使用方法
  2. 这里使用hibernate + struts2框架(既是对struts2框架的复习,,又是hibernate框架掌握)(本实验使用MySQL数据库)

f17df192afb354184ea90a1060b4e3e5.png

a50b0ceb062cdefd71b8cbe065a4b18d.png

d329e3cad6a6a21c1f1e92f6b8fae275.png

(一)Struts2框架 相关

  1. 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";

}

}

}

  1. 配置文件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>

  1. 相关的页面 (表示层)

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>

  1. 配置过滤器 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>

  1. 编写持久化类 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>

  1. 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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值