写在开始

  mybatis是一个持久化框架,支持手动sql、存储过程、高级映射。mybatis支持XML方式或注解方式将POJO与数据库表间建立映射。

maven依赖

  spring-boot、mysql、mybatis

项目目录结构

整体配置

  1 Entity实体;

  2 Mapper接口;

  3 基于XML的实体、表、接口映射表;

  4 配置文件中的映射表位置指向。

第一部分 Entity实体

package com.example.demo.entity;

/**
* @Author:tianminghai
* @Date:3:13 PM 2018/10/26
*/ public class CarEntity {
private int carId; private String carName; private int isDel; public int getCarId() {
return carId;
} public void setCarId(int carId) {
this.carId = carId;
} public String getCarName() {
return carName;
} public void setCarName(String carName) {
this.carName = carName;
} @Override
public String toString() {
return "CarEntity{" +
"carId=" + carId +
", carName='" + carName + '\'' +
", isDel=" + isDel +
'}';
} public int getIsDel() {
return isDel;
} public void setIsDel(int isDel) {
this.isDel = isDel;
}
}

第二部分 Mapper接口

接口提供三个方法:

1根据车辆ID查询车辆;

2查询所有车辆;

3根据车辆ID批量删除车辆

package com.example.demo.mapper;

import com.example.demo.entity.CarEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param; import java.util.List; /**
* @Author:tianminghai
* @Date:3:12 PM 2018/10/26
*/
@Mapper
public interface CarMapper { CarEntity selectByPrimaryKey(@Param("carId") Integer carId); List<CarEntity> selectAll(); boolean batchRemoveCarByPks(List<Integer> list);
}

第三部分 基于XML的实体、表、接口映射表

resultMap 定义实体和表的映射关系

sql定义返回值

del定义删除语句,foreach用于循环列表

#{carId,jdbcType=INTEGER}代码段用于在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.example.demo.mapper.CarMapper">
<resultMap id="BaseResultMap" type="com.example.demo.entity.CarEntity">
<id column="car_id" jdbcType="INTEGER" property="carId"/>
<result column="car_name" jdbcType="VARCHAR" property="carName"/>
<result column="is_del" jdbcType="INTEGER" property="isDel"/>
</resultMap>
<sql id="Base_Column_List">
car_id,car_name,is_del
</sql> <!-- 通过主键集合批量删除记录 -->
<delete id="batchRemoveCarByPks" parameterType="java.util.List">
DELETE FROM car
WHERE car_id in
<foreach collection="list" item="dataId" index="index"
open="(" close=")" separator=",">
#{dataId}
</foreach>
</delete> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from car
where car_id = #{carId,jdbcType=INTEGER}
</select>
<select id="selectAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from car
</select>
</mapper>

第四部分 配置文件中的映射表位置指向  

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=Aa123123 mybatis.mapper-locations=classpath:mapper/*.xml 

到目前为止项目已经可以跑起来了~~~

关于@MapperScan注解

Application里的@MapperScan注解指定mybatis搜索的包名。XML中mapper可以指定mybatis中一个mapper的命名空间。当填写了XML之后可以不用添加注解,项目同样可以跑通

注意XML文件当中不能随意加空格和TAB,这两个符号可能导致接口发生错误

spring-boot-mybatis搭建的更多相关文章

  1. spring boot+mybatis搭建项目

    一.创建spring boot项目 1.File->New->Project 2.选择 Spring Initializr ,然后选择默认的 url 点击[Next]: 3.修改项目信息 ...

  2. spring boot+mybatis+quartz项目的搭建完整版

    1. 利用spring boot提供的工具(http://start.spring.io/)自动生成一个标准的spring boot项目架构 2. 因为这里我们是搭建spring boot+mybat ...

  3. 快速搭建一个Spring Boot + MyBatis的开发框架

    前言:Spring Boot的自动化配置确实非常强大,为了方便大家把项目迁移到Spring Boot,特意总结了一下如何快速搭建一个Spring Boot + MyBatis的简易文档,下面是简单的步 ...

  4. java 搭建新项目,最佳组合:spring boot + mybatis generator

    java 搭建新项目,最佳组合:spring boot + mybatis generator

  5. spring boot+mybatis+swagger搭建

    环境概述 使用的开发工具:idea 2018 3.4 环境:jdk1.8 数据库:MariaDB (10.2.21) 包管理:Maven 3.5 Web容器:Tomcat 8.0 开发机系统:Wind ...

  6. Spring boot后台搭建一使用MyBatis集成Mapper和PageHelper

    目标: 使用 Spring  boot+MyBatis+mysql 集成 Mapper 和 PageHelper,实现基本的增删改查 先建一个基本的 Spring Boot 项目开启 Spring B ...

  7. spring boot + mybatis + druid配置实践

    最近开始搭建spring boot工程,将自身实践分享出来,本文将讲述spring boot + mybatis + druid的配置方案. pom.xml需要引入mybatis 启动依赖: < ...

  8. Spring Boot + Mybatis + Redis二级缓存开发指南

    Spring Boot + Mybatis + Redis二级缓存开发指南 背景 Spring-Boot因其提供了各种开箱即用的插件,使得它成为了当今最为主流的Java Web开发框架之一.Mybat ...

  9. Spring boot Mybatis 整合(完整版)

    个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...

  10. Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结

    Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...

随机推荐

  1. git提交代码到码云

    日常代码一般提交到github比较多,但我还是钟爱马爸爸,没错就是码云. 码云是中文版的代码托管的网站,不存在打开网速问题,使用也蛮方便的,日常自己保存托管代码已经足够,平时使用git提交代码到码云是 ...

  2. Linux下安装pip无法使用的情况

    不知道有没有安装成功,首先先卸载软件 sudo apt-get purge --auto-remove python3-pip sudo apt-get update 然后在重新安装 sudo apt ...

  3. VB

    on error resume next: 从该语句开始,遇到错误时程序不会中止,也不会出现错误提示,将继续运行.作用范围直至程序结束或语句所在函数等结束 Public Property :可读也可写 ...

  4. Docker 学习笔记 (一)Dockerfile 创建本地镜像

    一.测试环境 OS version: CentOS Linux release 7.5.1804 (Core) docker cluster : master 1 + data node 4 dock ...

  5. Python中级 —— 02函数式编程

    函数式编程 函数是Python内建支持的一种封装,而函数式编程通俗说来就是把函数本身作为参数传入另一个函数,允许返回一个函数. 函数名其实也是变量,也可以被赋值.如果函数名被赋值为其他值,则不再指向原 ...

  6. redis学习指南

    一.redis安装 # 安装最新版 yum install -y epel-release vim atop htop net-tools git wget gcc-c++ yum clean all ...

  7. Learning notes | Data Analysis: 1.1 data evaluation

    | Data Evaluation | - Use Shift + Enter or Shift + Return to run the upper box so as to make it disp ...

  8. Python学习笔记十一:模块

    标准库(内置模块) time与datetime 模块 时间表示方式 1.时间戳 2.格式化的字符串 3.元组形式 时间戳就是一个秒数 x=time.time(),从1970年1月1日0时0分0秒到现在 ...

  9. Leecode刷题之旅-C语言/python-389 找不同

    /* * @lc app=leetcode.cn id=389 lang=c * * [389] 找不同 * * https://leetcode-cn.com/problems/find-the-d ...

  10. git如何到精通

    git教程   目录 一.版本控制概要 1.1.什么是版本控制 1.2.常用术语 1.3.常见的版本控制器 1.4.版本控制分类 1.4.1.本地版本控制 1.4.2.集中版本控制 1.4.3.分布式 ...