1.导入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
2.连接数据库
spring.datasource.url=jdbc:mysql://(ip地址)/cart?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
spring.datasource.username=账号
spring.datasource.password=密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
3.写实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String email;
private int age;
private Date updateTime;
private Date createTime;
}
4.写mapper层 继承BaseMapper接口,传入实体类
@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {
}
5.在启动类中配置扫描包
@SpringBootApplication
@MapperScan("com.huang.mapper")
public class CartApplication {
public static void main(String[] args) {
SpringApplication.run(CartApplication.class, args);
}
}
6.用@Autowired注解定义mapper层的接口,就可以调用
@Autowired
private UserMapper userMapper;
//查
List<User> userList = userMapper.selectList(null);
userList.forEach(System.out::println);
@Test
void insert(){
User user = new User();
user.setAge(102);
user.setEmail("303303");
user.setName("huang0");
userMapper.insert(user);
}
@Test
void update(){
User user = new User();
user.setId(6);
user.setName("hh000");
System.out.println(JSON.toJSON(user));
userMapper.updateById(user);
}