Camel MyBatis + MyBatis小例子

最近单位要用Camel。在学习过程中发现目前国内网上camel的相关资料比较少,能够运行起来的例子就更少,这个刚刚学习的同学们带来了很多不便,本着让同学们少走弯路的目的。发个Camel+MyBatis+Spring的小例子。在tomcat7下运行正常。这个例子展示了camel通过mybatis从数据库读取数据。相关更新,删除数据形式类似。

目录结构

代码GoodsMapper.java

package dao;

import java.util.List;
import model.GoodsEntity;

public interface GoodsMapper {
	public List<GoodsEntity> getAll();
}

代码MybatisHandler.java

package Hanadler;
import java.util.List;
import model.GoodsEntity;

public class MybatisHandler {
	public void getData(List<GoodsEntity> lst){
		for (GoodsEntity goodsEntity : lst) {
			System.out.println(goodsEntity.getGoodsName());
		}
	}
}

代码GoodsMapper.xml

<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.GoodsMapper">
	<resultMap type="model.GoodsEntity" id="ResultMap">
		<result column="goodsId" property="goodsId" />
		<result column="goodsName" property="goodsName" />
		<result column="address" property="address" />
	</resultMap>

	<select id="getAll" resultMap="ResultMap" >
		select * from goods 
	</select>
<!-- 
	 【1】 
	<select id="getAll" resultMap="ResultMap" parameterType="String">
		select * from goods where goodsName = #{goodsName}
	</select>
	
	 【2】
	<select id="getAll" resultMap="ResultMap" parameterType="model.GoodsEntity">
		select * from goods where goodsId = #{goodsId} and oodsName = #{goodsName}
	</select>
 -->
</mapper>

代码GoodsEntity.java

package model;

public class GoodsEntity {
	private int goodsId;
	private String goodsName;
	private String address;
	
	public int getGoodsId() {
		return goodsId;
	}
	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}
	public String getGoodsName() {
		return goodsName;
	}
	public void setGoodsName(String goodsName) {
		this.goodsName = goodsName;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
}

代码MybatisRoute.java

package route;
import org.apache.camel.builder.RouteBuilder;
import Hanadler.MybatisHandler;
import model.GoodsEntity;

public class MybatisRoute extends RouteBuilder {
	public void configure() throws Exception {
//		//【2】
//		GoodsEntity ge = new GoodsEntity();
//		ge.setGoodsId(100);
//		ge.setGoodsName("汽车");
		
		//每隔60秒查询一次DB
		from("timer://queryAward?period=60s")
//		//【1】.传递1个参数
//		.setBody(constant("汽车"))
//		//【2】.传递多个参数
//		.setBody(constant(ge))
		.to("mybatis:getAll?statementType=SelectList")
		.bean(MybatisHandler.class,"getData");
	}
}

代码mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis
<think>好的,我现在需要帮助用户找到使用SpringBoot、MyBatis和Vue实现分页功能的示例代码和教程。用户可能正在开发一个需要分页功能的Web应用,希望了解前后端如何配合实现这一功能。首先,我应该回顾用户提供的引用内容,看看里面有没有相关的配置或代码示例。 引用1提到了pagehelper组件在SpringBoot中的使用,引用了配置文件和分页插件的设置。引用2和3都涉及到MyBatis Plus的分页配置,特别是引用3详细说明了如何配置MybatisPlusInterceptor并指定数据库类型。引用4则给出了SpringBoot的配置文件示例,包括数据源和MyBatis的相关设置。这些引用内容主要集中在后端配置上,但用户还提到了Vue,所以需要整合前端部分。 接下来,我需要构建一个分步的解决方案,涵盖后端和前端。后端部分应该包括SpringBoot整合MyBatis Plus的分页插件,创建控制器和服务层来处理分页请求。前端部分需要Vue组件发送分页请求并处理响应数据,展示分页导航。 首先,后端配置方面,根据引用3的内容,配置MybatisPlusInterceptor并添加分页插件,指定数据库类型为MySQL。然后在服务层使用Page对象进行查询,控制器返回分页结果。引用2中的@MapperScan注解也是关键,确保Mapper接口被正确扫描。 前端部分,用户可能需要使用Vue发送GET请求,传递当前页和每页大小的参数。使用axios进行HTTP通信,接收后端返回的分页数据,并在页面上展示列表和分页控件。引用4中的端口号和数据源配置也需要在后端配置文件中设置,确保前后端连接正确。 可能的问题包括分页插件的正确配置、前后端参数传递的一致性,以及Vue组件中处理分页事件的方法。需要确保后端返回的数据结构符合前端预期,比如包含记录列表、当前页、总页数等信息。此外,跨域问题也需要处理,可以在后端添加CORS配置。 最后,生成相关问题,帮助用户深入理解分页实现的各个方面,比如性能优化、异常处理、样式调整等。需要确保提供的代码示例正确,并符合用户的技术栈要求,比如MyBatis Plus和Vue的版本兼容性。</think>### SpringBoot+MyBatis+Vue分页实现方案 #### 一、后端实现(SpringBoot+MyBatis Plus) 1. **添加依赖** ```xml <!-- pom.xml --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3</version> </dependency> ``` 2. **配置分页插件** ```java @Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } } ``` 3. **Service层实现** ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { public Page<User> getPage(int current, int size) { Page<User> page = new Page<>(current, size); return this.page(page); } } ``` 4. **Controller层接口** ```java @RestController @RequestMapping("/api/user") public class UserController { @Autowired private UserService userService; @GetMapping("/page") public Result<Page<User>> getPage(@RequestParam(defaultValue = "1") int current, @RequestParam(defaultValue = "10") int size) { return Result.success(userService.getPage(current, size)); } } ``` #### 二、前端实现(Vue3+Element Plus) 1. **分页组件** ```vue <template> <el-table :data="tableData"> <!-- 列定义 --> </el-table> <el-pagination v-model:current-page="currentPage" :page-size="pageSize" :total="total" @current-change="handlePageChange" /> </template> <script setup> import { ref, onMounted } from 'vue' import axios from 'axios' const currentPage = ref(1) const pageSize = ref(10) const total = ref(0) const tableData = ref([]) const loadData = async () => { const res = await axios.get('/api/user/page', { params: { current: currentPage.value, size: pageSize.value } }) tableData.value = res.data.records total.value = res.data.total } onMounted(loadData) const handlePageChange = (newPage) => { currentPage.value = newPage loadData() } </script> ``` #### 三、关键配置说明 1. **application.yml配置** ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/dbname?useSSL=false&serverTimezone=UTC username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: configuration: map-underscore-to-camel-case: true ``` 2. **跨域配置(后端)** ```java @Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "POST"); } } ``` 该方案实现了以下功能特性: - 后端采用MyBatis Plus自动分页查询 - 前端使用Element Plus分页组件 - 支持动态页码切换 - 每页条数可配置化 - 自动处理分页参数映射
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值