Hibernate中HQL语句的left join的例子详解
来源:互联网 发布:linux c 编程 编辑:程序博客网 时间:2024/05/21 21:49
1.com.jcuckoo.entity添加ShowBook类,用来封装界面显示的数据
private int id;
private String name;
private String author;
private String publisher;
private double price;
private String pic;
private int storageId;
private int amount;
private Date inDate;
……
}
2.在hibernate.cfg.xml配置文件中,添加<mapping class="com.jcuckoo.entity.ShowBook"/>
3.在com.jcuckoo.entity.Book中添加属性及get/set方法
private int id;
private String name;
private String author;
private String publisher;
private double price;
private String pic;
private Set<Storage> storages;
4.在Book.hbm.xml中添加多对一配置
<set name="storages">
<key column="bookId"></key>
<one-to-many class="com.jcuckoo.entity.Storage"/>
</set>
5.在com.jcuckoo.entity.Storage添加属性及get/set方法
private int id;
private int bookId;
private String bookName;
private int amount;
private Date inDate;
private Book book;
6.在Storage.hbm.xml中添加一对多配置
<many-to-one name="book" class="com.jcuckoo.entity.Book" column="bookId" insert="false" update="false"/>
7.com.jcuckoo.dao中添加ShowBookDao,通过左连接完成数据查询from Book b left join fetch b.storages s
8.在applicationContext.xml配置ShowBookDao
<bean id="ShowBookDao" class="com.jcuckoo.dao.ShowBookDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
9.在com.jcuckoo.service添加ShowBookService类
10.在applicationContext.xml配置ShowBookService
<bean id="ShowBookService" class="com.jcuckoo.service.ShowBookService">
<property name="showBookDao" ref="ShowBookDao"/>
</bean>
11.在com.jcuckoo.action添加BuyBookAction类
private ShowBookService showBookService;//提供get/set方法
12.在applicationContext.xml配置
<bean id="BuyBookAction" class="com.jcuckoo.action.BuyBookAction">
<property name="ShowBookService" ref="ShowBookService"/>
</bean>
13. 在显示界面
@Entity
public class ShowBook {
private int id;
private String name;
private String author;
private String publisher;
private double price;
private String pic;
private int storageId;
private int amount;
private Date inDate;
……
}
2.在hibernate.cfg.xml配置文件中,添加<mapping class="com.jcuckoo.entity.ShowBook"/>
3.在com.jcuckoo.entity.Book中添加属性及get/set方法
private int id;
private String name;
private String author;
private String publisher;
private double price;
private String pic;
private Set<Storage> storages;
4.在Book.hbm.xml中添加多对一配置
<set name="storages">
<key column="bookId"></key>
<one-to-many class="com.jcuckoo.entity.Storage"/>
</set>
5.在com.jcuckoo.entity.Storage添加属性及get/set方法
private int id;
private int bookId;
private String bookName;
private int amount;
private Date inDate;
private Book book;
6.在Storage.hbm.xml中添加一对多配置
<many-to-one name="book" class="com.jcuckoo.entity.Book" column="bookId" insert="false" update="false"/>
7.com.jcuckoo.dao中添加ShowBookDao,通过左连接完成数据查询from Book b left join fetch b.storages s
public List<ShowBook> findAll() {List<ShowBook> showBookList = new ArrayList<ShowBook>();String hql = "from Book b left join fetch b.storages s";Query query = this.getSession().createQuery(hql);List<Book> list = query.list();for (int i = 0; i < list.size(); i++) {Book book = list.get(i);ShowBook showBook = new ShowBook(book.getId(), book.getName(),book.getAuthor(), book.getPublisher(), book.getPrice(),book.getPic());Iterator it = book.getStorages().iterator();if (it.hasNext()) {Storage storage = (Storage) it.next();showBook.setStorageId(storage.getId());showBook.setAmount(storage.getAmount());showBook.setInDate(storage.getInDate());}showBookList.add(showBook);}return showBookList;}
8.在applicationContext.xml配置ShowBookDao
<bean id="ShowBookDao" class="com.jcuckoo.dao.ShowBookDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
9.在com.jcuckoo.service添加ShowBookService类
10.在applicationContext.xml配置ShowBookService
<bean id="ShowBookService" class="com.jcuckoo.service.ShowBookService">
<property name="showBookDao" ref="ShowBookDao"/>
</bean>
11.在com.jcuckoo.action添加BuyBookAction类
private ShowBookService showBookService;//提供get/set方法
public String list() throws Exception { // 去数据库查数据,并显示处理 List<ShowBook> showBookList = showBookService.findAll(); ServletActionContext.getRequest().setAttribute("showBookList",showBookList); return "list";}
12.在applicationContext.xml配置
<bean id="BuyBookAction" class="com.jcuckoo.action.BuyBookAction">
<property name="ShowBookService" ref="ShowBookService"/>
</bean>
13. 在显示界面
<s:iterator value="#request.showBookList" id="myBook" status="status"> <s:if test="%{#status.index%5==0}"> <div> </s:if> <div class="${status.index%2==0?'div1':'div2' }"> <table width="200" border="1"> <tr> <td rowspan="4"><img src="${myBook.pic }" width="100px" height="180px"/></td> <td>作者:${myBook.name }${myBook.author }</td> </tr> <tr> <td>出版社:${myBook.publisher }</td> </tr> <tr> <td>价格:${myBook.price }</td> </tr> <tr> <td>上架时间:<%-- :${myBook.storage.dealInDate==null?'<font color=red>暂未上架</font>':myBook.storage.dealInDate } --%> <s:if test="%{#myBook.amount>0 }"> <!-- %{#myBook.storage!=null and #myBook.storage.amount>0 }或 %{#myBook.storage!=null && #myBook.storage.amount>0 } --> ${myBook.dealInDate } <input type="button" value="加入购入车" class="shopCarButton"/> </s:if> <s:else> <font color=red>暂未上架</font> </s:else> </td> </tr> </table> </div> <s:if test="%{#status.index%5==4}"> </div> <div class="clearDiv"></div> </s:if> </s:iterator>
0 0
- Hibernate中HQL语句的left join的例子详解
- Hibernate中SQL语句的left join的例子详解
- Hibernate中left join,right join等用法的详解
- hql的left join问题
- SQL、HQL中left join、right join、inner join的区别
- sql语句中left join、 inner join的使用
- left join 的SQL 转hql
- sql语句中inner join,left join,right join,full join,cross join之间的区别
- Hibernate中HQL语言的一些例子
- 有意思的 left join 语句
- Hibernate的HQL语句
- hibernate的hql语句
- Hibernate中HQL语句的使用
- Hibernate中HQL语句的使用
- Hibernate中常用的HQL语句总结
- Hibernate中HQL语句的使用
- SQL语句中inner join、left join、right join、full join的区别
- [转载]SQL语句中LEFT JOIN ON WHERE和LEFT JOIN ON AND的区别
- 初探CND
- IOS基础学习(5)------音频(一)AudioservicesPlaySystemSound和AVAudioPlayer的使用
- lsp socks5 客户端 支持tcp udp
- 论证是一门学问 如何让你的观点有说服力-12 类比论证 Arguments by Analogy
- Jquery中淡入淡出效果fadeIn()、fadeOut()、fadeToggle()、fadeTo()学习引发的思考----关于Jquery函数传参数
- Hibernate中HQL语句的left join的例子详解
- 从sap面试回来的感想
- 星座查询演示示例代码
- MySQL触发器 trigger之for each row
- java中使用代理访问网络的几种方法(一)
- HDU 5077 NAND
- 云计算学习(一)
- Linux 内核配置机制(make menuconfig、Kconfig、makefile)讲解
- 赵雅智_java网络编程(5)TCP和udp区别