踩坑:今天在做springboot整合mybatisplus,使用代码自动生成。
需求:根据id查询出用户信息
pom文件:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.9</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.1</version> </dependency>--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.6</version> <exclusions> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.18</version> </dependency> <dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>4.3.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <!--kinef4j--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.9</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
yml配置文件:
server: port: 9090 # 数据库配置 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root #你本地的数据库用户名 password: Auhgnay123@ #你本地的数据库密码 url: jdbc:mysql://localhost:3306/departmentrole?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true servlet: multipart: max-file-size: 100MB max-request-size: 100MB # 配置mybatis实体和xml映射 #mybatis: # mapper-locations: classpath:mapper/*.xml # type-aliases-package: com.example.pojo # configuration: # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # map-underscore-to-camel-case: true mybatis-plus: mapper-locations: classpath:mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl map-underscore-to-camel-case: true #驼峰命名发
controller层:
代码:
@RestController @RequestMapping("/role") public class SysRoleController { @Autowired private SysRoleService sysRoleService; //根据id查询角色信息 @GetMapping("/getById/{roleId}") public Result getById(@PathVariable Long roleId){ SysRole sysRole = sysRoleService.getById(roleId); return Result.success(sysRole); } @DeleteMapping("/deleteById/{roleId}") public Result deleteById(@PathVariable Long roleId){ sysRoleService.removeById(roleId); return Result.success(); } }
service层:
public interface SysRoleService extends IService<SysRole> { }
serviceImpl:
@Service public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService{ }
mapper层:
@Mapper public interface SysRoleMapper extends BaseMapper<SysRole> { }
mapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.SysRoleMapper"> <resultMap id="BaseResultMap" type="com.example.pojo.SysRole"> <id property="roleId" column="role_id" jdbcType="BIGINT"/> <result property="roleName" column="role_name" jdbcType="VARCHAR"/> <result property="roleKey" column="role_key" jdbcType="VARCHAR"/> <result property="roleSort" column="role_sort" jdbcType="INTEGER"/> <result property="dataScope" column="data_scope" jdbcType="CHAR"/> <result property="menuCheckStrictly" column="menu_check_strictly" jdbcType="TINYINT"/> <result property="deptCheckStrictly" column="dept_check_strictly" jdbcType="TINYINT"/> <result property="status" column="status" jdbcType="CHAR"/> <result property="delFlag" column="del_flag" jdbcType="CHAR"/> <result property="createBy" column="create_by" jdbcType="VARCHAR"/> <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/> <result property="updateBy" column="update_by" jdbcType="VARCHAR"/> <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/> <result property="remark" column="remark" jdbcType="VARCHAR"/> </resultMap> <sql id="Base_Column_List"> role_id,role_name,role_key, role_sort,data_scope,menu_check_strictly, dept_check_strictly,status,del_flag, create_by,create_time,update_by, update_time,remark </sql> </mapper> 实体类
@TableName(value ="sys_role") @Data public class SysRole implements Serializable { private Long roleId; private String roleName; private String roleKey; private Integer roleSort; private String dataScope; private Integer menuCheckStrictly; private Integer deptCheckStrictly; private String status; private String delFlag; private String createBy; private Date createTime; private String updateBy; private Date updateTime; private String remark; private static final long serialVersionUID = 1L; }
错误:
控制台打印信息
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.mapper.SysRoleMapper.selectById
找了半天找不到错误,原来是我的实体类id没有加@TableId这个注解,纯纯大冤种
修改之后重启代码可以了