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

Mybatis-Plus学习笔记:P3-使用方式3-SpringBoot+Mybatis+Mybatis Plus

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

Mybatis-Plus学习笔记:P3-使用方式3-SpringBoot+Mybatis+Mybatis Plus

P3-使用方式3-SpringBoot+Mybatis+Mybatis Plus
  • 1.SpringBoot+Mybatis+Mybatis Plus实现User查询
    • 1.1创建新工程,导入依赖
    • 1.2配置log4j.properties
    • 1.3编写application.properties
    • 1.4编写pojo
    • 1.5编写mapper
    • 1.6编写启动类
    • 1.7编写测试用例

1.SpringBoot+Mybatis+Mybatis Plus实现User查询 1.1创建新工程,导入依赖


    4.0.0

    org.jq
    mp-springboot
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.projectlombok
            lombok
            true
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.1.1
        
        
        
            mysql
            mysql-connector-java
            5.1.47
        
        
            org.slf4j
            slf4j-log4j12
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


1.2配置log4j.properties
log4j.rootLogger=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n
1.3编写application.properties
spring.application.name = mp-springboot
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mp?
useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
1.4编写pojo
package com.jq.pojo;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User {
    private Long id;
    private String userName;
    private String password;
    private String name;
    private Integer age;
    private String email;
}

1.5编写mapper
package com.jq.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jq.pojo.User;

public interface UserMapper extends BaseMapper {
}

1.6编写启动类
package com.jq;

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

import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.jq.mapper") //设置mapper接口的扫描包
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
1.7编写测试用例
package com.jq;

import com.jq.mapper.UserMapper;
import com.jq.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
    @Autowired
    private UserMapper userMapper;
    @Test
    public void testSelectList(){
        List users = userMapper.selectList(null);
        for (User user : users) {
            System.out.println(user);
        }
    }
}

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

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

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