首先要配置好MySQL数据库,并且建立af-school数据库。
思路流程是pom文件添加mybatis及mysql数据库驱动依赖--创建application.yml配置文件,并配置数据库参数---建立数据库具体类---建立mybatis接口类--建立数据库操作接口--将数据库操作接口实例化--controller调用数据库实例化类,完成数据库读写。
1、配置pom.xml依赖
<!--mybatis起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<!--mysql驱动依赖-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
2、创建application.yml配置文件,配置数据库驱动、url,账号及密码
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.101:3306/af_school
username: root
password: a1b2c3
3、在dao包里创建mybatis@Mapper注解接口
@Mapper
public interface StudentMapper {
@Select("select * from af_school.student")
public List<Student> findAll();
@Select("select * from af_school.student where id=#{id}")
public Student findById(Integer id);
}
4、在entity包里创建af_school数据库里的Student类
package com.zw.springboot.springbootmybatis.entity;
/** 本类由 POJO生成器 自动生成于 2024-04-26 22:45:56
作者:阿发你好 官网: http://afanihao.cn
*/
/** INSERT语句 ( 预处理方式 )
INSERT INTO `student`
(`id`, `name`, `sex`, `phone`, `birthday`)
VALUES(?, ?, ?, ?, ?)
*/
import java.util.Date;
/** INSERT语句 ( MyBatis方式 )
INSERT INTO `student`
(`id`, `name`, `sex`, `phone`, `birthday`)
VALUES(#{id}, #{name}, #{sex}, #{phone}, #{birthday})
自增主键: 无
*/
public class Student
{
public Integer id ;
public String name ;
public Boolean sex ;
public String phone ;
public Date birthday ;
public Student(){
}
public Student(Integer id,String name,Boolean sex,String phone,Date birthday){
this.id=id;
this.name=name;
this.sex=sex;
this.phone=phone;
this.birthday=birthday;
}
public void setId(Integer id)
{
this.id=id;
}
public Integer getId()
{
return this.id;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return this.name;
}
public void setSex(Boolean sex)
{
this.sex=sex;
}
public Boolean getSex()
{
return this.sex;
}
public void setPhone(String phone)
{
this.phone=phone;
}
public String getPhone()
{
return this.phone;
}
public void setBirthday(Date birthday)
{
this.birthday=birthday;
}
public Date getBirthday()
{
return this.birthday;
}
}
5、在service包里创建StudentService接口
public interface StudentService {
public List<Student> findAll();
public Student findById(Integer id);
}
6、在service的子包impl里创建StudentService接口的实现类StudentServiceImpl
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> findAll() {
return studentMapper.findAll();
}
@Override
public Student findById(Integer id) {
return studentMapper.findById(id);
}
}
7、在controller包里创建类StudentController,与数据库交互将数据传到web
package com.zw.springboot.springbootmybatis.controller;
import com.zw.springboot.springbootmybatis.entity.Student;
import com.zw.springboot.springbootmybatis.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* ClassName: StudentController
* Package: com.zw.springboot.springbootmybatis.controller
* Description:
*
* @Author 唱片
* @Create 2024/5/4 下午10:44
* @Version 1.0
*/
@RestController
@ResponseBody
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/findById")
public Student findById(Integer id) {
return studentService.findById(id);
}
@RequestMapping("/findAll")
public List<Student> findAll() {
return studentService.findAll();
}
@RequestMapping("/testStudent")
public Student testStudent() {
Student student = new Student();
student.setId(1001);
student.setName("dddd");
student.setSex(true);
student.setPhone("123456789");
return student;
}
@RequestMapping("/hello")
public String hello() {
return "hello";
}
}