CRUD based on Mybatis-Plus

Using mybatis-plus to automatically generate code for five modules (xml/bean/mapper/service/controller), here is a practice of CRUD under the mybatis-plus framework.

Or the original springboot project.

mybatis-plus is also an enhanced version of mybatis. It does not change the original function of mybatis, but adds some new functions to improve the development efficiency based on the original function of traditional mybatis.

For example, in the mybatis-plus framework, the project mapper layer interface can acquire basic CRUD functionality by inheriting BaseMapper without writing mapper.xml statements.

AutoGenerator automatically generates mapper.xml as follows:



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://www.mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<mapper namespace="cn.example.demo.mapper.SoldierMapper">

    <!-- Common Query Mapping Results -->
    <resultMap id="BaseResultMap" type="cn.example.demo.bean.Soldier">
        <id column="soldier_id" property="soldierId" />
        <result column="soldier_name" property="soldierName" />
        <result column="join_army_time" property="joinArmyTime" />
    </resultMap>

    <!-- Common Query Result Column -->
    <sql id="Base_Column_List">
        soldier_id, soldier_name, join_army_time
    </sql>

</mapper>


The following Mapper interface is also automatically generated, with nothing to write in it:



/**
 * <p>
 * Mapper Interface
 * </p>
 *
 * @author gene
 * @since 2019-09-11
 */
public interface SoldierMapper extends BaseMapper<Soldier> {
    
}


After the project integrates mybatis-plus, in most CRUD scenarios, you can skip the mapper layer, write interface methods directly to the business layer interface, and then call methods within the BaseMapper interface in the business implementation class.

Business Interface (write it yourself):



package cn.example.demo.service;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;

import cn.example.demo.bean.Soldier;

/**
 * <p>
 * Service Class
 * </p>
 *
 * @author gene
 * @since 2019-09-11
 */
public interface SoldierService extends IService<Soldier> {
    /**
     * increase
     * 
     * @param soldier
     * @return
     */
    int insert(Soldier soldier);

    /**
     * Query All
     * 
     * @return
     */
    List<Soldier> selectAll(Wrapper<Soldier> queryWrapper);

    /**
     * Check by ID
     * 
     * @param soldier
     * @return
     */
    Soldier selectById(Integer soldierId);

    /**
     * Change 1 line
     * 
     * @param soldier
     * @return
     */
    int updateOne(Soldier soldier);

    /**
     * Delete a line
     * 
     * @param soldier
     * @return
     */
    int deleteOne(Soldier soldier);
}



Implementation class:



package cn.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.example.demo.bean.Soldier;
import cn.example.demo.mapper.SoldierMapper;
import cn.example.demo.service.SoldierService;

import java.util.List;

import org.springframework.stereotype.Service;

/**
 * <p>
 * Service Implementation Class
 * </p>
 *
 * @author gene
 * @since 2019-09-11
 */
@Service
public class SoldierServiceImpl extends ServiceImpl<SoldierMapper, Soldier> implements SoldierService {

    @Override
    public int insert(Soldier soldier) {
        return baseMapper.insert(soldier);
    }

    @Override
    public List<Soldier> selectAll(Wrapper<Soldier> queryWrapper) {
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Soldier selectById(Integer soldierId) {
        return baseMapper.selectById(soldierId);
    }

    @Override
    public int updateOne(Soldier soldier) {
        return baseMapper.updateById(soldier);
    }

    @Override
    public int deleteOne(Soldier soldier) {
        return baseMapper.deleteById(soldier);
    }

}


Test method, paste an "add":


    @Autowired
    private SoldierService ss;


    @Test
    public void insertTest() {
        LocalDateTime now = LocalDateTime.now();

        Soldier soldier = new Soldier("yaobuqi", now);

        int affect = ss.insert(soldier);

        System.err.println("affect- " + affect);
    }

Finish work.

Finally, there are exceptions encountered during the test:

  • java.lang.NoSuchMethodError: org.apache.ibatis.session.Configuration.getDefaultScriptingLanguageInstance() Lorg/apache/ibatis/scripting/LanguageDriver

    • Upgrade mybatis-spring-boot-starter version to 2.1.0
    • Upgrade mybatis-generator-core version to 1.3.7

__

  • dao.InvalidDataAccessApiUsageException: Error attempting to get column 'join_army_time' from result set. Cause: java.sql.SQLFeatureNotSupportedException; null; nested exception is java.sql.SQLFeatureNotSupportedException

In short: using mybatis-plus reverse engineering converts the date type in the database to LocalDateTime, and there is an error accessing the interface: java.sql.SQLFeatureNotSupportedException.

This is due to the compatibility issue between druid and mybatis 3.5.1. mybatis-plus-generator 3.1.2 refers to mybatis 3.5.1, and druid-boot-1.1.18 is not compatible with it. You should reduce the version of mybatis-plus to 3.1.0 or less.

If the above method is invalid, replace the druid data source with the hikaricp data source:



    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>3.3.1</version>
    </dependency>


Another way is to change the LocalDateTime type of entity class members to Date type.

Tags: Java Mybatis SQL xml

Posted on Sun, 10 Nov 2019 01:14:06 -0500 by synking