基於Mybatis-Plus的CRUD

使用mybatis-plus自動生成了5個模塊(xml/bean/mapper/service/controller)的代碼,這裏練習一下mybatis-plus框架下的CRUD.java

仍是原先的那個springboot項目.spring

mybatis-plus也是mybatis的加強版,它並未改變mybatis原有功能,只是在傳統mybatis原有基礎上又新增了一些功能,用以提升開發效率.sql

好比,在mybatis-plus框架下,項目mapper層接口可經過繼承BaseMapper,獲取基本的CRUD功能,而無需編寫mapper.xml語句.數據庫

AutoGenerator自動生成的mapper.xml以下所示:apache

<?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">

    <!-- 通用查詢映射結果 -->
    <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>

    <!-- 通用查詢結果列 -->
    <sql id="Base_Column_List">
        soldier_id, soldier_name, join_army_time
    </sql>

</mapper>

下面的Mapper接口也是自動生成的,裏面什麼都不用寫:springboot

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

項目集成mybatis-plus以後,在大多數CRUD情景下,能夠跳過mapper層,直接到業務層接口寫接口方法,而後在業務實現類裏調用BaseMapper接口內的方法便可.session

業務接口(要本身動手寫了):mybatis

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>
 * 服務類
 * </p>
 *
 * @author gene
 * @since 2019-09-11
 */
public interface SoldierService extends IService<Soldier> {
    /**
     * 增長
     * 
     * @param soldier
     * @return
     */
    int insert(Soldier soldier);

    /**
     * 查詢所有
     * 
     * @return
     */
    List<Soldier> selectAll(Wrapper<Soldier> queryWrapper);

    /**
     * 據ID而查
     * 
     * @param soldier
     * @return
     */
    Soldier selectById(Integer soldierId);

    /**
     * 更改1行
     * 
     * @param soldier
     * @return
     */
    int updateOne(Soldier soldier);

    /**
     * 刪除一行
     * 
     * @param soldier
     * @return
     */
    int deleteOne(Soldier soldier);
}

實現類:app

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>
 * 服務實現類
 * </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);
    }

}

測試方法,貼一個"增":框架

@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);
    }

收工.


最後是測試期間遇到的異常:

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

    • 升級mybatis-spring-boot-starter版本至2.1.0
    • 升級mybatis-generator-core版本至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

簡而言之:使用mybatis-plus逆向工程會將數據庫中的date類型轉換爲LocalDateTime,訪問接口的時候報錯:java.sql.SQLFeatureNotSupportedException.

這是由於druid跟mybatis3.5.1兼容性問題,mybatis-plus-generator 3.1.2引用了mybatis3.5.1版本,而druid-boot-1.1.18還沒有與之兼容,應將mybatis-plus版本降至3.1.0或如下便可.

若是上面的法子無效,那就更換druid數據源爲hikaricp數據源:

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

還有一種方法是,把實體類成員的LocalDateTime類型換爲Date類型.

相關文章
相關標籤/搜索