MyBatis-Plus
为简化开发而生
润物无声
只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑。
效率至上
只需简单配置,即可快速进行单表 CRUD 操作,从而节省大量时间。
丰富功能
代码生成、物理分页、性能分析等功能一应俱全。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
spring.application.name=server-mybatis-plus
server.port=8000
## Mybatis
spring.datasource.url=jdbc:mysql://localhost:3306/db_test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
mybatis.typeAliasesPackage=com.shenmazong.servermybatisplus.mapper
mybatis.mapperLocations=classpath:/mapper/*.xml
logging.level.com.shenmazong.servermybatisplus.mapper=debug
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for tb_student
-- ----------------------------
DROP TABLE IF EXISTS `tb_student`;
CREATE TABLE `tb_student` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学生ID',
`student_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '学生名字',
`age` int(11) NULL DEFAULT 0 COMMENT '学生年龄',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of tb_student
-- ----------------------------
INSERT INTO `tb_student` VALUES (1, '吕布', 13);
INSERT INTO `tb_student` VALUES (2, '赵云', 23);
INSERT INTO `tb_student` VALUES (3, '典韦', 25);
INSERT INTO `tb_student` VALUES (4, '黄盖', 35);
INSERT INTO `tb_student` VALUES (5, '马超', 26);
INSERT INTO `tb_student` VALUES (6, '张飞', 33);
SET FOREIGN_KEY_CHECKS = 1;
package com.shenmazong.servermybatisplus.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName(value = "tb_student")
public class TbStudent {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String studentName;
private Integer age;
}
package com.shenmazong.servermybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.shenmazong.servermybatisplus.pojo.TbStudent;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface TbStudentMapper extends BaseMapper<TbStudent> {
}
package com.shenmazong.servermybatisplus.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenmazong.servermybatisplus.mapper.TbStudentMapper;
import com.shenmazong.servermybatisplus.pojo.TbStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@Autowired
TbStudentMapper tbStudentMapper;
@GetMapping(value = "/getById")
public Object getById(@RequestParam("id") Integer id) {
return tbStudentMapper.selectById(id);
}
@GetMapping(value = "/getOneById")
public Object getOneById(@RequestParam("id") Integer id) {
return tbStudentMapper.selectOne(
new QueryWrapper<TbStudent>().lambda().eq(TbStudent::getId, id)
);
}
@GetMapping(value = "/getList")
public Object getOneById() {
return tbStudentMapper.selectList(null);
}
@GetMapping(value = "/addStudent")
public Object addStudent(@RequestParam("name") String name,
@RequestParam("age") Integer age) {
TbStudent tbStudent = new TbStudent();
tbStudent.setStudentName(name);
tbStudent.setAge(age);
return tbStudentMapper.insert(tbStudent);
}
@GetMapping(value = "/setStudent")
public Object setStudent(@RequestParam("id") Integer id,
@RequestParam("name") String name,
@RequestParam("age") Integer age) {
TbStudent tbStudent = new TbStudent();
tbStudent.setId(id);
tbStudent.setStudentName(name);
tbStudent.setAge(age);
return tbStudentMapper.updateById(tbStudent);
}
@GetMapping(value = "/delStudent")
public Object setStudent(@RequestParam("id") Integer id) {
return tbStudentMapper.selectById(id);
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.shenmazong.servermybatisplus.mapper.TbStudentMapper">
<select id="getOneById" resultType="com.shenmazong.servermybatisplus.pojo.TbStudent">
select * from tb_student where id=#{id}
</select>
</mapper>
package com.shenmazong.servermybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.shenmazong.servermybatisplus.pojo.TbStudent;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface TbStudentMapper extends BaseMapper<TbStudent> {
TbStudent getOneById(@Param("id") Integer id);
}
package com.shenmazong.servermybatisplus.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenmazong.servermybatisplus.mapper.TbStudentMapper;
import com.shenmazong.servermybatisplus.pojo.TbStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@Autowired
TbStudentMapper tbStudentMapper;
@GetMapping(value = "/getById")
public Object getById(@RequestParam("id") Integer id) {
return tbStudentMapper.selectById(id);
}
@GetMapping(value = "/getOneById")
public Object getOneById(@RequestParam("id") Integer id) {
return tbStudentMapper.selectOne(
new QueryWrapper<TbStudent>().lambda().eq(TbStudent::getId, id)
);
}
@GetMapping(value = "/getList")
public Object getList() {
return tbStudentMapper.selectList(null);
}
@GetMapping(value = "/addStudent")
public Object addStudent(@RequestParam("name") String name,
@RequestParam("age") Integer age) {
TbStudent tbStudent = new TbStudent();
tbStudent.setStudentName(name);
tbStudent.setAge(age);
return tbStudentMapper.insert(tbStudent);
}
@GetMapping(value = "/setStudent")
public Object setStudent(@RequestParam("id") Integer id,
@RequestParam("name") String name,
@RequestParam("age") Integer age) {
TbStudent tbStudent = new TbStudent();
tbStudent.setId(id);
tbStudent.setStudentName(name);
tbStudent.setAge(age);
return tbStudentMapper.updateById(tbStudent);
}
@GetMapping(value = "/delStudent")
public Object setStudent(@RequestParam("id") Integer id) {
return tbStudentMapper.selectById(id);
}
@GetMapping(value = "/getOneByXml")
public Object getOneByXml(@RequestParam("id") Integer id) {
return tbStudentMapper.getOneById(id);
}
}