Spring boot 集成 MyBatis
一、idea 创建 Spring boot 工程
最终的结构
二、加入依赖
pom.xml 如下
<!-- web starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- test starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mybatis starter-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--mybatis 整合Spring的包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.0</version>
</dependency>
<!--数据库连接的 包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--这个版本原来是不写的,但是后面出现了一个错误,所以用低版本的 连接jar,所以改用 5.1.38 -->
<version>5.1.38</version>
</dependency>
三、创建 application.properties
application.properties 如下
mybatis.mapper.location=classpath:com/htb/springbootmybatis/mapper
#把xxx改成自己的信息
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:330xx/xxxx?useUnicode=true&characherEncoding=utf8&useSSL=false
#正确的写法
spring.datasource.username=xxxx
spring.datasource.password=xxxxx
#导致错误的写法
#spring.datasource.data-password=xxx
#spring.datasource.data-username=xxxx
四、利用 mybatis generator生成 mapper和实体类
有几种方式,参考网址
http://mybatis.org/generator/index.html
方式一、利用maven插件的方式
1)添加一个 generatormapper配置文件
2)generatormapper.xml 内容
<?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>
<!-- 指定 数据库连接的 jar的位置,需要确保指定的位置里面有这个 jar-->
<classPathEntry location="D:/software/maven/localwarehouse/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar" />
<context id="tables" targetRuntime="MyBatis3">
<!-- true 表示 不生成注释-->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--设置 数据库的连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:xxxx/xxxx"
userId="xxxx"
password="xxxxxxx">
</jdbcConnection>
<!--设置生成的model的位置 -->
<javaModelGenerator targetPackage="com.htb.springboot.model" targetProject="src/main/java">
<!-- 是否启用子包-->
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="false" />
</javaModelGenerator>
<!--设置生成的 mapper 文件的位置 -->
<sqlMapGenerator targetPackage="com.htb.springboot.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!--设置生成的 mapper 接口的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.htb.springboot.mapper" targetProject="src/main/java">
<!-- 是否启用子包-->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 需要生成的 表格-->
<table tableName="student"
domainObjectName="Student"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
/>
</context>
</generatorConfiguration>
3)在pom 文件里面 加一个插件
官网示例:
example如下:下面的意思 是加一个 新的插件 mybatis.generator
<project ...>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
</plugin>
...
</plugins>
...
</build>
...
</project>
我的文件
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<configurationFile>generatormapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
4) 点击 maven选项
5) 生成之后的文件目录
方式二、利用java
1)添加一个 generatormapper配置文件
2)内容同上
需要导入一个 jar包
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
3)写一个 java 类
内容参考如下
http://mybatis.org/generator/running/runningWithJava.html
我的java 类
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class MBGTest {
public static void main(String[] args) throws InvalidConfigurationException, IOException, XMLParserException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generatormapper.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
五、写controller、Service、ServiceImpl、Mapper、Mapper.xml、Bean
1)写Springboot的主类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.htb.springbootmybatis.mapper")
public class SpringbootmybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootmybatisApplication.class, args);
}
}
2)controller
import com.htb.springbootmybatis.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @description: Student 的控制层
* @author: htb
* @createDate: 2019/12/5
* @version: 1.0
*/
@RestController
public class StudentCtrl {
@Autowired
private StudentService studentService;
@RequestMapping("/springboot/getAllStudent")
public Object getAllStudent(){
return studentService.getAllStudent();
}
}
3)Service
import com.htb.springbootmybatis.model.Student;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @description: Student 的 Service层
* @author: htb
* @createDate: 2019/12/5
* @version: 1.0
*/
@Service
public interface StudentService {
public List<Student> getAllStudent();
}
4)ServiceImpl
/**
* @description:
* @author: htb
* @createDate: 2019/12/5
* @version: 1.0
*/
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> getAllStudent() {
return studentMapper.getAllStudent();
}
}
5)Mapper
import java.util.List;
public interface StudentMapper {
//目前用到的查询所有的学生的信息的接口
List<Student> getAllStudent();
}
6)Mapper.xml
<?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.htb.springbootmybatis.mapper.StudentMapper">
<resultMap id="BaseResultMap" type="com.htb.springbootmybatis.model.Student">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
</resultMap>
<sql id="Base_Column_List">
id, name
</sql>
<select id="getAllStudent" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from student
</select>
</mapper>
7)Bean
import org.springframework.stereotype.Repository;
@Repository
public class Student {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
六、验证,调试
ERROR
java.sql.SQLException: Access denied for user ‘’@‘localhost’ (using password: NO)
报这个错误的原因
application.properties 配置文件中
可能是和数据库驱动的版本有关
<!--数据库连接的 包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
解决错误之后
http://localhost:8080/springboot/getAllStudent
postman 测试的结果
整合完成