Java_Web三大框架之Hibernate+jsp+selvect+HQL注册用户
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java_Web三大框架之Hibernate+jsp+selvect+HQL注册用户,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8845字,纯文字阅读大概需要13分钟。
内容图文
![Java_Web三大框架之Hibernate+jsp+selvect+HQL注册用户](/upload/InfoBanner/zyjiaocheng/1142/2a286964ed734790a6c57dca2d04d0f5.jpg)
Hibernate比SQL语句简单多了,代码冗余少,切方便简洁明了。下面用Hibernate+jsp+selvect+HQL来实现注册用户。
第一步:编写用户实体类和Users2.hbm.xml映射。
/* * 用户实体类 */ public class Users2 { private int id;//用户idprivate String name;//用户昵称private String password;//用户密码private String telephone;//用户号码private String username;//用户姓名private String isadmin;//是否是管理员 //省略get和set方法 }
<? 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 package ="entity" > < class name ="Users2" table ="Users2" > < id name ="id" > < generator class ="increment" /> </ id > < property name ="name" /> < property name ="password" /> < property name ="telephone" /> < property name ="username" /> < property name ="isadmin" /> </ class > </ hibernate-mapping >
第二步:配置hibernate.cfg.xml数据库映射
<! DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" > < hibernate-configuration > < session-factory name ="foo" > <!-- 数据库方言 --> < property name ="dialect" > org.hibernate.dialect.OracleDialect </ property > <!-- 连接数据库Url --> < property name ="hibernate.connection.url" > jdbc:oracle:thin:@localhost:1521:orcl </ property > <!-- 连接驱动 --> < property name ="hibernate.connection.driver_class" > oracle.jdbc.driver.OracleDriver </ property > <!-- 用户名 --> < property name ="hibernate.connection.username" >epet</property><!-- 密码 --><property name="hibernate.connection.password">123456</property><!-- 在控制台打印sql信息 --><property name="show_sql">true</property><!-- 创建表结构 --><property name="hibernate.hbm2ddl.auto">update</property><!-- 配置映射信息 --><mapping resource="entity/Users2.hbm.xml"/></session-factory></hibernate-configuration>
第三步:编写HibernateUtil类,dao层和daoImpl层
package com.msit.hibernate.HibernateUtil; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private HibernateUtil(){ }; public static SessionFactory SessionFactory = null; static{ //hibernate Configuration cf = new Configuration(); cf.configure(); SessionFactory = cf.buildSessionFactory();//DriverManager.getconnection() //Session session = SessionFactory.openSession(); } publicstatic Session getSession(){ return SessionFactory.openSession(); } //关闭session publicstaticvoid closeSession(Session session){ if(session!=null){ session.clear(); } } }
public interface UserDao { /* * 用户注册 */ public int addUserList(Users2 user); }
package Dao.Impl; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import com.msit.hibernate.HibernateUtil.HibernateUtil; import entity.Users2; import Dao.UserDao; public class UserDaoImpl implements UserDao{ /* * 注册用户 * (non-Javadoc) * @see Dao.UserDao#addUserList(entity.Users2) */publicint addUserList(Users2 user) { Session session = HibernateUtil.getSession(); Transaction Transaction=session.beginTransaction(); try{ //对数据保存至数据库 session.save(user); //提交事务 Transaction.commit(); return 1; }catch(Exception e){ e.printStackTrace(); //如果出现异常,事务回滚 Transaction.rollback(); return 0; }finally{ HibernateUtil.closeSession(session); } } }
创建selvect(addUser)和配置web.xml
package selvect; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import entity.Users2; import Biz.Users2Biz; import Biz.Impl.Users2BizImpl; public class addUser extends HttpServlet { /** * Constructor of the object. */public addUser() { super(); } /** * Destruction of the servlet. <br> */publicvoid destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */publicvoid doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */publicvoid doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Users2 user=new Users2(); request.setCharacterEncoding("utf-8"); user.setName(request.getParameter("name")); user.setPassword(request.getParameter("password")); user.setTelephone(request.getParameter("telephone")); user.setUsername(request.getParameter("username")); Users2Biz biz=new Users2BizImpl(); int i=biz.addUserList(user); if(i>0){ response.sendRedirect("index.jsp"); }else{ response.sendRedirect("login.jsp"); System.out.print("cuol"); } } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */publicvoid init() throws ServletException { // Put your code here } }
<? xml version="1.0" encoding="UTF-8" ?> < web-app version ="2.5" xmlns ="http://java.sun.com/xml/ns/javaee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" > <!-- 注册用户 --> < servlet > < servlet-name >addUser</servlet-name><servlet-class>selvect.addUser</servlet-class></servlet><!-- 映射servlet --><servlet-mapping><servlet-name>addUser</servlet-name><url-pattern>/addUser</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
jsp页面:
<% @ page language = " java " import = " java.util.* " pageEncoding = " UTF-8 " %> <% String path = request.getContextPath(); String basePath = request.getScheme() + " :// " + request.getServerName() + " : " + request.getServerPort() + path + " / " ; %> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > < html > < head > < base href ="<%=basePath%>" > < title >My JSP ‘regs.jsp‘ starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!-- <link rel="stylesheet" type="text/css" href="styles.css"> --><LINK rel=stylesheet type=text/css href="css/style.css"></head><BODY><DIV id=header class=wrap><DIV id=logo><IMG src="images/logo.jpg" width="197px;" height="74px;"></DIV></DIV><DIV id=regLogin class=wrap><DIV class=dialog><DL class=clearfix><DT>新用户注册</DT><DD class=past>填写个人信息</DD></DL><DIV class=box><FORM action="addUser" method="post"><DIV class=infos><TABLE class=field><TBODY><TR><TD class=field>用 户 名:</TD><TD><INPUT class=text type=text name="name"></TD></TR><TR><TD class=field>密 码:</TD><TD><INPUT class=text type=password name="password"></TD></TR><TR><TD class=field>确认密码:</TD><TD><INPUT class=text type=password name="repassword"></TD></TR><TR><TD class=field>电 话:</TD><TD><INPUT class=text type=text name="telephone"></TD></TR><TR><TD class=field>用户姓名:</TD><TD><INPUT class=text type=text name="username"></TD></TR></TBODY></TABLE><DIV class=buttons><INPUT value=立即注册 type="submit"></DIV></DIV></FORM></DIV></DIV></DIV><DIV id=footer class=wrap><DL><DT>赶集网 ? 2010 赶集网 京ICP证1000001号</DT><DD>关于我们 · 联系方式 · 意见反馈 · 帮助中心</DD></DL></DIV></BODY></HTML>
原文:http://www.cnblogs.com/wlx520/p/4692993.html
内容总结
以上是互联网集市为您收集整理的Java_Web三大框架之Hibernate+jsp+selvect+HQL注册用户全部内容,希望文章能够帮你解决Java_Web三大框架之Hibernate+jsp+selvect+HQL注册用户所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。