Heim > Datenbank > MySQL-Tutorial > HibernateHQL语句使用总结

HibernateHQL语句使用总结

WBOY
Freigeben: 2016-06-07 16:01:31
Original
934 Leute haben es durchsucht

1. 查询整个映射对象所有字段 //直接from查询出来的是一个映射对象,即:查询整个映射对象所有字段 String hql = from Users ; Query query = session.createQuery(hql); ListUsers users = query.list(); for (Users user : users){ System.out.println(use

1. 查询整个映射对象所有字段

//直接from查询出来的是一个映射对象,即:查询整个映射对象所有字段 <code class="java spaces"><code class="java plain">String hql = <code class="java string">"from Users"<code class="java plain">; <code class="java spaces"><code class="java plain">Query query = session.createQuery(hql); <code class="java spaces"><code class="java spaces"><code class="java plain">List<users> users = query.list(); <code class="java spaces"><code class="java keyword">for<code class="java plain">(Users user : users){ <code class="java spaces"><code class="java plain">System.out.println(user.getName() + <code class="java string">" : " <code class="java plain">+ user.getPasswd() + <code class="java string">" : " <code class="java plain">+ user.getId()); <code class="java spaces"><code class="java plain">} <code class="java spaces"><code class="java spaces"><code class="java plain">输出结果为: <code class="java plain">name1 : password1 : <code class="java value">1 <code class="java plain">name2 : password2 : <code class="java value">2 <code class="java plain">name3 : password3 : <code class="java value">3

2.查询字段  

//查询其中几个字段 <code class="java spaces"><code class="java plain">String hql = <code class="java string">" select name,passwd from Users"<code class="java plain">; <code class="java spaces"><code class="java plain">Query query = session.createQuery(hql); <code class="java spaces"><code class="java comments">//默认查询出来的list里存放的是一个Object数组 <code class="java spaces"><code class="java plain">List<object> list = query.list(); <code class="java spaces"><code class="java keyword">for<code class="java plain">(Object[] object : list){ <code class="java spaces"><code class="java plain">String name = (String)object[<code class="java value">0<code class="java plain">]; <code class="java spaces"><code class="java plain">String passwd = (String)object[<code class="java value">1<code class="java plain">]; <code class="java spaces"><code class="java spaces"><code class="java plain">System.out.println(name + <code class="java string">" : " <code class="java plain">+ passwd); <code class="java spaces"><code class="java plain">} <code class="java plain">输出结果为: <code class="java plain">name1 : password1 <code class="java plain">name2 : password2 <code class="java plain">name3 : password3

3.修改默认查询结果(query.list())不以Object[]数组形式返回,以List形式返回

//查询其中几个字段,添加new list(),注意list里的l是小写的。也不需要导入包,这样通过query.list()出来的list里存放的不再是默认的Object数组了,而是List集合了 <code class="java spaces"><code class="java plain">String hql = <code class="java string">" select new list(name,passwd) from Users"<code class="java plain">; <code class="java spaces"><code class="java plain">Query query = session.createQuery(hql); <code class="java spaces"><code class="java comments">//默认查询出来的list里存放的是一个Object数组,但是在这里list里存放的不再是默认的Object数组了,而是List集合了 <code class="java spaces"><code class="java plain">List<list> list = query.list(); <code class="java spaces"><code class="java keyword">for<code class="java plain">(List user : list){ <code class="java spaces"><code class="java plain">String name = (String)user.get(<code class="java value">0<code class="java plain">); <code class="java spaces"><code class="java plain">String passwd = (String)user.get(<code class="java value">1<code class="java plain">); <code class="java spaces"><code class="java spaces"><code class="java plain">System.out.println(name + <code class="java string">" : " <code class="java plain">+ passwd); <code class="java spaces"><code class="java plain">} <code class="java spaces"><code class="java preprocessor">/** <code class="java spaces"><code class="java preprocessor">输出结果为: <code class="java spaces"><code class="java preprocessor">name1 : password1 <code class="java spaces"><code class="java preprocessor">name2 : password2 <code class="java spaces"><code class="java preprocessor">name3 : password3 <code class="java spaces"><code class="java preprocessor">*/

4.修改默认查询结果(query.list())不以Object[]数组形式返回,以Map形式返回

