Hibernate-----多条件查询,多对多添加查询案例

原创 2016年05月31日 14:36:42

======================================================================================================

1.表单输入条件

 <body>
   <form action="MovieServlet?opr=sselct" method="post">
   <table width="595" height="187" border="0">
  <tr>
    <td width="100">电影名称:</td>
    <td width="300"><input type="text" name="name" />&nbsp;&nbsp;&nbsp;&nbsp;输入关键字即可</td>
  </tr>
  <tr>
    <td>电影类型:</td>
    <td><select name="typeid">
    <option value=0>全选</option>
     <c:forEach items="${listType}" var="type" varStatus="status">
     
     <option value=${type.typeid}>${type.typename }</option>
     </c:forEach></select>
     
    
</td>
  </tr>
  <tr>
    <td>主演:</td>
    <td>&nbsp;<input type="text" name="actor" />&nbsp;&nbsp;&nbsp;&nbsp;输入关键字即可</td>
  </tr>
  <tr>
    <td>导演:</td>
    <td>&nbsp;<input type="text" name="author" />&nbsp;&nbsp;&nbsp;&nbsp;输入关键字即可</td>
  </tr>
  <tr>
    <td>价格:</td>
    <td><input name="smallprice" type="text"/>至<input name="bigprice" type="text"/></td>
  </tr>
</table>
<input type="submit" value="提交">
   </form>
  </body>


======================================================================================================

2.MovieServlet?opr=sselct


if("sselct".equals(opr)){
String name = request.getParameter("name").trim();
String actor = request.getParameter("actor").trim();
String author = request.getParameter("author").trim();
String smallprice = request.getParameter("smallprice").trim();
String bigprice = request.getParameter("bigprice").trim();
String typeid = request.getParameter("typeid").trim();



MovieCondition mc=new  MovieCondition();
if(typeid!="0"){
Integer i=Integer.parseInt(typeid);
Type t=tbi.findById(i);
mc.setTypeset(t);
}
if((name!=null)&&!"".equals(name)){
mc.setName(name);
}
if((actor!=null)&&!"".equals(actor)){
mc.setActor(actor);
}
if((author!=null)&&!"".equals(author)){
mc.setAuthor(author);
}
if((smallprice!=null)&&!"".equals(smallprice)){
mc.setSmallprice(Integer.parseInt(smallprice ));
}else{
mc.setSmallprice(0);
}
if((bigprice!=null)&&!"".equals(bigprice)){
mc.setBigprice(Integer.parseInt(bigprice ));
}else{
mc.setBigprice(new Integer(Integer.MAX_VALUE));
}
List<Movie> listMovie=mbi.check(mc);

request.getSession().setAttribute("listMovie", listMovie);
request.getRequestDispatcher("index.jsp?opt=chk").forward(request, response);
}




======================================================================================================

3.MovieCondition 查询类

public class MovieCondition {

private String name;
private Type typeset;
private String actor;
private String author;
private Integer smallprice;
private Integer bigprice;

}


======================================================================================================

4.Movie实体类

