springboot整合mybatisplus 2.3.3版本
时间: 2023-11-21 20:54:59 浏览: 101
SpringBoot 2.3.3版本中,可能会出现自动配置DataSource的问题,需要手动配置。同时,整合MybatisPlus需要引入相关依赖和配置文件。
1. 在pom.xml文件中引入MybatisPlus的依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
```
2. 在application.yml配置文件中添加数据库连接信息:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
```
3. 在启动类上添加注解@EnableTransactionManagement开启事务管理,并排除自动配置DataSource:
```java
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableTransactionManagement
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
4. 创建实体类和Mapper接口,并使用MybatisPlus提供的注解进行配置:
```java
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
```
5. 在Service层中注入Mapper,并使用MybatisPlus提供的方法进行数据库操作:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> list() {
return userMapper.selectList(null);
}
@Override
public User getById(Long id) {
return userMapper.selectById(id);
}
@Override
public void save(User user) {
userMapper.insert(user);
}
@Override
public void update(User user) {
userMapper.updateById(user);
}
@Override
public void delete(Long id) {
userMapper.deleteById(id);
}
}
```
阅读全文