spring-boot整合mybatis-plus

一、创建一张数据库表

CREATE TABLE `user`(
	`id` INT(10) AUTO_INCREMENT PRIMARY KEY,
	`name` VARCHAR(30) NOT NULL,
	`age` INT(10) NOT NULL,
	`email` VARCHAR(50) NOT NULL
);

# 添加数据
INSERT INTO USER(NAME,age,email) VALUES
('Jone',18,'test1@taobao,com'),
('Jack',19,'test2@taobao.com'),
('Tom',28,'test3@baidu.com'),
('Sandy',21,'test4@alibaba.com'),
('Mae',24,'mae814@aliyun.com');

在这里插入图片描述

二、创建实体类User

/**
 * @author Mae
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("user")
/**
* user 为表名
*/
public class User {
    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public User(int id, String name, int age, String email) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.email = email;
    }

    /**
     * TableFiled false 说明此字段在数据库中不存在
     */
    @TableField(exist = false)
    private String username;
    @TableField(exist = false)
    private String password;

    private int id;
    private String name;
    private int age;
    private String email;

}

三、添加依赖

 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3.4</version>
        </dependency>

四、在application.yml 文件中添加数据库相关配置

数据源使用的是德鲁伊

spring:
  datasource:
    druid:
      # 监控页的配置
      stat-view-servlet:
        enabled: true
        login-username: admin
        login-password: 123456
        reset-enable: false
      # 监控web应用
      web-stat-filter:
        enabled: true
      #  url-pattern:
      #  exclusions:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/book?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
      username: root
      password: 123456
      # 监控防火墙
      # proxy-filters: stat,wall,slf4j

五、在spring-boot启动类中添加注解

扫描 mapper 文件夹

/**
 * @author Mae
 */
@SpringBootApplication
@MapperScan("com.atmae.springboot_demo2.mapper")
public class SpringbootDemo2Application {

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

}

六、编写Mapper接口(userMapper)

/**
 * @author Mae
 */
public interface UserMapper extends BaseMapper<User> {
}

七、编写service层

1、userService接口

/**
 * @author Mae
 */
public interface UserService extends IService<User> {
}

2、其实现类

注意:一定要添加Service注解

/**
 * @author Mae
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

}

八、编写Controller

/**
 * @author Mae
 */
@Controller
public class TestController {
    @Resource
    private UserService userService;

    @GetMapping("/test")
    public String testQuery(Model model){
        List<User> users=userService.list();
        model.addAttribute("users0",users);
        return "test";
    }
}

九、简单的测试页面(使用themleaf模板引擎)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body>
<table>
    <thead>
    <tr>
        <th>#</th>
        <th>id</th>
        <th>name</th>
        <th>age</th>
        <th>email</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="user,stat:${users0}">
        <td th:text="${stat.count}">Trident</td>
        <td th:text="${user.id}">Trident</td>
        <td th:text="${user.name}"></td>
        <td th:text="${user.age}"></td>
        <td th:text="${user.email}"></td>
    </tr>
    </tbody>
</table>
</body>
</html>

十、测试

在这里插入图片描述

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mae_strive

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值