写在开始

  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. 一点一点看JDK源码(〇)

    一点一点看JDK源码(〇) liuyuhang原创,未经允许进制转载 写在前面: 几乎所有的大神都会强调看源码,也强调源码的重要性: 但是如何看源码,源码看什么?看了什么用?看了怎么用? 困扰很多人, ...

  2. eclipse安装activiti插件

    参考: https://blog.csdn.net/augustaurora/article/details/59618737 https://blog.csdn.net/qq_33547950/ar ...

  3. Web | Webpack快速上手

    概述 Webpack 是一个前端资源加载/打包工具.它将根据模块的依赖关系进行静态分析,然后将这些模块按照指定的规则生成对应的静态资源. 一般我们在开发中都是使用终端去进行安装,使用npm,关于npm ...

  4. java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'

    java.lang.IllegalStateException: Failed to load property source from location 'classpath:/applicatio ...

  5. Angular7教程-05-搭建项目环境

    1. 本节说明 本节以及后面的内容我们将会通过搭建一个简单的博客程序来对angular进行介绍,项目使用前端框架是bootstrap.版本v3.3.7,另外需要安装jquery.关于bootstrap ...

  6. 使用vue封装一个tab栏切换的左侧导航栏的公共组件

     首先看最终效果图: 1.compent文件夹里添加tab文件夹,里面创建index.vue index.js index.css index.vue内的template部份代码如下:(最新更正:代码 ...

  7. 1006.Sign in and Sign out(25)—PAT 甲级

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  8. mysql当前用户user()与current_user()

    Mysql在进行登陆时,会去匹配mysql库中的user表,并赋予相应的权限,但是怎么知道我们当时的登陆的用户名及相应的权限呢? 在Mysql中,有两个函数,一个是user(),一个是current_ ...

  9. WebGl 一个缓冲区传递颜色和坐标(矩形)

    效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

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

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