Skip to content

Commit 006f008

Browse files
committed
添加: Mapper通用插件&分页插件
1 parent 49b4290 commit 006f008

File tree

14 files changed

+255
-140
lines changed

14 files changed

+255
-140
lines changed

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@
3737
<artifactId>logback-core</artifactId>
3838
<version>1.1.11</version>
3939
</dependency>
40+
<!-- Mybatis 依赖 及 插件 -->
41+
<dependency>
42+
<groupId>org.mybatis</groupId>
43+
<artifactId>mybatis</artifactId>
44+
<version>3.4.1</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>tk.mybatis</groupId>
48+
<artifactId>mapper</artifactId>
49+
<version>3.4.3</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>com.github.pagehelper</groupId>
53+
<artifactId>pagehelper</artifactId>
54+
<version>4.2.1</version>
55+
</dependency>
4056
<!--代码生成器依赖-->
4157
<dependency>
4258
<groupId>com.google.guava</groupId>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.bigsea.sns.dao;
2+
3+
import tk.mybatis.mapper.common.BaseMapper;
4+
import tk.mybatis.mapper.common.ConditionMapper;
5+
import tk.mybatis.mapper.common.IdsMapper;
6+
import tk.mybatis.mapper.common.special.InsertListMapper;
7+
8+
/**
9+
* 通用 Mapper, 如果被扫描到会报异常
10+
* Created by zhh on 2017/09/18.
11+
*/
12+
public interface MyMapper<T> extends BaseMapper<T>, ConditionMapper<T>, IdsMapper<T>, InsertListMapper<T> {
13+
}
Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,7 @@
11
package com.bigsea.sns.dao.mapper.test;
22

3+
import com.bigsea.sns.dao.MyMapper;
34
import com.bigsea.sns.model.test.Demo;
4-
import java.util.List;
55

