1、问题概述?
MybatisPlus通过BaseMapper为我们带来了丰富的基础功能操作,非常使用。
但是在实际的操作中,我们还需要大量的自定义SQL的的时候,这时候就需要自定义xml,从而自定义sql语句。
2、创建工程
2.1、项目结构
2.2、在数据库中创建测试表格
表格需要创建主键
CREATE TABLE `student` (
`stu_id` varchar(50) NOT NULL,
`stu_name` varchar(25) DEFAULT NULL,
`stu_sex` varchar(255) DEFAULT NULL,
`stu_both` varchar(255) DEFAULT NULL,
`stu_addr` varchar(200) DEFAULT NULL,
`stu_pwd` varchar(10) DEFAULT NULL,
`stu_age` varchar(255) DEFAULT NULL,
PRIMARY KEY (`stu_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `student`(`stu_id`,`stu_name`,`stu_sex`,`stu_both`,`stu_addr`,`stu_pwd`,`stu_age`) values
('1001','晓春1','0','1990-05-06 15:27:56','安徽合肥','1001',NULL),
('1002','小春11','1','1998-05-06 15:27:56','安徽黄山','1002',NULL),
('1004','汤晓春','1','1997-05-06 15:27:56','安徽合肥','1004',NULL),
('1005','十一郎','1','1992-05-06 15:27:56','安徽合肥','1005',NULL),
('1006','十二郎','0','1991-05-06 15:27:56','安徽合肥','1006',NULL),
('1007','十三郎','1','1991-05-06 15:27:56','','1006',NULL),
('1008','十四郎','1','1991-05-06 15:27:56',NULL,'1006',NULL),
('1009','十五郎','1','1991-05-06 15:27:56',NULL,'1006',NULL),
('1010','','1','2020-05-08 00:00:00','','',NULL),
('1011',NULL,NULL,NULL,NULL,NULL,NULL);
2.3、工程的pom.xml配置我呢见
重点1:引入springboot+mybatisplus+mysql包
重点2:引入如下配置,否则工程不扫描xml配置文件
<resources>
<resource>
<directory>src/main/java</directory>
<includes><include>**/*.xml</include></includes>
</resource>
</resources>
【pom.xml配置我呢见】
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.16</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>mybatisplusxml</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mybatisplusxml</name>
<description>mybatisplusxml</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!--mybatisplus包-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
2.4、在application.yml中配置相关信息
spring:
#数据库连接配置,未使用连接池
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test11?useUnicode=true&characterEncoding=utf-8&useSSL=false&zeroDateTimeBehavior=convertToNull
username: root
password: 123456
# 配置mybatis-plus
mybatis-plus:
configuration:
#log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: false # 别名配置
type-aliases-package: com.*.*.bean
mapper-locations: classpath:/com/example/mybatisplusxml/mapper/*.xml
2.5、创建Student实体bean
记得添加set和get及构造器
@TableName("student")
public class Student {
@TableId(value = "stu_id")
private Integer stu_id;
private String stu_name;
private String stu_sex;
private String stu_age;
private String stu_addr;
private String stu_pwd;
}
2.6、创建StudentMapper接口
public interface StudentMapper extends BaseMapper<Student> {
public List<Student> queryStudentByIdAndName(Student student);
}
2.7、创建StudentMapper.xml配置文件
namespace的地址是StudentMapper接口的全限定名称
<?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.example.mybatisplusxml.mapper.StudentMapper">
<!--通过username查询firstname-->
<select id="queryStudentByIdAndName" resultType="student" parameterType="student">
select * from student
where stu_id=#{stu_id} and stu_name=#{stu_name}
</select>
</mapper>
2.8、创建StudentService业务层类
@Service
public class StudentService {
@Autowired(required = false)
private StudentMapper studentMapper;
public List<Student> queryStudentByIdAndName(Student student){
return studentMapper.queryStudentByIdAndName(student);
}
}
2.9、创建StudentController控制层类
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/queryStudentByIdAndName")
@ResponseBody
public List<Student> queryStudentByIdAndName(Student student){
System.out.println(student.getStu_id());
System.out.println(student.getStu_name());
return studentService.queryStudentByIdAndName(student);
}
}
2.10、配置启动类
重点1:@MapperScan注解扫描mapper
@SpringBootApplication
@MapperScan("com.example.mybatisplusxml.mapper")
public class MybatisplusxmlApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusxmlApplication.class, args);
}
}
2.11、通过浏览器测试
http://localhost:8080/queryStudentByIdAndName?stu_id=1001&stu_name=晓春1
3、源码下载
https://download.csdn.net/download/tangshiyilang/90078053