mp实现过程

步骤:

  1,编写配置文件

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root

  2,编写entity类

@Data
public class User {
    @TableId(type = IdType.ID_WORKER)
    private Long id;
    private String name;
    private Integer age;
    private String email;
    private Date createTime;
    private Date updateTime;
}

  3,编写mapper类

@Repository
public interface UserMapper extends BaseMapper<User> {
  // 需要继承BaseMapper<T> }

  4,再启动类上添加注解:扫描mapper

@SpringBootApplication
@MapperScan("com.nbg.mpdemo1010.mapper")
public class Mpdemo1010Application {

    public static void main(String[] args) {
        SpringApplication.run(Mpdemo1010Application.class, args);
    }

}

  5,测试

@SpringBootTest
class Mpdemo1010ApplicationTests {
    @Autowired
    private UserMapper userMapper;

    @Test
    void contextLoads() {
        for (User user : userMapper.selectList(null)) {
            System.out.println(user);
        }
    }

    @Test
    void update(){
        com.nbg.mpdemo1010.entity.User user1 = new com.nbg.mpdemo1010.entity.User();
        User user = new User();
        user.setId(2L);
        user.setAge(111);
        int i = userMapper.updateById(user);
        System.out.println(i);
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

posted @ 2021-03-22 20:05  nbg  阅读(68)  评论(0编辑  收藏  举报