Springboot3 整合 mybatis-plus 内容

Springboot3 整合 mybatis-plus 内容

一、导入依赖

<!--        数据库链接依赖-->
		<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>
			<scope>runtime</scope>
		</dependency>
		<!--       mybatis-plus依赖 -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.5.3</version>
		</dependency>

二、编写配置文件

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/user?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
    username: root
    password: 200718
mybatis-plus:
  mapper-location: classpath:/mapper/*.xml
  typeAliasesPackage: com.yun.entity

三、定义模型 entity 实体类

注意: 这里实现的 Serializable 序列化接口是可有可无的,不是plus必须的!

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("sys_user")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    private String userName;
    private String nickName;
    private String password;
    private String status;
    private String email;
    private String phonenumber;
    private String sex;
    private String avatar;
    private String userType;
    private Long createBy;
    private LocalDateTime createTime;
    private Long updateBy;
    private LocalDateTime updateTime;
    private Integer delFlag;
}

四、在启动类上添加注解,表示mapper接口所在位置

注意: 如果接口上面有 注解 @Mapper 的话,就可以不用在使用扫描包注解 @MapperScan 了(当然两个可以同时存在)

@SpringBootApplication
@MapperScan("com.yun.mapper") // 扫描的mapper
public class Springboot3MybatisPlusApplication {

	public static void main(String[] args) {
		SpringApplication.run(Springboot3MybatisPlusApplication.class, args);
	}
}

五、定义mapper接口

注意: 最好要加上 @Mapper注解,防止忘记开启扫描,这里要继承 BaseMapper 才能使用 mybatis-plus的方法(在这里已经完成了基础方法的设定,要更难的实现需要自己写 xml)

@Mapper
public interface UserMapper extends BaseMapper<User> {
    List<User> getAllUser();
}

六、定义mapper.xml映射文件

注意:头文件这里的网站链接是没有 www 的,且能识别到 文件时,里面的 SQL 是有颜色的,否则就是白色

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yun.mapper.UserMapper">
    <select id="getAllUser" resultType="com.yun.entity.User">
        select * from sys_user
    </select>
</mapper>

七、service层

注意: 接口和实现类最好把 @Service 加上,否则会出现找不到 bean 的问题
1、接口:

@Service
public interface IUserService extends IService<User> {

    List<User> getAllUser();
}

2、实现类:

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }
}

八、测试

这里测试是调用Service层的,也可以调用Mapper层来实现 查询

@SpringBootTest
class Springboot3MybatisPlusApplicationTests {
	@Autowired
	private UserMapper userMapper;
	@Qualifier("userServiceImpl")// 指定注入的bean
	@Autowired
	private IUserService userService;
	@Test
	void contextLoads() {
		System.out.println(userMapper.getAllUser());
	}
	@Test
	void test(){
		System.out.println(userService.getAllUser());
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你好!要在Spring Boot整合MyBatis-Plus,你可以按照以下步骤进行操作: 步骤1:添加依赖 在你的Spring Boot项目的pom.xml文件中,添加MyBatis-Plus的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本号</version> </dependency> ``` 请确保将`最新版本号`替换为MyBatis-Plus最新的版本号。 步骤2:配置数据源 在application.properties(或application.yml)文件中,配置数据库连接信息,例如: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/db_example spring.datasource.username=db_username spring.datasource.password=db_password spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 请将`db_example`、`db_username`和`db_password`分别替换为你的数据库名称、用户名和密码。 步骤3:创建实体类和Mapper接口 创建对应的实体类,并使用`@TableName`注解指定数据库表名。然后创建Mapper接口,继承自`BaseMapper`。 ```java import com.baomidou.mybatisplus.annotation.TableName; @TableName("user") public class User { private Long id; private String username; private String email; // getters and setters } ``` ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface UserMapper extends BaseMapper<User> { } ``` 步骤4:编写Service和Controller层代码 在Service层中,可以通过注入Mapper对象来使用MyBatis-Plus提供的便捷方法。例如: ```java import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class UserServiceImpl implements UserService { @Resource private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.selectById(id); } // 其他业务逻辑方法 } ``` 在Controller层中,可以直接调用Service层的方法来处理请求。例如: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController public class UserController { @Resource private UserService userService; @GetMapping("/users/{id}") public User getUserById(@PathVariable Long id) { return userService.getUserById(id); } // 其他请求处理方法 } ``` 这样,你就完成了Spring BootMyBatis-Plus整合。你可以根据自己的需求,使用MyBatis-Plus提供的便捷方法来进行数据库操作。 希望对你有所帮助!如果还有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值