SpringBoot快速整合通用Mapper的示例代碼
前言
后端業(yè)務開發(fā),每個表都要用到單表的 增刪改查 等通用方法,而配置了通用Mapper可以極大的方便使用Mybatis單表的增刪改查操作。
通用mapper配置
1、添加 maven :
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--通用mapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.1.5</version> </dependency> <!-- pagehelp --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency>
2、 Application 啟動文件添加 MapperScan 注解
在springboot啟動類添加 tk.mybatis 包下 MapperScan 注解
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.springboot.dao")
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}其中 com.springboot.dao 是 dao 層的路徑。
3、 Model 添加注解
添加 Table 注解和 Id 注解,
Table id
例如下方的 User 實體:
@Table(name = "t_user")
public class User {
//主鍵
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)//自增
private Integer id;
}4、創(chuàng)建 MyMapper
import tk.mybatis.mapper.common.IdsMapper;
import tk.mybatis.mapper.common.Mapper;
public interface MyMapper<T> extends Mapper<T>, IdsMapper<T> {
}需要實現(xiàn)的通用接口都寫在 MyMapper 的繼承類中,該類的包不能被 MapperScan 掃描到。
Mapper<T> IdsMapper<T>
5、每個 dao 繼承步驟4的 MyMapper
例如 UserDao 繼承 MyMapper<User> :
public interface UserDao extends MyMapper<User> {
}通用service
上面配置只是調(diào)用dao層可以有默認的增刪改查的方法,還是要在對應的service添加增刪查改,所以需要寫一個通用service,把公共的方法都抽象到一個基礎方法中。
BaseService.java 接口:
public interface BaseService<T> {
/**
* 查詢所有
*
* @return 返回所有數(shù)據(jù)
*/
List<T> selectAll();
/**
* 查詢數(shù)據(jù)數(shù)量
* @return
*/
int selectCount();
/**
* 添加
*
* @param t 實體
*
* @return
*/
int save(T t);
/**
* 修改
*
* @param t
* 實體
* @return
*/
int updateByPrimaryKey(T t);
/**
* 根據(jù)主鍵刪除
*
* @param t 主鍵
*
* @return
*/
int deleteByPrimaryKey(int t);
}BaseServiceImpl 實現(xiàn)類:
public class BaseServiceImpl<T> implements BaseService<T> {
@Autowired
private MyMapper<T> mapper;
@Override
public List<T> selectAll() {
return mapper.selectAll();
}
@Override
public int selectCount() {
return mapper.selectCount(null);
}
@Override
public int save(T t) {
return mapper.insert(t);
}
@Override
public int updateByPrimaryKey(T t) {
return mapper.updateByPrimaryKey(t);
}
@Override
public int deleteByPrimaryKey(int t) {
return mapper.deleteByPrimaryKey(t);
}
}所有的 service 和 serviceImpl 都分別繼承 BaseService 和 BaseServiceImpl ,例如 UserService 和 UserServiceImpl 分別繼承 BaseService 和 BaseServiceImpl :
public interface UserService extends BaseService<User>{
}@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService{
}配置完成之后,在對應的 controller 調(diào)用,比如 UserController :
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/add")
public Object add(User user) {
userService.save(user);
return null;
}
@PostMapping("/delete")
public Object delete(@RequestParam Integer id) {
userService.deleteByPrimaryKey(id);
return null;
}
@PostMapping("/update")
public Object update(User user) {
userService.updateByPrimaryKey(user);
return null;
}
@GetMapping("/detail")
public User detail(@RequestParam Integer id) {
User user = userService.selectById(id);
return user;
}
@GetMapping("/list")
public List<User> list() {
List<User> list = userService.list();
return list;
}
}總結
通用mapper:
- 創(chuàng)建SpringBoot啟動文件添加
MapperScan,掃描dao層的包。 - 創(chuàng)建
MyMapper<T>接口,根據(jù)自己需求繼承要用的接口,比如Mapper<T>。 - 每個dao接口繼承
MyMapper<T>接口。
通用service
- 創(chuàng)建
BaseService接口。 BaseServiceImpl實現(xiàn)類,調(diào)用MyMapper<T>實現(xiàn)增刪改查方法。- 每個
service接口和service實現(xiàn)類分別繼承BaseService接口和BaseServiceImpl實現(xiàn)類。 - 每個
controller就能調(diào)用通用方法。
遇到的問題
1、啟動報錯
required a bean of type 'com.jeremy.data.utils.MyMapper' that could not be found.
沒有找到
MyMapper對應的bean,無法注入。
解決方案:
1、 SpringBoot 啟動文件添加 MapperScan 注解。
2、每個 dao 接口都要繼承 MyMapper 。
以上兩個步驟 缺一不可 。
github源碼
https://github.com/jeremylai7/springboot-bootstrap
到此這篇關于SpringBoot快速整合通用Mapper的文章就介紹到這了,更多相關SpringBoot整合Mapper內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java實戰(zhàn)技巧之if-else代碼優(yōu)化技巧大全
代碼中如果if-else比較多,閱讀起來比較困難,維護起來也比較困難,很容易出bug,下面這篇文章主要給大家介紹了關于java實戰(zhàn)技巧之if-else代碼優(yōu)化技巧的相關資料,需要的朋友可以參考下2022-02-02
深入淺出講解Spring框架中AOP及動態(tài)代理的應用
在軟件業(yè),AOP為Aspect?Oriented?Programming的縮寫,意為:面向切面編程,通過預編譯方式和運行期間動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護的一種技術2022-03-03

