);
插入数据
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, ‘Jone’, 18, ‘test1@baomidou.com’),
(2, ‘Jack’, 20, ‘test2@baomidou.com’),
(3, ‘Tom’, 28, ‘test3@baomidou.com’),
(4, ‘Sandy’, 21, ‘test4@baomidou.com’),
(5, ‘Billie’, 24, ‘test5@baomidou.com’);
com.baomidou
mybatis-plus-boot-starter
3.3.1
mysql
mysql-connector-java
org.projectlombok
lombok
true
在 application.properties 配置文件中添加 MySQL 数据库的相关配置
- spring boot 2.0(内置jdbc5驱动)
#mysql数据库连接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
- spring boot 2.1及以上(内置jdbc8驱动)
注意:driver和url的变化
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
3.5.1、实体类:
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
3.5.2、添加mapper
创建包 mapper 编写UserMapper 接口: UserMapper.java
@Repository
public interface UserMapper extends BaseMapper {
}
3.5.3、启动类添加注解@MapperScan
作用:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
添加位置:是在Springboot启动类上面添加.
@SpringBootApplication
@MapperScan(“com.study.demomptest.mapper”)
public class DemomptestApplication {
public static void main(String[] args) {
SpringApplication.run(DemomptestApplication.class, args);
}
}
3.6.1、查询user表中所有数据
@SpringBootTest
class DemomptestApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void findAll() {
List users = userMapper.selectList(null);
System.out.println(users);
}
}
3.6.2、新增
@SpringBootTest
class DemomptestApplicationTests {
@Autowired
private UserMapper userMapper;
//新增
@Test
void insert(){
User user = new User();
user.setName(“Lucy”);
user.setAge(20);
user.setEmail(“12345@qq.com”);
int insert = userMapper.insert(user);
System.out.println(insert);
}
}
3.6.3、修改数据
4、要想在控制台看到sql输出日志,需要在application.properties配置文件中添加以下代码
=====================&#