项目目录
添加依赖:pom.xml
org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest mysql mysql-connector-javaorg.projectlombok lombokcom.baomidou mybatis-plus-boot-starter3.0.5 org.apache.velocity velocity-engine-core2.0 com.alibaba druid1.0.29
配置文件application.properties
# mysql 5 驱动不同 com.mysql.jdbc.Driver # mysql 8 驱动不同 com.mysql.cj.jdbc.Driver . 需要增加时区的配置 serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
创建一个MybatisPlus分页的拦截器MybatisPlusConfig,如果不创建,MybatisPlus就不会在sql上增加分页的语句
@Configuration @ConditionalOnClass(value = {PaginationInterceptor.class}) public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; } }
建立实体类User
@TableName("user") public class User { private Long id; private String name; private Integer age; private String email; //getter and setter }
mapper层,建立UserMapper继承baseMapper
@Repository//代表持久层 @Mapper public interface UserMapper extends baseMapper{}
修改启动类MybatisPlusApplication,添加@MapperScan注解
@MapperScan("com.wyx.mapper") @SpringBootApplication public class MybatisPlusApplication { public static void main(String[] args) { SpringApplication.run(MybatisPlusApplication.class, args); } }
service层
public interface UserService { //增 Integer add(User user); //删 Integer delete(Long id); //改 Integer update(User user); //查 User select(Long id); //分页 public IPageselectPage(long page, long size); }
建立serviceImpl
@Service public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; @Override public Integer add(User user) { return userMapper.insert(user); } @Override public Integer delete(Long id) { return userMapper.deleteById(id); } @Override public Integer update(User user) { return userMapper.updateById(user); } @Override public User select(Long id) { return userMapper.selectById(id); } @Override public IPageselectPage(long page, long size) { IPage iPage = userMapper.selectPage(new Page<>(page,size),null); return iPage; } }
controller层
@Controller @RequestMapping("/wyx") public class UserController { @Autowired UserService userService; @RequestMapping("/add") @ResponseBody public Integer addUser(@RequestBody User user){ return userService.add(user); } @RequestMapping("/delete") @ResponseBody public Integer deleteUser(@RequestBody User user){ return userService.delete(user.getId()); } @RequestMapping("/update") @ResponseBody public Integer updateUser(@RequestBody User user){ return userService.update(user); } @RequestMapping("/select") @ResponseBody public User selectUser(@RequestBody User user){ return userService.select(user.getId()); } @RequestMapping("/page") @ResponseBody public ListgetUserPage(@Param("page") long page){ long size = 5; IPage ipage =userService.selectPage(page,size); return ipage.getRecords(); } }
调用apipost接口测试
查 http://127.0.0.1:8080/wyx/select { "id": 2 } 增 http://127.0.0.1:8080/wyx/add { "name": "詹姆斯", "age": 36, "email": "222222222@qq.com" } 改 http://127.0.0.1:8080/wyx/update { "id": 3, "name": "库里", "age": 3, "email": "3333333@qq.com" } 删 http://127.0.0.1:8080/wyx/delete { "id": 5 } 分页 http://127.0.0.1:8080/wyx/page page 2
分页:
在控制台打印日志:执行的sql语句
在application.properties中加
logging.level.com.wyx.mapper=debug mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl联表查询接口编写
创建两张有关联的表
Mapper层UserMapper新增一个接口
@Mapper public interface UserMapper extends baseMapper{ //联表查询用户和他们的所属部门 List selectByUid(@Param("uid") Long uid); }
在resources目录下新建mapper目录,创建UserMapper.xml并编写sql语句
Service层
UserService新增接口
//联表查询 List selectByUid(Long uid);
实现类UserServiceImpl新增重写方法
@Override public List selectByUid(Long uid) { return userMapper.selectByUid(uid); }Controller层
UserController新增一个方法
@RequestMapping("/union") @ResponseBody public List selectByUid(@RequestBody User user){ return userService.selectByUid(user.getUid()); }apipost测试
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)