SpringBoot integrates Mybatis-Plus
1. Refer to the whole mybatis of springboot
Springboot integrates mybatis
with: corresponding maven dependencies, database connections, entity classes, database tables, etc.
Two.pom.xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
Three.application.properties
Note: For other database connections, refer to the integration of 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
The content is as follows:
mapper.xml location
print sql statement
hump enabled
primary key id, using self-increment
Four. Entity class
@NoArgsConstructor
@Data
@AllArgsConstructor
@TableName(value = "tb_user")
public class User {
private Integer id;
private String name;
private Integer age;
private Date createTime;
}
@tableName: the table name corresponding to the entity class
五.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);
}
6. Test
http://localhost:8080/query/1