使用如下方式删除数据时报了 can not use this method for “getEntity” 的错误。
this.remove(this.lambdaQuery().eq(SomeEntity::getCode, code));
使用如下方式删除数据时报了 can not use this method for “getEntity” 的错误。
this.remove(this.lambdaQuery().eq(SomeEntity::getCode, code));
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* MyBatisPlus插件配置
*
* @author 佳佳
*/
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
重新整理了一下 MyBatis 的使用,以作备忘。
作为开发的 IDE 来讲我更喜欢用 IntelliJ IDEA ,不过在通过 Maven 运行 MyBatis Generator 插件时,java client 文件(即 Mapper 文件)总是会被覆盖。如果在 Mapper 中增加了自定义的接口定义,重新生成时就没有了。
MyBatis 中可以通过全局的表前缀(table prefix)实现一定程度的动态指定表名。
假如表名为 t_xx_game ,对应 entity 的注解如下:
@TableName("t_xx_game")
官方文档 第五章 使用 SqlSession 的示例。
在 MyBatis 中,你可以使用 SqlSessionFactory
来创建 SqlSession
。一旦你获得一个 session 之后,你可以使用它来执行映射语句,提交或回滚连接。
最后,当不再需要它的时候, 你可以关闭 session。
参考官方文档 第四章 事务 实现了使用事务来更新数据。
在 applicationContext.xml 中添加
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
对方法使用 @Transactional
注解
@RequestMapping(value = "update/{account}", method = RequestMethod.POST)
@Transactional
public int updUser(@PathVariable("account") String account) {
return userMapper.updUserByAccount(account);
}
官方的文档里写的太简单了,这里是根据文档创建的示例程序。
使用 MyBatis-Spring 访问 SqlServer 数据库。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>liujiajia.me.learning.mybatis</groupId>
<artifactId>learning-mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
</dependencies>
</project>
通过在 mybatis-generator.xml
中配置 jdbcConnection
可以解决 MySql 和 Oracle 的问题,但是没有找到对应 SqlServer 的方法。
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}">
<!--MySQL-->
<property name="useInformationSchema" value="true"></property>
<!--Oracle-->
<property name="remarksReporting" value="true"></property>
</jdbcConnection>