mybatisplus注册功能
时间: 2023-09-03 10:10:43 浏览: 49
MyBatis-Plus 是 MyBatis 的增强工具,在 MyBatis 的基础上,提供了很多实用的功能,其中包括了自动生成代码的功能。下面是 MyBatis-Plus 注册功能的步骤:
1. 在 pom.xml 中添加 MyBatis-Plus 的依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.x.x</version>
</dependency>
```
2. 在 application.yml 中配置数据源信息和 MyBatis-Plus 的相关配置:
```yaml
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: root
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
global-config:
db-config:
id-type: auto
```
3. 创建实体类和 Mapper 接口:
```java
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
public interface UserMapper extends BaseMapper<User> {
}
```
4. 在启动类上添加 @MapperScan 注解,扫描 Mapper 接口:
```java
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
5. 在 Service 层中使用 UserMapper 接口,进行 CRUD 操作:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public boolean save(User user) {
int result = userMapper.insert(user);
return result > 0;
}
@Override
public boolean updateById(User user) {
int result = userMapper.updateById(user);
return result > 0;
}
@Override
public boolean removeById(Long id) {
int result = userMapper.deleteById(id);
return result > 0;
}
@Override
public User getById(Long id) {
return userMapper.selectById(id);
}
@Override
public List<User> list() {
return userMapper.selectList(null);
}
}
```
以上就是 MyBatis-Plus 注册功能的实现步骤。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)