Mybatis-Plus idea环境搭建

本文详细介绍了如何在IntelliJ IDEA环境下搭建Mybatis-Plus项目。从pom.xml配置到log4j.xml,再到db.properties、mybatis-config.xml、applicationContext.xml的设置,接着创建User类和UserMapper接口,最后编写测试类并展示数据库格式及运行截图,全面阐述了整个过程。
摘要由CSDN通过智能技术生成

使用IDEA搭建 mybatis-plus

1.pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>


4.0.0

org.example
mp
1.0-SNAPSHOT
war

mp Maven Webapp

http://www.example.com

junit junit 4.11 test
<!-- mysql驱动包 -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.38</version>
</dependency>
<!-- 日志文件 -->
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
</dependency>
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus</artifactId>
  <version>3.4.2</version>
</dependency>
<!-- c3p0 -->
<dependency>
  <groupId>com.mchange</groupId>
  <artifactId>c3p0</artif
1. 创建SpringBoot项目 首先,在IDE中创建一个SpringBoot项目,选择Maven作为构建工具,并在pom文件中添加SpringBoot和mybatis-plus的依赖。 2. 配置数据库连接 在application.properties文件中添加数据库连接信息,包括url、username、password等。 ``` spring.datasource.url=jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 3. 创建实体类和mapper 创建实体类和对应的mapper接口,使用mybatis-plus的注解来简化SQL操作。例如: ```java @Data @TableName("user") // 表名 public class User { @TableId(value = "id", type = IdType.AUTO) // id字段自增 private Long id; @TableField("name") // name字段 private String name; @TableField("age") // age字段 private Integer age; } ``` ```java public interface UserMapper extends BaseMapper<User> { } ``` 4. 编写业务逻辑 根据具体需求编写业务逻辑代码,例如实现对用户的增删改查操作。使用mybatis-plus的CRUD方法来简化操作。 ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public boolean saveUser(User user) { return userMapper.insert(user) > 0; } @Override public boolean updateUser(User user) { return userMapper.updateById(user) > 0; } @Override public boolean deleteUser(Long id) { return userMapper.deleteById(id) > 0; } @Override public User getUserById(Long id) { return userMapper.selectById(id); } @Override public List<User> getUserList() { return userMapper.selectList(null); } } ``` 5. 编写Controller层 编写Controller层代码,处理HTTP请求并调用对应的业务逻辑方法。使用@RestController注解来标注该类是一个REST风格的控制器。 ```java @RestController public class UserController { @Autowired private UserService userService; @PostMapping("/user") public Result saveUser(@RequestBody User user) { boolean result = userService.saveUser(user); return result ? Result.success() : Result.error("保存用户失败"); } @PutMapping("/user") public Result updateUser(@RequestBody User user) { boolean result = userService.updateUser(user); return result ? Result.success() : Result.error("更新用户失败"); } @DeleteMapping("/user/{id}") public Result deleteUser(@PathVariable Long id) { boolean result = userService.deleteUser(id); return result ? Result.success() : Result.error("删除用户失败"); } @GetMapping("/user/{id}") public Result getUserById(@PathVariable Long id) { User user = userService.getUserById(id); return Result.success(user); } @GetMapping("/user") public Result getUserList() { List<User> userList = userService.getUserList(); return Result.success(userList); } } ``` 6. 测试接口 启动SpringBoot应用程序,使用Postman等工具测试接口,确认接口功能正常。 以上便是搭建SpringBoot mybatis-plus项目的基本流程。通过使用mybatis-plus来简化SQL操作,可以极大的提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值