Spring Boot 集成Mybatis

 

 

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

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

application.yml中的配置如下:

server: 
  port: 8081
  servlet:
    context-path: /
spring: 
  profiles:
    active:
    - dev

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

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

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

<!-- mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>2.2.0</version>
</dependency>
<!-- druid数据源 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.6</version>
</dependency>
<!-- oracle数据驱动 -->
<dependency>
	<groupId>com.bijiangzb.oracle</groupId>
	<artifactId>ojdbc6</artifactId>
	<version>1.0.0</version>
</dependency>		

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

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

创建实体对象:

package com.bj.loan.entity;

import java.io.Serializable;

/**
 * @author huxiangen
 * @date 2021-06-17 14:40:00
 * @description BIZ_ORDER
 * @version 1.0
 */
public class Order implements Serializable{
	private static final long serialVersionUID = -196857271193419843L;

	/**
     * @description 主键
     */
    private String orderId;

    /**
     * @description 客户名称
     */
    private String custName;


    /**
     * @description 主键
     * @return ORDER_ID 主键
     */
    public String getOrderId() {
        return orderId;
    }

    /**
     *@description 主键
     * @param orderId 主键
     */
    public void setOrderId(String orderId) {
        this.orderId = orderId == null ? null : orderId.trim();
    }
    
    /**
     * @description 客户名称
     * @return CUST_NAME 客户名称
     */
    public String getCustName() {
        return custName;
    }

    /**
     *@description 客户名称
     * @param custName 客户名称
     */
    public void setCustName(String custName) {
        this.custName = custName == null ? null : custName.trim();
    }
}

创建dao操作数据库:

package com.bj.loan.dao;

import com.bj.loan.entity.Order;

public interface OrderMapper {

    Order selectByPrimaryKey(String orderId);

}

创建mapper XML执行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.bj.loan.dao.OrderMapper">
  <resultMap id="BaseResultMap" type="com.bj.loan.entity.Order">
    <id column="ORDER_ID" jdbcType="VARCHAR" property="orderId" />
    <result column="CUST_NAME" jdbcType="VARCHAR" property="custName" />
  </resultMap>
  <sql id="Base_Column_List">
    ORDER_ID, CUST_NAME
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from BIZ_ORDER
    where ORDER_ID = #{orderId,jdbcType=VARCHAR}
  </select>
</mapper>

创建service接口类

package com.bj.loan.service;

import com.bj.loan.entity.Order;

public interface OrderService {
	
	Order findOrder(String orderId);
	
}

创建接口的实现类

package com.bj.loan.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bj.loan.dao.OrderMapper;
import com.bj.loan.entity.Order;
import com.bj.loan.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private OrderMapper orderMapper;

	@Override
	public Order findOrder(String orderId) {
		return orderMapper.selectByPrimaryKey(orderId);
	}

}

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

package com.bj.loan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bj.loan.entity.Order;
import com.bj.loan.service.OrderService;

@RestController
public class OrderController {
	
	@Autowired
	private OrderService orderService;
	
	
	@GetMapping("/order")
	public Order getOrder() {
		System.out.println("进入订单查询");
		Order order=orderService.findOrder("f51fe14d-843d-11ea-8a2e-f58d34ffebe3");
		System.out.println("order:"+order.toString());
		return order;
	}
	
}

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

package com.bj;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;


@SpringBootApplication(scanBasePackages = {"com.bj.loan"})
@MapperScan("com.bj.loan.dao")
public class SpringBootTestApplication extends SpringBootServletInitializer{
	
	
	public static void main(String[] args) {
		SpringApplication.run(SpringBootTestApplication.class, args);
	}

}

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

页面:

后台: 

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

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

 

 

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

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

<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
        <!-- 新增配置 -->
		<resources>
			<resource>
				<directory>${project.basedir}/src/main/java</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
					<include>**/*.yml</include>
				</includes>
				<!-- 是否替换资源中的属性 -->
				<filtering>false</filtering>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
					<include>**/*.yml</include>
				</includes>
				<filtering>false</filtering>
			</resource>
		</resources>
	</build>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

huxiangen

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

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

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

打赏作者

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

抵扣说明:

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

余额充值