/** * 保存数据 */ @Test public void testInsert() { //加载 Hibernate 核心配置文件 Configuration configuration = new Configuration().configure(); //创建一个 SessionFactory 用来获取 Session 连接对象 SessionFactory sessionFactory = configuration.buildSessionFactory(); //获取session 连接对象 Session session = sessionFactory.openSession(); //开始事务 Transaction transaction = session.beginTransaction(); //创建实体类对象 User user = new User(); user.setUserId("003"); user.setUserName("编程帮新增用户"); user.setPassword("654321"); user.setEmail("14234567@qq.com"); //向 user 表中插入数据,返回值为新增数据的主键 id Serializable save = session.save(user); System.out.println("新增数据的主键 id:"+save); //提交事务 transaction.commit(); //释放资源 session.close(); sessionFactory.close(); }
Hibernate: insert into user (user_id, user_name, password, email) values (?, ?, ?, ?) 新增数据的主键 id:3
id | user_id | user_name | password | |
---|---|---|---|---|
1 | 001 | admin | admin | 12345678@qq.com |
2 | 002 | user | 123456 | 98765432@qq.com |
3 | 003 | 编程帮新增用户 | 654321 | 14234567@qq.com |
/** * 修改记录 */ @Test public void testUpdate() { //加载 Hibernate 核心配置文件 Configuration configuration = new Configuration().configure(); //创建一个 SessionFactory 用来获取 Session 连接对象 SessionFactory sessionFactory = configuration.buildSessionFactory(); //获取session 连接对象 Session session = sessionFactory.openSession(); //开始事务 Transaction transaction = session.beginTransaction(); //创建实体对象 User user = new User(); user.setId(3); //设置需要修改的字段 user.setUserName("更新用户名"); //直接调用 update() 方法进行修改 session.update(user); //提交事务 transaction.commit(); //释放资源 session.close(); sessionFactory.close(); }
Hibernate: update user set user_id=?, user_name=?, password=?, email=? where id=?
图1:直接使用 update() 方法修改记录
/** * 修改记录 */ @Test public void testUpdate() { //加载 Hibernate 核心配置文件 Configuration configuration = new Configuration().configure(); //创建一个 SessionFactory 用来获取 Session 连接对象 SessionFactory sessionFactory = configuration.buildSessionFactory(); //获取session 连接对象 Session session = sessionFactory.openSession(); //开始事务 Transaction transaction = session.beginTransaction(); //现将需要修改的记录查询出来 User user = session.get(User.class, 3); //设置需要修改的字段 user.setUserName("更新用户名"); //直接调用 update() 方法进行修改 session.update(user); //提交事务 transaction.commit(); //释放资源 session.close(); sessionFactory.close(); }
Hibernate: select user0_.id as id1_0_0_, user0_.user_id as user_id2_0_0_, user0_.user_name as user_nam3_0_0_, user0_.password as password4_0_0_, user0_.email as email5_0_0_ from user user0_ where user0_.id=? Hibernate: update user set user_id=?, user_name=?, password=?, email=? where id=?
图2:先查询在修改
/** * 删除记录 */ @Test public void testDelete() { //加载 Hibernate 核心配置文件 Configuration configuration = new Configuration().configure(); //创建一个 SessionFactory 用来获取 Session 连接对象 SessionFactory sessionFactory = configuration.buildSessionFactory(); //获取session 连接对象 Session session = sessionFactory.openSession(); //开始事务 Transaction transaction = session.beginTransaction(); User user = new User(); user.setId(3); //删除指定的记录 session.delete(user); //提交事务 transaction.commit(); //释放资源 session.close(); sessionFactory.close(); }
Hibernate: delete from user where id=?
图3:删除记录
INSERT INTO `user`(user_id, user_name, password, email) VALUES ( '003', 'user2', 'user2', '9dfasdfa@163.com'); INSERT INTO `user`(user_id, user_name, password, email) VALUES ( '004', 'user3', 'user3', '76543345@163.com'); INSERT INTO `user`(user_id, user_name, password, email) VALUES ( '005', 'user4', 'user4', '234543546@163.com');
/** * 使用 HQL 查询 */ @Test public void testHqlQuery() { //加载 Hibernate 核心配置文件 Configuration configuration = new Configuration().configure(); //创建一个 SessionFactory 用来获取 Session 连接对象 SessionFactory sessionFactory = configuration.buildSessionFactory(); //获取session 连接对象 Session session = sessionFactory.openSession(); //开始事务 Transaction transaction = session.beginTransaction(); //创建 HQL 语句,语法与 SQL 类似,但操作的是实体类及其属性 Query query = session.createQuery("from User where email like ?1"); //查询所有使用 163 邮箱的用户 query.setParameter(1, "%@163.com%"); //获取结果集 List<User> resultList = query.getResultList(); //遍历结果集 for (User user : resultList) { System.out.println(user); } //提交事务 transaction.commit(); //释放资源 session.close(); sessionFactory.close(); }
Hibernate: select user0_.id as id1_0_, user0_.user_id as user_id2_0_, user0_.user_name as user_nam3_0_, user0_.password as password4_0_, user0_.email as email5_0_ from user user0_ where user0_.email like ? net.biancheng.www.po.User{id=4, userId='003', userName='user2', password='user2', email='9dfasdfa@163.com'} net.biancheng.www.po.User{id=5, userId='004', userName='user3', password='user3', email='76543345@163.com'} net.biancheng.www.po.User{id=6, userId='005', userName='user4', password='user4', email='234543546@163.com'}
Criteria criteria = session.createCriteria(User.class); criteria.add(Restrictions.eq("userName", "admin")); List<Customer> list = criteria.list();
接口 | 功能描述 |
---|---|
CriteriaBuilder | 用来生成 CriteriaQuery 实例对象的工厂。 |
CriteriaQuery | QBC 主要的查询接口,通过它可以设定需要查询的数据。 |
Root | 指定需要检索的对象图的根节点对象。 |
Selection | 用来指定查询语句。 |
Expression | Selection 接口的子接口,用来指定查询表达式。 |
Predicate | Expression 接口的子接口,用来指定查询条件。 |
方法 | 描述 |
---|---|
equal() | 等于 |
notEqual() | 不等于 |
gt() | 大于 |
ge() | 大于等于 |
lt() | 小于 |
le() | 小于等于 |
between() | 在……之间 |
like() | 相似 |
isNotEmpty() | 不为空 |
and() | 和 |
or() | 或 |
/** * QBC 查询 */ @Test public void testQbcQuery() { //加载 Hibernate 核心配置文件 Configuration configuration = new Configuration().configure(); //创建一个 SessionFactory 用来获取 Session 连接对象 SessionFactory sessionFactory = configuration.buildSessionFactory(); //获取session 连接对象 Session session = sessionFactory.openSession(); //开始事务 Transaction transaction = session.beginTransaction(); //获得 CriteriaBuilder 对象 CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); //构建 CriteriaQuery 查询对象 CriteriaQuery<User> criteria = criteriaBuilder.createQuery(User.class); //添加查询条件 Root<User> from = criteria.from(User.class); Predicate like = criteriaBuilder.like(from.get("email"), "%@163.com%"); criteria.where(criteriaBuilder.and(like)); //获取结果集 List<User> list = session.createQuery(criteria).getResultList(); //遍历结果集 for (User user : list) { System.out.println(user); } //提交事务 transaction.commit(); //释放资源 session.close(); sessionFactory.close(); }
Hibernate: select user0_.id as id1_0_, user0_.user_id as user_id2_0_, user0_.user_name as user_nam3_0_, user0_.password as password4_0_, user0_.email as email5_0_ from user user0_ where user0_.email like ? net.biancheng.www.po.User{id=4, userId='003', userName='user2', password='user2', email='9dfasdfa@163.com'} net.biancheng.www.po.User{id=5, userId='004', userName='user3', password='user3', email='76543345@163.com'} net.biancheng.www.po.User{id=6, userId='005', userName='user4', password='user4', email='234543546@163.com'}
/** * SQL 查询 */ @Test public void testSqlQuery() { //加载 Hibernate 核心配置文件 Configuration configuration = new Configuration().configure(); //创建一个 SessionFactory 用来获取 Session 连接对象 SessionFactory sessionFactory = configuration.buildSessionFactory(); //获取session 连接对象 Session session = sessionFactory.openSession(); //开始事务 Transaction transaction = session.beginTransaction(); //构建 sql 查询 NativeQuery sqlQuery = session.createSQLQuery("select * from user where email like '%163.com%'"); sqlQuery.addEntity(User.class); //获得结果集 List<User> resultList = sqlQuery.getResultList(); //遍历结果集 for (User user : resultList) { System.out.println(user); } //提交事务 transaction.commit(); //释放资源 session.close(); sessionFactory.close(); }
Hibernate: select * from user where email like '%163.com%' net.biancheng.www.po.User{id=4, userId='003', userName='user2', password='user2', email='9dfasdfa@163.com'} net.biancheng.www.po.User{id=5, userId='004', userName='user3', password='user3', email='76543345@163.com'} net.biancheng.www.po.User{id=6, userId='005', userName='user4', password='user4', email='234543546@163.com'}
本文链接:http://task.lmcjl.com/news/15778.html