在SSM分模块的开发中模糊查询的实现(以根据博客标题中包含的关键字进行查询)
在SSM分模块的开发中模糊查询的实现(以根据博客标题中包含的关键字进⾏
查询)
在SSM分模块的开发中实现基本的模糊查询,以根据博客标题中包含的关键字进⾏查询:
1.在BlogDao下创建相应的⽅法
public interface BlogDao
{
void addBlog(Blog blog);
Blog getBlogById(int blogId);
void deleteBlogById(int blogId);
void updateBlogById(int blogId);
List<Blog>findBlogByuserId(String userId);
/
/根据博客标题进⾏模糊查询博客
List<Blog>findBlogByblogTitle(String blogTitle);
}
2.在l中添加查询语句
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="edu.whut.five.dao.BlogDao">
<!-- 根据博客标题查询-->
<select id="findBlogByblogTitle" parameterType="java.lang.String" resultType="edu.whut.five.beans.Blog">
select * from blog where blogTitle like concat('%',#{blogTitle},'%')
</select>
</mapper>
3.在BlogService中添加相应的⽅法
public interface BlogService
宫崎骏的作品{
void addBlog(Blog blog);
Blog getBlogById(int blogId);
void deleteBlogById(int blogId);
void updateBlogById(int blogId);
List<Blog>findBlogByuserId(String userId);
List<Blog>findBlogByuserId(String userId,int page,int size);
/
/根据标题查询博客
List<Blog>findBlogByblogTitle(String blogTitle);
}
4.在BlogServiceImpl中添加BlogService的实现⽅法
public class BlogServiceImpl implements BlogService
{
@Autowired
private BlogDao blogDao;
@Override
public void addBlog(Blog blog)
{
blogDao.addBlog(blog);
}
@Override
public void deleteBlogById(int blogId)
{
blogDao.deleteBlogById(blogId);
}
@Override
public void updateBlogById(int blogId)
{
blogDao.updateBlogById(blogId);
}
@Override
public Blog getBlogById(int blogId)
{
BlogById(blogId);
}
@Override
public List<Blog>findBlogByuserId(String userId)
{
return blogDao.findBlogByuserId(userId);
}
@Override
public List<Blog>findBlogByuserId(String userId,int page,int size) {
//告诉开始分页
PageHelper.startPage(page, size);
return blogDao.findBlogByuserId(userId);
}
//根据标题查询博客
@Override
public List<Blog>findBlogByblogTitle(String blogTitle){
return blogDao.findBlogByblogTitle(blogTitle);
}
}
5.在BlogController中的实现
@RequestMapping("/blog")
public class BlogController
{
@Autowired
private BlogService blogService;
@RequestMapping("/addBlog.do")
public String addBlog(Blog blog)
{
//System.out.println(blog);
Date date=new java.sql.Date(new java.util.Date().getTime());        blog.setBlogCreateTime(date);
blogService.addBlog(blog);
return"redirect:/login/findUserBlog.do";
}
@RequestMapping("/getBlogById.do")
public ModelAndView getBlogById(int blogId)
{
Blog blog = BlogById(blogId);外地车年审
ModelAndView mv=new ModelAndView();
mv.addObject("blog",blog);
mv.setViewName("showBlog");
return mv;
}
@RequestMapping("/deleteBlog.do")
public String deleteBlogById(int blogId)
{
blogService.deleteBlogById(blogId);
return null;
}
@RequestMapping("/updateBlogById.do")
public String updateBlogById(int blogId)
{
qq空间非主流
blogService.updateBlogById(blogId);
return null;
}
//  根据标题搜索博客
@RequestMapping("/findBlogByblogTitle.do")
public ModelAndView findBlogByblogTitle(String blogTitle)
{
List<Blog> blogList = blogService.findBlogByblogTitle(blogTitle);        ModelAndView mv =new ModelAndView();
mv.addObject("blogList", blogList);
mv.setViewName("blog-search");
return mv;
}
}
6.在JSP中的表单提交
中国十大墙纸品牌
<form action="${tPath}/blog/findBlogByblogTitle.do" method="post">
入党我的自传<div class="w_search">
<div class="w_searchbox">
<input type="text" placeholder="search" name="blogTitle"/>
<button>搜索</button>
</div>
</div>
</form>
7.最终在blog-search.jsp中的输出实现
<c:forEach var="blog" items="${blogList}">
<div class="layui-col-md12">
<div class="main list">
<div class="subject"><a href="javascript:;"class="caty">${blog.blogTag}</a><a href="javascript:;" title="⽤DTcms做⼀个独⽴博客⽹站(响应式模板)">${blog.blogTitle}</a><em>${blog.blogCreateTime}发布</em></div>
<div class="content layui-row">
<div class="layui-col-md4 list-img">
<a href="javascript:;"><img src="www.muzhuangnet/upload/201610/18/201610181739277776.jpg"></a>
</div>
<div class="layui-col-md8">
<div class="list-text">${blog.blogContents}这⾥是博客内容,这⾥是博客内容,这⾥是博客内容,这⾥是博客内容</div>
<div class="list-stat layui-row">
<div class="layui-col-xs3 layui-col-md3 Label">
<i class="layui-icon layui-icon-note"></i>
<a href="javascript:;">${blog.blogCatagory}</a>
<em>${blog.blogAgreeCount}次点赞</em>
</div>
<div class="layui-col-xs3 layui-col-md3">
<i class="layui-icon layui-icon-reply-fill"></i>
<em>${blog.blogKeepCount}次收藏</em>
让我们红尘作伴活的潇潇洒洒
</div>
<div class="layui-col-xs3 layui-col-md3">
<i class="layui-icon layui-icon-read"></i>
<em>${blog.blogVisitCount}次阅读</em>
</div>
<div class="layui-col-xs3 layui-col-md3 alink">
<a href="javascript:;"class="layui-btn layui-btn-xs">阅读原⽂</a>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="layui-col-md12 margin20"></div>
</c:forEach>
实现上述代码即可在SSM分模块的开发中中实现基本的模糊查询
MySQL 的模糊查询可参考:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。