6-
public interface DemoMapper {
7-
/**
8-
* This method was generated by MyBatis Generator.
9-
* This method corresponds to the database table gen_test_demo
10-
*
11-
* @mbg.generated Wed Sep 20 18:58:31 GMT+08:00 2017
12-
*/
13-
int deleteByPrimaryKey(Long id);
14-
15-
/**
16-
* This method was generated by MyBatis Generator.
17-
* This method corresponds to the database table gen_test_demo
18-
*
19-
* @mbg.generated Wed Sep 20 18:58:31 GMT+08:00 2017
20-
*/
21-
int insert(Demo record);
22-
23-
/**
24-
* This method was generated by MyBatis Generator.
25-
* This method corresponds to the database table gen_test_demo
26-
*
27-
* @mbg.generated Wed Sep 20 18:58:31 GMT+08:00 2017
28-
*/
29-
Demo selectByPrimaryKey(Long id);
30-
31-
/**
32-
* This method was generated by MyBatis Generator.
33-
* This method corresponds to the database table gen_test_demo
34-
*
35-
* @mbg.generated Wed Sep 20 18:58:31 GMT+08:00 2017
36-
*/
37-
List<Demo> selectAll();
38-
39-
/**
40-
* This method was generated by MyBatis Generator.
41-
* This method corresponds to the database table gen_test_demo
42-
*
43-
* @mbg.generated Wed Sep 20 18:58:31 GMT+08:00 2017
44-
*/
45-
int updateByPrimaryKey(Demo record);
6+
public interface DemoMapper extends MyMapper<Demo> {
467
}

src/main/java/com/bigsea/sns/model/test/Demo.java

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,52 @@
11
package com.bigsea.sns.model.test;
22

3+
import javax.persistence.*;
4+
5+
@Table(name = "gen_test_demo")
36
public class Demo {
47
/**
5-
*
6-
* This field was generated by MyBatis Generator.
7-
* This field corresponds to the database column gen_test_demo.id
8-
*
9-
* @mbg.generated Wed Sep 20 18:58:31 GMT+08:00 2017
8+
* 用户ID
109
*/
10+
@Id
11+
@GeneratedValue(strategy = GenerationType.IDENTITY)
1112
private Long id;
1213

1314
/**
14-
*
15-
* This field was generated by MyBatis Generator.
16-
* This field corresponds to the database column gen_test_demo.name
17-
*
18-
* @mbg.generated Wed Sep 20 18:58:31 GMT+08:00 2017
15+
* 用户姓名
1916
*/
2017
private String name;
2118

2219
/**
23-
* This method was generated by MyBatis Generator.
24-
* This method returns the value of the database column gen_test_demo.id
20+
* 获取用户ID
2521
*
26-
* @return the value of gen_test_demo.id
27-
*
28-
* @mbg.generated Wed Sep 20 18:58:31 GMT+08:00 2017
22+
* @return id - 用户ID
2923
*/
3024
public Long getId() {
3125
return id;
3226
}
3327

3428
/**
35-
* This method was generated by MyBatis Generator.
36-
* This method sets the value of the database column gen_test_demo.id
37-
*
38-
* @param id the value for gen_test_demo.id
29+
* 设置用户ID
3930
*
40-
* @mbg.generated Wed Sep 20 18:58:31 GMT+08:00 2017
31+
* @param id 用户ID
4132
*/
4233
public void setId(Long id) {
4334
this.id = id;
4435
}
4536

4637
/**
47-
* This method was generated by MyBatis Generator.
48-
* This method returns the value of the database column gen_test_demo.name
38+
* 获取用户姓名
4939
*
50-
* @return the value of gen_test_demo.name
51-
*
52-
* @mbg.generated Wed Sep 20 18:58:31 GMT+08:00 2017
40+
* @return name - 用户姓名
5341
*/
5442
public String getName() {
5543
return name;
5644
}
5745

5846
/**
59-
* This method was generated by MyBatis Generator.
60-
* This method sets the value of the database column gen_test_demo.name
61-
*
62-
* @param name the value for gen_test_demo.name
47+
* 设置用户姓名
6348
*
64-
* @mbg.generated Wed Sep 20 18:58:31 GMT+08:00 2017
49+
* @param name 用户姓名
6550
*/
6651
public void setName(String name) {
6752
this.name = name;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.bigsea.sns.service;
2+
3+
import java.lang.reflect.Field;
4+
import java.lang.reflect.ParameterizedType;
5+
import java.util.List;
6+
7+
import org.apache.ibatis.exceptions.TooManyResultsException;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
10+
import com.bigsea.sns.dao.MyMapper;
11+
12+
import tk.mybatis.mapper.entity.Condition;
13+
14+
/**
15+
* 基于通用MyBatis Mapper插件的Service接口的实现
16+
*/
17+
public abstract class AbstractService<T> {
18+
19+
@Autowired
20+
protected MyMapper<T> mapper;
21+
22+
private Class<T> modelClass; // 当前泛型真实类型的Class
23+
24+
public AbstractService() {
25+
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
26+
modelClass = (Class<T>) pt.getActualTypeArguments()[0];
27+
}
28+
29+
public void save(T model) {
30+
mapper.insertSelective(model);
31+
}
32+
33+
public void save(List<T> models) {
34+
mapper.insertList(models);
35+
}
36+
37+
public void deleteById(Integer id) {
38+
mapper.deleteByPrimaryKey(id);
39+
}
40+
41+
public void deleteByIds(String ids) {
42+
mapper.deleteByIds(ids);
43+
}
44+
45+
public void update(T model) {
46+
mapper.updateByPrimaryKeySelective(model);
47+
}
48+
49+
public T findById(Integer id) {
50+
return mapper.selectByPrimaryKey(id);
51+
}
52+
53+
@SuppressWarnings("unchecked")
54+
public T findBy(String fieldName, Object value) throws TooManyResultsException {
55+
try {
56+
T model = modelClass.newInstance();
57+
Field field = modelClass.getDeclaredField(fieldName);
58+
field.setAccessible(true);
59+
field.set(model, value);
60+
return mapper.selectOne(model);
61+
} catch (ReflectiveOperationException e) {
62+
throw new RuntimeException("查询操作异常!");
63+
}
64+
}
65+
66+
public List<T> findByIds(String ids) {
67+
return mapper.selectByIds(ids);
68+
}
69+
70+
public List<T> findByCondition(Condition condition) {
71+
return mapper.selectByCondition(condition);
72+
}
73+
74+
public List<T> findAll() {
75+
return mapper.selectAll();
76+
}
77+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.bigsea.sns.service;
2+
3+
import org.apache.ibatis.exceptions.TooManyResultsException;
4+
import tk.mybatis.mapper.entity.Condition;
5+
6+
import java.util.List;
7+
8+
/**
9+
* Service 层 基础接口,其他Service 接口 请继承该接口
10+
* Created by zhh on 2017/09/18.
11+
*/
12+
public interface Service<T> {
13+
14+
/**
15+
* 插入数据
16+
* @param model
17+
*/
18+
void save(T model);
19+
20+
/**
21+
* 批量插入数据
22+
* @param models
23+
*/
24+
void save(List<T> models);
25+
26+
/**
27+
* 通过主鍵刪除
28+
* @param id
29+
*/
30+
void deleteById(Integer id);
31+
32+
/**
33+
* 批量刪除
34+
* @param ids eg:ids -> "1,2,3,4"
35+
*/
36+
void deleteByIds(String ids);
37+
38+
/**
39+
* 更新
40+
* @param model
41+
*/
42+
void update(T model);
43+
44+
/**
45+
* 通过ID查找
46+
* @param id
47+
* @return
48+
*/
49+
T findById(Integer id);
50+
51+
/**
52+
* 通过Model中某个成员变量名称(非数据表中column的名称)查找,value需符合unique约束
53+
* @param fieldName
54+
* @param value
55+
* @return
56+
* @throws TooManyResultsException
57+
*/
58+
T findBy(String fieldName, Object value) throws TooManyResultsException;
59+
60+
/**
61+
* 通过多个ID查找
62+
* @param ids eg:ids -> "1,2,3,4"
63+
* @return
64+
*/
65+
List<T> findByIds(String ids);
66+
67+
/**
68+
* 根据条件查找
69+
* @param condition
70+
* @return
71+
*/
72+
List<T> findByCondition(Condition condition);
73+
74+
/**
75+
* 获取所有
76+
* @return
77+
*/
78+
List<T> findAll();
79+
}

src/main/java/com/bigsea/sns/service/impl/test/DemoServiceImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
import com.bigsea.sns.dao.mapper.test.DemoMapper;
44
import com.bigsea.sns.model.test.Demo;
55
import com.bigsea.sns.service.test.DemoService;
6+
import com.bigsea.sns.service.AbstractService;
67
import org.springframework.stereotype.Service;
7-
8-
import javax.annotation.Resource;
9-
8+
import org.springframework.beans.factory.annotation.Autowired;
109

1110
/**
1211
*
13-
* Created by zhh on 2017/09/20.
12+
* Created by zhh on 2017/09/21.
1413
*/
1514
@Service
16-
public class DemoServiceImpl implements DemoService {
17-
@Resource
15+
public class DemoServiceImpl extends AbstractService<Demo> implements DemoService {
16+
17+
@Autowired
1818
private DemoMapper genTestDemoMapper;
1919

2020
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.bigsea.sns.service.test;
22
import com.bigsea.sns.model.test.Demo;
3+
import com.bigsea.sns.service.Service;
34

45
/**
56
*
6-
* Created by zhh on 2017/09/20.
7+
* Created by zhh on 2017/09/21.
78
*/
8-
public interface DemoService {
9+
public interface DemoService extends Service<Demo> {
910

1011
}

src/main/java/com/bigsea/sns/web/controller/test/DemoController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
/**
1313
*
14-
* Created by zhh on 2017/09/20.
14+
* Created by zhh on 2017/09/21.
1515
*/
1616
@Controller
1717
@RequestMapping("/demo/")

0 commit comments

Comments
 (0)