how2j.cn

下载区
文件名 文件大小
spring_hibernate.rar 12m

解压rar如果失败,请用5.21版本或者更高版本的winrar

点击下载 winrar5.21

工具版本兼容问题
使用HibernateTemplate进行模糊查询
其思路和直接使用hibernate有所区别



分别使用Hql和Criteria进行模糊查询
package com.how2java.test; import java.util.List; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Restrictions; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.how2java.dao.CategoryDAO; import com.how2java.pojo.Category; public class TestSpring { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" }); CategoryDAO dao = (CategoryDAO) context.getBean("dao"); List<Category> cs =dao.find("from Category c where c.name like ?", "%c%"); for (Category c : cs) { System.out.println(c.getName()); } DetachedCriteria dc = DetachedCriteria.forClass(Category.class); dc.add(Restrictions.like("name", "%分类%")); cs =dao.findByCriteria(dc); for (Category c : cs) { System.out.println(c.getName()); } } }


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2017-08-30 第27行的“%分类%”,应该是“%c%”吧?
雾里晨光

第27行的“%分类%”,应该是“%c%”吧?




1 个答案

how2j
答案时间:2017-08-31
好像前面学习过程中,加了一些数据,那些数据是包含 "分类" 字符串的



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢





提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