一、创建Maven工程

二、pom.xml文件

引入 MyBatis Plus 的依赖,

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.md</groupId>
<artifactId>01-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>01-hello</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql版本-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>5.1.9</scope>
</dependency> <!--1. 加入依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

三、创建实体类

首先数据库中有对应的字段

通过lombok

package com.md.entity;

import lombok.Data;

/**
* @author md
* @Desc
* @date 2020/10/26 15:38
*/ // 2. 使用这个注解直接生成set和get方法
@Data
public class Student { private Integer id;
private String name;
private Integer age; }

四、Mapper接口

package com.md.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.md.entity.Student; /**
* @author md
* @Desc
* @date 2020/10/26 15:46
*/ // 3. 接口继承BaseMapper并指定类型
public interface StudentMapper extends BaseMapper<Student> { }

五、主配置

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8 spring.datasource.username=root
spring.datasource.password=123456 # 专门用于打印日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

六、启动类

package com.md;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
// 4. mapper扫描
@MapperScan("com.md.mapper")
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }

七、测试

package com.md;

import com.md.mapper.StudentMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest
class ApplicationTests { @Autowired
private StudentMapper mapper; // 5. 测试,查询数据
@Test
void contextLoads() {
// 不加条件全部查询,把每个对象迭代出来
mapper.selectList(null).forEach(System.out::println);
} }

这样就可以把对应的数据查询出来了

最新文章

  1. javascrip小笔记
  2. Visual Studio 2015速递(2)——提升效率和质量(VS2015核心竞争力)
  3. android studio手动加入jar包
  4. Android webview使用详解
  5. stm32 dac库函数解读
  6. 线程池(C#)
  7. GCC扩展 __attribute__ ((visibility(&quot;hidden&quot;)))
  8. python3和Python2的区别(被坑太久了)
  9. MySQL 设置远程访问
  10. Oracle中针对中文进行排序[Z]
  11. Bandit Wargame Level24 Writeup(brute-forcing with shell)
  12. 大数据 --&gt; 一致性Hash算法
  13. [转]谈谈Java中&quot;==&quot;与&quot;equals()&quot;
  14. django--admin组件
  15. SSM整合(1): spring 与 springmvc 整合
  16. 数据预取 __builtin_prefetch()
  17. Tensorflow结点打包和依赖控制
  18. BZOJ1461 字符串的匹配
  19. Web API 源码剖析之默认配置(HttpConfiguration)
  20. git本地项目上传远程

热门文章

  1. 不会吧!做了这么久开发还有不会NIO的,看看BAT大佬是怎么用的吧
  2. python实现一个无序单链表
  3. 数据恢复当选EasyRecovery,设备不再受限
  4. 如何制作C语言基本数据类型的思维导图
  5. 今天谁也别想阻止我好好学习!「CDR 6&#183;18特惠倒计时2天!」
  6. Mac升级资料丢失怎么办?EasyRecovery能恢复嘛?
  7. 思维导图iMindMap怎么做大型项目管理
  8. JUC并发工具包之CyclicBarrier
  9. 理解go语言的shellcode加载器
  10. JZOJ2020年8月14日提高组反思