😊 @ 作者: Eric
💖 @ 主页: https://blog.csdn.net/weixin_47316183?type=blog
🎉 @ 主题:SpringBoot集成MyBatisPlus+MySQL(超详细)
⏱️ @ 创作时间: 2023年08月06日
MyBatis-Plus是一个MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
MybatisPlus可以节省大量时间,所有的CRUD代码都可以自动化完成
特性
MyBatisPlus官方网站:https://baomidou.com/
1、先准备好数据库环境
CREATE DATABASE mybatisplus; USE mybatisplus; CREATE TABLE eric_user( `id` INT AUTO_INCREMENT, `name` VARCHAR(255) COMMENT '名称', PRIMARY KEY (`id`) ); INNER INTO eric_user(`name`) VALUES('Eric'),('blue'); INSERT INTO `eric_user`(`id`,`name`) VALUES (1,'Eric'), (2,'Jack'), (3,'Lucy');
2、创建一个SpringBoot项目
3、引入MyBatisPlus相关依赖(因为要操作数据库,所以这里也引入了MySQL依赖)
com.baomidou mybatis-plus-boot-starter 3.3.1 mysql mysql-connector-java org.projectlombok lombok
4、修改配置文件
server: port: 8080 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatisplus?serverTimezone=GMT%2B8 username: root password: root #开启日志 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
1、创建一个实体类
package com.eric.springbootmybatisplus.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; /** * @author Eric * @date 2023-08-01 15:38 */ @Data @TableName(value = "eric_user") public class EricUser { @TableId(value = "id", type = IdType.AUTO) private Integer id; @TableField(value = "name") private String name; }
2、Mapper层
package com.eric.springbootmybatisplus.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.eric.springbootmybatisplus.entity.EricUser; import org.apache.ibatis.annotations.Mapper; /** * @author Eric * @date 2023-08-01 15:39 */ @Mapper public interface EricUserMapper extends BaseMapper{ }
3、service层
package com.eric.springbootmybatisplus.service; import com.baomidou.mybatisplus.extension.service.IService; import com.eric.springbootmybatisplus.entity.EricUser; /** * @author Eric * @date 2023-08-01 15:40 */ public interface EricUserService extends IService{ }
impl
package com.eric.springbootmybatisplus.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.eric.springbootmybatisplus.entity.EricUser; import com.eric.springbootmybatisplus.mapper.EricUserMapper; import com.eric.springbootmybatisplus.service.EricUserService; import org.springframework.stereotype.Service; /** * @author Eric * @date 2023-08-01 15:42 */ @Service public class EricUserServiceImpl extends ServiceImplimplements EricUserService { }
4、controller
package com.eric.springbootmybatisplus.controller; import com.eric.springbootmybatisplus.entity.EricUser; import com.eric.springbootmybatisplus.service.EricUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @author Eric * @date 2023-08-01 15:43 */ @RestController @RequestMapping("/eric") public class EricUserController { @Autowired private EricUserService userService; @GetMapping("") public ListgetEricUserList(){ List list = userService.list(); return list; } }
此时运行项目,访问 IP+端口/接口地址
例如:http://127.0.0.1:8080/eric
结果如下
怎么样,是不是特别的方便和简单~
上一篇:云借阅图书管理系统