springboot整合mybatis

1.创建springboot项目

2.添加pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>springboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!--spring boot web 启动-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--mybatis依赖-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.0.1</version>
		</dependency>
		<!--mysql 依赖-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<!--spring boot 测试-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.2</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

3.创建数据库表

CREATE TABLE `tb_appeal` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `ywdh` varchar(255) DEFAULT NULL COMMENT '业务单号',
  `reason` varchar(255) DEFAULT NULL COMMENT '申诉原因',
  `time` varchar(30) DEFAULT NULL COMMENT '申诉时间',
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='申诉';

4.创建实体类Appeal

package com.example.springboot.entity;

import lombok.Data;

@Data
public class Appeal {

    private String id;
    private String orderNo;
    private String reason;
    private String appealTime;

}

5.创建dao

package com.example.springboot.dao;


import com.example.springboot.entity.Appeal;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface AppealDao {

    Appeal getAppeal(int id);

    int insertAppeal(Appeal record);

}

6.创建mybatis 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.example.springboot.dao.AppealDao" >

    <resultMap id="BaseResultMap" type="com.example.springboot.entity.Appeal">
        <result column="id" jdbcType="INTEGER" property="id" />
        <result column="ywdh" jdbcType="VARCHAR" property="orderNo" />
        <result column="reason" jdbcType="VARCHAR" property="reason" />
        <result column="time" jdbcType="VARCHAR" property="appealTime" />
    </resultMap>

    <select id="getAppeal" parameterType="int" resultMap="BaseResultMap">
        select id,ywdh,reason,time from tb_appeal where id=#{id}
    </select>

    <insert id="insertAppeal" useGeneratedKeys="true" keyProperty="id">
        insert into tb_appeal(ywdh,reason,time) value (#{orderNo},#{reason},#{appealTime})
    </insert>
</mapper>

7.创建service接口

package com.example.springboot.service;


import com.example.springboot.entity.Appeal;

public interface AppealService {
    Appeal getAppeal(int id);

    int insertAppeal(Appeal appeal);
}

8.创建service实现类

package com.example.springboot.service;

import com.example.springboot.dao.AppealDao;
import com.example.springboot.entity.Appeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AppealServiceImpl implements AppealService {
    @Autowired
    private AppealDao appealDao;
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,timeout = 1)
    public Appeal getAppeal(int id) {
        return appealDao.getAppeal(id);
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,timeout = 1)
    public int insertAppeal(Appeal appeal) {
        return appealDao.insertAppeal(appeal);
    }
}

9.创建controller

package com.example.springboot.controller;

import com.example.springboot.entity.Appeal;
import com.example.springboot.service.AppealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/appeal")
public class AppealController {
    @Autowired
    AppealService appealService;

    @RequestMapping("/getAppeal")
    @ResponseBody
    public Appeal getAppeal(int id){
        return appealService.getAppeal(id);
    }

    @RequestMapping("/insertAppeal")
    @ResponseBody
    public Map<String,Object> insertAppeal(Appeal appeal){
        Map<String,Object> result = new HashMap<>();
        int update = appealService.insertAppeal(appeal);
        result.put("success",update==1);
        result.put("appeal",appeal);
        return result;

    }
}

 

10.创建spring boot配置文件

application.yml

spring:
  profiles:
    active: dev

application-dev.yml

server:
  port: 8082

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://192.168.3.25:3306/db_forklift?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.example.springboot.entity

整个项目的文件结构

启动项目

在浏览器中输入

http://localhost:8082/appeal/insertAppeal?orderNo=78347384&reason=4483948&appealTime=48394839

返回结果

{"success":true,"appeal":{"id":"5","orderNo":"78347384","reason":"4483948","appealTime":"48394839"}}

 

springboot整合mybatis的时候出现的报错:

1.Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

报错由于mysql时区与本机时区不一致导致

在jdbc连接的url后面加上serverTimezone=GMT即可解决问题,如果需要使用gmt+8时区,需要写成GMT%2B8,否则会被解析为空

jdbc:mysql://192.168.3.25:3306/db_forklift?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT

2.异常报错:Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

解决方法:按照最新官方提示支持将com.mysql.jdbc.Driver  改为  com.mysql.cj.jdbc.Driver

3.Field dao in com.example.springboot.dao.AppealDao required a bean of type ‘com.example.springboot.dao.AppealDao’ that could not be found.

解决方法:mapper/dao接口一定要加注解@Mapper!!! 加@Component @Repository没用!!!否则spring无法识别mapper对应的bean,所以无法实例化,自然无法自动注入!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值