写在开始

  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. DG不同步,MRP0进程打不开

    问题描述:主库备库之前正常连接,但是昨天磁盘空间满了之后,由于不知什么原因将备库重做日志删了,今天早上发现DG不同步的报警. 当时思路如下:1.通过select thread#,low_sequenc ...

  2. web前端时间戳转时间类型显示

    1.jsp头部加:<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> ...

  3. iOS 地图相关

    参考博文:https://blog.csdn.net/zhengang007/article/details/52858198?utm_source=blogxgwz7 1.坐标系 目前常见的坐标系有 ...

  4. SpirteKit深度复制SKSpriteNode节点及convert转换其它Scene上的节点到当前场景坐标

    Playground输出的代码 ,注意右侧打印出的SpriteNode输出值,HAPPY_NODE的输出信息一致. SpriteNode //MARK:- 扩展SkSpriteNode属性 exten ...

  5. Oracle 表空间、段、区和块简述

    数据块(Block) 数据块Block是Oracle存储数据信息的最小单位.注意,这里说的是Oracle环境下的最小单位.Oracle也就是通过数据块来屏蔽不同操作系统存储结构的差异.无论是Windo ...

  6. python3爬虫-通过selenium获取TB商品

    from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from seleni ...

  7. pt-archiver数据归档

    可以使用percona-toolkit包中的pt-archiver工具来进行历史数据归档 pt-archiver使用的场景: 1.清理线上过期数据. 2.清理过期数据,并把数据归档到本地归档表中,或者 ...

  8. chrome浏览器页面获取绑定返回顶部动画事件插件

    在chrome浏览器下页面加载: var top = $("body").scrollTop()  ; console.log(top)                      ...

  9. Python中级 —— 01面向对象进阶

    面向对象进阶 总结.补充(http://blog.csdn.net/fgf00/article/details/52479307) 面向对象高级语法部分 静态方法.类方法.属性方法 类的特殊方法 反射 ...

  10. Python-type函数使用