3月 092020
 

在spring boot的application.yml文件中配置:

# 配置sql打印日志
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

如果是application.properties,添加:

# 配置sql打印日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

打印结果类似这样: Continue reading »

3月 092020
 

表现

仅配置单个数据源时,mybatis plus的save/saveBatch接口调用正常
配置多个数据源、动态切换时,mybatis plus的save接口调用正常,saveBatch调用失败,报错如下

org.apache.ibatis.exceptions.PersistenceException: 
### Error flushing statements.  Cause: org.apache.ibatis.executor.BatchExecutorException: com.xxx.survey.mapper.SurveyAnswerMapper.insert (batch index #1) failed. Cause: java.sql.BatchUpdateException: 对象名 't_survey_answer' 无效。
### Cause: org.apache.ibatis.executor.BatchExecutorException: com.xxx.survey.mapper.SurveyAnswerMapper.insert (batch index #1) failed. Cause: java.sql.BatchUpdateException: 对象名 't_survey_answer' 无效。
 at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
 at org.apache.ibatis.session.defaults.DefaultSqlSession.flushStatements(DefaultSqlSession.java:255)
 at com.baomidou.mybatisplus.extension.service.impl.ServiceImpl.saveBatch(ServiceImpl.java:128)
 at com.baomidou.mybatisplus.extension.service.IService.saveBatch(IService.java:58)
 at com.baomidou.mybatisplus.extension.service.IService$$FastClassBySpringCGLIB$$f8525d18.invoke(<generated>)
 at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
 at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:736)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
 at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
 at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
 at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
 at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:671)
 at com.xxx.survey.service.impl.SurveyAnswerServiceImpl$$EnhancerBySpringCGLIB$$dbeefaf3.saveBatch(<generated>)
 at com.xxx.survey.ServiceTest.testGetAttendanceRecord(ServiceTest.java:91)
 at sun.refl

Continue reading »

3月 032020
 

本文主要关注如何使用mybatis/mybatis plus连接SQL Server数据库,因此将省略其他项目配置、代码。

框架选择

应用框架:spring boot
ORM框架:mybatis plus(对于连接数据库而言,mybatis和mybatis plus其实都一样)
数据库连接池:druid

pom依赖

此处仅给出我的配置,mybatis/druid请依据自己项目的需要进行选择。 Continue reading »

2月 012020
 

找到两种方法:
1、采用mybatis注解的方式
参见:MyBatis Plus 自定义查询语句
DAO层:

@Select("select b.bomName, " +
		"b.bomProductType, b.bomMaterial, " +
		"o.customerID AS bomID, " +
		"o.ordersDataNo AS qrCode, " +
		"s.deliveryDate AS barCode, " +
		"s.mainType AS workshop " +
		"FROM mes_order_bom b " +
		"LEFT JOIN mes_order_ordersdata o ON b.ordersID = o.id " +
		"LEFT JOIN mes_order_soncontract s ON o.sonContractID = s.id " +
		"WHERE o.ordersDataNo IN (#{orderNoList})")
List<MesOrderBom> getBomAndOrderCodeNumber(@Param("orderNoList")List<String> orderNoList);

Continue reading »

2月 012020
 

mybatis-plus select查询语句默认是查全部字段,有两种方法可以指定要查询的字段

假定表结构如下:

CREATE TABLE `user` (
  `id` bigint(20) NOT NULL COMMENT '主键',
  `name` varchar(30) DEFAULT NULL COMMENT '姓名',
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
  `manager_id` bigint(20) DEFAULT NULL COMMENT '直属上级id',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

假设目前仅需要查询name,age两个字段。 Continue reading »

10月 192019
 

需求:xml中传入参数中包含一个list,需要在where中拼接in语句

假设查询person表,参数类型为XXXVo,XXXVo中包含一个List对象,保存了状态列表,此时可以参考如下查询

<select id="queryXXX" parameterType="XXXVo"
			resultMap="XXXResult">
		select *
		from person
		 WHERE 1=1
		<if test="statusFilter != null and statusFilter.size() > 0">
            and status in
            <foreach collection="statusFilter" item="statusId" index="i" open="(" close=")" separator=",">
                #{statusId}
            </foreach>
        </if>
		 ORDER BY DEPTID
	</select>

Continue reading »