第6节:SpringBoot整合Mybatis

# 1.SpringBoot整合MyBatis

①依赖的导入

<!-- MySQL驱动依赖 -->
<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.40</version>
</dependency>
<!-- MyBatis整合包依赖 -->
<dependency>
 <groupId>org.mybatis.spring.boot</groupId>
 <artifactId>mybatis-spring-boot-starter</artifactId>
 <version>1.3.2</version>
</dependency>
<!-- Druid数据源整合包依赖 -->
<dependency>
 <groupId>com.alibaba</groupId>
 <artifactId>druid-spring-boot-starter</artifactId>
 <version>1.1.10</version>
</dependency>

②Java文件结构的准备

实体类:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Job implements Serializable {
 private static final long serialVersionUID = 2388129295668997933L;
 private Integer jobId;
 private String jobTitle;
 private Double minSalary;
 private Double maxSalary;
}

Mapper接口:

@Repository
public interface JobMapper {
 List<Job> queryAllJob();
}

Mapper映射文件:

<?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.qf.springbootmybatis.mapper.JobMapper">
 <select id="queryAllJob" resultType="job">
 select * from Job
 </select>
</mapper>

③SpringBoot整合MyBatis

1.修改启动类,自动扫描所有Mapper接口:

@SpringBootApplication
@MapperScan(basePackages = "com.qf.testspringboot.mapper") 
public class TestSpringBootApplication {
 public static void main(String[] args) {
 SpringApplication.run(TestSpringBootApplication.class, args);
 }
}

2.配置文件整合Druid数据源:

spring:
 datasource:
 driver-class-name: com.mysql.jdbc.Driver
 url: jdbc:mysql://localhost:3306/company
 username: root
 password: root
 type: com.alibaba.druid.pool.DruidDataSource

3.配置文件整合Mybatis:

mybatis:
 mapper-locations: classpath:mapper/*.xml
 type-aliases-package: com.qf.testspringboot.pojo
 configuration:
 map-underscore-to-camel-case: true
 lazy-loading-enabled: true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
下面是一个简单的示例代码: 1. 添加依赖 在 `pom.xml` 文件中添加以下依赖: ```xml <dependencies> <!-- Spring Boot Mybatis Plus Starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> </dependency> </dependencies> ``` 2. 配置数据源 在 `application.yml` 文件中配置数据源,这里以 MySQL 数据库为例: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver ``` 3. 配置 Mybatis Plus 创建一个配置类 `MybatisPlusConfig.java`,添加以下配置: ```java @Configuration public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } } ``` 这里配置了分页插件 `PaginationInterceptor`。 4. 创建实体类和 Mapper 创建一个实体类 `User.java`,并创建对应的 Mapper 接口 `UserMapper.java`,这里不再赘述。 5. 编写查询全部并分页的代码 在 Service 层中添加以下代码: ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Override public IPage<User> selectAllByPage(int pageNo, int pageSize) { Page<User> page = new Page<>(pageNo, pageSize); QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.orderByDesc("id"); return baseMapper.selectPage(page, wrapper); } } ``` 这里使用了 Mybatis Plus 提供的 `Page` 类,通过传入页码和每页显示数量来创建分页对象。然后使用 `QueryWrapper` 来添加排序规则,最后调用 `baseMapper.selectPage()` 方法来查询并返回分页结果。 6. 测试 在 Controller 中添加以下代码: ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users") public IPage<User> selectAllByPage(@RequestParam("pageNo") int pageNo, @RequestParam("pageSize") int pageSize) { return userService.selectAllByPage(pageNo, pageSize); } } ``` 启动应用,访问 `http://localhost:8080/users?pageNo=1&pageSize=10` 即可查询第一页的数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fwy洛伦兹力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值