spring-boot-mybatis搭建
写在开始
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搭建的更多相关文章
- spring boot+mybatis搭建项目
一.创建spring boot项目 1.File->New->Project 2.选择 Spring Initializr ,然后选择默认的 url 点击[Next]: 3.修改项目信息 ...
- spring boot+mybatis+quartz项目的搭建完整版
1. 利用spring boot提供的工具(http://start.spring.io/)自动生成一个标准的spring boot项目架构 2. 因为这里我们是搭建spring boot+mybat ...
- 快速搭建一个Spring Boot + MyBatis的开发框架
前言:Spring Boot的自动化配置确实非常强大,为了方便大家把项目迁移到Spring Boot,特意总结了一下如何快速搭建一个Spring Boot + MyBatis的简易文档,下面是简单的步 ...
- java 搭建新项目,最佳组合:spring boot + mybatis generator
java 搭建新项目,最佳组合:spring boot + mybatis generator
- spring boot+mybatis+swagger搭建
环境概述 使用的开发工具:idea 2018 3.4 环境:jdk1.8 数据库:MariaDB (10.2.21) 包管理:Maven 3.5 Web容器:Tomcat 8.0 开发机系统:Wind ...
- Spring boot后台搭建一使用MyBatis集成Mapper和PageHelper
目标: 使用 Spring boot+MyBatis+mysql 集成 Mapper 和 PageHelper,实现基本的增删改查 先建一个基本的 Spring Boot 项目开启 Spring B ...
- spring boot + mybatis + druid配置实践
最近开始搭建spring boot工程,将自身实践分享出来,本文将讲述spring boot + mybatis + druid的配置方案. pom.xml需要引入mybatis 启动依赖: < ...
- Spring Boot + Mybatis + Redis二级缓存开发指南
Spring Boot + Mybatis + Redis二级缓存开发指南 背景 Spring-Boot因其提供了各种开箱即用的插件,使得它成为了当今最为主流的Java Web开发框架之一.Mybat ...
- Spring boot Mybatis 整合(完整版)
个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...
- Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结
Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...
随机推荐
- HTML小记
1.页面内跳转 当<a>元素用于页面内的锚点跳转时,应该先为该页面设置一些锚点,而定义锚点有两种办法: 通过<a>元素的name属性来定义,如:<a name=" ...
- iOS 地图相关
参考博文:https://blog.csdn.net/zhengang007/article/details/52858198?utm_source=blogxgwz7 1.坐标系 目前常见的坐标系有 ...
- eclipse手动安装alibaba代码规范插件+取消阿里编码规约插件扫描出来的警告及错误
如果你的开发环境无法访问外网,那么手动安装阿里巴巴的代码规范插件是一个不错的选择.另外,很多教程说该插件需要jdk1.8以上,我试了一下jdk1.7也是可以运行的,更低的版本就不知道了,貌似jdk1. ...
- Read a large file with python
python读取大文件 较pythonic的方法,使用with结构 文件可以自动关闭 异常可以在with块内处理 with open(filename, 'rb') as f: for line in ...
- MySQL学习【第五篇SQL语句上】
一.mysql命令 1.连接服务端命令 1.mysql -uroot -p123 -h127.0.0.1 2.mysql -uroot -p123 -S /tmp/mysql.sock 3.mysql ...
- select 宽度跟随option内容自适应
传统的select在没有设置固定宽度的情况,会因为自身的 option 选项的里,宽度最宽的option作为select本身的宽度 例如 可见效果为: select的宽度因为"宽度最宽的op ...
- Redis笔记 -- 链表和链表节点的API函数(三)
链表和链表节点API 函数 作用 时间复杂度 listSetDupMethod 将给定的函数设置为链表的节点值复制函数 复制函数可以通过链表的dup属性直接获得,O(1) listGetDupMeth ...
- Python学习 :反射 & 单例模式
反射 什么是反射? - 反射主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省) 面向对象中的反射 - 通过字符串的形式来操作(获取.检查.增加.删除)对象中的成员 - python中的 ...
- 用函数指针将字符串 ”hello world“ 输出为 "world hello”
/************************************************************************* > File Name: 指针数组2.c & ...
- Mypwd 的解读与实现 20155202
Mypwd 的解读与实现 20155202 linux下pwd命令的编写 实验要求: 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd ...