推荐 最新
老詹啊老詹

这个mybatis的@Select自己写sql还会被侵入怎么办?

这种mapper里自己写的sql,我以为mybatis-plus不会侵入了,谁知还是被侵入了,运行时自动给我添加了个条件 @Mapper public interface MyMapper extends BaseMapperX { @Select({"select * from system_users where 1=1 and deleted = 0 and username = #{username} limit 1"}) List getUser(@Param("username") String username); } "image.png" (https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20241231/0e404c32d8bd718537b53313b3b1cf6f.png) 请问大佬们,mybatis-pluse 下 怎么做自己的sql才不会被侵入

8
1
0
浏览量298
莫克先森

如何配置 MyBatis Plus 实现字段名自动转换?

这里,字段名为什么没有自动转换成下划线呢,"image.png" (https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241002/cef6286f669700c65d7a476a63d5c434.png) 这里是mybatis_plus不会自动转换呢,还是我配置错了? 如果这种情况不会自动转换,那么自动转换使用在什么情况下呢?"image.png" (https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241002/9bb2ca27161158e875907060a80421c4.png)

0
1
0
浏览量185
577739990

Mybatis的注解@ResultType的场景是什么?

看源码的时候发现@ResultType只有在返回值类型为void时才会生效。 //源码参考: org.apache.ibatis.builder.annotation.MapperAnnotationBuilder#getReturnType if (void.class.equals(returnType)) { ResultType rt = method.getAnnotation(ResultType.class); if (rt != null) { returnType = rt.value(); } } 那么假如我的应用代码如下,请问,queryStudent 如何返回Student对象?或者说**@ResultType** 的意义是什么? @Select("select * from Student") @ResultType(Student.class) void queryStudent();

0
1
0
浏览量170
卑微实习僧

如何系统学习并使用mybatis-plus框架及其API文档?

菜鸟求教:mybatis-plus的api文档在哪? 目前只是通过一些视频教程和博客了解了一些基本用法。看了官网,没有找到api的文档,请问该如何系统学习并使用该框架?

0
1
0
浏览量181
来自武功山的爱

在springboot中使用MyBatis-Plus处理JSON字符串时出现的异常?

在springboot中使用MyBatis-Plus处理JSON字符串时出现的异常 数据库的user表中有一个info字段,是JSON类型;目前User实体类中却是String类型;为了解决这个问题我使用了MybatisPlus中的JacksonTypeHandler处理器所以我定义了单独实体类来与info字段的属性匹配, @Data public class UserInfo { private Integer age; private String intro; private String gender; } 然后我将User类的info字段修改为UserInfo类型,并声明类型处理器 @Data @TableName(autoResultMap = true)//开启自动映射 public class User { private Long id; private String username; private String password; private String phone; @TableField(typeHandler = JacksonTypeHandler.class) private UserInfo info; private UserStatus status; private Integer balance; private LocalDateTime createTime; private LocalDateTime updateTime; } 为了让页面返回的结果也以对象格式返回,修改UserVO中的info字段: @Data @ApiModel(description = "用户VO实体") public class UserVO { @ApiModelProperty("用户id") private Long id; @ApiModelProperty("用户名") private String username; @ApiModelProperty("详细信息") private UserInfo info; @ApiModelProperty("使用状态(1正常 2冻结)") private UserStatus status; @ApiModelProperty("账户余额") private Integer balance; @ApiModelProperty("用户的收获地址") private List address; } 然后开始运行,好家伙直接报错 Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController' defined in file [F:\springcloud\mp-demo\target\classes\com\itheima\mp\controller\UserController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [F:\springcloud\mp-demo\target\classes\com\itheima\mp\mapper\UserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.io.IOException: Failed to parse mapping resource: 'file [F:\springcloud\mp-demo\target\classes\mapper\IUserService.xml]' 我把User中的info改回String类型,不报错,但是查询出错500因为跟用户VO实体UserVO中的info对不上,总的来说就是User中的info不能用UserInfo,一运行就报错,写进去啥事没有

0
1
0
浏览量175
无心插柳柳成萌

Java + Mybatis Plus 插入数据时ID乱序原因?

