mybatis-plus簡單使用方法


一,mybatis-plus介紹

  使用springboot配合mybatis-plus能大大提升開發效率,mybatis-plus相比mybatis來說,可以減少mapper.xml的開發,減少service層的編寫。

  下面say nothing without codes

二,代碼結構

  引包:

1,pom

 1     <dependency>
 2             <groupId>com.baomidou</groupId>
 3             <artifactId>mybatis-plus</artifactId>
 4             <version>3.0.3</version>
 5         </dependency>
 6 
 7         <dependency>
 8             <groupId>mysql</groupId>
 9             <artifactId>mysql-connector-java</artifactId>
10         </dependency>
11 
12         <dependency>
13             <groupId>org.springframework.boot</groupId>
14             <artifactId>spring-boot-starter-jdbc</artifactId>
15         </dependency>
16      <dependency>
17             <groupId>com.baomidou</groupId>
18             <artifactId>mybatis-plus-boot-starter</artifactId>
19             <version>3.1.2</version>
20         </dependency>
21 
22         <dependency>
23             <groupId>com.alibaba</groupId>
24             <artifactId>druid-spring-boot-starter</artifactId>
25             <version>1.1.9</version>
26         </dependency>
27         <dependency>
28             <groupId>com.baomidou</groupId>
29             <artifactId>mybatis-plus-annotation</artifactId>
30             <version>3.1.2</version>
31         </dependency>
32         <dependency>
33             <groupId>com.baomidou</groupId>
34             <artifactId>mybatis-plus-extension</artifactId>
35             <version>3.1.2</version>
36         </dependency>

jar包要全部引入,否則加載過程中會出現不能代理實例化接口,無法加載SQLSessionFactory等錯誤。

2,controller

@Controller
@RequestMapping("/chong")
public class CommonController {
    @Autowired
    UserService userService;

    @RequestMapping("/userList")
    @ResponseBody
    public List<UserEntity> getUserList(@RequestBody UserEntity userEntity) {
        return userService.selectUsers(userEntity);
    }
}

3,service

 
         
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zs.entity.UserEntity;
import com.zs.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public List<UserEntity> selectUsers(UserEntity userEntity) {

        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(userEntity.getName()), "name", userEntity.getName());

        return userMapper.selectList(queryWrapper);
    }
}

  這里可以看出使用plus的core包里的wrapper,減少了service層的判斷或者sql上的if判斷

特別注意的是這里的wrapper不要引錯了

4,mapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zs.entity.UserEntity;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper<UserEntity> {
}

  簡潔吧,其實基本的增刪改查,BaseMapper已經幫我們實現了,我們只需要引入plus的start包,就自動幫我們實現mapper的接口代理對象,所以在service里注入mapper,spring並不會提醒出錯,因為有了代理對象。

 

5,然后就可以運用

 

 這里要注意application的層級關系,否則springboot自動掃描application同級別及子級別的類,這是要通過scan指定包來解決掃描問題。

以下是執行結果:

 

 三,總結

mybatis-plus里面還有分頁等好用的插件,大大加快開發進程,是個不錯的選擇,也可以像通常mapper一樣,自定義一些特殊的方法。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM