后台访问流程
请求:用户从页面前端,也就是我们所说的view层进行查询访问,进入到controller层找到对应的接口,接着controller层进行对service层进行业务功能的调用,service要进入dao(mapper)层,dao层调用mapper.xml文件生成sql语句到数据库中进行查询。
响应:在数据库中查询到数据后,dao层拿到实体对象的数据,接着交付给service层,接着service层进行业务逻辑的处理,返回结果给controller层,controller根据结果进行最后一步处理,返回结果给前端页面。
创建一个访问用户信息的流程为:
entity->dao->mapper.xml->service->controller
建表
在数据库内创建一个student表,表结构如下:
创建springboot工程
以查找所有student信息为例:
使用idea创建springboot工程并加入相关依赖:
以上三个依赖即为:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
创建student实体类
package com.example.demo.entity;
public class Student
{
private int id;
private String username;
private String password;
private String sex;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
}
创建Mapper接口
package com.example.demo.mapper;
import com.example.demo.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface StudentMapper
{
//查询所有student方法
List <Student> queryStudentList();
}
创建mapper配置文件
<?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.demo.mapper.StudentMapper">
<resultMap id="BaseResultMap" type="com.example.demo.entity.Student">
<result column="id" jdbcType="INTEGER" property="id"/>
<result column="userName" jdbcType="VARCHAR" property="username"/>
<result column="passWord" jdbcType="VARCHAR" property="password"/>
<result column="sex" jdbcType="VARCHAR" property="sex"/>
<result column="age" jdbcType="INTEGER" property="age"/>
</resultMap>
<select id="queryStudentList" resultType="Student">
SELECT * FROM student
</select>
</mapper>
编写Service接口以及其实现类
package com.example.demo.service;
import com.example.demo.entity.Student;
import java.util.List;
public interface StudentService {
List <Student> queryStudentList();
}
package com.example.demo.service.impl;
import com.example.demo.entity.Student;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService
{
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> queryStudentList()
{
return studentMapper.queryStudentList();
}
}
编写Controller层
package com.example.demo.controller;
import com.example.demo.entity.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class StudentController
{
@Autowired
private StudentService studentService;
@GetMapping("/query")
public List <Student> queryStudentList()
{
List <Student> students=studentService.queryStudentList();
return students;
}
}
在application.properties配置数据库等信息
spring.application.name=demo
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useSSL=false&userUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#扫描entity和*mapper.xml
mybatis.type-aliases-package=com.example.demo.entity
mybatis.mapper-locations=classpath:mapper/*.xml
启动类
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication()
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
运行结果
如果想看sql语句的打印,可以在配置中加入如下配置
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
这样运行方法后就会看到控制台打印的sql语句