赞
踩
本篇记录一下SpringBoot整合MybatisPlus
MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
MybatisPlus的优点有:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.31</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </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> </dependency> <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.9</version> </dependency>
application.yml
#配置连接数据库 spring: datasource: username: 用户名 password: 密码 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/库名?serverTimezone=Asia/Shanghai&characterEncoding=utf8 type: com.alibaba.druid.pool.DruidDataSource #配置mybatis-plus mybatis-plus: configuration: #日志 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #驼峰形式显示 map-underscore-to-camel-case: true #扫描mapper.xml文件 mapper-locations: classpath:mapper/*.xml
实体类
package com.lzl.pojo; 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.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * --效率,是成功的核心关键-- * * @Author lzl * @Date 2023/3/7 16:57 */ //表名注解,标识实体类对应的表 @TableName("user1") @Data @AllArgsConstructor @NoArgsConstructor public class User { //主键注解,实体类主键字段,IdType设置主键生成策略 @TableId(type = IdType.AUTO)//这里如果设置为 IdType.AUTO,表中的主键字段也要设置成自增长 private Integer id; //字段注解(非主键),编写数据库中的字段名(可以解决属性名和列名不一致) @TableField("name") private String name; @TableField("password") private String password; }
控制器:
package com.lzl.controller; import com.lzl.mapper.UserMapper; import com.lzl.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * --效率,是成功的核心关键-- * * @Author lzl * @Date 2023/3/7 16:56 */ @RestController @RequestMapping("/user") public class UserController { @Autowired private UserMapper mapper; @RequestMapping("/findAll") public List<User> findAll(){ return mapper.selectList(null); } }
package com.lzl.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.lzl.pojo.User; import org.springframework.stereotype.Repository; /** * --效率,是成功的核心关键-- * * @Author lzl * @Date 2023/3/7 17:02 */ @Repository public interface UserMapper extends BaseMapper<User> { }
启动项目访问接口
成功!
本篇只是对MybatisPlus的简单整合,后续会再出一期详解