结合以前一个项目,将普通的jdbc进行了相关整合,全部改写成了hibernate接口

项目名称:短视频分享平台

主要功能:用户模块:注册、登录、编辑资料、查看用户相关

分类模块:分类添加、查看

视频共享模块:查看、上传、删除

点赞模块:是否已赞、点赞

评论模块:评论、查看

关注模块:关注、查看关注列表、是否已关注、取消关注

实体类:

用户 User

分类 Find

视频信息 Video

点赞信息 Praise

评论信息 Post

关注信息 Follow

对应关系:  一个用户对应多个视频,对应多条评论,对应多个点赞信息,对应多个关注信息。

一个分类对应多个视频

一个视频对应一个分类,对应一个用户,对应多个点赞信息,对应多个评论信息

      一个赞对应一个用户,对应一个视频

一条评论对应一个用户,对应一个视频

一个关注对应一个用户

实体类:

1.User

public class User{
        private Integer userID;
    private String username;
    private String password;
    private String sex;
    private String email;
    private String priority;
    private String icon;
    private String introduce;

    private Set<Video> videos = new HashSet<>();
    private Set<Post> posts = new HashSet<>();
    private Set<Praise> praises = new HashSet<>();

    private Set<Follow> follows1 = new HashSet<>();
    private Set<Follow> follows2 = new HashSet<>();
        //get and set方法
        ......
}    

2.Find

public class Find {
private int ID;
private String find;

private Set<Video> videos = new HashSet<>();
      //get and set
      ......
}

3.Video

public class Video{
    private int videoID;
    private String content;
    private Timestamp date;
    private String path;
    private int viewNum;
    private String isuse;
    private User user;
    private Find find;

    private Set<Post> posts = new HashSet<>();
    private Set<Praise> praises = new HashSet<>();
        //get and set
        ......
}    

4.Post

public class Post {
    private int postID;
    private String content;
    private Timestamp date;

    private Video video;
    private User user;
        //get and set
        ......
}

5.Praise

public class Praise {
    private Integer praiseID;

    private Video video;
    private User user;
        //get and set
        ......
}

6.Follow

public class Follow {
    private int followID;

    private User user1;
    private User user2;
       //get and set
       ......
}    

实体配置

1.User.hbm.xml

<hibernate-mapping>
    <class name="bean.User" table="user">
        <id name="userID" column="userID">
            <generator class="native"></generator>
        </id>
        <property name="username" length="16"></property>
        <property name="password" length="16"></property>
        <property name="sex" length="16"></property>
        <property name="email" ></property>
        <property name="priority"></property>
        <property name="icon"></property>
        <property name="introduce"></property>

        <set name="videos" inverse="true">
            <key column="userID"></key>
            <one-to-many class="bean.Video"/>
        </set>
        <set name="posts" inverse="true">
            <key column="userID"></key>
            <one-to-many class="bean.Post"/>
        </set>
        <set name="praises" inverse="true">
            <key column="userID"></key>
            <one-to-many class="bean.Praise"/>
        </set>

        <set name="follows1" inverse="true">
            <key column="user1"></key>
            <one-to-many class="bean.Follow"/>
        </set>
        <set name="follows2" inverse="true">
            <key column="user2"></key>
            <one-to-many class="bean.Follow"/>
        </set>
    </class>
</hibernate-mapping>

2.Find.hbm.xml

<hibernate-mapping>
    <class name="bean.Find" table="find">
        <id name="ID" column="ID">
            <generator class="native"></generator>
        </id>
        <property name="find" length="64"></property>
        <set name="videos">
            <key column="findID"></key>
            <one-to-many class="bean.Video"/>
        </set>
    </class>
</hibernate-mapping>

3.Video.hbm.xml

<hibernate-mapping>
    <class name="bean.Video" table="video">
        <id name="videoID" column="videoID">
            <generator class="native"></generator>
        </id>
        <property name="content"></property>
        <property name="date"></property>
        <property name="path"></property>
        <property name="viewNum"></property>
        <property name="isuse"></property>

        <set name="posts" cascade="delete">
            <key column="videoID"></key>
            <one-to-many class="bean.Post"/>
        </set>
        <set name="praises" cascade="delete">
            <key column="videoID"></key>
            <one-to-many class="bean.Praise"/>
        </set>

        <many-to-one name="user" class="bean.User" column="userID"></many-to-one>
        <many-to-one name="find" class="bean.Find" column="findID"></many-to-one>
    </class>
