SpringBoot 整合 MybatisPlus 3.0

SpringBoot整合MybatisPlus 3.0

  1. 添加pom依赖

    <!-- druid数据库连接池启动器 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.9</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
        <version>5.1.46</version>
    </dependency>
    <!-- mybatis-plus启动器 -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.0.1</version>
    </dependency>
  2. application.properties配置

    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    spring.datasource.username=root
    spring.datasource.password=123
    spring.datasource.url=jdbc:mysql://localhost:3306/mp_demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
  3. 编写实体类

    /**
     * @author wsyjlly
     * @create 2019.06.29 - 12:18
     **/
    @AllArgsConstructor
    @NoArgsConstructor
    @Accessors
    @Data
    @TableName(value = "message")
    public class Message implements Serializable {
        private Integer id;
        private Integer mid;
        private String content;
        @TableField(value = "column_200")
        private String column1;
        @TableField(value = "column_500")
        private String column2;
        @TableField(value = "column_5000")
        private String column3;
    }
  4. 编写mapper接口继承BaseMapper接口的数据操作方法

    /**
     * @author wsyjlly
     * @create 2019.06.29 - 12:30
     **/
    @Mapper
    public interface MessageMapper extends BaseMapper<Message> {}
  5. 编写Service

    /**
     * @author wsyjlly
     * @create 2019.06.29 - 12:33
     **/
    public interface MessageService {
        List<Message> getAllMessage();
        List<Message> getMessageByMid(Integer mid);
        Integer fetchOne(Integer id,Integer mid);
        Integer updateMessageByModuleIdAndMessageId(Message message);
        Integer addMessage(Message message);
        Integer deleteMessage(Integer mid,Integer id);
    }
    /**
     * @author wsyjlly
     * @create 2019.06.29 - 13:29
     **/
    @Service
    public class MessageServiceImpl implements MessageService {
        @Autowired
        private MessageMapper messageMapper;
        private Logger logger = LoggerFactory.getLogger(getClass());
    
        @Override
        public List<Message> getAllMessage() {
            return messageMapper.selectList(new QueryWrapper<Message>());
        }
    
        @Override
        public List<Message> getMessageByMid(Integer mid) {
            return messageMapper.selectList(new QueryWrapper<Message>().eq("mid",mid));
        }
    
        @Override
        public Integer fetchOne(Integer id,Integer mid){
            return messageMapper.selectCount(new QueryWrapper<Message>().eq("id",id).eq("mid",mid));
        }
    
        @Override
        public Integer updateMessageByModuleIdAndMessageId(Message message){
            System.out.println(message);
            logger.debug(message.toString());
            return messageMapper.updateById(message);
        }
        @Override
        public Integer addMessage(Message message){
            System.out.println(message);
            logger.debug(message.toString());
            return messageMapper.insert(message);
        }
        @Override
        public Integer deleteMessage(Integer mid,Integer id){
            logger.debug(mid+"-"+id);
            return messageMapper.delete(new QueryWrapper<Message>().eq("id",id).eq("mid",mid));
        }
    }
  6. 编写Controller

    /**
     * @author wsyjlly
     * @create 2019.06.29 - 12:52
     **/
    @RestController
    public class MainController {
        @Autowired
        private MessageService messageService;
    
        @GetMapping("/messages")
        public ModelMap getMessages(){
            ModelMap map = new ModelMap();
            return map.addAttribute("messages",messageService.getAllMessage());
        }
    
        @GetMapping("module1")
        public Object getMessageType1(){
            return messageService.getMessageByMid(1);
        }
        
        @GetMapping("module2")
        public Object getMessageType2(){
            return messageService.getMessageByMid(2);
        }
        
        @PostMapping("/message")
        public ModelMap addMessage(@RequestBody Message message){
            ModelMap map = new ModelMap();
            Integer result = messageService.addMessage(message);
            if (result==1){
                map.addAttribute("result",true);
                map.addAttribute("tip","添加成功!");
            }else{
                map.addAttribute("result",false);
                map.addAttribute("tip","添加失败!");
            }
            return map;
        }
        
        @DeleteMapping("/{mid}/{id}")
        public ModelMap deleteMessage(@PathVariable Integer mid,@PathVariable Integer id){
            System.out.println(mid);
            System.out.println(id);
            ModelMap map = new ModelMap();
            Integer result = messageService.deleteMessage(mid,id);
            if (result==1){
                map.addAttribute("result",true).addAttribute("tip","删除成功!");
            }else{
                map.addAttribute("result",false).addAttribute("tip","删除失败!");
            }
            return map;
        }
        
        @PatchMapping("/message")
        public ModelMap updateMessage(@RequestBody Message message){
            Integer one = messageService.fetchOne(message.getId(), message.getMid());
            ModelMap map = new ModelMap();
            if (one==1){
                Integer result = messageService.updateMessageByModuleIdAndMessageId(message);
                if (result==1){
                    map.addAttribute("result",true).addAttribute("tip","更新成功!");
                }else{
                    map.addAttribute("result",false).addAttribute("tip","更新失败!");
                }
                System.out.println(message);
            }else{
                map.addAttribute("tip","查询异常!");
            }
            return map;
        }
    }
  7. 显示结果
    SpringBoot 整合 MybatisPlus 3.0

    SpringBoot 整合 MybatisPlus 3.0

上一篇:SpringBoot 自定义Banner


下一篇:VS2010 dev控件 拖拽不到Winfrom上的原因