创建新项目
写好配置文件
server:
port: 8905
#配置MP控制台打印日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3307/shequ-acl?characterEncoding=utf-8&useSSL=false
username: root
password: root
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
使用mybatisX插件快速生成一张表的相关类
而后将生成好的文件夹剪切进创建好的目录中:
引入依赖:
<!-- -web依赖引入以后才会有@RestController注解 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--Lombok引入-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
启动类上加扫描注解:
@SpringBootApplication
@MapperScan("com.mybatistest.mybatistest.mapper")
public class MybatistestApplication {
public static void main(String[] args) {
SpringApplication.run(MybatistestApplication.class, args);
}
}
写好Controller层
@RestController
@RequestMapping("mybatisPlus/test")
public class PermissionController {
@Autowired
PermissionService permissionService;
@Autowired
PermissionServiceSecond permissionServiceSecond;
@GetMapping("/slectByNameSecond")
public List<Permission> slectByNameSecond(@RequestParam String name){
return permissionServiceSecond.slectByNameSecond(name);
}
@GetMapping("/slectByName")
public List<Permission> queryByName(@RequestParam String name){
return permissionService.queryByName(name);
}
}
Mapper层
如果要在Service层注入mapper的话需要在mapper接口上加上@Repository
注解
public interface PermissionMapper extends BaseMapper<Permission> {
}
@Repository
public interface PermissionMapperSecond extends BaseMapper<Permission> {
}
Service层
方式1:实现类不做继承
public interface PermissionService extends IService<Permission> {
List<Permission> queryByName(String name);
}
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
implements PermissionService{
@Override
public List<Permission> queryByName(String name) {
Permission permission = new Permission();
permission.setName(name);
QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>(permission);
List<Permission> permissions = baseMapper.selectList(queryWrapper);
return permissions;
}
}
方式二:实现类继承ServiceImpl<xxMapper, xxEntity>
public interface PermissionServiceSecond extends IService<Permission> {
List<Permission> slectByNameSecond(String name);
}
@Service
public class PermissionServiceSecondImpl implements PermissionServiceSecond {
@Autowired
PermissionMapperSecond permissionMapperSecond;
@Override
public List<Permission> slectByNameSecond(String name) {
Permission permission = new Permission();
permission.setName(name);
QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>(permission);
List<Permission> permissions = permissionMapperSecond.selectList(queryWrapper);
return permissions;
}
@Override
public boolean saveBatch(Collection<Permission> entityList, int batchSize) {
return false;
}
@Override
public boolean saveOrUpdateBatch(Collection<Permission> entityList, int batchSize) {
return false;
}
@Override
public boolean updateBatchById(Collection<Permission> entityList, int batchSize) {
return false;
}
@Override
public boolean saveOrUpdate(Permission entity) {
return false;
}
@Override
public Permission getOne(Wrapper<Permission> queryWrapper, boolean throwEx) {
return null;
}
@Override
public Map<String, Object> getMap(Wrapper<Permission> queryWrapper) {
return null;
}
@Override
public <V> V getObj(Wrapper<Permission> queryWrapper, Function<? super Object, V> mapper) {
return null;
}
@Override
public BaseMapper<Permission> getBaseMapper() {
return null;
}
@Override
public Class<Permission> getEntityClass() {
return null;
}
}
执行结果
[
{
"code": "btn.User.add",
"createTime": 1622455537000,
"id": 7,
"isDeleted": 0,
"name": "添加",
"pid": 3,
"type": 2,
"updateTime": 1622507669000
},
{
"code": "btn.Role.add",
"createTime": 1622455537000,
"id": 12,
"isDeleted": 0,
"name": "添加",
"pid": 4,
"type": 2,
"updateTime": 1622507768000
},
{
"code": "btn.Permission.add",
"createTime": 1622455537000,
"id": 16,
"isDeleted": 0,
"name": "添加",
"pid": 5,
"type": 2,
"updateTime": 1622507859000
},
{
"code": "btn.AttrGroup.add",
"createTime": 1624006384000,
"id": 28,
"isDeleted": 0,
"name": "添加",
"pid": 27,
"toCode": "AttrGroupAdd",
"type": 2,
"updateTime": 1624006445000
},
{
"createTime": 1624006770000,
"id": 34,
"isDeleted": 0,
"name": "添加",
"pid": 33,
"toCode": "SkuInfoAdd",
"type": 2,
"updateTime": 1624006874000
},
{
"code": "",
"createTime": 1624007141000,
"id": 38,
"isDeleted": 0,
"name": "添加",
"pid": 37,
"toCode": "ActivityInfoAdd",
"type": 2,
"updateTime": 1624007173000
},
{
"createTime": 1624007217000,
"id": 41,
"isDeleted": 0,
"name": "添加",
"pid": 40,
"toCode": "CouponInfoAdd",
"type": 2,
"updateTime": 1624007242000
}
]