//查询其中几个字段,添加new map(),注意map里的m是小写的。也不需要导入包,这样通过query.list()出来的list里存放的不再是默认的Object数组了,而是map集合了 <code class="java spaces"><code class="java plain">String hql = <code class="java string">" select new map(name,passwd) from Users"<code class="java plain">; <code class="java spaces"><code class="java plain">Query query = session.createQuery(hql); <code class="java spaces"><code class="java comments">//默认查询出来的list里存放的是一个Object数组,但是在这里list里存放的不再是默认的Object数组了,而是Map集合了 <code class="java spaces"><code class="java plain">List<map> list = query.list(); <code class="java spaces"><code class="java keyword">for<code class="java plain">(Map user : list){ <code class="java spaces"><code class="java comments">//一条记录里所有的字段值都是map里的一个元素,key是字符串0,1,2,3....,value是字段值 <code class="java spaces"><code class="java comments">//如果将hql改为:String hql = " select new map(name as username,passwd as password) from Users";,那么key将不是字符串0,1,2...了,而是"username","password"了 <code class="java spaces"><code class="java plain">String name = (String)user.get(<code class="java string">"0"<code class="java plain">);<code class="java comments">//get("0");是get(key),注意:0,1,2...是字符串,而不是整形 <code class="java spaces"><code class="java plain">String passwd = (String)user.get(<code class="java string">"1"<code class="java plain">); <code class="java spaces"><code class="java spaces"><code class="java plain">System.out.println(name + <code class="java string">" : " <code class="java plain">+ passwd); <code class="java spaces"><code class="java plain">} <code class="java spaces"><code class="java preprocessor">/** <code class="java spaces"><code class="java preprocessor">输出结果为: <code class="java spaces"><code class="java preprocessor">name1 : password1 <code class="java spaces"><code class="java preprocessor">name2 : password2 <code class="java spaces"><code class="java preprocessor">name3 : password3 <code class="java spaces"><code class="java preprocessor">*/

5.修改默认查询结果(query.list())不以Object[]数组形式返回,以自定义类型返回

6.条件查询

//条件查询,参数索引值从0开始,索引位置。通过setString,setParameter设置参数 <code class="java spaces"><code class="java plain">String hql = <code class="java string">"from Users where name=? and passwd=?"<code class="java plain">; <code class="java spaces"><code class="java plain">Query query = session.createQuery(hql); <code class="java spaces"><code class="java comments">//第1种方式 <code class="java comments">// query.setString(0, "name1"); <code class="java comments">// query.setString(1, "password1"); <code class="java spaces"><code class="java comments">//第2种方式 <code class="java spaces"><code class="java plain">query.setParameter(<code class="java value">0<code class="java plain">, <code class="java string">"name1"<code class="java plain">,Hibernate.STRING); <code class="java spaces"><code class="java plain">query.setParameter(<code class="java value">1<code class="java plain">, <code class="java string">"password1"<code class="java plain">,Hibernate.STRING); <code class="java spaces"><code class="java plain">List<users> list = query.list(); <code class="java spaces"><code class="java keyword">for<code class="java plain">(Users users : list){ <code class="java spaces"><code class="java plain">System.out.println(users.getId()); <code class="java spaces"><code class="java plain">}
//条件查询,自定义索引名(参数名):username,:password.通过setString,setParameter设置参数 <code class="java spaces"><code class="java plain">String hql = <code class="java string">"from Users where name=:username and passwd=:password"<code class="java plain">; <code class="java spaces"><code class="java plain">Query query = session.createQuery(hql); <code class="java spaces"><code class="java comments">//第1种方式 <code class="java comments">// query.setString("username", "name1"); <code class="java comments">// query.setString("password", "password1"); <code class="java spaces"><code class="java comments">//第2种方式,第3个参数确定类型 <code class="java spaces"><code class="java plain">query.setParameter(<code class="java string">"username"<code class="java plain">, <code class="java string">"name1"<code class="java plain">,Hibernate.STRING); <code class="java spaces"><code class="java plain">query.setParameter(<code class="java string">"password"<code class="java plain">, <code class="java string">"password1"<code class="java plain">,Hibernate.STRING); <code class="java spaces"><code class="java plain">List<users> list = query.list(); <code class="java spaces"><code class="java keyword">for<code class="java plain">(Users users : list){ <code class="java spaces"><code class="java plain">System.out.println(users.getId()); <code class="java spaces"><code class="java plain">}
//条件查询,通过setProperties设置参数 <code class="java spaces"><code class="java plain">String hql = <code class="java string">"from Users where name=:username and passwd=:password"<code class="java plain">; <code class="java spaces"><code class="java plain">Query query = session.createQuery(hql); <code class="java spaces"><code class="java comments">//MyUser类的2个属性必须和:username和:password对应 <code class="java spaces"><code class="java plain">MyUser myUser = <code class="java keyword">new <code class="java plain">MyUser(<code class="java string">"name1"<code class="java plain">,<code class="java string">"password1"<code class="java plain">); <code class="java spaces"><code class="java plain">query.setProperties(myUser); <code class="java spaces"><code class="java plain">List<users> list = query.list(); <code class="java spaces"><code class="java keyword">for<code class="java plain">(Users users : list){ <code class="java spaces"><code class="java plain">System.out.println(users.getId()); <code class="java spaces"><code class="java plain">}

7.update 数据

执行SQL语句(为什么要用SQL语句,我想是为了执行某些复杂的SQL语句吧) 

String sql=<code class="java string">"update Table set field = 'test'" <code class="java plain">Session session = HibernateSessionFactory.getSession(); <code class="java plain">session.createSQLQuery(sql).executeUpdate(); <code class="java plain">ts.commit();

执行HQL语句   

String hql=<code class="java string">"update Table set field = 'test'"
Session session = HiberanteSessionFactory.getSession(); <code class="java plain">Transaction ts = session.beginTransaction(); <code class="java plain">Query query = session.createQuery(hql); <code class="java plain">query.executeUpdate(); <code class="java plain">ts.commit();
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage