老师实体类
package com.olive.entity;import java.io.Serializable;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableId;import com.baomidou.mybatisplus.annotation.TableName;import lombok.Data;@Data@TableName("t_teacher")public class TeacherDO implements Serializable { @TableId(value = "https://www.huyubaike.com/biancheng/id", type = IdType.AUTO) private Long id; @TableField("user_name") private String name; @TableField("sex") private int sex; @TableField("office") private String office;}
数据库持久类StudentMapper持久类
package com.olive.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.olive.entity.StudentDO;import org.apache.ibatis.annotations.Mapper;@Mapperpublic interface StudentMapper extends BaseMapper<StudentDO> {}
TeacherMapper持久类
package com.olive.mapper;import com.baomidou.dynamic.datasource.annotation.DS;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.olive.entity.TeacherDO;import org.apache.ibatis.annotations.Mapper;@DS("slave_1")@Mapperpublic interface TeacherMapper extends BaseMapper<TeacherDO> {}
以上 , 两个持久类的实现都是继承BaseMapper
基类 , 该类提供了基本的增删改查的方法;要注意的是 , TeacherMapper
类使用了@DS("slave_1")
指定了数据源;而StudentMapper
没有指定 , 使用的是默认的数据源 。
服务类的实现先定义操作学生和老师的两个接口
StudentService接口
package com.olive.service;import com.baomidou.mybatisplus.extension.service.IService;import com.olive.entity.StudentDO;public interface StudentService extends IService<StudentDO> {}
TeacherService接口
package com.olive.service;import com.baomidou.mybatisplus.extension.service.IService;import com.olive.entity.TeacherDO;public interface TeacherService extends IService<TeacherDO> {}
再定义接口的实现类
StudentServiceImpl类
package com.olive.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import com.olive.entity.StudentDO;import com.olive.mapper.StudentMapper;import com.olive.service.StudentService;import org.springframework.stereotype.Service;@Servicepublic class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentDO>implements StudentService {}
TeacherServiceImpl类
package com.olive.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import com.olive.entity.TeacherDO;import com.olive.mapper.TeacherMapper;import com.olive.service.TeacherService;import org.springframework.stereotype.Service;@Servicepublic class TeacherServiceImpl extends ServiceImpl<TeacherMapper, TeacherDO>implements TeacherService {}
观察接口定义和接口实现 , 套路非常标准 。都是实现和继承Mybatis-Plus
提供的标准接口和基类
创建springboot引导类package com.olive;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@MapperScan("com.olive.mapper")@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class);}}
测试package com.olive;import com.olive.service.StudentService;import com.olive.service.TeacherService;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import com.olive.entity.StudentDO;import com.olive.entity.TeacherDO;@SpringBootTestpublic class MybatisPlusTest { @Autowired StudentService studentService; @Autowired TeacherService teacherService; @Test public void userSave() {StudentDO studentDO = new StudentDO();studentDO.setName("BUG弄潮儿");studentDO.setSex(1);studentDO.setGrade("一年级");studentService.save(studentDO);TeacherDO teacherDO = new TeacherDO();teacherDO.setName("Java乐园");teacherDO.setSex(2);teacherDO.setOffice("语文");teacherService.save(teacherDO); }}
经验总结扩展阅读
- 结合springboot实现,这里对接的是easy版本,工具用的是IDEA,WebStrom 支付宝沙箱服务
- 分布式存储系统之Ceph集群状态获取及ceph配置文件说明
- 原神3.2意识之舟所至之处任务怎么做
- 积极向上的8字格言
- vivox70参数配置_vivox70参数配置详情
- 云顶之奕龙王带什么装备
- 油表4分之一在哪个位置
- 有活化石之称的野生动物是什么 大熊猫喜欢吃什么
- 暗恋一个人很心酸的签名 很伤感的暗恋签名
- 原神3.2请饮下祝胜之酒任务怎么做