springboot整合mybatis一个简单实例
今天学习到了springboot整合mybatis,因为怕过几天就忘了,所以写了这篇个人博客,因为本人还在求学阶段,还是小白一枚,如有不足之处,请大神们点播。
1.在前面IDEA新建springboot项目的基础上导入myBatis,jdbc,mysql相关依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</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>
2.连接数据库,配置application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=******
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
3.根据数据库表先建一个实体类,我这里使用了lombox这个依赖,很方便,不需要写get,set,tostring等方法,具体使用自行百度
package com.wangningbo.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer id;
private String name;
private int age;
}
4.新建一个关于增删改查的StudentDao的接口
package com.wangningbo.dao;
import com.wangningbo.bean.Student;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface StudentDao {
List<Student> queryAllStudent();
Student queryStudentById(int id);
int addStudent(Student student);
int removeStudentById(int id);
int modifyStudent(Student student);
}
5.整合myBatis,在resources下面新建mybatis,再在mybatis下新建mapper包,在下面新建StudentDao.xml,相当于对接口的实现,注意namespace一定要配好
<?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.wangningbo.dao.StudentDao">
<select id="queryStudentById" resultType="Student">
select * from student where id = #{id}
</select>
<select id="queryAllStudent" resultType="Student">
select * from student
</select>
<insert id="addStudent" parameterType="Student">
insert into student (name,age) values (#{name},#{age})
</insert>
<update id="modifyStudent" parameterType="Student">
update student set name=#{name},age=#{age} where id=#{id}
</update>
<delete id="removeStudentById" parameterType="int">
delete from student where id=#{id}
</delete>
</mapper>
6.接着新建一个StudentController
package com.wangningbo.service;
import com.wangningbo.bean.Student;
import com.wangningbo.dao.StudentDao;
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 StudentDao studentService;
@GetMapping("/queryallstudent")
public List<Student> queryAllStudent(){
List<Student> studentList = studentService.queryAllStudent();
for (Student student : studentList) {
System.out.println(student);
}
return studentList;
}
}
7.最后找到application.java主类开启服务,浏览器打开访问http://127.0.0.1:8080/queryallstudent