基于SSH论坛小型项目 传送门
用户和发帖进行举例
多对多关系:多个用户可以回复多个帖子
因此引入了一张回复表,用来保存用户id和帖子id
CREATE TABLE `hforum`.`answer` ( `id` VARCHAR(50) NOT NULL, `userid` VARCHAR(50) NULL DEFAULT NULL, `pasteid` VARCHAR(50) NULL DEFAULT NULL, `content` VARCHAR(255) NULL, PRIMARY KEY (`id`));
数据库中帖子数据(测试时候是没有的~)
用户在login.jsp中进行登陆,登陆成功后跳转index.jsp,访问add.jsp后,用户可以发起新帖,用户发帖后,再访问detail.jsp,可以对发布的帖子进行回复。(注意:发布帖子时有一个pasteid,要保证回复帖子时回复该pasteid)
用户回帖后,数据库中answer表存放的数据
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <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_login" 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" style="color: red"><s:property value="error"/> </div> </div> <div class="arguement"> <input type="checkbox" id="xieyi" /> 阅读并同意 <a href="javascript:void(0)">《你问我答用户注册协议》</a> <a href="register.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>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" type="text/css" href="css/index.css"> </head> <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> <div class="dvContent"> <div class="dvquesleft"> <div class="dvqstitle"> <image class="imgbean" src="images/bean.jpg"> <span class="qsTitle">问答</span> <span class="back"><ab href="">《《返回上一页</a></span> </div> <div class="dvtabhead"> <div class="tabheads tabcurrent">全部问题</div> <div class="tabheads">我的问题</div> <div class="tabheads">关注问题</div> <div class="tabheads">问题标签</div> </div> <div class="tabContent"> <div class="dvtags"> <a class="curenttag">待解决</a><span class="line"></span><a>高分</a><span class="line"></span><a>新回答</a><span class="line"></span><a>已解决</a> </div> <div class="tab"> <div class="dvques"> <div class="quesCount"> <div class="count">8</div> <div class="ques">回答数</div> </div> <div class="quesContent"> <div class="quesTitle"> 500 <image src="images/bean.jpg" class="bean"> <span class="spanques">Excel开发的问题</span> </div> <div class="qContent">以前都是OWC控件在程序中做Excel做操作以前都是OWC控件在程序中做Excel做操作,主要包裹以前裹做Excel做操作,主要包裹以前都是OWC控件在程序中做Excel做操作,主要包裹主要包裹以前都是主要包中做Excel做操作,主要包裹,主要包裹...</div> <div class="tags"> <span class="tag">excel</span><span class="tag">程序</span> </div> <div class="quesUser"> <image src="images/0.gif" class="imguser" /> <div class="userName"> 张大值 <div class="liulan">浏览(9) 30分钟前</div> </div> </div> </div> </div> <div class="dvques"> <div class="quesCount"> <div class="count">8</div> <div class="ques">回答数</div> </div> <div class="quesContent"> <div class="quesTitle"> 500 <image src="images/bean.jpg" class="bean"> <span class="spanques">Excel开发的问题</span> </div> <div class="qContent">以前都是OWC控件在程序中做Excel做操作,以前都是OWC控件在程序中做Excel做操作主要包裹以前裹做Excel做操作,主要包裹以前都是OWC控件在程序中做Excel做操作,主要包裹主要包裹以前都是主要包中做Excel做操作,主要包裹,主要包裹...</div> <div class="tags"> <span class="tag">excel</span><span class="tag">程序</span> </div> <div class="quesUser"> <image src="images/0.gif" class="imguser" /> <div class="userName"> 张大值 <div class="liulan">浏览(9) 30分钟前</div> </div> </div> </div> </div> <div class="dvques"> <div class="quesCount"> <div class="count">8</div> <div class="ques">回答数</div> </div> <div class="quesContent"> <div class="quesTitle"> 500 <image src="images/bean.jpg" class="bean"> <span class="spanques">Excel开发的问题</span> </div> <div class="qContent">以前都是OWC控件在程序中做Excel做操作,以前都是OWC控件在程序中做Excel做操作主要包裹以前裹做Excel做操作,主要包裹以前都是OWC控件在程序中做Excel做操作,主要包裹主要包裹以前都是主要包中做Excel做操作,主要包裹,主要包裹...</div> <div class="tags"> <span class="tag">excel</span><span class="tag">程序</span> </div> <div class="quesUser"> <image src="images/0.gif" class="imguser" /> <div class="userName"> 张大值 <div class="liulan">浏览(9) 30分钟前</div> </div> </div> </div> </div> <div class="dvques"> <div class="quesCount"> <div class="count">8</div> <div class="ques">回答数</div> </div> <div class="quesContent"> <div class="quesTitle"> 500 <image src="images/bean.jpg" class="bean"> <span class="spanques">Excel开发的问题</span> </div> <div class="qContent">以前都是OWC控件在程序中做Excel做操作,以前都是OWC控件在程序中做Excel做操作主要包裹以前裹做Excel做操作,主要包裹以前都是OWC控件在程序中做Excel做操作,主要包裹主要包裹以前都是主要包中做Excel做操作,主要包裹,主要包裹...</div> <div class="tags"> <span class="tag">excel</span><span class="tag">程序</span> </div> <div class="quesUser"> <image src="images/0.gif" class="imguser" /> <div class="userName"> 张大值 <div class="liulan">浏览(9) 30分钟前</div> </div> </div> </div> </div> <div class="dvques"> <div class="quesCount"> <div class="count">8</div> <div class="ques">回答数</div> </div> <div class="quesContent"> <div class="quesTitle"> 500 <image src="images/bean.jpg" class="bean"> <span class="spanques">Excel开发的问题</span> </div> <div class="qContent">以前都是OWC控件在程序中做Excel做操作,主要包裹以前裹做Excel做操作,主要包裹以前都是OWC控件在程序中做Excel做操作,主要包裹主要包裹以前都是主要包中做Excel做操作,主要包裹,主要包裹...</div> <div class="tags"> <span class="tag">excel</span><span class="tag">程序</span> </div> <div class="quesUser"> <image src="images/0.gif" class="imguser" /> <div class="userName"> 张大值 <div class="liulan">浏览(9) 30分钟前</div> </div> </div> </div> </div> </div> <div class="tab hidden">2</div> <div class="tab hidden">3</div> <div class="tab hidden">4</div> </div> </div> <div class="dvquesright"> <div> <buton class="btnques" onclick="location.href='add.jsp'">提个问题</buton> </div> <div class="dvorder"> <div class="orderTitle">专家排行榜</div> <div class="users"> <image class="userface" src="images/0.gif" /> <div class="dvuser"> <div class="userTitle">陈有龙</div> <div class="userdeital">大牛6级 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/1.gif" /> <div class="dvuser"> <div class="userTitle">陈有龙</div> <div class="userdeital">大牛6级 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/2.gif" /> <div class="dvuser"> <div class="userTitle">陈有龙</div> <div class="userdeital">大牛6级 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/3.gif" /> <div class="dvuser"> <div class="userTitle">陈有龙</div> <div class="userdeital">大牛6级 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/4.gif" /> <div class="dvuser"> <div class="userTitle">陈有龙</div> <div class="userdeital">大牛6级 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/5.gif" /> <div class="dvuser"> <div class="userTitle">陈有龙</div> <div class="userdeital">大牛6级 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/6.gif" /> <div class="dvuser"> <div class="userTitle">陈有龙</div> <div class="userdeital">大牛6级 豆:14006</div> </div> </div> </div> </div> </div> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function() { $(".tabheads").click(function() { $(".tabheads").removeClass("tabcurrent").eq($(this).index()).addClass("tabcurrent"); $(".tab").hide().eq($(this).index()).show(); }); }); </script> <body> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>问题详情</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <meta name="keywords" content="fly,layui,前端社区"> <meta name="description" content=""> <link rel="stylesheet" href="res/layui/css/layui.css"> <link rel="stylesheet" href="res/css/global.css"> <script src="res/layui/layui.js"></script> <script src="js/jquery.js"></script> <style type="text/css" rel="stylesheet"> form { margin: 0; } .editor { margin-top: 5px; margin-bottom: 5px; } </style> </head> <body> <iframe src="head.html" scrolling="no" width="100%" height="65px"></iframe> <div class="main layui-clear"> <div class="wrap"> <div class="content detail"> <div class="fly-panel detail-box"> <h1>帖子标题</h1> <div class="fly-tip fly-detail-hint" data-id=""> <span class="fly-tip-stick">置顶帖</span><span class="jie-admin"> <a href="">点击置顶</a> </span> <span class="layui-btn layui-btn-mini jie-admin"> <a href="">取消置顶</a> </span> <span class="jie-admin" type="del" style="margin-left: 20px;"> <a>删除该帖</a> </span> </span> <div class="fly-list-hint"> <i class="iconfont" title="回答"></i> 2 </div> </div> <div class="detail-about"> <a class="jie-user" href=""> <img src="res/images/uer.jpg" alt="头像"> <cite> 压缩 <em>2017-05-01发布</em> </cite> </a> <div class="detail-hits" data-id="{{rows.id}}"> <span class="layui-btn layui-btn-mini jie-admin"><a href="#">已完帖,无法编辑</a> </span> <span class="layui-btn layui-btn-mini jie-admin" type="collect" data-type="add"> <a id="collectPost">收藏</a> </span> <span class="layui-btn layui-btn-mini jie-admin layui-btn-danger" type="collect" data-type="add"> <a>取消收藏</a> </span> </div> </div> <div class="detail-body photos" style="margin-bottom: 20px;"> <p>帖子内容</p> </div> </div> <div class="fly-panel detail-box" style="padding-top: 0;"> <a name="comment"></a> <ul class="jieda photos" id="jieda"> <li data-id="12" class="jieda-daan"><a name="item-121212121212"></a> <div class="detail-about detail-about-reply"> <a class="jie-user" href=""> <img src="res/images/uer.jpg" alt=""> <cite> <i>纸飞机</i> <!-- <em>(楼主)</em> <em style="color:#5FB878">(管理员)</em> --> </cite> </a> <div class="detail-hits"> <span>3分钟前</span> </div> <i class="iconfont icon-caina" title="最佳答案"></i> </div> <div class="detail-body jieda-body"> <p>么么哒</p> </div> <div class="jieda-reply"> <span class="jieda-zan zanok" type="zan"><i class="iconfont icon-zan"></i><em>12</em> </span> <!-- <div class="jieda-admin"> <span type="del">删除</span> <span class="jieda-accept" type="accept">采纳</span> </div> --> </div></li> <li data-id="13"><a name="item-121212121212"></a> <div class="detail-about detail-about-reply"> <a class="jie-user" href=""> <img src="res/images/uer.jpg" alt=""> <cite> <i>香菇</i> <em style="color: #FF9E3F">活雷锋</em> </cite> </a> <div class="detail-hits"> <span>刚刚</span> </div> </div> <div class="detail-body jieda-body"> <p>蓝瘦</p> </div> <div class="jieda-reply"> <span class="jieda-zan" type="zan"><i class="iconfont icon-zan"></i><em>0</em> </span> <div class="jieda-admin"> <span type="del"><a href="#" class="layui-btn layui-btn-danger layui-btn-small">删除</a></span> <span class="jieda-accept" type="accept"> <a href="#" class="layui-btn layui-btn-small">采纳</a></span> </div> </div></li> <!-- <li class="fly-none">没有任何回答</li> --> </ul> <span id="toName">@ 压缩(楼主)</span> <div class="layui-form layui-form-pane"> <form action="${pageContext.request.contextPath}/AnswerAction_addAnswer"> <input type="hidden" name="pasteid" value="8a8486706eb0ad4d016eb0ae20b70000"/> <div class="layui-form-item layui-form-text"> <div class="layui-input-block"> <div class="editor"> <textarea id="content" name="content" style=" 690px; height: 450px; visibility: hidden;"></textarea> </div> </div> </div> <div> <button class="layui-btn" lay-filter="*" lay-submit>提交回答</button> </div> </form> </div> </div> </div> </div> <div class="edge"> <dl class="fly-panel fly-list-one"> <dt class="fly-panel-title">最近热帖</dt> <dd> <a href="">使用 layui 秒搭后台大布局(基本结构)</a> <span><i class="iconfont"></i> 6087</span> </dd> <dd> <a href="">Java实现LayIM后端的核心代码</a> <span><i class="iconfont"></i> 767</span> </dd> <dd> <a href="">使用 layui 秒搭后台大布局(基本结构)</a> <span><i class="iconfont"></i> 6087</span> </dd> <dd> <a href="">Java实现LayIM后端的核心代码</a> <span><i class="iconfont"></i> 767</span> </dd> <dd> <a href="">使用 layui 秒搭后台大布局(基本结构)</a> <span><i class="iconfont"></i> 6087</span> </dd> <dd> <a href="">Java实现LayIM后端的核心代码</a> <span><i class="iconfont"></i> 767</span> </dd> <dd> <a href="">使用 layui 秒搭后台大布局(基本结构)</a> <span><i class="iconfont"></i> 6087</span> </dd> <dd> <a href="">Java实现LayIM后端的核心代码</a> <span><i class="iconfont"></i> 767</span> </dd> </dl> <dl class="fly-panel fly-list-one"> <dt class="fly-panel-title">近期热议</dt> <dd> <a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span> </dd> <dd> <a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span> </dd> <dd> <a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span> </dd> <dd> <a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span> </dd> <dd> <a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span> </dd> <dd> <a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span> </dd> <dd> <a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span> </dd> <dd> <a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span> </dd> </dl> </div> </div> <script type="text/javascript" charset="utf-8" src="js/kindeditor.js"></script> <script type="text/javascript"> KE.show({ id : 'content', resizeMode : 1, cssPath : './index.css', items : [ 'fontname', 'fontsize', 'textcolor', 'bgcolor', 'bold', 'italic', 'underline', 'removeformat', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', 'emoticons', 'image', 'link' ] }); </script> <script> layui.cache.page = ''; layui.cache.user = { username : '游客', uid : -1, avatar : '../res/images/avatar/00.jpg', experience : 83, sex : '男' }; layui.config({ version : "2.0.0", base : '../res/mods/' }).extend({ fly : 'index' }).use('fly'); </script> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>发表问题</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <link rel="stylesheet" href="css/head.css" /> <link rel="stylesheet" href="layui/css/layui.css"> <link rel="stylesheet" href="css/global.css"> <script src="layui/layui.js"></script> </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> <div class="main layui-clear"> <div class="fly-panel" pad20> <h2 class="page-title">发表问题</h2> <!-- <div class="fly-none">并无权限</div> --> <div class="layui-form layui-form-pane"> <form action="${pageContext.request.contextPath }/PasteAction_addPaste "> <div class="layui-form-item"> <label for="L_title" class="layui-form-label">标题</label> <div class="layui-input-block"> <input type="text" id="L_title" name="title" required lay-verify="required" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item layui-form-text"> <div class="layui-input-block"> <div class="editor"> <textarea id="content" name="content" style=" 1040px; height: 450px; visibility: hidden;"></textarea> </div> </div> <label for="L_content" class="layui-form-label" style="top: -2px;">描述</label> </div> <div class="layui-form-item"> <label for="L_title" class="layui-form-label">悬赏</label> <div class="layui-input-block"> <input type="number" name="offer" required lay-verify="required" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <button class="layui-btn" lay-filter="*" lay-submit>立即发布</button> </div> </form> </div> </div> </div> <script type="text/javascript" charset="utf-8" src="js/kindeditor.js"></script> <script type="text/javascript"> KE.show({ id : 'content', resizeMode : 1, cssPath : './index.css', items : [ 'fontname', 'fontsize', 'textcolor', 'bgcolor', 'bold', 'italic', 'underline', 'removeformat', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', 'emoticons', 'image', 'link' ] }); </script> <script> layui.cache.page = ''; layui.cache.user = { username : '游客', uid : -1, avatar : '../res/images/avatar/00.jpg', experience : 83, sex : '男' }; layui.config({ version : "2.0.0", base : '../res/mods/' }).extend({ fly : 'index' }).use('fly'); </script> </body> </html>
<?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> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <package name="hibernateTest" namespace="/" extends="struts-default"> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="UserAction_*" class="com.Gary.web.UserAction" method="{1}"> <result name="toIndex" type="redirect">/index.jsp</result> <result name = "login">/login.jsp</result> </action> <action name="PasteAction_*" class="com.Gary.web.PasteAction" method="{1}"> <result name="toIndex" type="redirect">/index.jsp</result> </action> <action name="AnswerAction_*" class="com.Gary.web.AnswerAction" method="{1}"> <result name="success">/index.jsp</result> </action> </package> </struts>
<?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> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hforum</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <!-- 将hibernate生成的sql语句打印到控制台 --> <property name="hibernate.show_sql"></property> <!-- 格式化hibernate生成的sql语句 --> <property name="hibernate.format_sql"></property> <!-- 配置hibernate自动创建表--> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置数据库的隔离级别--> <property name="hibernate.connection.isolation">4</property> <!-- 配置事务 (session与当前线程绑定) --> <property name="hibernate.current_session_context_class">thread</property> <mapping resource="com/Gary/domain/User.hbm.xml"/> <mapping resource="com/Gary/domain/Paste.hbm.xml"/> <mapping resource="com/Gary/domain/Answer.hbm.xml"/> </session-factory> </hibernate-configuration>
com.Gary.dao
package com.Gary.dao; import org.hibernate.Session; import com.Gary.domain.Answer; import com.Gary.utils.HibernateUtils; public class AnswerDao { public void addAnswer(Answer answer) { Session session = HibernateUtils.getCurrentSession(); session.save(answer); } }
package com.Gary.dao; import org.hibernate.Session; import com.Gary.domain.Paste; import com.Gary.utils.HibernateUtils; public class PasteDao { public void addPaste(Paste paste) { Session session = HibernateUtils.getCurrentSession(); session.save(paste); } public Paste findPasteById(String pasteid) { Session session = HibernateUtils.getCurrentSession(); return session.get(Paste.class, pasteid); } }
package com.Gary.dao; import org.hibernate.Session; import org.hibernate.query.NativeQuery; import com.Gary.domain.User; import com.Gary.utils.HibernateUtils; public class UserDao { //原生SQL查询 public User findUesr(User user) { Session session = HibernateUtils.getCurrentSession(); String sql = "select * from user where username = ? and password = ?"; NativeQuery query = session.createSQLQuery(sql); query.setParameter(1, user.getUsername()); query.setParameter(2, user.getPassword()); query.addEntity(User.class); User temp = (User) query.uniqueResult(); return temp; } }
com.Gary.domain
package com.Gary.domain; public class Answer { private String id; private String content; private User user; private Paste paste; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Paste getPaste() { return paste; } public void setPaste(Paste paste) { this.paste = paste; } }
package com.Gary.domain; import java.util.HashSet; import java.util.Set; public class Paste { private String id; private String title; private String content; private Integer offer; private Integer ansnum; private Integer glanceover; private String createtime; //一个帖子属于一个用户 private User user; private Set<User> userAnswerSet = new HashSet<User>(); public Set<User> getUserAnswerSet() { return userAnswerSet; } public void setUserAnswerSet(Set<User> userAnswerSet) { this.userAnswerSet = userAnswerSet; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Integer getOffer() { return offer; } public void setOffer(Integer offer) { this.offer = offer; } public Integer getAnsnum() { return ansnum; } public void setAnsnum(Integer ansnum) { this.ansnum = ansnum; } public Integer getGlanceover() { return glanceover; } public void setGlanceover(Integer glanceover) { this.glanceover = glanceover; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } }
package com.Gary.domain; import java.util.HashSet; import java.util.Set; public class User { private String id;; private String username; private String password; private String name; private String email; private String telephone; //一对多 private Set<Paste> pasteSet = new HashSet<Paste>(); private Set<Paste> answerPasteSet = new HashSet<Paste>(); public Set<Paste> getAnswerPasteSet() { return answerPasteSet; } public void setAnswerPasteSet(Set<Paste> answerPasteSet) { this.answerPasteSet = answerPasteSet; } public Set<Paste> getPasteSet() { return pasteSet; } public void setPasteSet(Set<Paste> pasteSet) { this.pasteSet = pasteSet; } 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 package="com.Gary.domain"> <class name ="Answer" table="answer"> <id name="id"> <generator class="uuid"></generator> </id> <property name="content" column="content"></property> <many-to-one name="user" column="userid" class="User"></many-to-one> <many-to-one name="paste" column="pasteid" class="Paste"></many-to-one> </class> </hibernate-mapping>
<?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 package="com.Gary.domain"> <class name = "Paste" table="paste"> <id name="id"> <generator class="uuid"></generator> </id> <property name="title" column="title"></property> <property name="content" column="content"></property> <property name="offer" column="offer"></property> <property name="ansnum" column="ansnum"></property> <property name="glanceover" column="glanceover"></property> <property name="createtime" column="createtime"></property> <!-- name:引用属性名 class:与他关系的对象的完整类名 column:外键列名 --> <!-- inverse:配置关系是否不维护 true: 不维护 false: 维护关系 insert属性: 性能优化: 无论怎么放弃维护,总有一方需要维护(按照默认值来就行) 一般的开发中,一的一方放弃维护,多的一方不放弃维护 --> <many-to-one name="user" class="User" column="userid" insert="true"></many-to-one> <!-- 多对多关系 转换为两个一对多 --> <set name="userAnswerSet"> <!-- 指定关联的外键的列名 --> <key column="userid"></key> <!-- 指定关联的外键的实体类名 --> <one-to-many class="Answer"/> </set> </class> </hibernate-mapping>
<?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 package="com.Gary.domain"> <class name="User" table="user"> <id name = "id"> <generator class="uuid"></generator> </id> <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> <!-- name:集合属性名字 column:外键列名 class:与他相关的对象的完整类名 --> <!-- cascade:级联操作 save-update:级联保存,级联更新 delete:级联删除 all --> <!-- inverse:配置关系是否不维护 true: 不维护 false: 维护关系 --> <set name="pasteSet"> <key column="userid"></key> <one-to-many class="Paste"/> </set> <set name="answerPasteSet"> <key column="pasteid"></key> <one-to-many class="Answer"/> </set> </class> </hibernate-mapping>
com.Gary.service
package com.Gary.service; import org.hibernate.Transaction; import com.Gary.dao.AnswerDao; import com.Gary.domain.Answer; import com.Gary.utils.HibernateUtils; public class AnswerService { public void addAnswer(Answer answer) { AnswerDao answerDao= new AnswerDao(); Transaction beginTransaction = HibernateUtils.getCurrentSession().beginTransaction(); try { answerDao.addAnswer(answer); }catch(Exception e) { beginTransaction.rollback(); } beginTransaction.commit(); } }
package com.Gary.service; import org.hibernate.Transaction; import com.Gary.dao.PasteDao; import com.Gary.domain.Paste; import com.Gary.utils.HibernateUtils; public class PasteService { public void addPaste(Paste paste) { PasteDao pasteDao = new PasteDao(); Transaction beginTransaction = HibernateUtils.getCurrentSession().beginTransaction(); try { pasteDao.addPaste(paste); } catch(Exception e) { beginTransaction.rollback(); } beginTransaction.commit(); } //查找 不用进行事务保护 public Paste findPasteById(String pasteid) { PasteDao pasteDao = new PasteDao(); Paste paste = null; Transaction beginTransaction = HibernateUtils.getCurrentSession().beginTransaction(); try { paste = pasteDao.findPasteById(pasteid); } catch(Exception e) { beginTransaction.rollback(); } beginTransaction.commit(); return paste; } }
package com.Gary.service; import org.hibernate.Transaction; import com.Gary.dao.UserDao; import com.Gary.domain.User; import com.Gary.utils.HibernateUtils; public class UserService { public User findUesr(User user) { UserDao userDao = new UserDao(); //开启事务(查询不需要数据库保护) Transaction transaction = HibernateUtils.getCurrentSession().beginTransaction(); User temp = null; try { temp = userDao.findUesr(user); } catch(Exception e) { transaction.rollback(); } //提交事务 transaction.commit(); return temp; } }
com.Gary.utils
package com.Gary.utils; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { private static SessionFactory sessionFactory = null; static { Configuration config = new Configuration().configure(); sessionFactory = config.buildSessionFactory(); } public static Session getSession() { return sessionFactory.openSession(); } public static Session getCurrentSession() { return sessionFactory.getCurrentSession(); } }
com.Gary.web
package com.Gary.web; import com.Gary.domain.Answer; import com.Gary.domain.Paste; import com.Gary.domain.User; import com.Gary.service.AnswerService; import com.Gary.service.PasteService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class AnswerAction extends ActionSupport implements ModelDriven<Answer>{ public Answer answer = new Answer(); public String pasteid; public String addAnswer() throws Exception { AnswerService answerService = new AnswerService(); PasteService pasteService = new PasteService(); Paste paste = pasteService.findPasteById(pasteid); answer.setPaste(paste); User user = (User) ActionContext.getContext().getSession().get("user"); answer.setUser(user); System.out.println(pasteid + " --- "+answer.getContent()); answerService.addAnswer(answer); return "success"; } public String getPasteid() { return pasteid; } public void setPasteid(String pasteid) { this.pasteid = pasteid; } @Override public Answer getModel() { return answer; } }
package com.Gary.web; import java.text.SimpleDateFormat; import java.util.Date; import com.Gary.domain.Paste; import com.Gary.domain.User; import com.Gary.service.PasteService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class PasteAction extends ActionSupport implements ModelDriven<Paste>{ public Paste paste = new Paste(); //addPaste public String addPaste() throws Exception { //没有的数据手动封装 //title content offer paste.setAnsnum(0); paste.setGlanceover(0); Date date = new Date(System.currentTimeMillis()); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String createtime = format.format(date); paste.setCreatetime(createtime); User user = (User) ActionContext.getContext().getSession().get("user"); paste.setUser(user); PasteService pasteService = new PasteService(); pasteService.addPaste(paste); System.out.println("PasteAction"+paste); return "toIndex"; } @Override public Paste getModel() { return paste; } }
package com.Gary.web; import com.Gary.domain.User; import com.Gary.service.UserService; import com.opensymphony.xwork2.ActionContext; 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 login() throws Exception { UserService userService = new UserService(); User temp = userService.findUesr(user); System.out.println("UserAction"+temp); if(temp!=null) { ActionContext.getContext().getSession().put("user", temp); return "toIndex"; } else { ActionContext.getContext().put("error", "用户名或密码错误!!"); return "login"; } } @Override public User getModel() { return user; } }