基于ssm框架的个人博客(5)--Dao层设计及测试

前台的页面完成了一部分,那么是时候开发Dao层了,前面我们提到了mybatis的逆向工程,也展示了一些逆向生成的一些配置文件,实体类......现在我们就基于ssm框架,对Dao进行开发,由于代码比较简单,在此不做解释,直接看注释。

贴上代码

package testDao;

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.common.utils.EasyUIDataGridResult;

import com.lailai.entity.TBlogtype;
import com.lailai.entity.TBlogtypeExample;
import com.lailai.mapper.TBlogtypeMapper;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:conf/applicationContext-dao.xml")
public class testDao {
	/**
	 * 注入TBlogtypeMapper bean
	 */
	@Autowired
	private TBlogtypeMapper blogtypeMapper;
	/**
	 * 增加博客
	 */
	@Test
	public void addBlog(){
		System.out.println(blogtypeMapper);
		TBlogtype tBlogtype = new TBlogtype();
		tBlogtype.setId(3);
		//tBlogtype.setOrdernum(ordernum);
		tBlogtype.setTypename("java web");
		int insert = blogtypeMapper.insert(tBlogtype);
		System.out.println(insert);
	}
	/**
	 * 删除博客
	 */
	@Test
	public void deleteBlog(){
		System.out.println(blogtypeMapper);
		int n = blogtypeMapper.deleteByPrimaryKey(new Integer(3));
		System.out.println(n);
	}
	/**
	 * 更新博客
	 */
	@Test
	public void updateBlog(){
		TBlogtype tBlogtype = new TBlogtype();
		tBlogtype.setTypename("c#");
		tBlogtype.setId(new Integer(3));
		int n = blogtypeMapper.updateByPrimaryKey(tBlogtype);
		System.out.println(n);
	}
	/**
	 * 按id查询博客
	 */
	@Test
	public void selectBlogByid( ){
		TBlogtype n = blogtypeMapper.selectByPrimaryKey(new Integer(3));
		System.out.println(n.toString());
	}
	/**
	 * 分页查询博客(所有博客)
	 * 注意:这里有些注释现在不需要理解可以,作用是封装数据到前台
	 */
	@Test
	public void selectByPage(){	
		//设置分页信息
		PageHelper.startPage(1,2);
		//执行查询
		TBlogtypeExample example = new TBlogtypeExample();	
		List<TBlogtype> list = blogtypeMapper.selectByExample(example);
		//创建一个返回值对象
		//EasyUIDataGridResult result = new EasyUIDataGridResult();
		//result.setRows(list);
		
		//取分页结果
		PageInfo<TBlogtype> pageInfo = new PageInfo<>(list);
		//取总记录数
		long total = pageInfo.getTotal();
		//result.setTotal(total);	
		//System.out.println(total);
		System.out.println(pageInfo.getPageSize());
		System.out.println(pageInfo.getPageNum());
		System.out.println(pageInfo.getTotal());
		System.out.println(pageInfo.getList());
		//Page{pageNum=1, pageSize=2, startRow=0, endRow=2, total=5, pages=3}
		//System.out.println(result.getTotal());
		//return result;
	}
}


 上述测试,使用了

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations="classpath:conf/applicationContext-dao.xml")

这种写法是为了让测试在Spring容器环境下执行。

Spring的容器环境是什么?

比如常见的 Service  Dao  Action,都在Spring容器里,junit需要将他们拿到,并且使用来测试。

这些Dao,都是能够经得起测试的,现在我们来编辑bolog相关的Dao


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
个人博客系统主要用于发表个人博客,记录个人生活日常,学习心得,技术分享等,供他人浏览,查阅,评论等。本系统结构如下: (1)博主端: 登录模块:登入后台管理系统:首先进入登录页面,需要输入账号和密码。它会使用Shiro进行安全管理,对前台输入的密 码进行加密运算,然后与数据库中的进行比较。成功后才能登入后台系统。 博客管理模块: 博客管理功能分为博客和博客信息管理。博客是博主用来发表编博客的,需要博客标题,然后选择博 客类型,最后将博客内容填入百度的富文本编辑器中,点击发布博客按钮即可发布博客。 博客类别管理模块:博主类别管理系统可以添加,修改和删除博客类型名称和排序序号。将会显示到首页的按日志类别区域。 游客可以从这里查找相关的感兴趣的博客内容 评论信息管理模块:评论管理功能分为评论审核和评论信息管理两部分。评论审核是当有游客或自己发表了评论之后,博主需 要在后台管理系统中审核评论。若想将此评论显示在页面上则点击审核通过,否则点击审核不通过。 个人信息管理模块:修改博主的个人信息,可以修改昵称,个性签名,可以添加个人头像,修改个人简介; 系统管理功能模块:友情链接管理,修改密码,刷新系统缓存和安全退出,友情链接管理可以添加,修改,删除友情链接网址 (2)游客端: 查询博客: 查询具体哪一篇博客 查看博客内容: 查看博客内容 查看博主个人信息:查看博主个人简介 发表评论: 可以评论具体某篇博客 友情链接: 查看友情链接
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值