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* ...
随机推荐
- Unity 4.7 导出工程在XCode10.1上编译报错
Unity 4.7 导出工程在XCode 10.1上编译报错,而在XCode 9.3上是可以正常编译运行的.原因是Unity4.7所依赖的头文件和库文件在XCode10上没有了,解决办法如下,把XCo ...
- HBase 文件读写过程描述
HBase 数据读写过程描述 我们熟悉的在 Hadoop 使用的文件格式有许多种,例如: Avro:用于 HDFS 数据序序列化与 Parquet:常见于 Hive 数据文件保存在 HDFS中 HFi ...
- VS2015创建WDK的问题
在微软官网找了半天.. 搜索window driver kit,好吧.进入一页英文页面.. https://docs.microsoft.com/en-us/windows-hardware/driv ...
- ARM设备树
学习目标:学习设备树相关内容: 一.概念 在Linux 2.6中,ARM架构的板极硬件细节过多地被硬编码在arch/arm/plat-xxx和arch/arm/mach-xxx,在kernel中存在大 ...
- 利用wireshark抓取TCP的整个过程分析。
原文地址:https://www.cnblogs.com/NickQ/p/9226579.html 最近,已经很久都没有更新博客了.看看时间,想想自己做了哪些事情,突然发现自己真的是太贪心,到头来却一 ...
- Zeta--S3 Linux优化/缩短开机时间
U-Boot1)axp20_set_ldo3实现里面把两个__msdelay(200);去掉,节省400ms2)sys_config.fex把下面的used设置为0,不使用开机指示灯闪烁,可以省掉35 ...
- angularjs与vue循环数组对象是区别
一直都觉得angularjs和vue是想类似的,今天在限制加载的数据条数时发现 其不同,话不多说,直接看代码: 1.angularjs <li ng-repeat="item in d ...
- 时间戳Unix timestamp
(1)定义 Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01 ...
- Egret 菜鸟级使用手册--第二天
################新的一天,我还是大佬 今天加载个英雄,先在GameScene里搞一个英雄出来,然后再创建一个Hreo类 接下来又一个新的API egret.TouchEvent.TOU ...
- noone is not in the sudoers file ubuntu
Login as root or su to get root prompt type visudo an editor will open find a line says root ALL=( ...