JavaWeb_(Hibernate框架)使用Hibernate开发用户注册功能
使用Hibernate开发用户注册功能:
用户在register.jsp表单成功后,页面跳转到login.html,数据库中会存放用户注册的信息
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" type="text/css" href="css/login.css" /> <link rel="stylesheet" href="css/head.css" /> <link rel="stylesheet" type="text/css" href="css/login.css" /> </head> <body> <div class="dvhead"> <div class="dvlogo"><a href="index.html">你问我答</a></div> <div class="dvsearch">10秒钟注册账号,找到你的同学</div> <div class="dvreg"> 已有账号,立即 <a href="login.html">登录</a> </div> </div> <section class="sec"> <form action="${pageContext.request.contextPath }/UserAction_register" method="post"> <div class="register-box"> <label for="username" class="username_label"> 用 户 名 <input maxlength="20" name="username" type="text" placeholder="您的用户名和登录名" /> </label> <div class="tips"> </div> </div> <div class="register-box"> <label for="username" class="other_label"> 设 置 密 码 <input maxlength="20" type="password" name="password" placeholder="建议至少使用两种字符组合" /> </label> <div class="tips"> </div> </div> <div class="register-box"> <label for="username" class="other_label"> 确 认 密 码 <input maxlength="20" type="password" placeholder="请再次输入密码" /> </label> <div class="tips"> </div> </div> <div class="register-box"> <label for="username" class="username_label"> 真实姓名 <input maxlength="20" name="name" type="text" placeholder="您的真实姓名" /> </label> <div class="tips"> </div> </div> <div class="register-box"> <label for="username" class="username_label"> 邮箱 <input maxlength="20" name="email" type="text" placeholder="您的邮箱" /> </label> <div class="tips"> </div> </div> <div class="register-box"> <label for="username" class="username_label"> 手机号 <input maxlength="20" name="telephone" type="text" placeholder="您的手机号" /> </label> <div class="tips"> </div> </div> <div class="arguement"> <input type="checkbox" id="xieyi" /> 阅读并同意 <a href="javascript:void(0)">《错题用户注册协议》</a> <a href="login.html">已有账号,立即登录</a> <div class="tips"> </div> </div> <div class="submit_btn"> <button type="submit" id="submit_btn"> 立 即 注 册 </button> </div> </form> </section> <script src="js/index.js" type="text/javascript" charset="utf-8"></script> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>HibernateForum</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<?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> <!-- 负责初始化hibernate --> <session-factory> <!-- 连接数据库驱动 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 数据库地址 --> <property name="hibernate.connection.url">jdbc:mysql:///hibernatest</property> <!-- 数据库用户名 --> <property name="hibernate.connection.username">root</property> <!-- 数据库密码 --> <property name="hibernate.connection.password">123456</property> <!-- 配置数据库的方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <mapping resource= "com/Gary/domain/User.hbm.xml"/> </session-factory> </hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <package name="hibernate" namespace="/" extends="struts-default"> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="UserAction_*" class="com.Gary.web.UserAction" method="{1}"> <result name="toLogin" type="redirect">/login.html</result> </action> </package> </struts>
package com.Gary.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.Gary.domain.User; public class UserDao { public void addUser(User user) { //使用Hibernate //得到配置信息 Configuration config = new Configuration().configure(); //创建sessionFactiory对象 SessionFactory sessionFactory = config.buildSessionFactory(); //获取session Session session = sessionFactory.openSession(); //打开事务 Transaction transaction = session.beginTransaction(); //存储User对象 session.save(user); //提交事务 transaction.commit(); //关闭资源 session.close(); } }
package com.Gary.domain; public class User { private String id; private String username; private String password; private String name; private String email; private String telephone; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.Gary.domain.User" table="user"> <!-- id元素 name:实体中的属性 colum(可选):数据库的列名 type(可选): 填写列(属性)的类型.hibernate会自动检测实体的属性类型. 每个类型有三种填法: java类型|hibernate类型|数据库类型 length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度 --> <id name="id" column="id"> <!-- 主键生成策略(手动生成) (最后讲) 5种 identity:主键自增 sequence:oracle中主键生成的策略 native:identity+sequence (hibernate会根据连接的数据库自动选择(identity,sequence)) uuid:产生随机字符串作为主键,主键必须为String assigned:我们要手动去指定 --> <generator class="assigned"></generator> </id> <!-- property:除了id之外的普通属性 name:实体中的属性 colum(可选):数据库的列名 type(可选): 填写列(属性)的类型.hibernate会自动检测实体的属性类型. 每个类型有三种填法: java类型|hibernate类型|数据库类型 length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度 not-null(可选):配置该属性(列)是否不能为空. 默认值:false --> <property name="username" column="username"></property> <property name="password" column="password"></property> <property name="name" column="name"></property> <property name="email" column="email"></property> <property name="telephone" column="telephone"></property> </class> </hibernate-mapping>
package com.Gary.service; import com.Gary.dao.UserDao; import com.Gary.domain.User; public class UserService { public void addUser(User user) { UserDao userDao = new UserDao(); userDao.addUser(user); } }
package com.Gary.web; import java.util.UUID; import com.Gary.domain.User; import com.Gary.service.UserService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class UserAction extends ActionSupport implements ModelDriven<User>{ public User user = new User(); public String register() throws Exception { user.setId(UUID.randomUUID().toString()); UserService userService = new UserService(); System.out.println(user); userService.addUser(user); return "toLogin"; } @Override public User getModel() { // TODO Auto-generated method stub return user; } }
数据库表hibernatest.user
【项目结构】
注册表单页面register.jsp
<form action="${pageContext.request.contextPath }/UserAction_register" method="post"> <div class="register-box"> <label for="username" class="username_label"> 用 户 名 <input maxlength="20" name="username" type="text" placeholder="您的用户名和登录名" /> </label> <div class="tips"> </div> </div> <div class="register-box"> <label for="username" class="other_label"> 设 置 密 码 <input maxlength="20" type="password" name="password" placeholder="建议至少使用两种字符组合" /> </label> <div class="tips"> </div> </div> <div class="register-box"> <label for="username" class="other_label"> 确 认 密 码 <input maxlength="20" type="password" placeholder="请再次输入密码" /> </label> <div class="tips"> </div> </div> <div class="register-box"> <label for="username" class="username_label"> 真实姓名 <input maxlength="20" name="name" type="text" placeholder="您的真实姓名" /> </label> <div class="tips"> </div> </div> <div class="register-box"> <label for="username" class="username_label"> 邮箱 <input maxlength="20" name="email" type="text" placeholder="您的邮箱" /> </label> <div class="tips"> </div> </div> <div class="register-box"> <label for="username" class="username_label"> 手机号 <input maxlength="20" name="telephone" type="text" placeholder="您的手机号" /> </label> <div class="tips"> </div> </div> <div class="arguement"> <input type="checkbox" id="xieyi" /> 阅读并同意 <a href="javascript:void(0)">《错题用户注册协议》</a> <a href="login.html">已有账号,立即登录</a> <div class="tips"> </div> </div> <div class="submit_btn"> <button type="submit" id="submit_btn"> 立 即 注 册 </button> </div> </form>
用户实体层
private String id; private String username; private String password; private String name; private String email; private String telephone;
用户提交表单,发送请求UserAction_register到Web层userService.java
public String register() throws Exception { user.setId(UUID.randomUUID().toString()); UserService userService = new UserService(); System.out.println(user); userService.addUser(user); return "toLogin"; }
在domain层中的User.hbm.xml配置传传入数据库Hibernate的事务
<hibernate-mapping> <class name="com.Gary.domain.User" table="user"> <!-- id元素 name:实体中的属性 colum(可选):数据库的列名 type(可选): 填写列(属性)的类型.hibernate会自动检测实体的属性类型. 每个类型有三种填法: java类型|hibernate类型|数据库类型 length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度 --> <id name="id" column="id"> <!-- 主键生成策略(手动生成) (最后讲) 5种 identity:主键自增 sequence:oracle中主键生成的策略 native:identity+sequence (hibernate会根据连接的数据库自动选择(identity,sequence)) uuid:产生随机字符串作为主键,主键必须为String assigned:我们要手动去指定 --> <generator class="assigned"></generator> </id> <!-- property:除了id之外的普通属性 name:实体中的属性 colum(可选):数据库的列名 type(可选): 填写列(属性)的类型.hibernate会自动检测实体的属性类型. 每个类型有三种填法: java类型|hibernate类型|数据库类型 length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度 not-null(可选):配置该属性(列)是否不能为空. 默认值:false --> <property name="username" column="username"></property> <property name="password" column="password"></property> <property name="name" column="name"></property> <property name="email" column="email"></property> <property name="telephone" column="telephone"></property> </class> </hibernate-mapping>
Dao层UserDao.java向数据库传入用户信息
public void addUser(User user) { //使用Hibernate //得到配置信息 Configuration config = new Configuration().configure(); //创建sessionFactiory对象 SessionFactory sessionFactory = config.buildSessionFactory(); //获取session Session session = sessionFactory.openSession(); //打开事务 Transaction transaction = session.beginTransaction(); //存储User对象 session.save(user); //提交事务 transaction.commit(); //关闭资源 session.close(); }
(如需转载学习,请标明出处)