基于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`));

Gary.sql

  数据库中帖子数据(测试时候是没有的~)

  

  用户在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">
已有账号,立即&nbsp;<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>

login.jsp

<%@ 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">
已有账号,立即&nbsp;<a href="login.html">登录</a>
</div>
</div>
<div class="dvContent">
<div class="dvquesleft"> <div class="dvqstitle">
<image class="imgbean" src="data: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="data: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="data: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="data: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="data: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="data: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="data: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="data: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="data: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="data: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="data: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="data: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="data: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="data: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="data: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="data: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="data: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="data: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>

index.jsp

<%@ 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="width: 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>

detail.jsp

<%@ 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">
已有账号,立即&nbsp;<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="width: 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>

add.jsp

<?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>

struts.xml

<?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>

hibernate.xml

  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); } }

AnswerDao.java

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); } }

PasteDao.java

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; } }

UserDao.java

  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;
} }

Answer.java

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;
} }

Paste.java

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;
} }

User.java

<?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>

Answer.hbm.xml

<?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>

Paste.hbm.xml

<?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>

User.hbm.xml

  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(); } }

AnswerService.java

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; } }

PasteService.java

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;
} }

UserService.java

  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();
} }

HibernateUtils.java

  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;
} }

AnswerAction.java

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;
} }

PasteAction.java

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;
} }

UserAction.java

JavaWeb_(Hibernate框架)Hibernate论坛项目中多对多案例的更多相关文章

  1. JavaWeb_(Hibernate框架)Hibernate论坛项目中一对多案例

    基于SSH论坛小型项目 传送门 用户和发帖进行举例 一对多关系:一个用户可以发表多个帖子 一对一关系:一个帖子属于一个用户发布 创建数据库用户user表 CREATE TABLE `hforum`.` ...

  2. WebCollector2.7爬虫框架——在Eclipse项目中配置

    WebCollector2.7爬虫框架——在Eclipse项目中配置 在Eclipse项目中使用WebCollector爬虫非常简单,不需要任何其他的配置,只需要导入相关的jar包即可. Netbea ...

  3. JavaWeb_(Hibernate框架)Hibernate中事务

    Hibernate中事务 事务的性质 事物的隔离级别 配置事务的隔离级别 事务的性质 原子性:原子,不可再分,一个操作不能分为更小的操作,要么全都执行,要么全不执行. 一致性:事务在完成时,必须使得所 ...

  4. JavaWeb_(Hibernate框架)Hibernate中创建实体

    Hibernate中创建实体 创建实体五个基本规则 --提供无参的构造器 --成员变量的私有化,提供get.set方法,提供属性 --尽量使用包装类型 --主键(一定要有) --不要加final(hi ...

  5. JavaWeb_(Hibernate框架)Hibernate中重要的api

    Hibernate中重要的api Configuration SessionFactory Session(重点) Transaction 在Dao层中UserDao.java使用Hibernate向 ...

  6. JavaWeb_(Hibernate框架)Hibernate与c3p0与Dbutils的区别

    JavaWeb_(Hibernate框架)使用Hibernate开发用户注册功能 传送门 JavaWeb_(Hibernate框架)使用c3p0与Dbutils开发用户注册功能 传送门 Hiberna ...

  7. Web项目中使用Log4net 案例

    简介: 几乎所有的大型应用都会有自己的用于跟踪调试的API.因为一旦程序被部署以后,就不太可能再利用专门的调试工具了.然而一个管理员可能需要有一套强大的日志系统来诊断和修复配置上的问题. 经验表明,日 ...

  8. JavaWeb_(Hibernate框架)Hibernate中数据查询语句SQL基本用法

    本文展示三种在Hibernate中使用SQL语句进行数据查询基本用法 1.基本查询 2.条件查询 3.分页查询 package com.Gary.dao; import java.util.List; ...

  9. JavaWeb_(Hibernate框架)Hibernate中数据查询语句Criteria基本用法

    Criteria进行数据查询与HQL和SQL的区别是Criteria完全是面向对象的方式在进行数据查询,将不再看到有sql语句的痕迹,使用Criteria 查询数据包括以下步骤: 1. 通过sessi ...

随机推荐

  1. 轻松搭建CAS 5.x系列(3)-连接数据库,使用数据库表中的帐号做登录

    概要说明 前面的CAS SEVER中的登录帐号名是配置中写死的,实际情况中不太可能用这些方法.通常情况下,数据库的帐号名密码都是在数据库表中的,这样可以对登录帐号进行增删改的处理. 如果您对搭建固定帐 ...

  2. CentOS7 mysql支持中文

    # vim /etc/my.cnf # For advice on how to change settings please see# http://dev.mysql.com/doc/refman ...

  3. IOC之MEF学习

    MEF原理上很简单,找出有共同接口的导入.导出.然后找到把导出的实例化,赋给导入.说到底MEF就是找到合适的类实例化,把它交给导入.Export 特性可修饰类.字段.属性或方法,而 Import 特性 ...

  4. FlowPortal BPM 安装环境的配置

    l  操作系统:Windows Server 2003 及以上: l  IIS: 在Internet信息服务(IIS)管理器中将ISAPI和CGI限制全部设为“允许” l  需要安装.Net Fram ...

  5. PXE批量部署安装Linux系统

    PXE介绍 1)Preboot Excution Environment 预启动执行环境 2)Intel公司研发 3)基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载映 ...

  6. 将TensorFlow训练好的模型迁移到Android APP上(TensorFlowLite)

    转自:https://blog.csdn.net/u012328159/article/details/81101074 https://blog.csdn.net/masa_fish/article ...

  7. [Abp vNext微服务实践] - vue-element-admin管理Identity

    一.简介 abp vNext微服务框架中已经提供通用权限和用户角色管理模块,管理UI使用的是MVC模式,不适用于国内主打的MVVM开发模式.在前端框架选型后笔者决定改造abp vNext微服务框架中原 ...

  8. Codeforces 1148 E - Earth Wind and Fire

    E - Earth Wind and Fire 思路: 栈模拟 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC opti ...

  9. P2050 [NOI2012]美食节 动态加边加点

    修车数据加强版 需要动态加边加点 #include<bits/stdc++.h> using namespace std; const int INF = 0x7f7f7f7f; , MA ...

  10. ShedLock日常使用

    首发于个人博客:ShedLock日常使用 场景模拟 定时器Scheduler在平时使用比较频繁,比如定时数据整理,定时向客户发送问候信息等...,定时任务的配置比较简单,比如在springboot中, ...