|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
而学习JAVA我觉得最应该避免的就是:只学习,不思考,只记忆,不实践!
仍旧接着后面的例子来说:http://www.bianceng.cn/Programming/Java/201101/23637.htm
示例代码以下:- 1.packagecom.hb3.pack_01;2.3.importjava.util.Iterator;4.importjava.util.List;5.
- 6.importorg.hibernate.Criteria;7.importorg.hibernate.Hibernate;8.importorg.hibernate.Session;9.importorg.hibernate.SessionFactory;10.importorg.hibernate.cfg.Configuration;11.importorg.hibernate.criterion.Example;12.importorg.hibernate.criterion.Order;13.importorg.hibernate.criterion.ProjectionList;14.importorg.hibernate.criterion.Projections;15.importorg.hibernate.criterion.Property;16.importorg.hibernate.criterion.Restrictions;17.importorg.hibernate.type.Type;18.
- 19.importcom.hb3.pack_01.model.User;20.
- 21.publicclassBusinessService{22.
- 23.publicstaticvoidmain(String[]args){24.25.Configurationconfig=newConfiguration().configure();26.SessionFactorysessionFactory=config.buildSessionFactory();27.Sessionsession=sessionFactory.openSession();28.29.Criteriacriteria=session.createCriteria(User.class);30.criteria.addOrder(Order.asc("age"));31.List<?>users=criteria.list();32.printUserInfo(users);33.34.criteria=session.createCriteria(User.class);35.criteria.add(Restrictions.gt("age",newInteger(22)));36.criteria.add(Restrictions.lt("age",newInteger(27)));37.users=criteria.list();38.printUserInfo(users);39.40.criteria=session.createCriteria(User.class);41.criteria.add(Restrictions.or(42.Restrictions.eq("age",newInteger(23)),43.Restrictions.isNull("age")44.));45.users=criteria.list();46.printUserInfo(users);47.48.criteria=session.createCriteria(User.class);49.criteria.add(Restrictions.sqlRestriction("{alias}.nameLIKE(?)","%ya%",Hibernate.STRING));50.users=criteria.list();51.printUserInfo(users);52.53.criteria=session.createCriteria(User.class);54.Integer[]ages={newInteger(26),newInteger(28)};55.Type[]types={Hibernate.INTEGER,Hibernate.INTEGER};56.criteria.add(Restrictions.sqlRestriction("{alias}.ageBETWEEN(?)AND(?)",ages,types));57.users=criteria.list();58.printUserInfo(users);59.60.criteria=session.createCriteria(User.class);61.criteria.setFirstResult(3);62.criteria.setMaxResults(2);63.users=criteria.list();64.printUserInfo(users);65.66.criteria=session.createCriteria(User.class);67.criteria.setProjection(Projections.rowCount());68.users=criteria.list();69.Iterator<?>iterator=users.iterator();70.while(iterator.hasNext()){71.System.out.println(iterator.next());72.}73.74.criteria=session.createCriteria(User.class);75.criteria.setProjection(Projections.avg("age"));76.users=criteria.list();77.iterator=users.iterator();78.while(iterator.hasNext()){79.System.out.println(iterator.next());80.}81.82.criteria=session.createCriteria(User.class);83.criteria.setProjection(Projections.groupProperty("age"));84.users=criteria.list();85.iterator=users.iterator();86.while(iterator.hasNext()){87.System.out.println(iterator.next());88.}89.90.ProjectionListprojectionList=Projections.projectionList();91.projectionList.add(Projections.groupProperty("age"));92.projectionList.add(Projections.groupProperty("name"));93.projectionList.add(Projections.rowCount());94.criteria=session.createCriteria(User.class);95.criteria.setProjection(projectionList);96.users=criteria.list();97.iterator=users.iterator();98.while(iterator.hasNext()){99.Object[]o=(Object[])iterator.next();100.System.out.println(o[0]+" "+o[1]+" "+o[2]);101.}102.103.criteria=session.createCriteria(User.class);104.criteria.add(Property.forName("name").like("%ya%"));105.criteria.addOrder(Property.forName("age").desc());106.users=criteria.list();107.printUserInfo(users);108.109.Useruser=newUser();110.user.setAge(newInteger(26));111.criteria=session.createCriteria(User.class);112.criteria.add(Example.create(user));113.users=criteria.list();114.printUserInfo(users);115.116.117.session.close();118.sessionFactory.close();119.}120.121.publicstaticvoidprintUserInfo(List<?>users){122.123.Iterator<?>iterator=users.iterator();124.System.out.println("id name/age");125.while(iterator.hasNext()){126.Useruser=(User)iterator.next();127.System.out.println(user.getId()+" "+user.getName()+"/"+user.getAge());128.}129.}130.}
复制代码 <p>
令人可喜的是java现在已经开源了,所以我想我上述的想法也许有一天会实现,因为java一直都是不断创新的语言,每次创新都会给我们惊喜,这也是我喜欢java的一个原因。 |
|