Springboot集成MyBatis逆向工程
本次主要说明mybatis逆向工程的创建以及集成springboot
逆向概念
- 以前做开发的时候,mapper文件和实体类都是手写,效率较低且没有什么意义,通过mybatis逆向工程可以一键创建所有需要的实体类和mapper文件。
步骤
新建一个springboot工程
- 打开project Structure,new一个module,选择SpringInitializr,点击next(这里需要联网才能创建)
- 输入自己的group和java版本号
- 选择web后点击springweb,点击next后点击finish后apply完成创建
数据库建表
- sql语句如下:
(
id int(10) not null auto_increment,
name varchar(20) null,
age int(10) null,
constraint PK_T_STUDENT primary key clustered (id)
);
insert into t_student(name,age) values("zhangsan",25);
insert into t_student(name,age) values("lisi",28);
insert into t_student(name,age) values("wangwu",23);
insert into t_student(name,age) values("Tom",21);
insert into t_student(name,age) values("Jck",55);
insert into t_student(name,age) values("Lucy",27);
insert into t_student(name,age) values("zhaoliu",75);
加入插件和依赖
- 在pom文件加入mybatis和mysql依赖和逆向工程的插件
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
</dependencies>
<build>
<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>
</build>
加入逆向配置文件
- 在项目根目录下新建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>
<!-- 指定连接数据库的 JDBC 驱动包所在位置,指定到你本机的完整路径 -->
<classPathEntry location="C:\Java\maven\maven_repository\mysql\mysql-connector-java\5.1.32\mysql-connector-java-5.1.32.jar"/>
<!-- 配置 table 表信息内容体,targetRuntime 指定采用 MyBatis3 的版本 -->
<context id="tables" targetRuntime="MyBatis3">
<!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL=""
userId=""
password="">
</jdbcConnection>
<!-- 生成 model 类,targetPackage 指定 model 类的包名, targetProject 指定
生成的 model 放在 eclipse 的哪个工程下面-->
<javaModelGenerator targetPackage="cn.lstf666.springboot.model"
targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="false" />
</javaModelGenerator>
<!-- 生成 MyBatis 的 Mapper.xml 文件,targetPackage 指定 mapper.xml 文件的
包名, targetProject 指定生成的 mapper.xml 放在 eclipse 的哪个工程下面 -->
<sqlMapGenerator targetPackage="cn.lstf666.springboot.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成 MyBatis 的 Mapper 接口类文件,targetPackage 指定 Mapper 接口类的包
名, targetProject 指定生成的 Mapper 接口放在 eclipse 的哪个工程下面 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="cn.lstf666.springboot.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 数据库表名及对应的 Java 模型类名 -->
<table tableName="t_student" domainObjectName="Student"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
运行插件
创建完成
- 可以看到所有的实体类和mapper已经创建完成
测试
- 分别创建Controller、Service类
- Controller
@RestController
public class indexController {
@Autowired
private StudentService studentService;
@RequestMapping("/getS")
public Student getStudent(Integer id){
Student student = studentService.getStudentById(id);
return student;
}
}
- Service
public interface StudentService {
Student getStudentById(Integer id);
}
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public Student getStudentById(Integer id) {
Student student = studentMapper.selectByPrimaryKey(id);
return student;
}
}
* mapper上加上注解
@Mapper
public interface StudentMapper {
int deleteByPrimaryKey(Integer id);
int insert(Student record);
int insertSelective(Student record);
Student selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Student record);
int updateByPrimaryKey(Student record);
}
* 启动类加上mapperScan注解
@SpringBootApplication
@MapperScan(basePackages = "cn.lstf666.mapper")
public class MybatisdemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisdemoApplication.class, args);
}
}
-
配置数据库连接信息
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc: spring.datasource.username= spring.datasource.password=
-
配置xml的映射位置
- 因为框架规定了mapper与xml必须在同级目录下,而java包下只有.java文件可以被编译到targets目录下,所以需要手动指定xml的映射位置。
- 打开pom文件,build标签下加入以下代码
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources>
-
启动