pom.xml
<dependencies>
<!--mybatis starter-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
Controller
@Controller
public class TestController {
@Autowired
private TestServiceImpl testService;
@RequestMapping("/testMapper")
@ResponseBody
public String testMapper(){
return testService.findAllStudent().toString();
}
}
ServiceImpl
@Service
public class TestServiceImpl implements TestService {
@Autowired
private TestDao testDao;
@Override
public List<Student> findAllStudent() {
List<Student> studentList=null;
studentList=testDao.findAllStudent();
return studentList;
}
}
Dao
@Repository
public interface TestDao {
public List<Student> findAllStudent();
}
***Mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mxd.springbootproxy01.dao.TestDao">
<select id="findAllStudent" resultType="Student">
select * from student
</select>
</mapper>
application.properties
#端口
server.port=8080
#编码格式
server.tomcat.uri-encoding=UTF-8
#数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/mxd
spring.datasource.username=root
spring.datasource.password=190125
#mybatis配置
#映射文件通配
mybatis.mapper-locations=classpath:mapper/'*.xml
#mybatis扫描别名包,和注解@Alias联用
mybatis.type-aliases-package=com/mxd/springbootproxy01/pojo
SpringBootApplication
package com.mxd.springbootproxy01;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.mxd.springbootproxy01.dao")
@SpringBootApplication
public class SpringBootProxy01Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootProxy01Application.class, args);
}
}
目录结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c2da113c8cfa7903e7dff16f195d07ff.png)
测试结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9b506b25afb7e7343dc646916dd97acf.png)