springboot整合mybatis
新建一个springboot项目
准备数据库
DROP DATABASE IF EXISTS `mybatis`;
CREATE DATABASE mybatis;
USE mybatis;
CREATE TABLE IF NOT EXISTS student (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(25) NOT NULL,
`sex` VARCHAR(4) ,
`age` INT(3),
`phone` VARCHAR(11)
);
INSERT INTO student (NAME,sex,age,phone) VALUES
('小明','男',11,'17634653483'),
('小红','女',12,'18334467486');
配置application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: admin
url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8&serverTimezone=UTC
server:
port: 8080
运行MybatisApplication检测是否配置成功
问题:Web server failed to start. Port 8080 was already in use.
答案:关闭8080端口占用的进程,或者更改8080端口
运行成功
准备实体类
package com.bo.wei.chen.mybatis.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author DELL
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Student {
private Integer id;
private String name;
private String sex;
private int age;
private String phone;
}
准备mapper层
StudentMapper
package com.bo.wei.chen.mybatis.mapper;
import java.util.List;
import com.bo.wei.chen.mybatis.model.Student;
/**
* @author DELL
*/
public interface StudentMapper {
/**
* 增加
* @param student 条件
* @return int
*/
int add(Student student);
/**
* 修改
* @param student 条件
* @return int
*/
int update(Student student);
/**
* 删除
* @param id 条件
* @return int
*/
int delete(int id);
/**
* 查询全部
*
* @return List<Student>
*/
List<Student> select();
}
StudentMapper.xml
<?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.bo.wei.chen.mybatis.mapper.StudentMapper">
<insert id="add" parameterType="com.bo.wei.chen.mybatis.model.Student">
insert student (name,sex,age,phone) values (#{name},#{sex},#{age},#{phone});
</insert>
<update id="update" parameterType="com.bo.wei.chen.mybatis.model.Student">
update student set name = #{name},sex = #{sex},age = #{age}, phone = #{phone} where id = #{id};
</update>
<delete id="delete" parameterType="int">
delete from student where id = #{id};
</delete>
<select id="select" resultType="com.bo.wei.chen.mybatis.model.Student">
select * from student;
</select>
</mapper>
准备service层
idea报错,不影响运行,可以设置关闭
StudentService
package com.bo.wei.chen.mybatis.service;
import java.util.List;
import com.bo.wei.chen.mybatis.model.Student;
/**
* @author DELL
*/
public interface StudentService {
/**
* 查询全部
*
* @return List<Student>
*/
List<Student> select();
/**
* 增加
* @param student 条件
* @return int
*/
int add(Student student);
/**
* 修改
* @param student 条件
* @return int
*/
int update(Student student);
/**
* 删除
* @param id 条件
* @return int
*/
int delete(int id);
}
StudentServiceImpl
package com.bo.wei.chen.mybatis.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bo.wei.chen.mybatis.mapper.StudentMapper;
import com.bo.wei.chen.mybatis.model.Student;
import com.bo.wei.chen.mybatis.service.StudentService;
/**
* @author DELL
*/
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
/**
* 查询全部
*
* @return List<Student>
*/
@Override
public List<Student> select() {
return studentMapper.select();
}
/**
* 增加
*
* @param student 条件
*
* @return int
*/
@Override
public int add(Student student) {
return studentMapper.add(student);
}
/**
* 修改
*
* @param student 条件
*
* @return int
*/
@Override
public int update(Student student) {
return studentMapper.update(student);
}
/**
* 删除
*
* @param id 条件
*
* @return int
*/
@Override
public int delete(int id) {
return studentMapper.delete(id);
}
}
准备controller层
先对controller进行一波小测试
package com.bo.wei.chen.mybatis.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bo.wei.chen.mybatis.model.Student;
/**
* @author DELL
*/
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping("/student")
public String student() {
Student student = Student.builder()
.name("测试")
.build();
return student.getName();
}
}
重启MybatisApplication,出现如下错误信息
解决方法:
package com.bo.wei.chen.mybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author DELL
*/
@SpringBootApplication
@MapperScan("com.bo.wei.chen.mybatis.mapper")
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
重启MybatisApplication成功后,浏览器访问http://localhost:8080/test/student
成功!
StudentController
package com.bo.wei.chen.mybatis.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bo.wei.chen.mybatis.model.Student;
import com.bo.wei.chen.mybatis.service.StudentService;
/**
* @author DELL
*/
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
/**
* 查询全部
*
* @return int
*/
@RequestMapping("/select")
public List<Student> select() {
return studentService.select();
}
/**
* 增加
*
* @param student 条件
*
* @return int
*/
@RequestMapping("/add")
public int add(Student student) {
return studentService.add(student);
}
/**
* 修改
*
* @param student 条件
*
* @return int
*/
@RequestMapping("/update")
public int update(Student student) {
return studentService.update(student);
}
/**
* 删除
*
* @param id 条件
*
* @return int
*/
@RequestMapping("/delete")
public int delete(int id) {
return studentService.delete(id);
}
}
准备测试类
package com.bo.wei.chen.mybatis01;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.bo.wei.chen.mybatis01.model.Student;
import com.bo.wei.chen.mybatis01.service.StudentService;
@SpringBootTest
class Mybatis01ApplicationTests {
@Autowired
private StudentService studentService;
@Test
void contextLoads() {
}
@Test
void select() {
List<Student> students = studentService.select();
for (Student student : students) {
System.out.println(student);
}
}
@Test
void add() {
Student student = Student.builder()
.name("小李")
.sex("男")
.age(18)
.phone("15234653748").build();
int add = studentService.add(student);
System.out.println(add);
}
@Test
void update() {
Student student = Student.builder()
.id(3)
.name("小李")
.sex("男")
.age(14)
.phone("15234653748").build();
int update = studentService.update(student);
System.out.println(update);
}
@Test
void delete() {
int delete = studentService.delete(4);
System.out.println(delete);
}
}
问题:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.bo.wei.chen.mybatis.mapper.StudentMapper.select
答案:在pom.xml文件build下增加
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</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.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bo.wei.chen</groupId>
<artifactId>mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<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.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
进行单元测试
使用浏览器进行测试
链接: 查询
http://localhost:8080/student/select
链接: 增加
http://localhost:8080/student/add?name=小刚&sex=男&age=10&phone=18767523348
链接: 修改
http://localhost:8080/student/update?name=小刚&sex=男&age=10&phone=18767523999&id=3
链接: 删除
http://localhost:8080/student/delete?id=3
使用postman进行单元测试
地址同上
使用SQLyog查看测试结果
常见问题
8080端口被占用
1、win + R ,输入cmd,进入dos界面
2、输入netstat -ano|findstr 8080
查看占用8080端口的进程
3、输入taskkill /pid 84980 /f
关闭占用8080端口的进程