步骤:

  1. pom文件添加依赖

  2. 创建包结构

  3. 添加application.yml, 配置数据库信息

  4. 使用mybatis-gennerator 生成三个文件

    • 实体类
    • 接口类
    • xml 文件
  5. 错误信息

    xml 绑定异常,没有扫描的 XXXmapper.xml文件

  6. 启动类扫描接口类

  7. 编写测试类

  8. 与传统使用mybatis操作数据的区别

    1. 没有必要使用sqlsession 来使用各种接口类的方法,操作数据库。
    2. 使用接口直接调用方法。

1. pom文件添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.plugens</groupId>
<artifactId>spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<!--springboot项目-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency> <!--配置thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <!--配置web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!--配置mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency> <!--配置mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency> <!--配置jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> </plugins>
</build>
</project>

2 创建包结构

3配置yml mybatis-location扫描路径

#端口号配置
server:
port: 8088
spring:
#模板引擎配置
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML
encoding: UTF-8
cache: false
servlet:
content-type: text/html
#静态文件配置
resources:
static-locations: classpath:/static,classpath:/META-INF/resources,classpath:/templates/
#jdbc配置
datasource:
url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf8
username: root
password: 12345678
driver-class-name: com.mysql.cj.jdbc.Driver
#mybatis配置
mybatis:
#映射文件路径
mapper-locations: classpath:mapper/*.xml
#模型所在的保命
type-aliases-package: com.plugens.spring.bean

4. mybatis 逆向生成代码

![zhuyi](/Users/yangtao/Library/Application Support/typora-user-images/image-20191118233058081.png)

Mybatis 生成插件的官方网站

https://github.com/kmaster/better-mybatis-generator/blob/master/README.md

必须要写数据库和这个serverTimezone=GMT

5. XXmapper文件

<?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.plugens.spring.dao.UserDaoMapper"> <resultMap id="BaseResultMap" type="com.plugens.spring.bean.User">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="passwd" jdbcType="VARCHAR" property="passwd" />
</resultMap> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER}
</select> </mapper>

5. 启动类

6. 测试类

7. 配置XXmapper.xml 到其他路径,在pom 文件中的build 标签中添加

<resources>
<resource>
<directory>src/main/java/com/plugens/spring/dao</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>

spring 与mybatis 整合的更多相关文章

  1. Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...

  2. Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...

  3. Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析

    前言 本文将分析mybatis与spring整合的MapperScannerConfigurer的底层原理,之前已经分析过java中实现动态,可以使用jdk自带api和cglib第三方库生成动态代理. ...

  4. Mybatis学习--spring和Mybatis整合

    简介 在前面写测试代码的时候,不管是基于原始dao还是Mapper接口开发都有许多的重复代码,将spring和mybatis整合可以减少这个重复代码,通过spring的模板方法模式,将这些重复的代码进 ...

  5. 九 spring和mybatis整合

    1       spring和mybatis整合 1.1     整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用Sq ...

  6. Mybatis学习(7)spring和mybatis整合

    整合思路: 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spr ...

  7. 框架篇:Spring+SpringMVC+Mybatis整合开发

    前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...

  8. SpringMVC, Spring和Mybatis整合案例一

    一  准备工作 包括:spring(包括springmvc).mybatis.mybatis-spring整合包.数据库驱动.第三方连接池. 二  整合思路 Dao层: 1.SqlMapConfig. ...

  9. MyBatis学习七:spring和MyBatis整合

    <\mybatis\day02\16mybatis和spring整合-sqlSessionFactory配置.avi;> MyBatis学习七:spring和MyBatis整合.逆向工程 ...

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

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

随机推荐

  1. es6 中let与const的简析

    1.let 它的作用类似于var,用来声明变量,但是所声明的变量,只在let命令所在的代码块内有效. if(true){ ; let b = ; } document.write(a); docume ...

  2. Libev源码分析03:Libev使用堆管理定时器

    Libev中在管理定时器时,使用了堆这种结构,而且除了常见的最小2叉堆之外,它还实现了更高效的4叉堆. 之所以要实现4叉堆,是因为普通2叉堆的缓存效率较低,所谓缓存效率低,也就是说对CPU缓存的利用率 ...

  3. 06多次查询某区间内topk问题

            题目描述:给定一个数组,需要多次查找不同区间内的,第k大或者第k小的元素.         考虑题目是多次查找,如果采用只对查询区间内的元素进行排序的思路,然后输出第k大的数的策略,那 ...

  4. HZOJ string

    正解炸了…… 考试的时候想到了正解,非常高兴的打出来了线段树,又调了好长时间,对拍了一下发现除了非常大的点跑的有点慢外其他还行.因为复杂度算着有点高…… 最后正解死于常数太大……旁边的lyl用同样的算 ...

  5. 一、JVM内存区域组成

    一.JVM内存区域组成  java把内存分四种:  1.栈区(stack segment)— 由编译器自动分配释放,存放函数的参数值,局部变量的值等,具体方法执行结束之后,系统自动释放内存资源  2. ...

  6. LRJ-Example-06-17-Uva10562

    main() 函数中的这两行 fgets(buf[0], maxn, stdin); sscanf(buf[0], "%d", &T); 不能简单替换为 scanf(&qu ...

  7. 2011-04-21 运程连Oracle的方法

    oracle无法远程连接重要原因,即使防火墙开放1521端口, 但是返回包可能是随机端口,所以仍有可能被防火墙阻止. 解决方法: 在注册表中增加一个字符串值如下.可解决 花费两天找到的方法 [HKEY ...

  8. ubuntu14.04 dnsmasq搭建本地名字服务器

    1 修改dnsmasq配置文件/etc/dnsmasq.conf 在/etc/dnsmasq.conf文件底部增加 #++++++++++++++++++++++++++++++++++++++++ ...

  9. 4-2 setting中一定要将ROBOTSTXT_OBEY = False的注释去掉

    # Obey robots.txt rules##默认遵循robots协议的,默认去读取每个网站上的robots协议ROBOTSTXT_OBEY = False

  10. codeforce 378 div 2 F —— Drivers Dissatisfaction (最小生成树,LCA,倍增)

    官方题解: If you choose any n - 1 roads then price of reducing overall dissatisfaction is equal to min(c ...