引入依赖

1
2
3
4
5
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>

application.yml配置

1
2
3
4
5
6
7
8
#mybatis的相关配置
mybatis:
#mapper配置文件
mapper-locations: classpath:mybatis/mapper/*.xml
type-aliases-package: com.ledao.entity
#开启驼峰命名
configuration:
map-underscore-to-camel-case: true

加入@MapperScan注解

在项目的启动类中加入@MapperScan注解,这样就可以指定要扫描的Mapper类的包的路径了

img

项目代码示例

以对一个博客类进行操作为例

Blog类

已省略setter和getter方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.ledao.entity;

import java.util.Date;

/**
* 博客实体
*
* @author LeDao
* @company
* @create 2020-09-11 00:08
*/
public class Blog {

/**
* id
*/
private Integer id;
/**
* 标题
*/
private String title;
/**
* 摘要
*/
private String summary;
/**
* 内容
*/
private String content;
/**
* 发布时间
*/
private Date releaseDate;
/**
* 点击次数
*/
private Integer click;
/**
* 博客类别id
*/
private Integer blogTypeId;
/**
* 博客类别
*/
private BlogType blogType;
/**
* 博客里存在的第一张图片,主要用于列表展示的缩略图
*/
private String imageName;
/**
* 博客数量 非博客实际属性 主要是 根据发布日期归档查询数量用到
*/
private Integer blogCount;
/**
* 发布日期的字符串 只取年和月
*/
private String releaseDateStr;
/**
* 该类型的博客数量
*/
private Integer blogNum;
/**
* 用于判断当前IP是否点赞过这篇博客
*/
private Integer isLike;
/**
* 点赞数
*/
private Integer likeNum;
/**
* 是否是导航条文章(0或空代表不是,1代表是)
*/
private Integer isMenuBlog;
/**
* 设置成为导航条文章的时间
*/
private Date setMenuBlogDate;
}

BlogMapper接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.ledao.mapper;

import com.ledao.entity.Blog;

import java.util.List;
import java.util.Map;

/**
* 博客Mapper接口
*
* @author LeDao
* @company
* @create 2020-09-11 00:11
*/
public interface BlogMapper {

/**
* 分页分条件查询博客
*
* @param map
* @return
*/
List<Blog> list(Map map);

/**
* 获取记录数
*
* @param map
* @return
*/
Long getCount(Map map);

/**
* 添加博客
*
* @param blog
* @return
*/
Integer add(Blog blog);

/**
* 修改博客
*
* @param blog
* @return
*/
Integer update(Blog blog);

/**
* 根据id删除博客
*
* @param id
* @return
*/
Integer delete(Integer id);

/**
* 根据id查找博客
*
* @param id
* @return
*/
Blog findById(Integer id);

/**
* 根据日期分月分组查询
*
* @return
*/
List<Blog> countList();

/**
* 根据博客类型查询博客
*
* @param blogTypeId
* @return
*/
List<Blog> findByBlogTypeId(Integer blogTypeId);

/**
* 获取上一篇博客
*
* @param id
* @return
*/
Blog getPreviousBlog(Integer id);

/**
* 获取下一篇博客
*
* @param id
* @return
*/
Blog getNextBlog(Integer id);

/**
* 获取导航条文章(根据设置时间升序排列)
*
* @return
*/
List<Blog> getMenuBlogList();
}

