基于ssm框架的个人博客(8)--Blog相关数据库操作实现

来源:互联网 发布:手机截屏是那个软件 编辑:程序博客网 时间:2024/05/20 20:56

          在前面实现BlogType的时候,不知道大家是否记得在删除博客类别的时候,提到,如果博客类别下有博客的话,不允许用户删除博客。( blogtype---blog 是一对多的关系,存在外键关系),现在就要把博客这块实现,并在该位置补上相关的判断与处理。

         由上面可以得出,逆向工程将我们数据库中的一些长度较长的varchar字段,或者text类型的字段.在很多情况下,我们不需要操作所有的字段,所以在使用方法时,需要选择类似selectByExampleWithBLOBs方法这种带withblobs的才能操作所有字段。

老规矩,我们还是来实现以下Dao层的测试

(有些朋友可能不理解,我为什么还要多此一举,直接写在BlogServiceImpl下不就好了吗,为什么还要多次一举,搞个testDao?实际上,我们不去实现testDao也是ok的,直接从浏览器访问controller,由controller调用Service,Service调用Dao也是可行的。但是在这,我是从博客与代码同步的角度,出错的话,修改起来甚是麻烦,为了保证我的代码都是能跑通的,并且正确,所以在真正实现前我要先测!)

testDaoBlog.java

package com.lailai.common.vo;import java.util.List;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;import com.lailai.entity.TBlog;import com.lailai.entity.TBlogExample;import com.lailai.entity.TBlogExample.Criteria;import com.lailai.mapper.TBlogMapper;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:conf/applicationContext-dao.xml")public class testBlogDAo {@Autowiredprivate TBlogMapper blogMapper;/** * 保存博客 */@Testpublic void saveBlog() {TBlog blog = new TBlog();blog.setKeyword("关键字");blog.setTitle("标题");blog.setContent("qqqqqqqqq");int insert = blogMapper.insert(blog);System.out.println("回来" + insert);}/** * 修改博客(无法更新content) */@Testpublic void updateBlog() {TBlog blog = new TBlog();blog.setId(new Integer(9));blog.setTitle("差标题");blog.setKeyword("咕咕咕咕");blog.setContent("aisidfds");int i = blogMapper.updateByPrimaryKeyWithBLOBs(blog);System.out.println("回来" + i);}/** * 删除博客 * */@Testpublic void deleteBlog() {int i = blogMapper.deleteByPrimaryKey(new Integer(11));System.out.println("回来" + i);}/** * 按ID查询博客 */@Testpublic void getById() {TBlogExample example = new TBlogExample();Criteria criteria = example.createCriteria();criteria.andIdEqualTo(new Integer(2));List<TBlog> list = blogMapper.selectByExampleWithBLOBs(example);for (TBlog t : list) {System.out.println(t.getTitle());System.out.println(t.getContent());}}/** * 修改博客 */@Testpublic void SelectAllBlog() {// 设置分页信息PageHelper.startPage(1, 2);TBlogExample example = new TBlogExample();Criteria criteria = example.createCriteria();criteria.andTitleLike("%三%");List<TBlog> list = blogMapper.selectByExampleWithBLOBs(example);System.out.println(list.get(0).getTitle());PageInfo<TBlog> pageInfo = new PageInfo<>(list);// 取总记录数/*long total = pageInfo.getTotal();System.out.println("验证");System.out.println(pageInfo.getPageSize());System.out.println(pageInfo.getPageNum());System.out.println(pageInfo.getTotal());System.out.println(pageInfo.getList());*/}}


基本的一些常用的操作,我都已经测试过了,暂时可行。

现在就真正的在三层中实现


阅读全文
0 0