新闻发布系统:dao层数据操作的接口设计

概要

 

前两篇博客对项目的需求分析,数据库的设计和编码,基本的code环境已经搭建:

传送门:新闻发布系统:需求分析以及项目的设计

        新闻发布系统:数据库的编码和ssm环境的搭建

我们现在开始对数据表的增删改查的编码。

 

Dao层设计

User:实体类的设计编码:

我们的数据库中有4个表:user表(7个字段),news表(7个字段),comment表(5个字段),category表(2个字段)。除了category外其他表都有多个字段,所以我们需要针对表字段设计实体类,category只有两个字段我们可以使用enum(枚举类型)来定义。

user表的实体类的设计:字段对应属性名,并且在User类中自动增加getter和setter方法,toString方法。

 

 

为什么要设计getter和setter方法:为了方便获取/设置User的属性值。

为什么要设计toString方法:方便于字符流的输出。

  1 package entity;
  2 
  3 import java.util.Date;
  4 
  5 /**
  6  * @author yangxin
  7  * @time 2018/12/24  14:20
  8  */
  9 public class User {
 10     private long userId;
 11 
 12     private long userType;
 13 
 14     private String userName;
 15 
 16     private String userPassword;
 17 
 18     private String userEmail;
 19 
 20     private int userAge;
 21 
 22     private Date createTime;
 23 
 24     public User(long userType, String userName, String userPassword, String userEmail, int userAge) {
 25         this.userType = userType;
 26         this.userName = userName;
 27         this.userPassword = userPassword;
 28         this.userEmail = userEmail;
 29         this.userAge = userAge;
 30     }
 31 
 32     public User() {
 33     }
 34 
 35     public String getUserPassword() {
 36         return userPassword;
 37     }
 38 
 39     public void setUserPassword(String userPassword) {
 40         this.userPassword = userPassword;
 41     }
 42 
 43     @Override
 44     public String toString() {
 45         return "User{" +
 46                 "userId=" + userId +
 47                 ", userType=" + userType +
 48                 ", userName='" + userName + '\'' +
 49                 ", userPassword='" + userPassword + '\'' +
 50                 ", userEmail='" + userEmail + '\'' +
 51                 ", userAge=" + userAge +
 52                 ", createTime=" + createTime +
 53                 '}';
 54     }
 55 
 56     public long getUserId() {
 57         return userId;
 58     }
 59 
 60     public void setUserId(long userId) {
 61         this.userId = userId;
 62     }
 63 
 64     public long getUserType() {
 65         return userType;
 66     }
 67 
 68     public void setUserType(long userType) {
 69         this.userType = userType;
 70     }
 71 
 72     public String getUserName() {
 73         return userName;
 74     }
 75 
 76     public void setUserName(String userName) {
 77         this.userName = userName;
 78     }
 79 
 80     public String getUserEmail() {
 81         return userEmail;
 82     }
 83 
 84     public void setUserEmail(String userEmail) {
 85         this.userEmail = userEmail;
 86     }
 87 
 88     public int getUserAge() {
 89         return userAge;
 90     }
 91 
 92     public void setUserAge(int userAge) {
 93         this.userAge = userAge;
 94     }
 95 
 96     public Date getCreateTime() {
 97         return createTime;
 98     }
 99 
100     public void setCreateTime(Date createTime) {
101         this.createTime = createTime;
102     }
103 }
View Code

User:dao接口设计

在dao包中新建一个UserDao类,我们对User表的操作有:

①登录:通过用户和密码验证是否有用户,那么有个username和password的用户查询接口。

②:注册:首先先根据username查询是否已经存在用户,那么有个根据username查询用户的接口;并且将整个数据插入数据库,那么有个插入接口。

③:在管理员界面:将对某个用户操作时,根据Id来查询用户。

④:在管理员界面:查询所有用户。

⑤:通过邮箱找回密码,那么需要通过邮箱查询用户。

⑥:更新用户信息:更新接口。

⑦:在管理员界面 模糊查询用户列表。