</hibernate-mapping>

4.Praise.hbm.xml

<hibernate-mapping>
    <class name="bean.Praise" table="praise">
        <id name="praiseID" column="praiseID">
            <generator class="native"></generator>
        </id>

        <many-to-one name="user" class="bean.User" column="userID"></many-to-one>
        <many-to-one name="video" class="bean.Video" column="videoID"></many-to-one>
    </class>
</hibernate-mapping>

5.Post.hbm.xml

<hibernate-mapping>
    <class name="bean.Post" table="post">
        <id name="postID" column="postID">
            <generator class="native"></generator>
        </id>
        <property name="content"></property>
        <property name="date"></property>

        <many-to-one name="user" class="bean.User" column="userID"></many-to-one>
        <many-to-one name="video" class="bean.Video" column="videoID"></many-to-one>
    </class>
</hibernate-mapping>

6.Follow.hbm.xml

<hibernate-mapping>
    <class name="bean.Follow" table="follow">
        <id name="followID" column="followID">
            <generator class="native"></generator>
        </id>

        <many-to-one name="user1" class="bean.User" column="user1"></many-to-one>
        <many-to-one name="user2" class="bean.User" column="user2"></many-to-one>
    </class>
</hibernate-mapping>

dao层

用户dao

public class UserDaoImpl extends AbstractDao implements UserDao{

    @Override
    public int addUser(User user) {
        if (queryUserByUsername(user)!=null) {
            return 404;
        }
        SessionFactory sessionFactory = null;
        Session session = null;
        Transaction transaction =null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.getCurrentSession();
            transaction = session.beginTransaction();
            session.save(user);
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
            return 0;
        }
        return 1;
    }

    @Override
    public User getUser(User user) {
        SessionFactory sessionFactory = null;
        Session session = null;
        Transaction transaction =null;
        User iUser = null;
        try {

            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.getCurrentSession();
            transaction = session.beginTransaction();
            Query<User> query = session.createQuery("from User u where u.username=:username and u.password=:password ");
            query.setParameter("username", user.getUsername());
            query.setParameter("password", user.getPassword());
            List<User> uList = query.list();
            if (uList.size()==1) {
                iUser = uList.get(0);
            }
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return iUser;
    }

    @Override
    public User updateUser(File icon, String extensions, User iUser, User user) {
        user.setUserID(iUser.getUserID());
        user.setPassword(iUser.getPassword());
        SessionFactory sessionFactory = null;
        Session session = null;
        Transaction transaction =null;
        if (extensions!=null) {
            String iconPath = UploadUtil.saveFile(icon, extensions, iUser.getUserID());
            user.setIcon(iconPath);
        }

        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.getCurrentSession();
            transaction = session.beginTransaction();
            session.update(user);
            user = session.get(User.class, iUser.getUserID());
            transaction.commit();

        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return user;
    }

    @Override
    public User queryUserByID(User iUser) {
        SessionFactory sessionFactory = null;
        Session session = null;
        Transaction transaction =null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.getCurrentSession();
            transaction = session.beginTransaction();
            iUser = session.get(User.class, iUser.getUserID());
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return iUser;
    }

    public User queryUserByUsername(User iUser) {
        SessionFactory sessionFactory = null;
        Session session = null;
        Transaction transaction =null;
        User user = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.getCurrentSession();
            transaction = session.beginTransaction();
            Query query = session.createQuery("from User u where u.username=:username");
            query.setParameter("username", iUser.getUsername());
            List<User> uList = query.list();
            if(uList.size()>0)
            user = (User) query.list().get(0);
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return user;
    }

}

视频dao

public class VideoDaoImpl extends AbstractDao implements VideoDao{

    @Override
    public Find queryFind(int findID) {
        Session session = null;
        Transaction transaction = null;
        Query<Find> query = null;
        Find find = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            query = session.createQuery("from Find f where f.ID=:ID");
            query.setParameter("ID", findID);
            find = query.list().get(1);
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return find;
    }

