基于Mybatis-Plus的CRUD的实现
人气:0使用mybatis-plus自动生成了5个模块(xml/bean/mapper/service/controller)的代码,这里练习一下mybatis-plus框架下的CRUD.
还是原先的那个springboot项目.
mybatis-plus也是mybatis的增强版,它并未改变mybatis原有功能,只是在传统mybatis原有基础上又新增了一些功能,用以提高开发效率.
比如,在mybatis-plus框架下,项目mapper层接口可通过继承BaseMapper,获取基本的CRUD功能,而无需编写mapper.xml语句.
AutoGenerator自动生成的mapper.xml如下所示:
<?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接口也是自动生成的,里面什么都不用写:
/** * <p> * Mapper 接口 * </p> * * @author gene * @since 2019-09-11 */ public interface SoldierMapper extends BaseMapper<Soldier> { }
项目集成mybatis-plus之后,在大多数CRUD情景下,可以跳过mapper层,直接到业务层接口写接口方法,然后在业务实现类里调用BaseMapper接口内的方法即可.
业务接口(要自己动手写了):
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); }
实现类:
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类型.
您可能感兴趣的文章:
加载全部内容