Mybatis-puls的简单CURD和分页

直接进入主题:

1、首先添加依赖

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency> 

2、填好Springboot的配置文件

# 应用名称
spring.application.name=test

#数 据 源 配 置
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名?characterEncoding=UTF-8&serverTimezone=GMT
spring.datasource.username=账号
spring.datasource.password=密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# 应用服务 WEB 访问端口
server.port=8080

#相关映射
mybatis-plus.mapper-locations=classpath:mappers/*xml
mybatis-plus.type-aliases-package=com.example.test.mybatis.entity
        
#驼峰命名
mybatis-plus.configuration.map-underscore-to-camel-case=true

#统一字节编码
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8

3、CURD

开始实践:
首先先写个简单的实体类
需注意如下:

@Date  免写get和set方法
@TableName(value = "表名")

变量注释
@TableId(value = "列名",type = IdType.AUTO)  //一般用于自动增长的id 主键,注意要和数据库的自动增长类型对于。
@TableField(value = "列名")  //常用方式,若不想写注释需要变量名和数据库列名一样。
@TableField(value = "列名",fill = 填充类型) //一般用于创建时间和修改时间。若无填充类型默认不填充
@TableLogic(value = "0",delval ="1")  //使用这个注释就不执行物理删除,实现逻辑删除

自动填充就必须实现MetaObjectHandler的方法,才能实现,例如:

自动填充就必须实现MetaObjectHandler的方法,才能实现
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "CreateTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "Delete", Integer.class, 0);
    }
    
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject,"ReworkTime", Date.class, new Date());
    }
}

Mapper接口:

例如:
public interface UserMapper extends BaseMapper<Student> {
}
这样就可以直接调用Mybatis-plus封装好的CRUD

BaseMapper源码:

public interface BaseMapper<T> extends Mapper<T> {
    int insert(T entity);

    int deleteById(Serializable id);

    int deleteByMap(@Param("cm") Map<String, Object> columnMap);

    int delete(@Param("ew") Wrapper<T> wrapper);

    int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);

    int updateById(@Param("et") T entity);

    int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);

    T selectById(Serializable id);

    List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);

    List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);

    T selectOne(@Param("ew") Wrapper<T> queryWrapper);

    Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);

    List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);

    List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);

    List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);

    <E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);

    <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param("ew") Wrapper<T> queryWrapper);
}

最后创建UserMapper的对象,直接调用相应的方法,就可以实现简单的CRUD:
以下我自己写的几个简单例子:

@RestController
@RequestMapping("/student")
public class ControllerStudent {

    @Autowired
    StudentMapper studentMapper;

    /**
     * @return 所有学生信息
     * http://127.0.0.1:8080/student/findAll
     */
    @GetMapping("/findAll")
    public List findAll(){
        List<Student> listStudent = studentMapper.selectList(null);
        return listStudent ;
    }

    /**
     * 添加学生
     * @return 添加成功的学生信息
     * http://127.0.0.1:8080/student/add
     */
    @GetMapping("/add")
    public Student add(){
        Student student = new Student();
        student.setClassName("普通一班");
        student.setStudentName("刘金");
        student.setStudentSex("男");
        Integer result = studentMapper.insert(student);
        System.out.println("result="+result);
        return student;
    }

    /**
     * 实现逻辑删除
     * @param studentID
     * @return 逻辑结果
     * http://127.0.0.1:8080/student/delete?studentID=
     */
    @GetMapping("/delete")
    public Integer delete(@RequestParam("studentID") long studentID){
        Integer result = studentMapper.deleteById(studentID);
        return result;
    }

    /**
     * 根据ID进行修改信息
     * @param studentID
     * @return
     * http://127.0.0.1:8080/student/update?studentID=
     */
    @GetMapping("/update")
    public Integer update(@RequestParam("studentID") long studentID){
        Student student = new Student();
        student.setStudentID(studentID);
        student.setClassName("普通二班");
        student.setStudentName("张非");
        student.setStudentSex("男");
        Integer result = studentMapper.updateById(student);
        return result;
    }

    /**
     * 根据ID进行查询信息
     * @param studentID
     * @return 查询结果
     */
    @GetMapping("/find")
    public Student find(@RequestParam("studentID") long studentID){

        return studentMapper.selectById(studentID);
    }
}

4、分页

直接上代码
这是xml文件

<?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.example.test.mybatis.mapper.StudentMapper">
    <select id="findAll" resultType="com.example.test.mybatis.entity.Student">
        SELECT * FROM student_dp
    </select>
</mapper>

这是接口文件

@Mapper
@Component
public interface StudentMapper extends BaseMapper<Student> {

    IPage<Student> findAll(Page<Student> page);
}

再加给config的代码

@Configuration
@MapperScan("com.example.test.mybatis.mapper")
public class Config {
    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

然后最后接口调用直接显示

    @GetMapping("/selectUserPage")
    public IPage<Student> selectUserPage(Page<Student> page) {
        page.setSize(2);
        return studentMapper.findAll(page);
    }

这里我只使用了setSize大小设置每页数据量,还有其他封装方法再使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值