    @Override
    public List<Find> queryFindList() {
        if(findNum()==0) {
            saveFinds();
        }
        Session session = null;
        Transaction transaction = null;
        Query<Find> query = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            query = session.createQuery("from Find");
            List<Find> finds = query.list();
            transaction.commit();
            return finds;
        }catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return null;
    }

    @Override
    public long findNum() {
        Session session = null;
        Transaction transaction = null;
        Query query = null;
        long num = 0;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            query = session.createQuery("select count(*) from Find");
            num = (long)query.uniqueResult();
            transaction.commit();
        }catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return num;
    }

    @Override
    public void saveFinds() {
        Session session = null;
        Transaction transaction = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            Find find1 = new Find();
            find1.setFind("游戏");
            Find find2 = new Find();
            find2.setFind("音乐");
            Find find3 = new Find();
            find3.setFind("学习");
            session.save(find1);
            session.save(find2);
            session.save(find3);
            transaction.commit();
        }catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
    }

    @Override
    public Video addVideo(File resource,Video video,int findID, String extensions) {
        Session session = null;
        Transaction transaction = null;
        String videoPath = UploadUtil.saveFile(resource, extensions, video.getUser().getUserID());
        video.setPath(videoPath);
        video.setDate(new Timestamp(new Date().getTime()));
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            Find find = session.get(Find.class, findID);
            video.setFind(find);
            session.save(video);
            transaction.commit();
            return video;
        }catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }

        return null;
    }

    @Override
    public int getVideoID(String path) {
        Session session = null;
        Transaction transaction = null;
        Query query = null;
        int videoID = 0;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            query = session.createQuery("select v.videoID from Video v where v.path=:path");
            query.setParameter("path", path);
            videoID = (int) query.uniqueResult();
            transaction.commit();
        }catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return videoID;
    }

    @Override
    public List<VideoView> getVideos(String type) {
        List<VideoView> videoViews = new ArrayList<>();
        Session session = null;
        Transaction transaction = null;
        Query query = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            String hql = "from Video v order by videoID desc";
            query = session.createQuery(hql);
            List<Video> videos = query.list();
            for(Video video :videos) {
                VideoView videoView = new VideoView();

                videoView.setVideo(video);

                videoView.setUser(video.getUser());

                videoView.setFind(video.getFind());
                //videoView.setPraiseNum(praiseDao.getPraiseNum(video.getVideoID()));
                videoView.setPraiseNum(video.getPraises().size());
                //videoView.setPostNum(postDao.getPostNum(video.getVideoID()));
                videoView.setPostNum(video.getPosts().size());
                System.out.println(videoView);
                videoViews.add(videoView);
            }
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return videoViews;
    }

    @Override
    public VideoView queryVideoView(int videoID) {
        Session session = null;
        Transaction transaction = null;
        Query query = null;
        VideoView videoView = new VideoView();
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            Video video = session.get(Video.class,videoID);
            video.setViewNum(video.getViewNum()+1);
            User user = video.getUser();
            Find find = video.getFind();
            videoView.setFind(find);

            videoView.setPostNum(video.getPosts().size());

            videoView.setPraiseNum(video.getPraises().size());
            videoView.setUser(user);
            videoView.setVideo(video);
            System.out.println(videoView);
            transaction.commit();
            return videoView;
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return null;

    }

    @Override
    public List<VideoView> getVideoViewsByUserID(int userID) {
        List<VideoView> videoViews = new ArrayList<>();
        Session session = null;
        Transaction transaction = null;
        Query query = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            User user = session.get(User.class, userID);
            query = session.createQuery("from Video where userID=? order by videoID desc");
            query.setParameter(0, user.getUserID());
            List<Video> videos = query.list();
            for(Video video :videos) {
                VideoView videoView = new VideoView();
                videoView.setVideo(video);
                videoView.setUser(user);
                videoView.setFind(video.getFind());
                videoView.setPraiseNum(video.getPraises().size());
                videoView.setPostNum(video.getPosts().size());
                System.out.println(videoView);
                videoViews.add(videoView);
            }
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return videoViews;
    }

    @Override
    public int deleteVideo(int videoID, int userID) {
        Session session = null;
        Transaction transaction = null;
        Query query = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            String hql = "from Video v where videoID=? and userID=?";
            query = session.createQuery(hql);
            query.setParameter(0, videoID);
            query.setParameter(1, userID);
            //query.executeUpdate();
            Video video = (Video) query.list().get(0);
            session.delete(video);
            transaction.commit();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return 0;
    }

}

PraiseDao

public class PraiseDaoImpl extends AbstractDao implements PraiseDao{

