Spring Boot 集成Mybatis

偏执的太偏执、 2022-10-07 10:43 266阅读 0赞

注:接上一篇Spring Tool Suite4搭建spring boot项目

1、把搭建好的Spring Boot项目中的application.properties改成application.yml同时配置本地***-dev.yml 文件,

application.yml中的配置如下:

  1. server:
  2. port: 8081
  3. servlet:
  4. context-path: /
  5. spring:
  6. profiles:
  7. active:
  8. - dev

application-dev.yml中的配置如下(主要是数据库连接配置和mybatis的xml扫描路径配置):

  1. spring:
  2. datasource:
  3. druid:
  4. url: jdbc:oracle:thin:@IP地址:端口:服务名
  5. username: 用户名
  6. password: 密码
  7. driver-class-name: oracle.jdbc.OracleDriver
  8. maxWait: 10000
  9. initial-size: 10
  10. max-active: 30
  11. min-idle: 10
  12. validation-query: select 1 from dual
  13. mybatis:
  14. mapper-locations: classpath*:com/bj/loan/mapper/*.xml

2、需要在POM中引入oracle、spring druid、mybatis的相关jar包

  1. <!-- mybatis -->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. <version>2.2.0</version>
  6. </dependency>
  7. <!-- druid数据源 -->
  8. <dependency>
  9. <groupId>com.alibaba</groupId>
  10. <artifactId>druid-spring-boot-starter</artifactId>
  11. <version>1.2.6</version>
  12. </dependency>
  13. <!-- oracle数据驱动 -->
  14. <dependency>
  15. <groupId>com.bijiangzb.oracle</groupId>
  16. <artifactId>ojdbc6</artifactId>
  17. <version>1.0.0</version>
  18. </dependency>

注意:ojdbc在Maven仓库中是不存在的可能会报红,直接在百度下载jar包上传到本地maven个人仓库中就行了。

3、在com.bj包下创建实体包entity、dao、mapper、service、controller已经在service包下创建impl业务实现包

创建实体对象:

  1. package com.bj.loan.entity;
  2. import java.io.Serializable;
  3. /**
  4. * @author huxiangen
  5. * @date 2021-06-17 14:40:00
  6. * @description BIZ_ORDER
  7. * @version 1.0
  8. */
  9. public class Order implements Serializable{
  10. private static final long serialVersionUID = -196857271193419843L;
  11. /**
  12. * @description 主键
  13. */
  14. private String orderId;
  15. /**
  16. * @description 客户名称
  17. */
  18. private String custName;
  19. /**
  20. * @description 主键
  21. * @return ORDER_ID 主键
  22. */
  23. public String getOrderId() {
  24. return orderId;
  25. }
  26. /**
  27. *@description 主键
  28. * @param orderId 主键
  29. */
  30. public void setOrderId(String orderId) {
  31. this.orderId = orderId == null ? null : orderId.trim();
  32. }
  33. /**
  34. * @description 客户名称
  35. * @return CUST_NAME 客户名称
  36. */
  37. public String getCustName() {
  38. return custName;
  39. }
  40. /**
  41. *@description 客户名称
  42. * @param custName 客户名称
  43. */
  44. public void setCustName(String custName) {
  45. this.custName = custName == null ? null : custName.trim();
  46. }
  47. }

创建dao操作数据库:

  1. package com.bj.loan.dao;
  2. import com.bj.loan.entity.Order;
  3. public interface OrderMapper {
  4. Order selectByPrimaryKey(String orderId);
  5. }

创建mapper XML执行SQL

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.bj.loan.dao.OrderMapper">
  4. <resultMap id="BaseResultMap" type="com.bj.loan.entity.Order">
  5. <id column="ORDER_ID" jdbcType="VARCHAR" property="orderId" />
  6. <result column="CUST_NAME" jdbcType="VARCHAR" property="custName" />
  7. </resultMap>
  8. <sql id="Base_Column_List">
  9. ORDER_ID, CUST_NAME
  10. </sql>
  11. <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
  12. select
  13. <include refid="Base_Column_List" />
  14. from BIZ_ORDER
  15. where ORDER_ID = #{orderId,jdbcType=VARCHAR}
  16. </select>
  17. </mapper>

创建service接口类

  1. package com.bj.loan.service;
  2. import com.bj.loan.entity.Order;
  3. public interface OrderService {
  4. Order findOrder(String orderId);
  5. }

创建接口的实现类

  1. package com.bj.loan.service.impl;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. import com.bj.loan.dao.OrderMapper;
  5. import com.bj.loan.entity.Order;
  6. import com.bj.loan.service.OrderService;
  7. @Service
  8. public class OrderServiceImpl implements OrderService {
  9. @Autowired
  10. private OrderMapper orderMapper;
  11. @Override
  12. public Order findOrder(String orderId) {
  13. return orderMapper.selectByPrimaryKey(orderId);
  14. }
  15. }

在controller下创建控制层对外提供方法实现业务操作后返回结果

  1. package com.bj.loan.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import com.bj.loan.entity.Order;
  6. import com.bj.loan.service.OrderService;
  7. @RestController
  8. public class OrderController {
  9. @Autowired
  10. private OrderService orderService;
  11. @GetMapping("/order")
  12. public Order getOrder() {
  13. System.out.println("进入订单查询");
  14. Order order=orderService.findOrder("f51fe14d-843d-11ea-8a2e-f58d34ffebe3");
  15. System.out.println("order:"+order.toString());
  16. return order;
  17. }
  18. }

然后在启动类增加dao包扫描 @MapperScan(“com.bj.loan.dao”)

  1. package com.bj;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  6. @SpringBootApplication(scanBasePackages = {"com.bj.loan"})
  7. @MapperScan("com.bj.loan.dao")
  8. public class SpringBootTestApplication extends SpringBootServletInitializer{
  9. public static void main(String[] args) {
  10. SpringApplication.run(SpringBootTestApplication.class, args);
  11. }
  12. }

启动项目页面输入http://127.0.0.1:8081/order

页面:20210617160746263.png

后台:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2h1eGlhbmdlbg_size_16_color_FFFFFF_t_70

至此集成mybatis结束,项目中使用到的数据库表及字段自行创建,上面mapper.xml中有映射关系;

项目下载地址:https://download.csdn.net/download/huxiangen/19699619

问题处理:Invalid bound statement (not found):

出现这个问题主要是我们打包的文件没有将resource下面的打包进去在maven中加入如下配置即可

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. </plugin>
  7. </plugins>
  8. <!-- 新增配置 -->
  9. <resources>
  10. <resource>
  11. <directory>${project.basedir}/src/main/java</directory>
  12. <includes>
  13. <include>**/*.properties</include>
  14. <include>**/*.xml</include>
  15. <include>**/*.yml</include>
  16. </includes>
  17. <!-- 是否替换资源中的属性 -->
  18. <filtering>false</filtering>
  19. </resource>
  20. <resource>
  21. <directory>src/main/resources</directory>
  22. <includes>
  23. <include>**/*.properties</include>
  24. <include>**/*.xml</include>
  25. <include>**/*.yml</include>
  26. </includes>
  27. <filtering>false</filtering>
  28. </resource>
  29. </resources>
  30. </build>

发表评论

表情:
评论列表 (有 0 条评论,266人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring Boot集成MyBatis

    > 你要搞清楚自己人生的剧本:不是你父母的续集,不是你子女的前传,更不是你朋友的外篇。对待生命你不妨大胆冒险一点,因为好歹你要失去它。——源自尼采 开始前… 上面的金句