Mybaties-plus的初使用

一、创建springBoot项目
1- IDEA创建项目
在这里插入图片描述

2-报错:找不到项目 ‘org.springframework.boot:spring-boot-starter-parent:2.6.7’
这个问题主要是因为ide的缓存导致的,我们直接清理缓存并重启ide
在这里插入图片描述
插件报错:输入版本号(联网会有提示)2.3.7.RELEASE
在这里插入图片描述
3-无报错后,还是要检查maven的配置改成自己的,进入Application,能看到启动按钮,成功!
在这里插入图片描述
4-检查pom文件的依赖,后修改properties为yml文件
在这里插入图片描述
yml文件添写必要配置
在这里插入图片描述
新建实体类,lombok简化开发就是不写set和get(加上@Data注解)
在这里插入图片描述
因Mybaties-plus创建项目时不能直接选择,需要手动导入pom文件
在这里插入图片描述
接着在Mapper里新建UserMapper接口类引入Mybaties,这时接口接口就有CRUD功能了,下面进行测试
在这里插入图片描述
在Test里tao包下新建一个java文件做测试
在这里插入图片描述
运行报错:BeanCreationException
在这里插入图片描述
UserMapper不可用(没有实例化)解决:
①在UserMapper接口里加@Mapper
在这里插入图片描述
②在启动类里加一个扫描:(注意是主启动类,不是test里面的)
在这里插入图片描述
测试成功!
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot整合Mybatis-Plus操作数据库的流程一般如下: 1. 配置数据库连接 在application.properties或application.yml中配置数据库连接信息,包括数据库类型、IP地址、端口号、数据库名、用户名、密码等。 2. 添加Mybatis-Plus依赖 在pom.xml文件中添加Mybatis-Plus的依赖,例如: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> ``` 3. 配置Mybatis-Plus 在application.properties或application.yml中添加Mybatis-Plus的配置信息,例如: ``` mybatis-plus.mapper-locations=classpath:mapper/*.xml mybatis-plus.type-aliases-package=com.example.demo.entity ``` 其中,mapper-locations配置了Mybatis-Plus的mapper文件的位置,type-aliases-package配置了Mybatis-Plus的实体类的包名。 4. 编写实体类 编写Java实体类,用于映射数据库表中的数据。实体类可以使用注解@Table和@Column来指定对应的数据库表名和列名。 ``` @Table(name = "user") public class User { @TableId(type = IdType.AUTO) private Long id; @Column(name = "username") private String username; @Column(name = "password") private String password; // 省略getter和setter方法 } ``` 5. 编写Mapper接口 编写Mapper接口,继承Mybatis-Plus提供的BaseMapper接口,用于定义数据库操作方法。例如: ``` public interface UserMapper extends BaseMapper<User> { } ``` 6. 使用Mybatis-Plus进行数据库操作 在需要进行数据库操作的地方,例如Service类中,注入对应的Mapper接口,使用Mybatis-Plus提供的API进行数据库操作。例如: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.selectById(id); } // 其他方法省略 } ``` 在这个例子中,我们注入了UserMapper接口,并使用selectById方法根据主键查询用户数据。 总之,Spring Boot整合Mybatis-Plus操作数据库的流程包括配置数据库连接、添加Mybatis-Plus依赖、配置Mybatis-Plus、编写实体类、编写Mapper接口,使用Mybatis-Plus进行数据库操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值