    @Override
    public int addPraise(Praise praise,int videoID) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            Video video = session.get(Video.class, videoID);
            praise.setVideo(video);
            session.save(praise);
            transaction.commit();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return 0;
    }

    @Override
    public int queryPraise(User iUser, VideoView videoView) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            String hql = "from Praise where userID=? and videoID=?";
            Query<Praise> query = session.createQuery(hql);
            query.setParameter(0, iUser.getUserID());
            query.setParameter(1, videoView.getVideo().getVideoID());
            List<Praise> praises = query.list();
            transaction.commit();
            return praises.size();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return 0;

    }

}

PostDao

public class PostDaoImpl extends AbstractDao implements PostDao {

    @Override
    public List<PostView> getPostViews(int videoID) {
        List<PostView> postViews = new ArrayList<>();
        Session session = null;
        Transaction transaction = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            //Video video = session.get(Video.class, videoID);
            Query<Post> query = session.createQuery("from Post where videoID=? order by postID desc");
            query.setParameter(0, videoID);
            List<Post> posts = query.list();
            for(Post post:posts) {
                PostView postView = new PostView();
                User user = post.getUser();
                postView.setPost(post);
                postView.setUser(user);
                System.out.println(postView);
                postViews.add(postView);
            }
            transaction.commit();

        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return postViews;

    }

    @Override
    public int addPost(int videoID, int userID, String content) {
        Session session = null;
        Transaction transaction = null;
        Post post = new Post();
        post.setDate(new Timestamp(new Date().getTime()));
        post.setContent(content);
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            Video video = session.get(Video.class, videoID);
            User user = session.get(User.class, userID);
            post.setVideo(video);
            post.setUser(user);
            session.save(post);
            transaction.commit();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return 0;
    }}

FollowDao

public class FollowDaoImpl extends AbstractDao implements FollowDao{
    @Override
    public List<FollowView> queryFollows(int userID) {
        List<FollowView> followViews = new ArrayList<>();
        Session session = null;
        Transaction transaction = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            String hql = "from Follow f where f.user1=:user1";
            User user = session.get(User.class, userID);
            Query<Follow> query = session.createQuery(hql);
            query.setParameter("user1",user );
            List<Follow> follows  = query.list();
            for(Follow follow :follows) {
                FollowView followView = new FollowView();
                followView.setFollow(follow);
                User user2 = follow.getUser2();
                followView.setUser(user2);
                System.out.println(followView);
                followViews.add(followView);
            }
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return followViews;

    }
    @Override
    public int deleteFollow(User iUser, int userID2) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            User user1 = session.get(User.class, iUser.getUserID());
            User user2 = session.get(User.class, userID2);
            String hql = "from Follow f where f.user1=:user1 AND f.user2=:user2";
            Query<Follow> query = session.createQuery(hql);
            query.setParameter("user1",user1);
            query.setParameter("user2", user2);
            Follow follow = query.list().get(0);
            session.delete(follow);
            transaction.commit();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return 0;
    }
    @Override
    public long queryFollowedNum(int userID) {
        long num = 0;
        Session session = null;
        Transaction transaction = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            User user = session.get(User.class, userID);
            String hql ="select count(*) from Follow f where f.user2=:user2";
            Query query = session.createQuery(hql);
            query.setParameter("user2", user);
            num =(long) query.uniqueResult();
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return num;
    }
    @Override
    public long queryFollowNum(int userID) {
        long num = 0;
        Session session = null;
        Transaction transaction = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            User user = session.get(User.class, userID);
            String hql ="select count(*) from Follow f where f.user1=:user1";
            Query query = session.createQuery(hql);
            query.setParameter("user1", user);
            num = (long) query.uniqueResult();
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return num;
    }
    @Override
    public long getFollow(int userID1, int userID2) {
        long num = 0;
        Session session = null;
        Transaction transaction = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            User user1 = session.get(User.class, userID1);
            User user2 = session.get(User.class, userID2);
            String hql ="select count(*) from Follow f where f.user1=:user1 and f.user2=:user2";
            Query query = session.createQuery(hql);
            query.setParameter("user1", user1);
            query.setParameter("user2", user2);
            num = (long) query.uniqueResult();
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return num;

    }
    @Override
    public int addFollow(int userID1, int userID2) {
        int num = 0;
        Session session = null;
        Transaction transaction = null;
        Follow follow = new Follow();
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            User user1 = session.get(User.class, userID1);
            User user2 = session.get(User.class, userID2);
            follow.setUser1(user1);
            follow.setUser2(user2);
            session.save(follow);
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(transaction!=null) {
                transaction.rollback();
            }
        }
        return num;
    }

}

     

05-12 14:04