SpringBoot-Mybatis
10. SpringBoot-Mybatis
10.1 导入 MyBatis 所需要的依赖
<!-- mybatis-spring-boot-starter : 整合 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
10.2 配置数据库信息
spring.datasource.username=root
spring.datasource.password=aadzj
spring.datasource.url=jdbc:mysql://localhost:3306/userdb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# mybatis整合 全限定类别名,关联配置文件
mybatis.type-aliases-package=com.dzj.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
10.3 测试数据库连接
10.4 创建实体类
导入lombok 依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
创建实体类Student
文件路径:java--com--dzj--pojo--Student.java
package com.dzj.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private String sno;
private String sname;
private String sclass;
private String cname;
}
10.5 创建 Mapper 接口
文件路径:java--com--dzj--mapper--StudentMapper.java
package com.dzj.mapper;
import com.dzj.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
// 这个注解表示了这是一个Mybatis的mapper类
@Mapper
@Repository
public interface StudentMapper {
List<Student> queryStudentList();
Student queryByStudentId(int sno);
int addStudent(Student student);
int updateStudent(Student student);
int deleteStudent(int sno);
}
10.6 对应的Mapper映射文件
文件路径:resource--mybatis--mapper--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.dzj.mapper.StudentMapper">
<select id="queryStudentList" resultType="Student">
select * from student
</select>
<select id="queryByStudentId" resultType="Student">
select * from student where sno=#{sno}
</select>
<insert id="addStudent" parameterType="Student">
insert into student(sno,sname,class,cname) values(#{sno},#{sname},#{sclass},#{cname})
</insert>
<update id="updateStudent" parameterType="Student">
update student set sname=#{sname},class=#{sclass},cname=#{cname} where sno = #{sno}
</update>
<delete id="deleteStudent" parameterType="int">
delete from student where sno = #{sno}
</delete>
</mapper>
10.7 maven配置资源过滤问题
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
10.8 编写 StudentController进行测试
package com.dzj.controller;
import com.dzj.mapper.StudentMapper;
import com.dzj.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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
public class StudentController {
@Autowired
private StudentMapper studentMapper;
@RequestMapping("/listStudent")
public List<Student> queryStudentList(){
List<Student> students = studentMapper.queryStudentList();
for (Student student : students) {
System.out.println(student);
}
return students;
}
@GetMapping("/queryStudent/{sno}")
public Student queryStudent(@PathVariable("sno")int sno){
Student student = studentMapper.queryByStudentId(sno);
return student;
}
@GetMapping("/addStudent")
public String addStudent(){
int i = studentMapper.addStudent(new Student("105", "dengzi", "5班", "English"));
return "添加成功";
}
@GetMapping("/deleteStudent/{id}")
public String deleteStudent(@PathVariable("id")int id){
studentMapper.deleteStudent(id);
return "删除成功";
}
@GetMapping("/updateStudent")
public String updateStudent(){
studentMapper.updateStudent(new Student("101","wenliu","2班","语文"));
return "修改成功";
}
}
本文来自博客园,作者:小公羊,转载请注明原文链接:https://www.cnblogs.com/aadzj/p/15636759.html