BlogMapper的XML文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?xml version="1.0" encoding="UTF-8" ?>
<!--suppress ALL-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ledao.mapper.BlogMapper">
<resultMap id="BlogResult" type="Blog">
<result property="id" column="id"/>
<result property="title" column="title"/>
<result property="summary" column="summary"/>
<result property="releaseDate" column="releaseDate"/>
<result property="click" column="click"/>
<result property="content" column="content"/>
<result property="blogTypeId" column="blogTypeId"/>
<result property="isMenuBlog" column="isMenuBlog"/>
<result property="setMenuBlogDate" column="setMenuBlogDate"/>
</resultMap>
<select id="list" parameterType="map" resultMap="BlogResult">
select *
from t_blog
<where>
<if test="title != null and title != ''">
and title like #{title}
</if>
<if test="blogTypeId != null">
and blogTypeId = #{blogTypeId}
</if>
<if test="isMenuBlog != null">
and isMenuBlog = #{isMenuBlog}
</if>
<if test="isMenuBlogKey != null">
and isMenuBlog != 1
</if>
<if test="releaseDateStr != null and releaseDateStr != ''">
and date_format(releaseDate, '%Y年%m月') = #{releaseDateStr}
</if>
</where>
<if test="sortByReleaseDate == null">
order by releaseDate desc
</if>
<if test="start != null and size != null">
limit #{start},#{size}
</if>
</select>
<select id="getCount" parameterType="map" resultType="java.lang.Long">
select count(*)
from t_blog
<where>
<if test="title != null and title != ''">
and title like #{title}
</if>
<if test="blogTypeId != null">
and blogTypeId = #{blogTypeId}
</if>
<if test="isMenuBlog != null">
and isMenuBlog = #{isMenuBlog}
</if>
<if test="isMenuBlogKey != null">
and isMenuBlog != 1
</if>
<if test="releaseDateStr != null and releaseDateStr != ''">
and date_format(releaseDate, '%Y年%m月') = #{releaseDateStr}
</if>
</where>
</select>
<insert id="add" parameterType="Blog">
insert into t_blog (title, summary, content, releaseDate, click, blogTypeId,isMenuBlog)
values (#{title}, #{summary}, #{content}, now(), 0, #{blogTypeId},0);
</insert>
<update id="update" parameterType="Blog">
update t_blog
<set>
<if test="title != null and title != ''">
title=#{title},
</if>
<if test="summary != null and summary != ''">
summary=#{summary},
</if>
<if test="content != null and content != ''">
content=#{content},
</if>
<if test="click != null">
click=#{click},
</if>
<if test="blogTypeId != null">
blogTypeId=#{blogTypeId},
</if>
<if test="isMenuBlog != null">
isMenuBlog = #{isMenuBlog},
</if>
<if test="setMenuBlogDate != null">
setMenuBlogDate=now(),
</if>
</set>
where id = #{id}
</update>
<delete id="delete" parameterType="integer">
delete
from t_blog
where id = #{id}
</delete>
<select id="findById" parameterType="integer" resultMap="BlogResult">
select *
from t_blog
where id = #{id}
</select>
<select id="countList" resultMap="BlogResult">
select date_format(releaseDate, '%Y年%m月') as releaseDateStr, count(*) as blogCount
from t_blog where isMenuBlog=0
group by date_format(releaseDate, '%Y年%m月')
order by date_format(releaseDate, '%Y年%m月') desc
</select>
<select id="findByBlogTypeId" parameterType="integer" resultMap="BlogResult">
select *
from t_blog
where blogTypeId = #{blogTypeId}
order by releaseDate desc
</select>

<select id="getPreviousBlog" parameterType="integer" resultType="com.ledao.entity.Blog">
SELECT *
FROM t_blog
WHERE id &lt;
#{id}
ORDER BY id DESC
LIMIT 1;
</select>

<select id="getNextBlog" parameterType="integer" resultType="com.ledao.entity.Blog">
SELECT *
FROM t_blog
WHERE id &gt;
#{id}
ORDER BY id asc
LIMIT 1;
</select>

<select id="getMenuBlogList" resultMap="BlogResult">
select *
from t_blog
where isMenuBlog = 1
order by setMenuBlogDate asc
</select>
</mapper>

BlogService接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.ledao.service;

import com.ledao.entity.Blog;

import java.util.List;
import java.util.Map;

/**
* 博客Service接口
*
* @author LeDao
* @company
* @create 2020-09-11 00:33
*/
public interface BlogService {

/**
* 分页分条件查询博客
*
* @param map
* @return
*/
List<Blog> list(Map map);

/**
* 获取记录数
*
* @param map
* @return
*/
Long getCount(Map map);

/**
* 添加博客
*
* @param blog
* @return
*/
Integer add(Blog blog);

/**
* 修改博客
*
* @param blog
* @return
*/
Integer update(Blog blog);

/**
* 根据id删除博客
*
* @param id
* @return
*/
Integer delete(Integer id);

/**
* 根据id查找博客
*
* @param id
* @return
*/
Blog findById(Integer id);

/**
* 根据日期分月分组查询
*
* @return
*/
List<Blog> countList();

/**
* 根据博客类型查询博客
*
* @param blogTypeId
* @return
*/
List<Blog> findByBlogTypeId(Integer blogTypeId);

/**
* 获取上一篇博客
*
* @param id
* @return
*/
Blog getPreviousBlog(Integer id);

/**
* 获取下一篇博客
*
* @param id
* @return
*/
Blog getNextBlog(Integer id);

/**
* 获取导航条文章(根据设置时间升序排列)
*
* @return
*/
List<Blog> getMenuBlogList();
}

BlogService接口的实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.ledao.service.impl;

import com.ledao.entity.Blog;
import com.ledao.mapper.BlogMapper;
import com.ledao.service.BlogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
* 博客Service接口实现类
*
* @author LeDao
* @company
* @create 2020-09-11 00:34
*/
@Service("blogService")
public class BlogServiceImpl implements BlogService {

@Resource
private BlogMapper blogMapper;

@Override
public List<Blog> list(Map map) {
return blogMapper.list(map);
}

@Override
public Long getCount(Map map) {
return blogMapper.getCount(map);
}

@Override
public Integer add(Blog blog) {
return blogMapper.add(blog);
}

@Override
public Integer update(Blog blog) {
return blogMapper.update(blog);
}

@Override
public Integer delete(Integer id) {
return blogMapper.delete(id);
}

@Override
public Blog findById(Integer id) {
return blogMapper.findById(id);
}

@Override
public List<Blog> countList() {
return blogMapper.countList();
}

@Override
public List<Blog> findByBlogTypeId(Integer blogTypeId) {
return blogMapper.findByBlogTypeId(blogTypeId);
}

@Override
public Blog getPreviousBlog(Integer id) {
return blogMapper.getPreviousBlog(id);
}

@Override
public Blog getNextBlog(Integer id) {
return blogMapper.getNextBlog(id);
}

@Override
public List<Blog> getMenuBlogList() {
return blogMapper.getMenuBlogList();
}
}