- package Dao;
- import java.util.List;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import org.junit.Test;
- import domain.User;
- import utils.HibernateUtils;
- public class UserDao {
- //增加用户
- @Test
- public void addUser(){
- Session session = null;
- Transaction ts = null;
- try {
- session = HibernateUtils.getSession();
- ts = session.beginTransaction();
- User user = new User();//瞬时对象
- user.setName("小王");
- user.setSex("女");
- user.setAge(33);
- user.setHeight(180);
- user.setHobby("做饭");
- session.save(user);//持久对象
- ts.commit();
- } catch (Exception e) {
- if(ts != null)
- {
- ts.rollback();
- }
- e.printStackTrace();
- }
- }
- //查询用户
- @Test
- public void findUser(){
- Session session = null;
- Transaction ts = null;
- try {
- session = HibernateUtils.getSession();
- ts = session.beginTransaction();
- User user = session.get(User.class, 4);//持久对象
- System.out.println(user);
- ts.commit();
- } catch (Exception e) {
- if(ts != null)
- {
- ts.rollback();
- }
- e.printStackTrace();
- }
- }
- //修改用户
- @Test
- public void updateUser(){
- Session session = null;
- Transaction ts = null;
- try {
- session = HibernateUtils.getSession();
- ts = session.beginTransaction();
- User user = session.get(User.class, 4);
- user.setAge(25);
- user.setHobby("吃饭");
- session.update(user);//持久对象
- System.out.println(user);
- ts.commit();
- } catch (Exception e) {
- if(ts != null)
- {
- ts.rollback();
- }
- e.printStackTrace();
- }
- }
- //删除用户
- @Test
- public void deleteUser(){
- Session session = null;
- Transaction ts = null;
- try {
- session = HibernateUtils.getSession();
- ts = session.beginTransaction();
- User user = session.get(User.class, 6);
- session.delete(user);
- System.out.println(user);
- ts.commit();
- } catch (Exception e) {
- if(ts != null)
- {
- ts.rollback();
- }
- e.printStackTrace();
- }
- }
- //简单查询
- @Test
- public void queryUser(){
- Session session = null;
- Transaction ts = null;
- try {
- session = HibernateUtils.getSession();
- ts = session.beginTransaction();
- Query query = session.createQuery("from User");
- List<User> users = query.list();
- for (User user : users) {
- System.out.println(user);
- }
- ts.commit();
- } catch (Exception e) {
- if(ts != null)
- {
- ts.rollback();
- }
- e.printStackTrace();
- }
- }
- //属性查询
- @Test
- public void queryUserName(){
- Session session = null;
- Transaction ts = null;
- try {
- session = HibernateUtils.getSession();
- ts = session.beginTransaction();
- Query query = session.createQuery("select u.name from User u");
- List<String> names = query.list();
- for (String user : names) {
- System.out.println(user);
- }
- ts.commit();
- } catch (Exception e) {
- if(ts != null)
- {
- ts.rollback();
- }
- e.printStackTrace();
- }
- }
- //实例化查询
- @Test
- public void queryUserExample(){
- Session session = null;
- Transaction ts = null;
- try {
- session = HibernateUtils.getSession();
- ts = session.beginTransaction();
- Query query = session.createQuery("from User where age = 21");
- List<User> users = query.list();
- for (User user : users) {
- System.out.println(user);
- }
- ts.commit();
- } catch (Exception e) {
- if(ts != null)
- {
- ts.rollback();
- }
- e.printStackTrace();
- }
- }
- //统计查询
- @Test
- public void queryUserGroup(){
- Session session = null;
- Transaction ts = null;
- try {
- session = HibernateUtils.getSession();
- ts = session.beginTransaction();
- Query query = session.createQuery("select avg(u.age) from User u");
- Number avgage = (Number) query.uniqueResult();
- System.out.println("平均年龄为:"+avgage);
- ts.commit();
- } catch (Exception e) {
- if(ts != null)
- {
- ts.rollback();
- }
- e.printStackTrace();
- }
- }
- //子查询
- @Test
- public void queryUserIn(){
- Session session = null;
- Transaction ts = null;
- try {
- session = HibernateUtils.getSession();
- ts = session.beginTransaction();
- Query query = session.createQuery("from User u where u.age>(select avg(age) from User)");
- User user = null;
- List list = query.list();
- for (int i =0;i<list.size();i++) {
- user = (User) list.get(i);
- System.out.println(i+"姓名:"+user.getName()+"——年龄:"+user.getAge());
- }
- ts.commit();
- } catch (Exception e) {
- if(ts != null)
- {
- ts.rollback();
- }
- e.printStackTrace();
- }
- }
- }
对象增删改查与SQL查询
最新推荐文章于 2024-06-02 06:45:00 发布