经过分析:有8个接口如下:

 1 package dao;
 2 
 3 import entity.User;
 4 import org.apache.ibatis.annotations.Param;
 5 
 6 import java.util.List;
 7 
 8 /**
 9  * @author yangxin
10  * @time 2018/12/24  14:30
11  */
12 public interface UserDao {
13     /*
14     * 插入用户
15     * */
16      int insertUser(User user);
17 
18     /*
19     * 根据昵称和密码查询用户验证登录
20     * */
21     User queryById(@Param("userName") String userName, @Param("userPassword") String userPassword);
22 
23     /*
24     * 根据名称查找用户
25     * */
26     User queryByName(@Param("userName") String userName);
27 
28     /*
29      * 根据id查找用户
30      * */
31     User queryByOnlyId(@Param("userId") long userId);
32 
33     /*
34     * 更新用户信息
35     * */
36     int updateUser(User user);
37 
38     /*
39     * 通过邮箱验证
40     * */
41     User queryByOnlyEmail(@Param("email") String email,@Param("userName") String userName);
42 
43     /*
44     * 查询所有用户信息
45     * */
46     List<User> queryAllUser();
47 
48     /*
49     * 模糊查询用户列表
50     * */
51     List<User> selectUserByLike(@Param("key")String key);
52 }
View Code

 

User:Mapper设计dao

