注解查询
public interface UserMapper extends BaseMapper<User> {("select * from user ${ew.customSqlSegment}")List<User> selectAll((Constants.WRAPPER) Wrapper<User> wrapper);
}
使用XML查询
maven 资源
默认只有resources 中的文件最终会解析到target/classes
中。
如果我们想要把src/main/java
中的文件也被复制到target/classes
中
<resources><resource><!-- 设定主资源目录 --><directory>src/main/resources</directory></resource><resource><!-- 设定主资源目录 --><directory>src/main/java</directory><includes><!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,只处理如下配置中包含的资源类型 --><include>**/*.xml</include></includes><!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,不处理如下配置中包含的资源类型(剔除下如下配置中包含的资源类型)--><excludes><exclude>**/*.yaml</exclude></excludes></resource></resources>
或者使用maven插件maven-resources-plugin
<plugins><plugin><artifactId>maven-resources-plugin</artifactId><version>2.5</version><executions><execution><id>copy-xmls</id><phase>process-sources</phase><goals><goal>copy-resources</goal></goals><configuration><outputDirectory>${basedir}/target/classes</outputDirectory><resources><resource><directory>${basedir}/src/main/java</directory><includes><include>**/*.xml</include></includes></resource></resources></configuration></execution></executions></plugin></plugins>
配置XML
- 配置mapper路径
在application.properties配置mapper路径
mybatis-plus.mapper-locations=classpath:*/*Mapper.xml
- 编写XML文件
注意: xml文件名和Mapper的文件名必须保持一致。比如userMapper,必须对应着UserMapper.xml否则就会找不到对应绑定
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dankun.mp.dao.UserMapper"><select id="selectAll" resultType="com.dankun.mp.entity.User">select * from user ${ew.customSqlSegment}</select>
</mapper>
- 最后别忘记了配置MaperScan注解
@SpringBootApplication
@MapperScan("com.dankun.samples.quickstart.mapper")
public class Application {public static void main(String[] args) {SpringApplication.run(QuickStartApplication.class, args);}
}