Mybatis和Mybaits-plus在springboot项目中使用,需要先引入依赖
Mybatis和Mybaits-plus的sprongboot项目依赖在中央仓库链接:
Mybatis依赖:https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
Mybaits-plus依赖:https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.0version>
dependency>
还需要mysql依赖:
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
https://mvnrepository.com/artifact/mysql/mysql-connector-java
要想使用mybatis和mybatis-plus,还需要其它必备依赖:
先引入parent:
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.5version>
<relativePath/>
parent>
spring-boot-starter-parent
提供了springboot统一的依赖管理和插件管理;主要的依赖其实是继承了spring-boot-dependencies(通过标签dependencyManagement管理依赖声明),本质是继承了它然后扩展了插件配置。
其它依赖:
@RequestMapper
等注解需要用到)# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/tzq?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
xml文件
如果写在java文件夹
下,在编译的时候是不会被编译的(编译器只会编译java文件夹下的.java文件)。否则运行会出现Invalid bound statement (not found)错误(找不到映射xml文件)。 <resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
resource>
resources>
注意事项二:需要在启动类或者配置类上加上@MapperScanner( )
,否则在启动的时候报错(找不到mapper):
注意事项三:需要在配置文件application.properties
中添加mybatis.mapper-locations
:
mybatis.mapper-locations=classpath*:com/example/test002/mapper/**/*.xml
否则 在运行时也会报错Invalid bound statement (not found):(与注意事项第一个一样的错误)
mybatis-plus.mapper-locations
,必须加plus,不可以mybatis.mapper-locations
,否则配置无效!!!!!
<mapper namespace="com.example.testmybatisplus.mapper.StudentsMapper">
<resultMap id="BaseResultMap" type="com.example.testmybatisplus.entity.Students">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="VARCHAR" property="age" />
resultMap>
<select id="findTzq" resultType="com.example.testmybatisplus.entity.Students">
select * from students where id = #{id}
select>
mapper>
server.port=8004
spring.application.name=testApplication
# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/tzq?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
# 配置mybatis-plus的mapper位置
mybatis-plus.mapper-locations=classpath*:com/example/testmybatisplus/mapper/**/*.xml
# 配置mybatis的mapper位置
mybatis.mapper-locations=classpath*:com/example/testmybatisplus/mapper/**/*.xml
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
resource>
resources>