Spring Boot 学习笔记 06

0x01 Mybatis服务接口及调用

在我们已经通过逆向生成了Mapper接口类、映射文件以及JAVA模型的之后,我们还需要通过业务层的服务来调用数据。

我们首先在控制层@Controller注解的方法下添加@Autowired注解来注入业务层提取的数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
@Controller
public class StudentController {

@Autowired
private StudentService studentService;

@RequestMapping(value = "/student")
public @ResponseBody Object student(Integer id) {
Student student = studentService.queryStudentById(id);
return student;

}
}

并且在下方的@RequestMapping注解中调用该服务注入的数据存放到Student类中。

这时候肯定还是不够的,因为我们在这里创建了一个叫做StudentService的类,那我们就在Application.java同级目录或下级目录中创建这个服务类和接口。
在Application.java同级目录新增一个包,包名为service,包内新建一个接口类,接口名为StudentService:

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.bjpowernode.springboot.service;

import com.bjpowernode.springboot.model.Student;
public interface StudentService {

/**
* 根据学生ID查询详情
* @param id
* @return
*/
Student queryStudentById(Integer id);
}

该接口的功能为根据学生ID查询学生详情数据。

对应这个接口,我们还需要一个实现类,我们在这个接口同级目录下新建一个包,包名为Impl,包内新建java文件StudentServiceImpl.java来实现接口的功能。

而且我们的业务接口实现类还得通过__@Service__注解放入Spring容器中:

1
2
3
4
5
6
7
8
9
10
11
package com.bjpowernode.springboot.service.impl;

import com.bjpowernode.springboot.mapper.StudentMapper;
import com.bjpowernode.springboot.model.Student;
import com.bjpowernode.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService {

在这个业务层,需要调用数据持久层,就通过@Autowired这个注解注入进来,这里我们就将StudentMapper注入进来

1
2
3
4
5

@Autowired
private StudentMapper studentMapper;


那我们注入的studentMapper就要给我们提供一个方法,那就是通过之前Mybatis逆向生成的selectByPrimaryKey()方法。

1
2
3
4
5
6

@Override
public Student queryStudentById(Integer id) {
return studentMapper.selectByPrimaryKey(id);
}
}

到这里还不够,我们还必须添加一个扫描器,来扫描DAO接口到Spring容器,我们才能在业务接口实现类中实现studentMapper的注入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.bjpowernode.springboot.mapper;

import com.bjpowernode.springboot.model.Student;
import org.apache.ibatis.annotations.Mapper;

@Mapper //扫描DAO接口到spring容器
public interface StudentMapper {
int deleteByPrimaryKey(Integer id);

int insert(Student record);

int insertSelective(Student record);

Student selectByPrimaryKey(Integer id);

int updateByPrimaryKeySelective(Student record);

int updateByPrimaryKey(Student record);
}

但是现在还有一个问题,那就是xml文件在src里面是不能被项目编译执行的,所以我们必须在pom.xml中的添加resources项来让项目编译xml文件:

1
2
3
4
5
6
7
8
9
<!--手动指定文件夹为resources-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>

这样当我们在浏览器输入http://localhost:8080/student?id=1时,我们就能获得id=1的学生信息了。

数据库中的表的信息如下:
id,name,age
1,zhangsan,25
2,lisi,28
3,wangwu,23
4,Tom,21
5,Jck,55
6,Lucy,27
7,zhaoliu,75

浏览器返回{“id”:1,”name”:”zhangsan”,”age”:25}

说明我们配置成功!

0x02 Mybatis扫描多个Mapper接口及子目录

在上面我们说到了扫描器**@Mapper,但是如果我们有多个Mapper接口类和映射文件呢?这里我们就不再使用@Mapper**这个单一扫描器了。

我们在Application.java这个入口类的**@SpringBootApplication注释旁添加一个注释:@MapperScan(“”)** ,来开启扫描Mapper接口的包以及子目录:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.bjpowernode.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication //开启spring配置
@MapperScan("com.bjpowernode.springboot.mapper") //开启扫描Mapper接口的包以及子目录
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}

这样我们就不用再额外添加**@Mapper这个注释了!(“”)** 内写的是Mapper接口的包所在路径。