public class Movie implements java.io.Serializable {


// Fields


private Integer movId;
private String movName;
private String movAuthor;
private Integer movPrice;
private Date movDate;
private String movActor;
private Set<Type> settype=new HashSet<Type>();




======================================================================================================

5. Type 实体类


public class Type  implements java.io.Serializable {

private Integer typeid;
private String typename;
Set<Movie> setmovie=new HashSet<Movie>();


======================================================================================================

6.底层多条件查询movie方法

public  List<Movie> check(MovieCondition hc){
Criteria criteria = null;
Session session=null;
List<Movie> list =null;
try {
session=HibernateSessionFactory.getSession();
   DetachedCriteria dc = DetachedCriteria. forClass (Movie.class  ); 
   if(hc.getName()!=null){
    dc.add(Restrictions.ilike( "movName",hc.getName(),MatchMode.ANYWHERE)); 
   } if(hc.getActor()!=null){
    dc.add(Restrictions.ilike( "movAuthor",hc.getActor(),MatchMode.ANYWHERE)); 
   } if(hc.getAuthor()!=null){
    dc.add(Restrictions.ilike( "movActor",hc.getAuthor(),MatchMode.ANYWHERE)); 
   }  
   if(hc.getTypeset()!=null){
    Set<Integer> settype=new HashSet<Integer>();
    Typebiz tbi=new ITypebiz();
    Type t=tbi.findById(hc.getTypeset().getTypeid());
    Set<Movie> setmovie=t.getSetmovie();
    System.out.println(setmovie.size());
       Iterator<Movie> it=setmovie.iterator();
    while(it.hasNext()){
    Movie m=it.next();
    settype.add(m.getMovId());
    }
    dc.add(Restrictions.in( "movId",settype)); 
    //dc.add(Restrictions.in( "setid",settype)); 
   } 
   dc.add(Restrictions.ge ( "movPrice",hc.getSmallprice()));
   dc.add(Restrictions.le ( "movPrice",hc.getBigprice()));
  
   Criteria c = dc.getExecutableCriteria(session);
   list = c.list();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}




======================================================================================================

7.多对多关联添加=----------servlet添加

if("add".equals(opr)){
String name = request.getParameter("name");
String actor = request.getParameter("actor");
String author = request.getParameter("author");
String price = request.getParameter("price");
String[] type = request.getParameterValues("type");
Movie m=new Movie();
m.setMovActor(actor);
m.setMovAuthor(author);
m.setMovDate(new Date());
m.setMovName(name);
m.setMovPrice(Integer.parseInt(price));
Set<Type> s1=new HashSet<Type>();
for(String s:type){
Integer i=Integer.parseInt(s);
Type t=tbi.findById(i);
s1.add(t);
}
m.setSettype(s1);
mbi.save(m);
response.sendRedirect( "MovieServlet?opr=list");
// request.getRequestDispatcher("MovieServlet?opr=list").forward(request, response);
}



======================================================================================================

8.多对多关联添加=----------底层方法

public void save(Movie m) {
Session session = null;
try {
session = HibernateSessionFactory.getSession();
  session.save(m);
} catch (org.hibernate.HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



======================================================================================================

9.多对多关联添加=----------映射文件1

<hibernate-mapping>
    <class name="com.yunhe.entity.Movie" table="MOVIE" schema="SCOTT">
        <id name="movId" type="java.lang.Integer">
            <column name="MOVID" precision="4" scale="0" />
            <generator class="increment" />
        </id>
        <property name="movName" type="java.lang.String">
            <column name="MOVNAME" length="20"  unique="true" />
        </property>
        <property name="movAuthor" type="java.lang.String">
            <column name="MOVAUTHOR" length="20" />
        </property>
        <property name="movPrice" type="java.lang.Integer">
            <column name="MOVPRICE" precision="20" scale="0" />
        </property>
        <property name="movDate" type="java.util.Date">
            <column name="MOVDATE" length="7" />
        </property>
        <property name="movActor" type="java.lang.String">
            <column name="MOVACTOR" length="20"  />
        </property>
        <set name="settype" table="MVOIE_TYPE" >
        <key column="tmmovieid"></key>
        <many-to-many class="com.yunhe.entity.Type" column="tmtypeid"></many-to-many>
        </set>
    </class>
</hibernate-mapping>




======================================================================================================

9.多对多关联添加=----------映射文件2

<hibernate-mapping>
    <class name="com.yunhe.entity.Type" table="typess" schema="SCOTT">
        <id name="typeid" type="java.lang.Integer">
            <column name="typeid" precision="4" scale="0" />
            <generator class="increment" />
        </id>
        <property name="typename" type="java.lang.String">
            <column name="typename" length="20" unique="true" />
        </property>
        <set name="setmovie" table="MVOIE_TYPE" cascade="all"  >
        <key column="tmtypeid"></key>
        <many-to-many class="com.yunhe.entity.Movie" column="tmmovieid"></many-to-many>
        </set> 
    </class>
</hibernate-mapping>



======================================================================================================

10.index页面,实现所有的显示以及相关的控制,多条件查询结果的显示

<c:if test="${!(param.opt eq 'chk')}">
<c:if test="${empty listMovie}">
<%
response.sendRedirect("MovieServlet?opr=list");
%>
</c:if>
</c:if>
<body>

<table width="940" height="98" border="1">


<tr>
<th width="144" align="center">
电影名称
</th>
<th width="294" align="center">
电影类型
</th>
<th width="121" align="center">
主演
</th>
<th width="108" align="center">
导演
</th>
<th width="125" align="center">
价格
</th>
<th width="208" align="center">
发布时间
</th>
</tr>
<c:choose>
<c:when test="${fn:length(listMovie)==0}">
<td colspan="6" align="center">


&nbsp;暂无相关影视信息,请重新查询!






</td>
</c:when>
<c:otherwise>
<c:forEach items="${listMovie}" var="movie" varStatus="status">
<tr>
<td align="center">
<a href=#>&nbsp;${movie.movName }</a>
</td>
<td align="center">
<c:forEach items="${movie.settype }" var="type"
varStatus="status">${type.typename}&nbsp;&nbsp;</c:forEach>


</td>
<td align="center">
&nbsp;${movie.movActor }
</td>
<td align="center">
&nbsp;${movie.movAuthor }
</td>
<td align="center">
&nbsp;¥${movie.movPrice}.00
</td>
<td align="center">
&nbsp;${movie.movDate }
</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>


<tr>
<td colspan="3" align="center">
&nbsp;
</td>
<td align="center">
<a href="MovieServlet?opr=list">查询所有信息</a>
</td>
<td align="center">
<a href="sselect.jsp">查询相关信息</a>
</td>
<td align="center">
<a href="add.jsp">发布最新信息</a>
</td>
</tr>
</table>
</body>
</html>






版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

hibernate多对多关系查询

用户sys_userinfo和角色sys_role,典型的多对多关系, 用户表:sys_userinfo 角色表:sys_role 关联表:rel_user_role 用户sysUserInfo:...

Hibernate中多对多关系如何查询

今天遇到了HIbernate中的多对对查询问题,由于多对多的关系在实体中都是集合:所以查询时的方式有所不同:代码如下:public List getListNameByPtyg() {  String...

Hibernate多对多双向关联

以Student和Course为例,一个学生可以选多门课程,一门课程也可以被多个学生选取; 持久化类Student: package bean; import java.util.Set; ...

myeclipse中配置weblogic 9运行环境

以下是在项目开发中,myeclipse中配置weblogic 9运行环境的详细配置项注:安装weblogic时最好安装在根目录下面,以免出错下面的一些截图是在配置weblogic时需要注意的一些事项注...

Hibernate多对多查询

一个老师教许多学生,一个学生被许多老师教,一个学生有好多书,同一种书被许多同学拥有. 要查询教拥有书"a"的学生的老师!HQL如何写呀?如何取值? class Teacher{ String id; ...

多条件查询 仿淘宝URL传参方式

最近项目在做一个电商

HaloDao之前后台全自动动态多条件查询组件实现(一)

https://github.com/VonChange/haloDao-Hibernate3   同为程序猿的我们,相信你也厌倦了根据不同条件动态生成hql或者sql方式的代码逻辑的编写.大片大片...

分页和多条件查询功能

转载自:http://m.blog.csdn.net/blog/ankeyuan/38376351 /** * 辅助拼接HQL语句的工具类 * @author G-Xia * */ publ...

Bootstrap列表分页及查询(数据与页面分离形式)

Bootstrap两种分页形式:        全部加载数据,然后在js中分页和查询;后台分页,通过后台加载一页的数据;后台发送json格式的数据到前台,前台通过js处理,生成table等,html...

sql 多条件模糊查询例子

select * from A_tabl a where a.status like ? and a.trdate between ? and ? and(a.name like ? or a...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)