通用mapper基础

介绍

配置

配置Maven

1
2
3
4
5
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>

配置实体生成

generatorConfig.xml(Postgresql版)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
<context id="webapi" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<!--处理sql中的`符号-->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>

<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
<property name="caseSensitive" value="true"/>
</plugin>

<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="org.postgresql.Driver"
connectionURL="jdbc:postgresql://127.0.0.1:5432/postgres?characterEncoding=utf8"
userId="postgres"
password="postgres">
</jdbcConnection>

<!-- 生成model模型,对应的包路径,以及文件存放路径(targetProject),targetProject可以指定具体的路径,如./src/main/java,
也可以使用“MAVEN”来自动生成,这样生成的代码会在target/generatord-source目录下 -->
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.wanglibing.postgresql.pojo" targetProject="src/main/java" />
<javaClientGenerator targetPackage="com.wanglibing.postgresql.mapper" targetProject="src/main/java"
type="ANNOTATEDMAPPER"/>

<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="demo1">
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
</context>
</generatorConfiguration>

generatorConfig.xml(MySQL版)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
<context id="webapi" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<!--处理sql中的`符号-->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>

<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
<property name="caseSensitive" value="true"/>
</plugin>

<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/webapi?characterEncoding=utf8"
userId="root"
password="123456">
</jdbcConnection>

<!-- 生成model模型,对应的包路径,以及文件存放路径(targetProject),targetProject可以指定具体的路径,如./src/main/java,
也可以使用“MAVEN”来自动生成,这样生成的代码会在target/generatord-source目录下 -->
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.wanglibing.webapi.pojo" targetProject="src/main/java" />
<javaClientGenerator targetPackage="com.wanglibing.webapi.mapper" targetProject="src/main/java"
type="ANNOTATEDMAPPER"/>

<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="User">
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
</context>
</generatorConfiguration>

使用

添加

删除

更新

查询

Example方法

1
2
3
4
5
6
7
8
// 创建Example
Example example = new Example(Demo1.class);
// 设置排序字段
example.setOrderByClause("字段名 ASC/DESC");
// 创建Criteria
Example.Criteria criteria = example.createCriteria();
// 添加条件
criteria.andEqualTo("name","wlb");

常见问题

通用mapper无法注入问题

问题描述

1
2
3
4
5
6
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.wanglibing.webapi.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1506)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583)
... 42 more

解决办法

在Application.java上加上注解@MapperScan(mapper所在的包路径)。

1
@MapperScan("com.wanglibing.webapi.mapper")

could not autowire

问题描述

Could not autowire.No beans of “Demo1Mapper” type found.
虽然不影响使用,但是看着不爽。

解决办法

在Mapper加一个注解。如所示:

1
2
3
@Component(value = "demo1Mapper")
public interface Demo1Mapper extends Mapper<Demo1> {
}

参考

坚持原创技术分享,您的支持将鼓励我继续创作!