Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Implementation of CRUD based on Mybatis-Plus

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the implementation of CRUD based on Mybatis-Plus". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the implementation of CRUD based on Mybatis-Plus".

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

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

The mapper.xml automatically generated by AutoGenerator is as follows:

Soldier_id, soldier_name, join_army_time

The following Mapper interface is also automatically generated, and nothing needs to be written in it:

/ *

* Mapper API *

* * @ author gene * @ since 2019-09-11 * / public interface SoldierMapper extends BaseMapper {}

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 the methods in the BaseMapper interface in the business implementation class.

Business interface (to 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;/** *

* Service class *

* * @ author gene * @ since 2019-09-11 * / public interface SoldierService extends IService {/ * add * * @ param soldier * @ return * / int insert (Soldier soldier); / * query all * * @ return * / List selectAll (Wrapper queryWrapper); / * * check according to 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 / *

* Service implementation class *

* * @ author gene * @ since 2019-09-11 * / @ Servicepublic class SoldierServiceImpl extends ServiceImpl implements SoldierService {@ Override public int insert (Soldier soldier) {return baseMapper.insert (soldier);} @ Override public List selectAll (Wrapper 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, post 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);}

Call it a day.

Finally, there are the 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: reverse engineering using mybatis-plus will convert the date type in the database to LocalDateTime, and an error will be reported when accessing the interface: java.sql.SQLFeatureNotSupportedException.

This is due to the compatibility problem between druid and mybatis3.5.1. Mybatis-plus-generator 3.1.2 refers to the mybatis3.5.1 version, but druid-boot-1.1.18 is not yet compatible with it. You should reduce the mybatis-plus version to 3.1.0 or less.

If the above method does not work, change the druid data source to the hikaricp data source:

Com.zaxxer HikariCP 3.3.1

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

Thank you for reading, the above is the content of "the implementation of CRUD based on Mybatis-Plus". After the study of this article, I believe you have a deeper understanding of the implementation of CRUD based on Mybatis-Plus, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report