在mapper中,根据到接口的功能来写对应的标签:查询:<select>;插入:<insert>;更新:<update>;删除:<delete>。根据接口函数的参数:parameterType;根据接口函数的返回值:resultType

 

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="dao.UserDao">
 6     <insert id="insertUser" parameterType="entity.User">
 7           insert ignore into user(user_type,user_password,user_name,user_email,user_age)
 8           values (0,#{userPassword},#{userName},#{userEmail},#{userAge})
 9     </insert>
10     <select id="queryById" resultType="entity.User">
11           select user_id,user_type,user_password,user_name,user_email,user_age,create_time
12           from user
13           where user_name=#{userName}
14           and  user_password=#{userPassword}
15     </select>
16     <select id="queryAllUser" resultType="entity.User">
17           select user_id,user_type,user_password,user_name,user_email,user_age,create_time
18           from user
19     </select>
20     <select id="selectUserByLike" resultType="entity.User">
21         <bind name="keys" value="'%' + key + '%'" />
22           select user_id,user_type,user_password,user_name,user_email,user_age,create_time
23           from user
24           where
25           user_name like #{keys}
26           or user_email like #{keys}
27           or create_time like #{keys}
28     </select>
29     <select id="queryByName" resultType="entity.User">
30           select user_id,user_type,user_password,user_name,user_email,user_age,create_time
31           from user
32           where user_name=#{userName}
33     </select>
34     <select id="queryByOnlyId" resultType="entity.User">
35           select user_id,user_type,user_password,user_name,user_email,user_age,create_time
36           from user
37           where user_id=#{userId}
38     </select>
39     <select id="queryByOnlyEmail" resultType="entity.User">
40           select user_id,user_type,user_password,user_name,user_email,user_age,create_time
41           from user
42           where user_email=#{email}
43           and user_name=#{userName}
44     </select>
45     <update id="updateUser" parameterType="entity.User">
46             update
47                 user
48             set
49                  user_type=#{userType},
50                  user_password=#{userPassword},
51                  user_name=#{userName},
52                  user_email=#{userEmail},
53                  user_age=#{userAge}
54             where user_id=#{userId}
55     </update>
56 </mapper>
View Code

 

New:实体类的设计

在entity包创建New类;按照字段设计类。

 

New:dao接口设计

在Dao包创建NewsDao接口。对News表的分析:

①:插入新闻(插入接口)。

②:根据id查询新闻。

③:根据新闻title查询新闻。

④:通过category_id获取这个类相关的新闻列表,返回值是List<User>。

⑤:通过作者Id获取这个作者相关的新闻列表。由于在作者个人中心,除了显示基础的信息外,还需要显示类型category_name,但是news表中存的是category_id。所以我们封装一个数据包NewsData,使其返回值为List<NewsData>。NewsData内容字段如下。

⑥:后台管理员获取全部新闻,需要新闻的user_name和category_name那么返回值也是List<NewsData>。

⑦:根据Id删除新闻。

⑧:模糊查询,返回值为<NewsData>。

⑨:关键字查询返回值为<NewsData>

接口设计如下:

 

 1 package dao;
 2 
 3 import dto.NewsData;
 4 import entity.New;
 5 import org.apache.ibatis.annotations.Param;
 6 
 7 import java.util.List;
 8 
 9 /**
10  * @author yangxin
11  * @time 2018/12/24  14:45
12  */
13 public interface NewDao {
14     /*
15     * 插入新闻
16     * */
17     int insertNew(New news);
18 
19     /*
20     * 根据新闻id查询新闻
21     * */
22     New queryByNewId(@Param("newId") long newId);
23 
24     /*
25      * 根据新闻name查询新闻
26      * */
27     New queryByNewName(@Param("title") String title);
28 
29     /*
30     * 根据类别id获取新闻列表
31     * */
32     List<New> queryByCategoryId(@Param("categoryId") long categoryId);
33 
34     /*
35     * 根据作者Id获取作者所写的新闻列表
36     * */
37     List<NewsData> queryByUserId(@Param("userId") long userId);
38 
39     /*
40      * 后台管理员查看所有发表的新闻,并对其操作
41      * */
42     List<NewsData> queryAllNews();
43 
44     /*
45     * 删除指定Id的新闻条目
46     * */
47     int deleteNew(@Param("newId")long newId);
48 
49     /*
50     * 更新新闻数据
51     * */
52     int updateNew(New news);
53 
54     /*
55     * 模糊查询
56     * */
57     List<NewsData> selectByLike(@Param("key")String key);
58 
59     /*
60      * 关键字查询
61      * */
62     List<NewsData> selectByKeyWords(@Param("keyWords")String keyWords);
63 
64 }
View Code

 

User:Mapper设计

在Mapper文件夹创建NewDao.xml,头标签拷贝即可,其他类型都和UserDao.xml一致,但是这里有一个我们的封装类。

他的写法对应如下:

 

模糊查询

 

 

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <!DOCTYPE mapper
  3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5 <mapper namespace="dao.NewDao">
  6     <insert id="insertNew" parameterType="entity.New">
  7         insert ignore into new(category_id,user_id,title,content,key_words)
  8         values(#{categoryId},#{userId},#{title},#{content},#{keyWords})
  9     </insert>
 10 
 11     <select id="queryByNewId" resultType="entity.New">
 12         select new_id,category_id,user_id,title,content,create_time,key_words
 13         from new
 14         where
 15         new_id=#{newId}
 16     </select>
 17 
 18     <select id="queryByNewName" resultType="entity.New">
 19         select new_id,category_id,user_id,title,content,create_time,key_words
 20         from new
 21         where
 22         title=#{title}
 23     </select>
 24 
 25     <select id="queryByCategoryId" resultType="entity.New">
 26         select new_id,category_id,user_id,title,content,create_time,key_words
 27         from new
 28         where category_id=#{categoryId}
 29     </select>
 30 
 31     <select id="queryByUserId" resultType="dto.NewsData">
 32         select n.new_id "aNew.new_id",
 33         n.category_id "aNew.category_id",
 34         n.user_id "aNew.user_id",
 35         n.title "aNew.title",
 36         n.content "aNew.content",
 37         n.create_time "aNew.create_time",
 38         n.key_words "aNew.key_words",
 39         category_name "typeName",
 40         u.user_name
 41         from new n,user u,category cate
 42         where n.user_id=u.user_id
 43         and n.category_id=cate.category_id
 44         and u.user_id=#{userId}
 45     </select>
 46 
 47     <select id="queryAllNews" resultType="dto.NewsData">
 48         select n.new_id "aNew.new_id",
 49         n.category_id "aNew.category_id",
 50         n.user_id "aNew.user_id",
 51         n.title "aNew.title",
 52         n.content "aNew.content",
 53         n.create_time "aNew.create_time",
 54         n.key_words "aNew.key_words",
 55         category_name "typeName",
 56         u.user_name
 57         from new n,user u,category cate
 58         where n.user_id=u.user_id
 59         and n.category_id=cate.category_id
 60         order by cate.category_id desc
 61     </select>
 62 
 63 
 64     <select id="selectByLike" resultType="dto.NewsData">
 65         <bind name="keys" value="'%' + key + '%'" />
 66         select n.new_id "aNew.new_id",
 67         n.category_id "aNew.category_id",
 68         n.user_id "aNew.user_id",
 69         n.title "aNew.title",
 70         n.content "aNew.content",
 71         n.create_time "aNew.create_time",
 72         n.key_words "aNew.key_words",
 73         category_name "typeName",
 74         u.user_name
 75         from new n inner join user u on n.user_id=u.user_id
 76         inner join category cate on n.category_id=cate.category_id
 77         where
 78             cate.category_name like #{keys}
 79             or u.user_name like #{keys}
 80             or n.title like #{keys}
 81             or n.key_words like #{keys}
 82     </select>
 83 
 84     <select id="selectByKeyWords" resultType="dto.NewsData">
 85         <bind name="keys" value="'%' + keyWords + '%'" />
 86         select n.new_id "aNew.new_id",
 87         n.category_id "aNew.category_id",
 88         n.user_id "aNew.user_id",
 89         n.title "aNew.title",
 90         n.content "aNew.content",
 91         n.create_time "aNew.create_time",
 92         n.key_words "aNew.key_words",
 93         category_name "typeName",
 94         u.user_name
 95         from new n inner join user u on n.user_id=u.user_id
 96         inner join category cate on n.category_id=cate.category_id
 97         where
 98         n.key_words like #{keys}
 99     </select>
100 
101     <delete id="deleteNew">
102         delete
103         from  new
104         where new_id=#{newId}
105     </delete>
106 
107     <update id="updateNew" parameterType="entity.New">
108         update new
109         set
110             category_id=#{categoryId},
111             user_id=#{userId},
112             title=#{title},
113             content=#{content},
114             key_words=#{keyWords}
115         where new_id=#{newId}
116 
117     </update>
118 </mapper>
View Code

 

Comment:实体类的设计

在entity中创建Comment实体类,按照表字段设计如下:

 

 

Comment:Dao接口类的设计

在dao包中创建CommentDao,根据需求设计接口如下:

①:插入一条评论。

②:根据新闻Id获取该新闻的评论列表,返回值List<CommentData>;CommentData是封装的一个数据类,字段如下:默认有getter/setter和toString方法。

③:获取所有人的评论列表。返回值List<CommentData>;

④:根据用户id返回该用户曾经评论的列表。返回值List<CommentData>;

⑤:根据comment_id查询对应的评论字段。

⑥:删除评论。

⑦:模糊查询。

 

 

 1 package dao;
 2 
 3 import dto.CommentData;
 4 import entity.Comment;
 5 import org.apache.ibatis.annotations.Param;
 6 
 7 import javax.xml.crypto.Data;
 8 import java.util.List;
 9 
10 /**
11  * @author yangxin
12  * @time 2018/12/24  15:00
13  */
14 public interface CommentDao {
15     /*
16     * 添加评论
17     * */
18     int insertComment(Comment comment);
19 
20     /*
21     * 根据新闻获取评论
22     * */
23     List<CommentData> queryCommentByNewId(@Param("newId") long newId);
24 
25     /*
26      * 获取所有评论
27      * */
28     List<CommentData> queryAllComment();
29 
30     /*
31     * 显示用户参与的评论
32     * */
33     List<CommentData> queryCommentByUserId(@Param("userId") long userId);
34 
35     /*
36      * 根据id查询
37      * */
38     Comment queryCommentById(@Param("commentId") long commentId);
39 
40     /*
41     * 用户自己删除评论
42     * */
43     int deleteComment(@Param("commentId") long commentId,@Param("userId") long userId);
44 
45     /*
46      * 模糊查询
47      * */
48     List<CommentData> selectCommentByLike(@Param("key") String key);
49 }
View Code

 

Comment:mapper设计

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.CommentDao">
<insert id="insertComment" parameterType="entity.Comment">
insert ignore into new_comment(new_id,user_id,content)
values (#{newId},#{userId},#{content})
</insert>



<select id="queryAllComment" resultType="dto.CommentData">
select com.comment_id "comment.comment_id",
com.new_id "comment.new_id",
com.user_id "comment.user_id",
com.content "comment.content",
com.create_time "comment.create_time",
u.user_name "userName",
news.title "newtitle"
from new_comment com , user u,new news
where com.user_id=u.user_id
and com.new_id=news.new_id
</select>


<select id="selectCommentByLike" resultType="dto.CommentData">
<bind name="keys" value="'%' + key + '%'" />
select com.comment_id "comment.comment_id",
com.new_id "comment.new_id",
com.user_id "comment.user_id",
com.content "comment.content",
com.create_time "comment.create_time",
u.user_name "userName",
news.title "newtitle"
from new_comment com inner join user u on com.user_id=u.user_id
inner join new news on com.new_id=news.new_id
where
com.content like #{keys}
or u.user_name like #{keys}
or news.title like #{keys}
</select>

<select id="queryCommentByNewId" resultType="dto.CommentData">
select com.comment_id "comment.comment_id",
com.new_id "comment.new_id",
com.user_id "comment.user_id",
com.content "comment.content",
com.create_time "comment.create_time",
u.user_name "userName",
news.title "newtitle"
from new_comment com , user u,new news
where com.user_id=u.user_id
and com.new_id=news.new_id
and com.new_id=${newId}
</select>

<select id="queryCommentByUserId" resultType="dto.CommentData">
select com.comment_id "comment.comment_id",
com.new_id "comment.new_id",
com.user_id "comment.user_id",
com.content "comment.content",
com.create_time "comment.create_time",
u.user_name "userName",
news.title "newtitle"
from new_comment com , user u,new news
where com.user_id=u.user_id
and com.new_id=news.new_id
and com.user_id=#{userId}
</select>

<select id="queryCommentById" resultType="entity.Comment">
select comment_id,new_id,user_id,content,create_time
from new_comment
where comment_id=#{commentId}
</select>

<delete id="deleteComment">
delete from new_comment
where comment_id=#{commentId}
and user_id=#{userId}
</delete>

</mapper

 

建立测试类测试所写接口

UserDaoTest

 

 

 

 1 package dao;
 2 
 3 import entity.User;
 4 import org.junit.Test;
 5 import org.junit.runner.RunWith;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.test.context.ContextConfiguration;
 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 9 
10 import java.util.List;
11 
12 import static org.junit.Assert.*;
13 
14 /**
15  * @author yangxin
16  * @time 2018/12/24  16:40
17  */
18 @RunWith(SpringJUnit4ClassRunner.class)
19 @ContextConfiguration({"classpath:spring/springDao-config.xml"})
20 public class UserDaoTest {
21 
22     @Autowired
23     private UserDao userDao;
24 
25     @Test
26     public void insertUserTest(){
27         User user=new User(2,"王玉凤","123456a","855566@163.com",18);
28         System.out.println(userDao.insertUser(user));
29     }
30 
31     @Test
32     public void queryByIdTest(){
33         User user=userDao.queryById("yangxin","123");
34         System.out.println("User:"+user);
35     }
36 
37     @Test
38     public void updateUserTest(){
39         User user=new User(2,"yangxin","123","7574",18);
40         System.out.println(":"+userDao.updateUser(user));
41     }
42 
43     @Test
44     public void queryByNameTest(){
45         User user=userDao.queryByName("yangxin");
46         System.out.println("User:"+user);
47     }
48     @Test
49     public void queryByOnlyIdTest(){
50         User user=userDao.queryByOnlyId(1007);
51         System.out.println("User:"+user);
52     }
53 
54     @Test
55     public void queryByOnlyEmailTest(){
56         System.out.println("User:"+userDao.queryByOnlyEmail("7574","yangxin"));
57     }
58 
59     @Test
60     public void queryAllUserTest(){
61         System.out.println("List<user>:"+userDao.queryAllUser());
62     }
63 
64     @Test
65     public void selectUserByLikeTest(){
66         String key="yangxin";
67         System.out.println("List<User>:"+userDao.selectUserByLike(key));
68     }
69 
70 
71 }

 

点击每个测试函数的左边运行按钮。即可测试,我就只演示这一个:

结果:

 

 

NewDaoTest:

 1 package dao;
 2 
 3 import dto.NewsData;
 4 import entity.New;
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.test.context.ContextConfiguration;
 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 
11 import java.util.List;
12 
13 import static org.junit.Assert.*;
14 
15 /**
16  * @author yangxin
17  * @time 2018/12/24  20:39
18  */
19 @RunWith(SpringJUnit4ClassRunner.class)
20 @ContextConfiguration({"classpath:spring/springDao-config.xml"})
21 public class NewDaoTest {
22 
23     @Autowired
24     private NewDao newDao;
25 
26     @Test
27     public void insertNewTest(){
28         New n=new New(2,1009,"对对对的治国策略","fasdifjasodpaf","对对对");
29         System.out.println(":"+newDao.insertNew(n));
30     }
31 
32     @Test
33     public void queryByNewIdTest(){
34         int id=1;
35         New aNew = newDao.queryByNewId(id);
36         System.out.println("New:"+aNew);
37     }
38 
39     @Test
40     public void queryByCategoryIdTest(){
41         int id=2;
42         List<New> list=newDao.queryByCategoryId(id);
43         System.out.println("List<New>:"+list);
44     }
45 
46     @Test
47     public void queryByUserIdTest(){
48         int id=1009;
49         List<NewsData> list=newDao.queryByUserId(id);
50         System.out.println("List<New>:"+list);
51     }
52 
53     @Test
54     public void deleteNewTest(){
55         int newId=1;
56         int userId=1009;
57         System.out.println("delete:"+newDao.deleteNew(newId));
58     }
59 
60     @Test
61     public void updateNewTest(){
62         New n=new New(2,1009,"对对对","545","等待22");
63         n.setNewId(2);
64         System.out.println(":"+newDao.updateNew(n));
65 
66     }
67 
68     @Test
69     public void queryByNewNameTest(){
70         String name="对对对";
71         New aNew = newDao.queryByNewName(name);
72         System.out.println("New:"+aNew);
73     }
74 
75     @Test
76     public void queryAllNewsTest(){
77         List<NewsData> list=newDao.queryAllNews();
78         System.out.println("List<New>:"+list);
79     }
80 
81     @Test
82     public void selectByLikeTest(){
83         String key="yangxin";
84         System.out.println("List<News>:"+newDao.selectByLike(key));
85     }
86 
87     @Test
88     public void selectByKeyWordsTest(){
89         String key="视频";
90         System.out.println("List<New>:"+newDao.selectByKeyWords(key));
91 
92     }
93 
94 
95 
96 }

 

CommentDaoTest:

 1 package dao;
 2 
 3 import dto.CommentData;
 4 import entity.Comment;
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.test.context.ContextConfiguration;
 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 
11 import java.util.List;
12 
13 import static org.junit.Assert.*;
14 
15 /**
16  * @author yangxin
17  * @time 2018/12/24  21:52
18  */
19 @RunWith(SpringJUnit4ClassRunner.class)
20 @ContextConfiguration({"classpath:spring/springDao-config.xml"})
21 public class CommentDaoTest {
22 
23     @Autowired
24     private CommentDao commentDao;
25 
26     @Test
27     public void insertCommentTest(){
28         Comment comment=new Comment(2,1009,"好极了");
29         System.out.println(":"+commentDao.insertComment(comment));
30     }
31 
32     @Test
33     public void queryCommentByNewIdTest(){
34         List<CommentData> list=commentDao.queryCommentByNewId(2);
35         System.out.println("List<Comment>:"+list);
36     }
37 
38     @Test
39     public void queryCommentByUserIdTest(){
40         List<CommentData> list=commentDao.queryCommentByUserId(1009);
41         System.out.println("List<Comment>:"+list);
42     }
43 
44     @Test
45     public void deleteCommentTest(){
46         System.out.println(":"+commentDao.deleteComment(1,1009));
47     }
48 
49     @Test
50     public void queryCommentByIdTest(){
51 
52         Comment comment=commentDao.queryCommentById(2);
53         System.out.println("List<Comment>:"+comment);
54     }
55 
56     @Test
57     public void queryAllCommentTest(){
58         System.out.println("info:"+commentDao.queryAllComment());
59     }
60 
61     @Test
62     public void selectCommentByLikeTest(){
63         String key="好极了";
64         System.out.println("List<Comment>:"+commentDao.selectCommentByLike(key));
65     }
66 
67 }

 

 

 结语:

 dao层的设计主要是利用mybatis对数据库表的操作,基本的逻辑还是得在Service层操作。

在entity实体类设计和数据库表的字段的设计:实体类的字段命名按照驼峰命名法,数据库的字段是下划线分割以防和数据库保留字段冲突。

 

 

本项目地址:https://github.com/fireshoot/YxNews

 

 

 

 

posted @ 2019-01-03 22:22  轻抚丶两袖风尘  阅读(1737)  评论(2编辑  收藏  举报