springboot集成mybatis-plus 环境搭建(一)

项目结构:

springboot集成mybatis-plus 环境搭建(一)_第1张图片

集成mybatis-plus的坑挺多的,算是踩了一遍。

pom文件:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.6.RELEASE
         
    
    MPTest
    mybatistest
    0.0.1-SNAPSHOT
    mybatistest
    Demo project for Spring Boot

    
        1.8
    
    
        
            maven-ali
            http://maven.aliyun.com/nexus/content/groups/public/
            
                true
            
            
                true
                always
                fail
            
        
    
    
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            mysql
            mysql-connector-java
            runtime
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.2.0
        
        
            net.sf.json-lib
            json-lib
            2.4
            jdk15
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


坑1:首先用idea默认的远程仓库下载的jar都有问题,将远程仓库地址改为阿里云的。

坑2:mybatis-plus不能和mybatis并存。

坑3:有一个类似的mybatis-plus包,但不是支持springboot的。

数据库:

springboot集成mybatis-plus 环境搭建(一)_第2张图片

接下来是代码:

1.entity:

@TableName("user_test")对应的是数据库的表名,属性对应字段

@TableId对应主键,value为字段值,type为生成策略auto为自增

@TableField对应非主键

package mptest.mybatistest.entity;



import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;

@TableName("user_test")
public class Good implements Serializable {
    @TableId(value = "id",type = IdType.AUTO)
    private int id;
	@TableField(value = "name")
    private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

 

 

2.mapper

继承BaseMapper,注意加上mapper注解或者在启动类加扫描mapper

package mptest.mybatistest.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import mptest.mybatistest.entity.Good;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface GoodDao extends BaseMapper {
}

3.service

继承IService

package mptest.mybatistest.service;
import com.baomidou.mybatisplus.extension.service.IService;
import mptest.mybatistest.entity.Good;

public interface IGoodService extends IService {
}

4.impl

继承extends ServiceImpl,两个参数一个为dao,一个为实体类

package mptest.mybatistest.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mptest.mybatistest.mapper.GoodDao;
import mptest.mybatistest.entity.Good;
import mptest.mybatistest.service.IGoodService;
import org.springframework.stereotype.Service;



@Service
public class GoodServiceImpl extends ServiceImpl implements IGoodService {

}

5.controller

goodService.list()为IService接口自带的方法。

package mptest.mybatistest.controller;

import mptest.mybatistest.service.IGoodService;
import net.sf.json.JSONArray;
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;

@RestController
@RequestMapping("/work")
public class GoodsController {

    @Autowired
    private IGoodService goodService;
    @RequestMapping("/good")
    public String index() {
        List goodList = goodService.list();
        JSONArray jsonArray=  JSONArray.fromObject(goodList);
        return jsonArray.toString();
    }
}

访问项目,能看到数据即为环境搭建完成。

 

你可能感兴趣的:(springboot,mybatis-plus)