本文共 5215 字,大约阅读时间需要 17 分钟。
随心所欲的使用面向对象思想操纵数据库.
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库,从而无需顾及数据库的实现究竟是SQLServer还是Mysql还是Oracle
搭建在一般工程中
官网进入后看到hibenate ORM,点击more,左侧边栏选择releases中的一个版本,页面最下方,选择download下载即可
官方jar包:lib文件:requeired文件里的所有jar拷贝到自己的新建工程中去,然后在加上连接数据库相关的包,mysql-connectorHibernate.cfg.xml
放置在src下,需要修改url,username和password
jdbc:mysql://localhost:3306/mycms com.mysql.jdbc.Driver root 123456
Node.hbm.xml
映射到具体的pojo,一个pojo配置一个映射的xml
pojo的主键属性名称对应到数据库实体的主键名称写在id中
,其他的写在property中在一个入口方法或者测试类方法中,加入
//构建上下文换肩加配置连接池,开启事务Configuration configuration = new Configuration().configure();SessionFactory sessionFactory = configuration.buildSessionFactory();Session session = sessionFactory.openSession();Transaction transaction = session.beginTransaction();//这里填写测试代码//提交事务并关闭各种流transaction.commit();session.close();sessionFactory.close();
以下的测试代码,分别放在上面代码的`这里填写测试代码
处
Node node = new Node();; node.setNodeId("testNodeId1"); node.setPid("testPid1"); node.setType((byte)1); node.setLevel((short)1); node.setName("testName1"); session.save(node);
Node node=(Node)session.get(Node.class,1);System.out.println(node);//Node.class后面的1是数据库中的主键值
Node node=(Node)session.get(Node.class,10);node.setName("updateName");session.update(node);
Node node = (Node) session.get(Node.class, 10);session.delete(node);
这里需要注意,如果你的pojo叫 myuser,而数据库中对应的表叫user,所有使用createQuery的地方,涉及到了表,就该填myuser
Query query = session.createQuery("FROM Node");List list = query.list();System.out.println("list = " + list);
Query query = session.createQuery("SELECT nodeId FROM Node");List list = query.list();System.out.println("list = " + list);
Query query = session.createQuery("FROM Node WHERE type=?");query.setParameter(0,10);List list = query.list();System.out.println("list = " + list);
//需要pojo有相应的构造器Query query = session.createQuery("SELECT new Node(id,name,nodeId) FROM Node");Listlist = query.list();System.out.println("list = " + list);
Query query = session.createQuery("SELECT type,SUM(id) FROM Node GROUP BY type");List list = query.list();for (Object o : list) { Object[] result = (Object[]) o; System.out.println(Arrays.toString(result));}
Query query = session.createQuery("FROM Node ORDER BY id DESC");List list = query.list();System.out.println("list = " + list);
Query query = session.createQuery("FROM Node ORDER BY id DESC");query.setFirstResult(2);query.setMaxResults(3);List list = query.list();System.out.println("list = " + list);
String colName = "id";String sql = "FROM Node WHERE " + colName + "=?";Query query = session.createQuery(sql);query.setParameter(0,6);Node node = (Node) query.uniqueResult();System.out.println("node = " + node);
或者这种
Query query = session.createQuery("FROM Node WHERE id:id");query.setParameter("id",7);List list = query.list();System.out.println("list = " + list);
Query query = session.createQuery("FROM Node WHERE id IN(:ids)");query.setParameterList("ids",new Object[]{4,6,7});List list = query.list();System.out.println("list = " + list);//In 的效率很低
User.hbm.xml
... FROM Node WHERE id between ? AND ? ...
代码部分
Query query = session.getNamedQuery("getUserByAge");query.setParameter(0,6);query.setParameter(1,8);List list = query.list();System.out.println("list = " + list);
仿照第一种半sql写法,格式变化就可以
1.原生sql写法
SQLQuery query = session.createSQLQuery("SELECT * FROM tree");query.addEntity(Node.class);List list = query.list();System.out.println("list = " + list);
仿照第一种半sql写法,格式变化就可以
Criteria criteria = session.createCriteria(Node.class);criteria.add(Restrictions.eq("id",6));List list = criteria.list();System.out.println("list = " + list);
相当于查出来了所有的放在criteria里面
Criteria criteria = session.createCriteria(Node.class);criteria.add(Restrictions.ne("id",1));List list = criteria.list();System.out.println("list = " + list);
Criteria criteria = session.createCriteria(Node.class);criteria.addOrder(Order.desc("id"));List list = criteria.list();System.out.println("list = " + list);
Criteria criteria = session.createCriteria(Node.class);criteria.add(Restrictions.ne("id",1));criteria.setFirstResult(0);criteria.setMaxResults(2);List list = criteria.list();System.out.println("list = " + list);
Criteria criteria = session.createCriteria(Node.class);ProjectionList projectionList = Projections.projectionList();projectionList.add(Projections.sum("id"));projectionList.add(Projections.groupProperty("type"));criteria.setProjection(projectionList);List list = criteria.list();for (Object o : list) { Object[] result = (Object[]) o;System.out.println(Arrays.toString(result));}
转载地址:http://rcjla.baihongyu.com/