前面实例中,mapper文件 继承 BaseMapper 类,直接调用BaseMapper 封装的方法;但是BaseMapper 封装的方法不一定都能满足业务,所以需要我们自定义实现
configLocations
configLocations即MyBatis 配置文件位置,如果有单独的 MyBatis 配置,请将其路径配置到 configLocations中。
- application.properties
spring.application.name = mp-springboot
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mp
spring.datasource.username=root
spring.datasource.password=root
# Logger Config
logging.level.root: debug
#指定mybatis-config.xml的位置
mybatis-plus.config-location=classpath:mybatis-config.xml
- mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
mapperLocations
mapperLocations即MyBatis Mapper 所对应的 mapper配置 文件位置,如果在 Mapper 中有自定义方法(XML 中有自定义实现),需要进行该配置,告诉 Mapper 所对应的 XML 文件位置。
- application.properties
spring.application.name = mp-springboot
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mp
spring.datasource.username=root
spring.datasource.password=root
# Logger Config
logging.level.root: debug
#指定mybatis-config.xml的位置
mybatis-plus.mapper-locations = classpath*:li/chen/com/mapper/*.xml
注意:
1 mapper配置文件UserMapper.xml在resources中路径与java对应的UserMapper.java 路径一致时,可不配mapper-locations;(注意resources中目录写法,如果报错找不到文件可以去target检查文件路径)
2 若UserMapper.xml在resources路径与UserMapper.java 路径不一致时,需要配置。
3 若UserMapper.xml 与 UserMapper.java 在同一目录下,需要在pom.xml加上如下配置(且不需要配置mapper-locations)
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!--mapper文件与xml温江放在同一文件夹下,需添加此配置-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
typeAliasesPackage
设置MyBaits 别名包扫描路径,通过该属性可以给包中的类注册别名,注册后在 Mapper 对应的 XML 文件中可以直接使用类名,而不用使用全限定的类名(即 XML 中调用的时候不用包含包名)。
# 配置实体类的别名路径
mybatis-plus.type-aliases-package=li.chen.com.pojo
mapUnderscoreToCamelCase
是否开启自动驼峰命名规则(camel case)映射,即从数据库列名 A_COLUMN(下划线命名) 到经典 Java属性名 aColumn(驼峰命名) 的类似映射。
1 在application.properties中配置
#指定mybatis-config.xml的位置
# mybatis-plus.config-location=classpath:mybatis-config.xml 注释config‐location
#开启自动驼峰映射,注意:配置configuration.map‐underscore‐to‐camel‐case则不能配置config‐location
mybatis‐plus.configuration.map‐underscore‐to‐camel‐case=true
2 在mybatis-config.xml中配置(不注释config‐location)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--#开启自动驼峰映射,注意:配置configuration.map‐underscore‐to‐camel‐case则不能配置config‐location-->
<setting