前言
在学习Springboot过程中,整合mybatis框架实现表数据的增删改查,话不多说,开始贴代码!
Spring Boot提供了一个名为spring-boot-starter-parent的工程,里面已经对各种常用依赖(并非全部)的版本进行了管理,我们的项目需要以这个项目为父工程,这样我们就不用操心依赖的版本问题了,需要什么依赖,直接引入坐标即可!
org.springframework.boot spring-boot-starter-parent 2.1.5.RELEASE
springboot完整pom.xml文件
4.0.0 org.example springboot_demo 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.1.5.RELEASE 1.8 org.springframework.boot spring-boot-starter-web
springboot整合mybatis
mybatis依赖:
tk.mybatis mapper-spring-boot-starter 2.1.5
druid依赖:com.alibaba druid-spring-boot-starter 1.2.8
mysql驱动依赖mysql mysql-connector-java 8.0.11
配置 application.yml
server:port: 9527
spring:datasource:druid:driver-class-name: com.mysql.cj.jdbc.Driver # mysql是8.x版本的需要加cj,如果是低版本5.x的不需要加这个cjurl: jdbc:mysql://localhost:3306/cyw-study?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8username: rootpassword: Wangchunyu123
mybatis:#classpath指的是resource文件夹mapper-locations: classpath:/mapper/*.xmlconfiguration:#开启驼峰命名规则 比如数据库字段是有下划线的t_no,那么代码中就要写成Tno代码里面自动把下划线的字母变成大写map-underscore-to-camel-case: truetype-aliases-package: com.o2o.data
logging:level:com:o2o:cy: debug
配置Mapper层包路径扫描注解@MapperScan
@MapperScan("com.o2o.mapper")
public class AutoTestApplication {public static void main(String[] args) {SpringApplication.run(AutoTestApplication.class, args);}
}
Springboot实战
一,首先我们先创建一个实体类Teacher,实体类其实就是对应一个数据表,其中的属性对应数据表中的字段。好处: 1.对对象实体的封装。 2.属性可以对字段定义和状态进行判断和过滤
package com.o2o.data;import lombok.Data;@Data
public class Teacher {private String Tno;private String Tname;public Teacher(String t_no, String t_name) {this.Tno = Tno;this.Tname = Tname;}public Teacher(){}public String getT_no() {return Tno;}public void setT_no(String t_no) {this.Tno = t_no;}public String getT_name() {return Tname;}public void setT_name(String t_name) {this.Tname = Tname;}@Overridepublic String toString() {return "Teacher{" +"t_no='" + Tno + '\'' +", t_name='" + Tname + '\'' +'}';}
}
二,接着在mapper层创建一个interface接口,需要加@Mapper注解,提供findAll()方法
package com.o2o.mapper;import com.o2o.data.Teacher;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface TeacherMapper {//查询全部List findAll();
}
三,再创建一个service层interface接口和impl层实现类,需要加@Server注解
package com.o2o.service;import com.o2o.data.Teacher;import java.util.List;public interface TeacherService {List findAll();
}
package com.o2o.service.impl;import com.o2o.data.Teacher;
import com.o2o.mapper.TeacherMapper;
import com.o2o.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class TeacherServiceImpl implements TeacherService {@Autowiredprivate TeacherMapper teacherMapper;@Overridepublic List findAll() {List teachers = teacherMapper.findAll();return teachers;}
}
四,创建一个mapper.xml文件,配置各种SQL语句(例如SELECT,INSERT,UPDATE和DELETE)的语句
这里重点说下的含义
五,创建controller层控制器,用来接收前端传入的参数
public返回的是一个list列表,因为我们查询全部getTeacher()没有参数,映射一个GetMapping请求方法,传入路径名/test
package com.o2o.controller;import com.o2o.data.Teacher;
import com.o2o.service.TeacherService;
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 TeacherController {@Autowiredprivate TeacherService teacherService;@GetMapping("/test")public List getTeacher(){List allTeachers = teacherService.findAll();return allTeachers;}
}
六,启动SpingApplication类进行测试

浏览器输入{ip}+端口号进行访问,我们看到已经成功查询到数据库teacher表里的数据啦

表数据
