SpringBoot整合MybatisPlus详细教程

首先创建maven项目

pom.xml

<!--       web-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
<!--      模板引擎-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
<!--      mysql驱动 默认8.0-->
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
      </dependency>
<!--      yaml使用有提示-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-configuration-processor</artifactId>
         <optional>true</optional>
      </dependency>
<!--      lombok-->
      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <optional>true</optional>
      </dependency>
<!--      mybatis plus整合(不用导入mybatis了里面自带)-->
      <dependency>
         <groupId>com.baomidou</groupId>
         <artifactId>mybatis-plus-boot-starter</artifactId>
         <version>3.4.3.1</version>
      </dependency>

创建application.yaml

#配置数据源
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

 

 1.创建pojo类

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "user")
public class tUser {
    private int id;
    private  String name;
    private  int age;
    private String email;

}

2.创建mapper类继承BaseMapper就可以获得封装好的CRUD加上@mapper注解不然扫描不到

@Mapper
public interface UserMapper extends BaseMapper<tUser> {
}

 3.创建service业务层(继承IService<tUser>获取CRUD)

 * Mybatis plus CRUD
 */
public interface Userservice extends IService<tUser> {


}

4.实现service代码() 

继承extends ServiceImpl<T, T>

第一个填使用的Mapper 

第二个填实现的实现类pojo

@Service
public class Userserviceimpl extends ServiceImpl<UserMapper, tUser> implements Userservice {

}

源码: 

 

5.控制器调用封装好的底层代码使用就可以了

@Controller
public class CRUDpage {

    @Autowired
    Userservice userservice;

    @GetMapping("/dynamic_table")
    public String dynamic_tablePage(Model model) {
        List<tUser> list = userservice.list();
        model.addAttribute("users",list);
        return "table/dynamic_table";
    }
    @GetMapping("/del/{id}")
    public String del(@PathVariable(value = "id",required = false) Integer id) {
        userservice.removeById(id);
        return "redirect:/dynamic_table";
    }
}

 

遍历数据库的数据这里用的thymeleaf模板引擎

创建html页面

 

<table  class="display table table-bordered table-striped" id="dynamic-table">
<thead>
<tr>
    <th>Rendering engine</th>
    <th>Browser</th>
    <th>Platform(s)</th>
    <th class="hidden-phone">Engine version</th>
    <th class="hidden-phone">CSS grade</th>
</tr>
</thead>
    <tbody>
        <tr th:each="user,stats:${users}">
            <td th:text="${stats.count}"></td>
            <td th:text="${user.getId()}"></td>
            <td th:text="${user.getName()}"></td>
            <td th:text="${user.getAge()}"></td>
            <td th:text="${user.getEmail()}"></td>
            <td>
                <a th:href="@{/del/{id}(id=${user.getId()})}">删除</a>
            </td>
        </tr>
    </tbody>
</table>

页面遍历

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

水果不是橙子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值