用了一下午和半个晚上才在Springboot中使用Mybatis成功连接MySql。百度上众多纷纭,一人一个做法,被安排了。。。出于此我写下最终成功的方法和配置。
1.新建Springboot Project 在web中勾选web初次不建议全选中(即快速创建Spring Boot项目)
2.在pox.xml中配置mysql-connector和 mybatis
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
3.在src/main/resources/application.properties中配置数据库连接信息
#配置mapper所在地址
mybatis.mapper-locations=classpath:/com/wayy/mybatis/mapper/*.xml
#配置数据库连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/questions(数据库名)?useUnicode=true&characterEncoding=UTF-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=MySql密码
*4.新建GeneratorMapper.xml配置 蓝色为需要自动生成的bean
<?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驱动包所在的位置-->
<classPathEntry location="E:/JAVA WEB/jar包/mysql-connector-java-5.1.26/mysql-connector-java-5.1.26-bin.jar"></classPathEntry>
<context id="tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" /><!-- 是否取消注释 -->
<property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳 -->
</commentGenerator>
<!--此处需要修改-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/questions(数据库名)"
userId="root"
password="MySql密码">
</jdbcConnection>
<!--此处需要修改-->
<!--生成model类 targetPaceage指定model类的包名 targetProject指定生成的model放在哪个工程下-->
<javaModelGenerator targetPackage="com.wayy.mybatis.model" targetProject="src/main/java" >
<property name="enableSubPackages" value="false"></property>
<property name="trimStrings" value="false"></property>
</javaModelGenerator>
<!--此处需要修改-->
<!--生成MyBaits的Mapper.xml文件 targetPackage指定mapper文件的包名 targetProject指定生成的mapper.xml放在哪个工程下-->
<sqlMapGenerator targetPackage="com.wayy.mybatis.mapper" targetProject="src/main/java" >
<property name="enableSubPackages" value="false"></property>
</sqlMapGenerator>
<!--此处需要修改-->
<!--生成MyBatis的Mapper接口类文件 targetPackage指定mapper接口类的包名 targetProject指定生成的mapper接口放在哪个工程下---->
<javaClientGenerator targetPackage="com.wayy.mybatis.mapper" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false"></property>
</javaClientGenerator>
<!--此处需要修改-->
<!--蓝色为需要自动生成的bean-->
<table tableName="User"
domainObjectName="User"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
*5.在pom.xml中配置自动生成代码的插件
<build>
<plugins>
<!--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>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
*6.在IDEA右侧点mAVEN Poeject下的本项目名下的Plugins下的mybatis-generator下的mybatis-generator:generate 双击自动生成代码 控制爱输出build sucess
8.再生产的UserMapper加上@Mapper或者在MapScan()扫描
9.新建Userservice接口 UserServiceImpl
public interface UserService {
public List<User> getAllUser();
}
public interface UserService {
public List<User> getAllUser();
}
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public List<User> getAllUser() {
return userMapper.selectAllUser();
}
}
10.在UserMapper.xml中添加
<select id="selectAllUser" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
</select>
11.新建Controller
(1)@RestController
(2)public class MybatisController {
(3) @Autowired
(4) private UserService userService;
(5) @GetMapping("/getUser")
(6) public List<User> users(){
(7) return userService.getAllUser();
(8) }
(9)}
12.运行主函数 访问页面http://localhost:8080/getUser
可能会报错 报错原因:Mapper包下没有mapper.xml
打开pom.xml
<build>
<plugins>
<!--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>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
<!--加入如下-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>
**/*.*
</include>
</includes>
</resource>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>