Mybatis-plus实现简单的CRUD
Mybatis-plus简介
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生
代码
entity.java
entity.java
`@TableName("location")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Location {
@TableField(value = "id")
private int id;
@TableField(value = "name")
private String name;
@TableField(value = "latitude")
private String latitude;
}
`
service接口
int add(Location location);
int delete(int id);
int update(Location id);
List<Location> find(String name);
List<Location> selectPage(Integer current,Integer size);
}
serviceImpl
@Service("locationService")
@Transactional
public class LocationServiceImpl implements LocationService{
@Autowired
private LocationMapper locationMapper;
@Override
public int add(Location location) {
return locationMapper.insert(location);
}
@Override
public int delete(int id) {
return locationMapper.deleteById(id);
}
@Override
public int update(Location id) {
return locationMapper.updateById(id);
}
@Override
public List<Location> find(String name) {
Wrapper<Location> wrapper =new EntityWrapper<>();
return locationMapper.selectList(wrapper);
}
@Override
public List<Location> selectPage(Integer current,Integer size) {
Wrapper<Location> wrapper = new EntityWrapper<>();
Page<Location> page = new Page<>(current,size);
List<Location> locations = locationMapper.selectPage(page,wrapper);
return locations;
}
Controller层
package com.liu.controller;
import com.liu.entity.Location;
import com.liu.service.LocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@ResponseBody
@RequestMapping("/cc")
public class LocationController {
@Autowired
private LocationService locationService;
@RequestMapping("/add")
public int add(@RequestBody Location location) {
int a = locationService.add(location);
return a;
}
@RequestMapping("/delete/{id}")
public int delete(@PathVariable("id") int id) {
return locationService.delete(id);
}
@RequestMapping("/update")
public int update(@RequestBody Location id) {
return locationService.update(id);
}
@RequestMapping("/find/{name}")
public List<Location> findByName(@PathVariable("name") String name) {
List<Location> name1 = locationService.find(name);
return name1;
}
@RequestMapping("/page/{current}/{size}")
public List<Location> selectBookPage(@PathVariable Integer current, @PathVariable Integer size) {
List<Location> locations = locationService.selectPage(current, size);
return locations;
}
}
mapper层
@Mapper
public interface LocationMapper extends BaseMapper<Location> {
配置类
config
@EnableTransactionManagement
@Configuration
@MapperScan("com.liu.mapper") //Dao接口所在包
public class MybatisConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor page = new PaginationInterceptor();
page.setDialectType("mysql");
return page;
}
}
.yml
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8
username: 自己的账户
password: 自己的密码
driver-class-name: com.mysql.jdbc.Driver
#mybatis
mybatis-plus:
mapper-locations: classpath:/mapper/*.xml
#实体扫描,多个package用逗号或者分号分隔
type-aliases-package: com.liu.entity
#typeEnumsPackage: com.baomidou.springboot.entity.enums