SpringBoot整合Mybatis-Plus注解版

一.参考springboot整个mybatis

springboot整合mybatis
内含:相应的maven依赖、数据库连接、实体类、数据库表等等


二.pom.xml

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

三.application.properties

注意:其它的数据库连接,参考整合mybatis中

mybatis-plus.mapper-locations=common-mapper/classpath:com/jd/lean/mjp/dal/mapper/*.xml
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.configuration.map-underscore-to-camel-case= true
mybatis-plus.global-config.db-config.id-type=auto

内容依次为:
mapper.xml位置
打印sql语句
驼峰启用
主键id,采用自增


四.实体类

@NoArgsConstructor
@Data
@AllArgsConstructor
@TableName(value = "tb_user")
public class User {
    
    
    private Integer id;
    private String name;
    private Integer age;
    private Date createTime;
}

@tableName:实体类对应的表名

五.controller、service、mapper

3.mapper

@Mapper
public interface UserMapperMP extends BaseMapper<User >{
    
    
}

1.controller

    @Resource
    private UserService userService;
    @GetMapping("/query/{id}")
    public User query(@PathVariable("id") Long uid) {
    
    
        User user = userService.query(pid);
        return user ;
    }

2.service

    @Resource
    private UserMapperMP userMapperMP;
    public User query(Long uid) {
    
    
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", uid);//表列名称、值
        return userMapperMP.selectOne(wrapper);
    }

六.测试

http://localhost:8080/query/1

猜你喜欢

转载自blog.csdn.net/tmax52HZ/article/details/119078500