mysql自增,id设置成了自增key,有插入失败后id会乱序,比如原来是1,一直失败了5个,到了6。这时候正确插入一条,id返回6。再正确插入一条结果id是5,不知道问题出在哪? 用的是 java + mybatis plus + mysql 8 读写数据,手动无法重现 1.实体类 public class SaledData implements Serializable { private static final long serialVersionUID = 1L; @TableId(value = "id", type = IdType.AUTO) private Long id; private LocalDate recordDate; // 设置了唯一约束 ... } 2.从远程获取数据并入库 @Scheduled(cron = "0 0 1/1 \* \* ?") public void getSaledData() { @Resource private ISaledDataService iSaledDataService; ... SaledData saledData = new SaledData(); ... iSaledDataService.save(saledData); // 有时候数据会重复,会插入失败 } 3.出现的错误结果 "id": 158, "createTime": "2024-05-13T04:00:08", "recordDate": "2024-05-12" "id": 160, "createTime": "2024-05-12T20:00:12", "recordDate": "2024-05-11"

0
1
0
浏览量164
momo

不明白mybatis源码中queryStack变量的作用是什么?

在阅读mybatis源码时,发现问题: 源码org.apache.ibatis.executor.BaseExecutor#query ,如下: protected int queryStack; public List query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException { ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId()); if (closed) { throw new ExecutorException("Executor was closed."); } if (queryStack == 0 && ms.isFlushCacheRequired()) { clearLocalCache(); } List list; try { queryStack++; list = resultHandler == null ? (List) localCache.getObject(key) : null; if (list != null) { //处理Callable类型,还需要绑定IN/OUT参数 handleLocallyCachedOutputParameters(ms, key, parameter, boundSql); } else { //本地缓存没有结果,直接查询数据库 list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql); } } finally { queryStack--; } if (queryStack == 0) { // ... codeA ... } return list; } 我无法明白这个queryStack变量的意义。 通过控制流程来看: 1. 假如这段代码允许多线程并发,那么int变量一定会出现线程安全问题。 2. 假如这段代码仅允许单线程跑,那么只可能是因为存在递归的情况,才需要引用int来记录stack,但是我跟代码半天,也没有发现哪里会递归。 有没有大佬讲讲啥时候这个方法会递归调用?或者是其他作用。

0
1
0
浏览量140
一只臭美的Doggg

Mybatis-Plus报错optimize this sql to a count sql has exception是为什么?

mybatis报错:optimize this sql to a count sql has exception:…………………… exception: null 我的sql语句是这样的: (select distinct projects.*,unitUser.uname as unitName,adminUser.uname as userName from projects inner join unitUser on projects.unitId=unituser.id inner join adminProject on adminProject.projectId = projects.id inner join adminUser on adminUser.id = adminproject.userId ${ew.customSqlSegment} ) UNION (select distinct projects.*,unitUser.uname as unitName,null as userName from projects inner join unitUser on projects.unitId=unituser.id and projects.id not in ( select projects.id from projects inner join adminProject on adminProject.projectId = projects.id ) ${ew.customSqlSegment} ) mybatis-plus的版本是4.0以上的,在网上看了博客报错的都是3.4,3.5; "image.png" (https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241012/15b5e52e0777d5ee27ddd5eb7fe72dd4.png) 该mapper方法的形参是这样的,ew是我的一个queryWrapper。

0
1
0
浏览量118
代码日记

获取子栏目方法应该放在Entity还是Service层?

大佬们,我想写个获取子栏目的方法,这个写在entity(po)里合适吗?还是写到service里或其他什么地方? 目前写在了实体类里(entity?po?domain?) Cat.java package com.test.blog.pojo.po; import java.util.List; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.activerecord.Model; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * 栏目实体类 */ @Data @NoArgsConstructor @AllArgsConstructor public class Cat extends Model { // 配置主键为自增类型 @TableId(type = IdType.AUTO) private Integer catid; private String catname; private Integer pid; private String title; private String description; private Integer sortid; private Short pagesize; // 获取子栏目 public List getChildren() { QueryWrapper queryWrapper = new QueryWrapper(); queryWrapper.eq("pid", this.catid); List list = selectList(queryWrapper); return list; } }

0
1
0
浏览量132
中年复健狗

SpringBoot2中集成Seata和Mybatis-plus以及多数据源,批量操作事务不兼容?

yaml核心配置如下(参考mp的dynamic的官方文档) seata: enable-auto-data-source-proxy: false spring: datasource: dynamic: primary: master strict: true #开启seata代理,开启后默认每个数据源都代理,如果某个不需要代理可单独关闭 seata: true #支持XA及AT模式,默认AT seata-mode: AT 实现层代码如下(执行mp的批量) @Log4j2 @Service public class TestInfoCustom3541ServiceImpl extends ServiceImpl implements TestInfoService { @Override @GlobalTransactional(rollbackFor = Exception.class) public void doBatch() { List addList = new ArrayList(); for (int i = 0; i union.system.test.service.impl.TestInfoCustom3541ServiceImpl.executeBatch[189]-> SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@749ad02c] Transaction not enabled 如果是单条单条的save,则不会有任何问题。基于seata,官方也说和ORM层框架是不会影响的。dynamic-datasource官方文档也没有更多的说明,搜索目前也更多是说多数据源问题,参考调整了下,有些直接没法起,有些结果还是一样,不是很懂这方面,有没有大佬能解答一下

0
1
0
浏览量133