栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合mybatisplus

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合mybatisplus

1.创建一个springboot项目  pom.xml引用
        
            org.projectlombok
            lombok
            1.18.10
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.1
        
        
            com.baomidou
            mybatis-plus-generator
            3.5.0
        
        
        
            mysql
            mysql-connector-java
            runtime
        
        
        
            org.apache.velocity
            velocity-engine-core
            2.2
        
        
            junit
            junit
            4.12
            compile
        
        
            org.springframework.boot
            spring-boot-test
            2.6.5
            compile
        
        
            org.springframework
            spring-test
            5.3.17
            compile
        
        
            org.springframework
            spring-test
            5.3.13
        

2.application.yml文件添加配置并且创建测试表

server:
  port: 8087

  mvc:
    static-path-pattern: *.xml
  type-aliases-package: com.mabp.emptity
create table `user_test1` (
	`id` int (11),
	`username` varchar (60),
	`age` int (11),
	`tel` int (11),
	`create_time` timestamp ,
	`update_time` timestamp ,
	`version` int (11)
); 
insert into `user_test1` (`id`, `username`, `age`, `tel`, `create_time`, `update_time`, `version`) values('1','张三','18','180',NULL,NULL,NULL);
insert into `user_test1` (`id`, `username`, `age`, `tel`, `create_time`, `update_time`, `version`) values('2','李四','20','137',NULL,NULL,NULL);
insert into `user_test1` (`id`, `username`, `age`, `tel`, `create_time`, `update_time`, `version`) values('3','王五','22','138',NULL,NULL,NULL);

3.创建一个实体类

@Data    //使用lombok 简化getset方法
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "user_test1", autoResultMap = true)
public class UserTest {
    @TableId(value = "id", type = IdType.AUTO)
    private int id;

    private String username;

    private int age;

    private int tel;

}

4.创建mapper继承与   baseMapper

package com.mabp.mapper;

import com.baomidou.mybatisplus.core.mapper.baseMapper;
import com.mabp.emptity.UserTest;
import org.apache.ibatis.annotations.Mapper;


@Mapper
public interface UserTestMapper extends baseMapper {


}

4.编写测试类测试

package com.mabp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mabp.SpmpApplication;
import com.mabp.emptity.UserTest;
import com.mabp.mapper.UserTestMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/v1/test/my")
@SpringBootTest(classes = SpmpApplication.class) //SpmpApplication启动类名字
@RunWith(SpringJUnit4ClassRunner.class)
@Api(tags = "swagger测试模块")
public class TestMyController {

    //如果出现红色下划线不影响运行,可以设置idea不报红
    @Autowired
    private UserTestMapper userTestMapper;

    @Test
    public void Insert() {
        UserTest userTest = new UserTest();
        userTest.setAge(17);
        userTest.setTel(17);
        userTest.setUsername("我是付林");
        int result = userTestMapper.insert(userTest);
        System.out.println(result);             // 受影响的行数
        System.out.println(userTest);               // 发现,未设置的id会自动回填

    }



    

    @GetMapping("list")
    @ApiOperation(value="查询所有用户信息",notes = "查询所有用户信息",httpMethod = "get")
    public List test(){
        QueryWrapper wrapper = new QueryWrapper<>();
//        wrapper.select("id","username");
        //直接调用baseMapper封装好的CRUD方法,就可实现无条件查询数据
        List list = userTestMapper.selectList(null);

        //循环获取用户数据
        for (UserTest userTest:list){
            //获取用户名称
            System.out.println(userTest.getUsername());
        }
        return list;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/782589.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号