springboot整合mybatis的整体架构
因为要整合mybatis,因此要导入响应的依赖:mybatis-spring-boot-starter
mybatis-spring-boot-starter类似一个中间件,链接springBoot和Mybatis
注意:这里的版本不是随意的,
1、导入依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
2、建user表:
在mysql中创建一个名为“mybatis”数据库,在库中创建一个user表,并插入一些数据(或者使用自己的表)
user表如下:具体怎么操作,学到这里,你应该已经懂了
3、在pojo中创建一个User类
用来和数据库建立联系,分别对应各个字段:id,name,pwd,detail
@Data //如果一开始在pom中没有导入lombok依赖,这里会爆红,或者直接Alt+回车就可以自动导入了
@AllArgsConstructor
@NoArgsConstructor
public class User {
//与数据库字段一一对应
private int id;
private String name;
private String pwd;
private String detail;
}
4、在mapper下创建一个UserMapper接口
实现增删改查
@Mapper注解:在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类
@Repository用在持久层的接口上,这个注解是将接口的一个实现类交给spring管理
@Mapper
@Repository
public interface UserMapper {
List<User> queryUserList(