1.在pom.xml中配置相关jar依赖:
<!--加载mybatis整合springboot-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--mysql的jdbc驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis 自动生成插件-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
2.在Springboot的核心配置文件application.properties中配置Mapper.xml文件所在位置:
mybatis.mapper-locations=classpath:com/example/mapper/*.xml
3.在springboot的核心配置文件application.properties中配置数据源:
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
连接本地数据库,其中url中127.0.0.1:3306/test为数据库路径,test为数据库名
4.在mybatis的mapper接口中添加@mapper注解或者在运行的主类上添加@MapperScan("com/example/mapper")注解包扫描
这一项为写代码的时候用到的,到此。配置就完成了,然后开始应用
--------------------------------------------------以下为应用-----------------------------------------------------
1.使用反向工程将数据库代码生成一下,避免写数据库字段,太繁琐
在项目下创建GeneratorMapper.xml文件,和src同级
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--指定连接数据库的jdbc驱动包所在位置,指定到你本机的完整路径,maven仓库下目录下mysql/mysql-connector-java/8.0.19/mysql-connector-java-8.0.19.jar-->
<classPathEntry location="/Users/xxx/xxx/xxx/xxx/mysql/mysql-connector-java/8.0.19/mysql-connector-java-8.0.19.jar"></classPathEntry>
<!--配置table表信息内容体,targetruntime指定采用mybatis3的版本-->
<context id="tables" targetRuntime="MyBatis3">
<!--抑制生成注释,由于生成的注释都是英文的,可以不让它生成-->
<commentGenerator>
<property name="suppressAllComments" value="true"></property>
</commentGenerator>
<!--配置数据库连接信息-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/test"
userId="root"
password="123456">
</jdbcConnection>
<!--生成model类,targetPackage指定model类的包名,targetProject指定生成的model放在哪个工程下面-->
<javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成mybatis的Mapper.xml文件,targetPackage指定Mapper.xml文件包名,targetProject指定生成的Mapper.xml文件放在哪个工程下面-->
<sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成mybatis的Mapper接口类文件,targetPackage指定Mapper接口类文件包名,targetProject指定生成的Mapper接口类文件放在哪个工程下面-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--数据库表名及对应的java模型类名,domainObjectName定义的model类的名称-->
<table tableName="student"
domainObjectName="StudentModel"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
2.在pom文件<build>中配置mybatis代码自动生成插件
<!--mybatis代码自动生成插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<verbose>true</verbose>
</configuration>
</plugin>
配置完成后,在右侧maven projects对应项目下的plugins下面找到配置的插件,双击,代码会自动生成
做一个例子:
获取student表中所有数据
1.创建service接口StudentService
/**
* @create 2020/02/23
**/
public interface StudentService {
public List<StudentModel> getAllStudent();
}
和StudentService实现类,为了扫描到这个类,使用@Service注解
/**
* @create 2020/02/23
**/
@Service
public class StudentServiceImpl implements StudentService{
@Autowired
private StudentModelMapper studentModelMapper;
@Override
public List<StudentModel> getAllStudent() {
return studentModelMapper.selectAllStudent();
}
}
2.在studentModelMapper接口中添加selectAllStudent方法,接口StudenModelMapper.java上使用@Mapper注解,这就是配置中所说的第四项
/**
* 这个类需要Mapper这个注解
*/
@Mapper
public interface StudentModelMapper {
int insert(StudentModel record);
int insertSelective(StudentModel record);
/**
* 获取student表中所有信息
* @return 列表
*/
List<StudentModel> selectAllStudent();
}
然后在studentModelMapper.xml文件中增加selectAllStudent查询
<resultMap id="BaseResultMap" type="com.example.model.StudentModel">
<result column="name" jdbcType="CHAR" property="name" />
<result column="sex" jdbcType="INTEGER" property="sex" />
<result column="classno" jdbcType="INTEGER" property="classno" />
<result column="classname" jdbcType="CHAR" property="classname" />
</resultMap>
<sql id="Base_Column_List">
name,sex,classno,classname
</sql>
<select id="selectAllStudent" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from student
</select>
3.创建controller,获取student表中所有数据
/**
* @create 2020/02/23
**/
@RestController
public class MyBatisController {
@Autowired
private StudentService studentService;
@GetMapping("/boot/students")
public Object students(){
return studentService.getAllStudent();
}
}
编译成功后:运行网页报错,在控制台可能会出现如下错误信息
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.mapper.StudentModelMapper.selectAllStudent
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:225) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65) ~[mybatis-3.4.5.jar:3.4.5]
大意就是找不到StudentModelMapper.selectAllStudent方法,但是方法我们已经写了,这里找不到是因为没有将xml文件编译到class下,这时需要在pom文件中<bulid>标签中加入如下配置:
<resources>
<!--表示将src/main/java下的xml也编译成class文件-->
<!--使用mybatis时会使用到,mybatis下的查询语句有xml文件-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--表示将src/main/resources下的所有文件编译到class-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<!--必须添加,表示src/main/webapp下的页面,编译之后要到META-INF/resources目录下-->
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
为了防止以后发现类似错误,在pom文件中这个配置最好都写入,再次运行程序成功,
整体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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>springmybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<!--maven项目转springboot项目,增加这两项配置-start-->
<!--继承Springboot的父级项目的依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.12.RELEASE</version>
<relativePath/>
</parent>
<!--属性配置-->
<properties>
<java.version>1.8</java.version>
</properties>
<!--maven项目转springboot项目,增加这两项配置-end-->
<dependencies>
<!--srpingboot 开发web项目的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--加载mybatis整合springboot-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--mysql的jdbc驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis 自动生成插件-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
<!-- lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- logback 依赖-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<!--Slf4j 依赖-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--springboot提供的项目编译打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--mybatis代码自动生成插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
<resources>
<!--表示将src/main/java下的xml也编译成class文件-->
<!--使用mybatis时会使用到,mybatis下的查询语句有xml文件-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--表示将src/main/resources下的所有文件编译到class-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<!--必须添加,表示src/main/webapp下的页面,编译之后要到META-INF/resources目录下-->
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
</project>
相关资料:
https://blog.csdn.net/chenjin_chenjin/article/details/79525996
https://blog.csdn.net/chenjin_chenjin/article/details/79525996