SpringBoot整合MyBatis实现增删改查(简单,详细)
SpringBoot整合MyBatis连接数据库实现增删改查
·
新建springboot工程
若选择https://start.spring.io下一步失败
则选择Custom,输入:https://start.aliyun.com后下一步
添加需要的依赖
添加其他依赖,全部依赖如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--可省略set,get方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
修改配置文件:
application.properties:改为application.yml
application.yml:
#端口号8080
server:
port: 8080
#数据库名:mysql,用户名root,密码123456
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapping/*.xml
type-aliases-package: com.example.demo.entity
#showSql
logging:
level:
com.example.demo.mapper: debug
/src/main/java/com.example.demo下设置:service,controller,mapper,entity包
/src/main/resouces下设置mapping文件夹
controller创建StudentController类;
service下创建StudentService接口,并创建impl包,创建StudentServiceImpl类;
mapper下创建StudentMapper接口;
entity下创建StudentInfo类;
mapping文件夹下创建StudentMapper.xml文件;
结构如下图所示:
新建数据库测试表:student
CREATE TABLE student(
id VARCHAR(2) COMMENT '学生ID',
sname VARCHAR(20) COMMENT '学生姓名',
classId VARCHAR(3) COMMENT '班级ID',
birthday VARCHAR(5) COMMENT '学生生日',
email VARCHAR(20) COMMENT '学生电子邮箱'
);
INSERT INTO student(id,sname,class_id,birthday,email)
VALUES(1,'张三',101,1016,'1@163.com'),(2,'李四',101,511,'2@163.com'),
(3,'王五',101,1016,'3@163.com'),(4,'赵六',103,615,'4@163.com');
StudentInfo类:
import lombok.Data;
//与数据库表结构相同
@Data
public class StudentInfo {
private String id;
private String sname;
private String classId;
private String birthday;
private String email;
}
StudentMapper
import com.example.demo.entity.StudentInfo;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface StudentMapper {
/**
* 根据学生ID查询学生信息
* @param id
* @return
*/
StudentInfo getInfo(int id);
/**
* 插入新学生信息
* @param studentInfo
*/
void insertInfo(StudentInfo studentInfo);
/**
* 根据ID删除学生信息
* @param id
*/
int deleteById(int id);
/**
* 根据id修改学生信息
* @param studentInfo
* @return
*/
int updateById(StudentInfo studentInfo);
/**
* 查询全部学生信息
* @return
*/
List<StudentInfo> selectAll();
}
StudentService
import com.example.demo.entity.StudentInfo;
import java.util.List;
public interface StudentService {
StudentInfo getStudentInfo(int id);
StudentInfo insertInfo(StudentInfo studentInfo);
int deleteById(int id);
int updateById(StudentInfo studentInfo);
List<StudentInfo> selectAll();
}
StudentServiceImpl
import com.example.demo.entity.StudentInfo;
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(required = false)
private StudentMapper studentMapper;
@Override
public StudentInfo getStudentInfo(int id){
return studentMapper.getInfo(id);
}
@Override
public StudentInfo insertInfo(StudentInfo studentInfo){
studentMapper.insertInfo(studentInfo);
return studentInfo;
}
@Override
public int deleteById(int id){
return studentMapper.deleteById(id);
}
@Override
public int updateById(StudentInfo studentInfo){
return studentMapper.updateById(studentInfo);
}
@Override
public List<StudentInfo> selectAll(){
return studentMapper.selectAll();
}
}
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.example.demo.mapper.StudentMapper">
<select id="getInfo" resultType="com.example.demo.entity.StudentInfo">
select *
from student
where id=#{id}
</select>
<insert id="insertInfo" parameterType="com.example.demo.entity.StudentInfo">
insert into student(id,sname,classId,birthday,email)
values (#{id},#{sname},#{classId},#{birthday},#{email});
</insert>
<delete id="deleteById">
delete
from student
where id=#{id}
</delete>
<update id="updateById" parameterType="com.example.demo.entity.StudentInfo">
update student
set sname = #{sname},classId = #{classId},
birthday = #{birthday}, email = #{email}
where id = #{id}
</update>
<select id="selectAll" resultType="com.example.demo.entity.StudentInfo">
select *
from student
</select>
</mapper>
StudentController:
import com.example.demo.entity.StudentInfo;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/demo10")
public class StudentController {
@Autowired(required = false)
private StudentService studentService;
/**
* 通过id查询学生信息
* @param id
* @return
*/
@RequestMapping("getInfo/{id}")
public StudentInfo getInfo( @PathVariable int id){
return studentService.getStudentInfo(id);
}
/**
* 插入新学生信息
* @param studentInfo
* @return
*/
@RequestMapping("/insert/*")
public StudentInfo insert(StudentInfo studentInfo){
return studentService.insertInfo(studentInfo);
}
/**
* 根据id删除学生信息
* @param id
* @return
*/
@RequestMapping("/delete")
public String deleteInfo(int id){
int result = studentService.deleteById(id);
if (result >= 1) {
return "删除成功";
} else {
return "删除失败";
}
}
/**
* 根据id修改学生信息
* @param studentInfo
* @return
*/
@RequestMapping("/update/*")
public String updateById(StudentInfo studentInfo){
int result = studentService.updateById(studentInfo);
if (result >= 1) {
return "修改成功";
} else {
return "修改失败";
}
}
/**
* 查看全部学生信息
* @return
*/
@RequestMapping("/selectAll")
public List<StudentInfo> ListStudent(){
return studentService.selectAll();
}
}
启动程序
使用postman测试
测试:查询id为1的学生信息
URL:
localhost:8080/demo10/getInfo/1
测试:插入一条学生信息
URL:
localhost:8080/demo10/insert/?id=5&sname=小明&classId=103&birthday=1231&email=5@163.com
测试:删除id为5的学生信息
URL:
localhost:8080/demo10/delete?id=5
测试:修改id为4的学生信息
URL:
localhost:8080/demo10/update/?id=4&sname=小明&classId=103&birthday=1231&email=4@163.com
测试:查询学生表中全部信息
URL:
localhost:8080/demo10/selectAll
测试完成
继续学习SpringBoot整合MybatisPlus?请移步SpringBoot整合MybatisPlus(详细)
对控制台输出效果不满意?可添加日志使内容更详细,请移步为SpringBoot项目添加日志:slf4j
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献3条内容
所有评论(0)