image.png

数据库

image.png
image.png

驱动(yml文件)

  1. spring:
  2. datasource:
  3. driver-class-name: com.mysql.cj.jdbc.Driver
  4. url: jdbc:mysql://127.0.0.1:3306/db2019?useUnicode=true&characterEncoding=utf-8
  5. username: root
  6. password: fawde0615
  7. mybatis-plus:
  8. configuration:
  9. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

实例

  1. package com.fawde.mybatisplus.entity;
  2. import com.baomidou.mybatisplus.annotation.IdType;
  3. import com.baomidou.mybatisplus.annotation.TableId;
  4. import com.baomidou.mybatisplus.annotation.TableName;
  5. import lombok.AllArgsConstructor;
  6. import lombok.Data;
  7. import lombok.NoArgsConstructor;
  8. @Data//注解在类上, 为类提供读写属性, 此外还提供了 equals()、hashCode()、toString() 方法
  9. @AllArgsConstructor
  10. @NoArgsConstructor
  11. //实体类
  12. @TableName("user")
  13. public class User {
  14. //指定主键
  15. @TableId(value = "id",type = IdType.AUTO)
  16. private int id;
  17. private String name;
  18. private String email;
  19. private int age;
  20. public User(String name,String email,int age){
  21. this.name = name;
  22. this.email=email;
  23. this.age =age;
  24. }
  25. }
  1. //Dept类似,但是继承了Model
  2. @Data//注解在类上, 为类提供读写属性, 此外还提供了 equals()、hashCode()、toString() 方法
  3. @AllArgsConstructor
  4. @NoArgsConstructor
  5. @TableName("dept")
  6. public class Dept extends Model<Dept> {
  7. //指定主键
  8. @TableId(value = "id",type = IdType.AUTO)
  9. private Integer id;
  10. private String name;
  11. private String mobile;
  12. private Integer manager;
  13. public Dept(String name,String mobile,Integer manager){
  14. this.name= name;
  15. this.mobile=mobile;
  16. this.manager=manager;
  17. }
  18. }

接口

继承BaseMapper

  1. package com.fawde.mybatisplus.mapper;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.fawde.mybatisplus.entity.User;
  4. public interface UserMapper extends BaseMapper<User> {
  5. }
  6. public interface DeptMapper extends BaseMapper<Dept> {
  7. }

使用

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. class MybatisplusApplicationTests {
  4. @Autowired
  5. private UserMapper userDao ;
  6. @Test
  7. void contextLoads() {
  8. }
  9. @Test
  10. public void testInsert(){
  11. User user =new User("zhangsan22","zhangsan@126.com",20);
  12. int rows = userDao.insert(user);
  13. System.out.println("insert user result:"+rows);
  14. }
  15. @Test
  16. public void testARInsert(){
  17. Dept dept = new Dept("事业部","010-12346",2);
  18. boolean result = dept.insert();
  19. System.out.println("AR Insert:"+result);
  20. }
  21. }