关键字相等的函数是自动生成的:
public List findByProperty(String propertyName, Object value) {
log.debug("finding Courseware instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Courseware as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}现在我想把他改为关键字模糊查询,就是包含关键字的都查询到,请问应该怎么改
还有,我在查询的时候,只要是查询有中文的就无法得到结果集,但如果查询的是英文和数字就可以,为什么呢?
public List findByProperty(String propertyName, Object value) {
log.debug("finding Courseware instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Courseware as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}现在我想把他改为关键字模糊查询,就是包含关键字的都查询到,请问应该怎么改
还有,我在查询的时候,只要是查询有中文的就无法得到结果集,但如果查询的是英文和数字就可以,为什么呢?
<prop key="hibernate.query.factory_class">
org.hibernate.hql.classic.ClassicQueryTranslatorFactory
</prop>
<prop key="connection.characterEncoding">utf8</prop>
public List<Resource> findByProperty(String propertyName, Object value) {
log.debug("finding Resource instance with property: " + propertyName
+ ", value: " + value);
try {
Session session=getSessionFactory().getCurrentSession();
Transaction trans=session.beginTransaction();
String queryString = "from Resource as model where model."+propertyName+"like"+ value;
Query query = session.createQuery(queryString);
query.setString("value", "%"+value+"%");
List result=query.list();
for(int i=0;i<result.size();i++){
Resource resource=(Resource)result.get(i);
Integer resourceId=resource.getResourceId();
String resourceName=resource.getResourceName();
String resourceType=resource.getResourceType();
String location=resource.getLocation();
String assort=resource.getAssort();
String discription=resource.getDiscription();
}
trans.commit();
session.close();
return result;
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}但这个却会抛到异常,不知道哪里错了,纠结了我两天两夜了,求大神们帮帮我,不然我后面的功能都没心情做下去了。。
String queryString = "from Resource as model where model."+propertyName+" like ?";
Query query = session.createQuery(queryString);
query.setString(0, "%"+value+"%");貌似楼主like 前面少个空格,楼主我的这样你试试
log.debug("finding Courseware instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Courseware as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, "%"+value+"%");
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
或者是<prop key="connection.characterEncoding">utf8</prop>但是我加了之后一样不行,5楼的方法我头一天就试过了。我都不知道怎么可以解决中文无法查询的问题了