spring boot整合mybatis plus实现CURD操作

mybatis plus是个啥?别问,问就是不知道!!!!

官方文档:https://mp.baomidou.com/guide/quick-start.html#%E5%BC%80%E5%A7%8B%E4%BD%BF%E7%94%A8

 

行吧,你懒得看文档的话,问看你这么有诚意,那问就大发慈悲的告诉你~

这个东西吧,就是省略你工程里面的mapper,seivice,甚至xml都省略了,任何单表的 CRUD 功能,都不用写!!

集成MyBatis-Plus非常的简单,只需要引入 starter 工程,并配置 mapper 扫描路径即可。

上代码,maven依赖:


    org.springframework.boot
    spring-boot-starter


    org.springframework.boot
    spring-boot-starter-test
    test



    org.projectlombok
    lombok
    true


    org.springframework.boot
    spring-boot-starter-web



    mysql
    mysql-connector-java
    5.1.28
    runtime



    com.alibaba
    druid-spring-boot-starter
    1.1.10


    com.baomidou
    mybatis-plus-boot-starter
    3.3.1.tmp

配置文件的代码:application.properties

server.port=8099

#mysql 你们不会以为我下面的ip地址跟账号密码是真实的吧~~~
spring.datasource.url=jdbc:mysql://187.54.242.87:3306/amos_cloud
spring.datasource.username=wer
spring.datasource.password=2323231!
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

#mybatis-plus
mybatis-plus:
mapper-locations: classpath*:/mapper/*.xml
type-aliases-package:com.china.great.amos.chinagreatblog.entity

mybatis-plus.configuration.map-underscore-to-camel-case: true

entity:Blog代码

package com.china.great.amos.chinagreatblog.entity;

import lombok.Data;

import java.util.Date;

/**
 * @Author: Liu HongYang
 * @DateTime: 2020/5/8 9:39 下午
 * @Description: TODO
 */
@Data
public class Blog {
    private int id;

    private String title;

    private String content;

    private String introduction;

    private Date createTime;

    private Date updateTime;

    private int type;

}

 mapper:BlogMapper

package com.china.great.amos.chinagreatblog.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.china.great.amos.chinagreatblog.entity.Blog;


/**
 * @Author: Liu HongYang
 * @DateTime: 2020/5/8 9:37 下午
 * @Description: TODO
 */
public interface BlogMapper extends BaseMapper {

}

 

我自己封装了一层service跟impl:

package com.china.great.amos.chinagreatblog.service;

import com.china.great.amos.chinagreatblog.entity.Blog;

import java.util.List;

/**
 * @Author: Liu HongYang
 * @DateTime: 2020/5/14 11:42 下午
 * @Description: TODO
 */
public interface BlogService {

    List getList();

}

 

package com.china.great.amos.chinagreatblog.service.impl;

import com.china.great.amos.chinagreatblog.entity.Blog;
import com.china.great.amos.chinagreatblog.mapper.BlogMapper;
import com.china.great.amos.chinagreatblog.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Liu HongYang
 * @DateTime: 2020/5/14 11:43 下午
 * @Description: TODO
 */
@Service
public class BlogServiceImpl implements BlogService {

    
    @Autowired
    private BlogMapper blogMapper;

    @Override
    public List getList(){
        List userList = blogMapper.selectList(null);
        return userList;
    }

}

启动类:

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.china.great.common.dao")
public class AmosComonApplication {

    public static void main(String[] args) {
        SpringApplication.run(AmosComonApplication.class, args);
    }

}

controller接口请求代码:

package com.china.great.amos.chinagreatblog.controller;

import com.china.great.amos.chinagreatblog.entity.Blog;
import com.china.great.amos.chinagreatblog.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: Liu HongYang
 * @DateTime: 2020/5/14 11:36 下午
 * @Description: TODO
 */
@RestController
public class BlogController {

    @Autowired
    private BlogService blogService;

    @RequestMapping("get")
    public List get(){
        List list = blogService.getList();
        return list;
    }

}

 

 

你可能感兴趣的:(java,spring,boot)