一、搭建步骤
1.1引入jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--添加springboot-mybatis的依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--mybatis 代码自动生成-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
1.2 启动类中开启事务代理
@EnableTransactionManagement(proxyTargetClass=true)
1.3 自动生成mapper、xml、dao、po等代码
借助idea的 myBatis逆向生成工具EasyCode 等插件或者第三方其他工具生成代码
EasyCode 可以帮我们连接数据库,一键生成entity、dao、service、serviceimpl、controller、mapper,如下图
安装好easyCode 插件后,在DataSoure 面板中添加我们对应的数据源后
点开连接选中对应的表右键在弹出的右键中选择EasyCode->Generate Code,帮主我们生成对应的代码
选择要生成的模块、位置以及要生成的内容:
最后生成的代码如下:
本测试用例生成配置的xml、dao 、实体等调整后的在项目中的位置如下
1.4 配置 aplication.yml mybatis 配置文件的位置和实体文件的位置
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mybatis:
mapperLocations: classpath:mapper/*.xml
# 兼容系统管理服务部分代码,其他的应用可以不配置
type-aliases-package: com.zhang.buiness.amaster.workserver.dao.entity
# 打印sql日志,开发环境可以放开,生产环境不允许放开,对性能影响很大
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
配置说明:
用type-aliases-package中指定entity扫描包类让mybatis自定扫描到自定义的entity
mapper-locations:指定配置文件xml的扫描位置
1.4.1 classpath:和classpath*的区别
classpath:和classpath*:都是用于指定Java类路径的前缀。
它们的区别在于:
- classpath: 只会在指定的类路径中查找类,不包括其它jar包或目录下的类,它只会加载指定路径下的类文件。
- classpath*: 会扫描所有的类路径,包括jar包和目录,查找类文件。
因此:
1.如果你想要加载指定路径下的类文件,可以使用classpath:前缀。
2.而如果你想要加载所有的类文件,包括jar包和目录中的类文件,可以使用classpath*:前缀。
需要注意的是,由于classpath*:需要扫描所有的类路径,因此可能会导致性能问题,应该尽量避免在大型应用中使用。
classpath本质是JVM的根路径,JVM获取资源都是从该根路径下查找的
注意:这个路径是一个逻辑路径,并不是磁盘路径
一般classpath指向的是 target下的classes,也就是编译路径的根路径,而一般classes中放着这些文件
1、.java 编译好的 .class
2、properties配置文件
3、xml配置文件
4、一些模板文件 eg:*.flt
5、其他需要用classpath获取到的文件
在jar包中的对应的位置如下,在BOOT-INF\classes 文件夹下
1.4.2 指定 mapper-locations位置的两种种方式
方式一 xml文件与mapper类放在一起。
yml 配置
mybatis:
config-location: classpath:mybatis-config.xml
mapper-locations: classpath:com/example/pgsqldemo/mybatis/dao/xml/*.xml
# mapper-locations: classpath:static/mybatis/mapper/*.xml
pom.xml 配置
<build>
<resources>
<!-- 扫描src/main/java下所有xx.xml文件 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!-- 扫描resources下所有资源 -->
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
方式二 resources创建Mapper接口同名文件夹用来存放Mapper.xml文件(mybatis默认方式)
如同本配置样例的配置方式
application配置文件的配置方式
mybatis:
config-location: classpath:mybatis-config.xml
pom文件的集成
<resources>
<!-- 复制logback-spring.xml-->
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>logback-spring.xml</include>
<include>mapper/*</include>
</includes>
</resource>
1.4.3 type-aliases-package作用
在Mybatis的mapper.xml文件中resultType的type或者paramterType会返回自定义entity,此时可以用全类名名来指定这些实体。
<select id="getUserInfoList" resultType="com.leo.model.UserInfo">
SELECT
<include refid="BaseSql"/>
FROM USER_INFO WHERE 1=1
</select>
里面的resultType返回的就是全类名。
其实可以使用type-aliases-package中指定entity扫描包类让mybatis自定扫描到自定义的entity。
springboot中type-aliases-package配置
mybatis.type-aliases-package=com.leo.model
1.5 配置pom.xml
将mybatis的xml文件在编码打包的时候作为资源文件一块打进jar包,要不然在编译后的或者jar包中没有这些mybtis的xml文件,程序执行sql的时候会报Invalid bound statement (not found)
<resources>
<!-- 复制logback-spring.xml-->
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>logback-spring.xml</include>
<include>mapper/*</include>
</includes>
</resource>
二 myabtis 常见报错问题
2.1 Mybatis报错Invalid bound statement (not found)
1、Mapper.xml中路径不准确
2、TextDao(接口)与TextMapper.xml id标签不一致
3、application.properties中配置mybatis.type-aliases-package=com.demo.entity需要与Text实体类路径一致
4、pom.xml文件中需要配置标签
<resources>
<!--将Java代码下的xml编译到class下面去-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
</resource>
</resources>
确认mapper.xml文件有没有在target目录下成功构建
- mybatis. mapperLocations 配置的位置,扫描不到对应的xml文件位置