您的位置:首页 > 编程语言 > Java开发

用mybatis generator在springboot中快速搭建Mybaits框架(Spring版图文教程)

2019-01-08 10:34 337 查看

在前一个版本中,写了如何用mybatis generator快速搭建Mybaits。https://blog.csdn.net/shang1989/article/details/86014873
但是这个版本其实没有用到springboot的特性,这个版本补上,其实只是简单的配置就可以了。

文章目录

系统环境

  • Intellj 2018
  • Java 1.8
  • Springboot 2.1.1 Release
  • Maven 3.3.9
  • mybatis-generator-maven-plugin 1.3.7

构建步骤

1. 初始化工程


这里用springboot自带的模板初始化新的工程


填入maven的工程信息


勾选springboot版本,并且选择JDBC和MyBatis。 下一步

点击finish,创建工程


这是现在的目录结构,pom文件里写入了基本的依赖。但是还不够,还需要我们进行配置

2. pom文件配置

  1. 加入mysql jdbc驱动依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
  1. 配置mybatis-generator-maven-plugin插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
</dependencies>
</plugin>

3. 创建generatorConfig.xml文件

在resource目录下创建generatorConfig.xml, 这个文件是mybatis-generator-maven-plugin所需要的。

<?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>
<!-- 引入配置文件 -->
<properties resource="application.properties"/>

<context id="test" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
<commentGenerator>
<!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
<!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
<property name="suppressDate" value="true" />
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false" />
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="${spring.datasource.driver-class-name}" connectionURL="${spring.datasource.url}" userId="${spring.datasource.username}" password="${spring.datasource.password}">
<!-- 这里面可以设置property属性,每一个property属性都设置到配置的Driver上 -->
</jdbcConnection>
<javaTypeResolver>
<!-- This property is used to specify whether MyBatis Generator should
force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.chaincomp.quickmybaits.model" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="com.chaincomp.quickmybaits.mapper" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.chaincomp.quickmybaits.mapper" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>

<!-- 要生成哪些表 -->
<table tableName="testuser" domainObjectName="SysUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />

</context>
</generatorConfiguration>

4. 添加数据库连接信息

为了便于管理,数据的连接信息写在了application.properties文件里面。

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/yourMysqlDbName
spring.datasource.username=root
spring.datasource.password=root

5. 运行maven插件,构建工程结构

这里用了inteljj内建的maven, 运行这个mybatis-generator:generate,或者也可以在命令行中运行

mvn mybatis-generator:generate


这时候的目录结构是这样的。model下面的SysUser是自动通过mysql中的表结构生成的。这个功能很有用,因为很多时候是先有表结构再开始写代码的,这个功能可以省去很多写model的工作。

6. 运行mybatis

现在只是有了基本的目录结构,mybatis还不能跑起来。下一步是把mybatis跟spring结合起来。

这里要使用@MapperScan注解,把这个注解加在spring的配置类上

@MapperScan("com.chaincomp.quickmybaits.mapper")
package com.chaincomp.quickmybaits;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
@MapperScan("com.chaincomp.quickmybaits.mapper")public class QuickmybaitsApplication {

public static void main(String[] args) {

ApplicationContext context = SpringApplication.run(QuickmybaitsApplication.class, args);

}
}

然后编写一个test类做测试

package com.chaincomp.quickmybaits;

import com.chaincomp.quickmybaits.mapper.SysUserMapper;
import com.chaincomp.quickmybaits.model.SysUser;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class QuickmybaitsApplicationTests {

@Autowired
SysUserMapper mapper;

@Test
public void contextLoads() {
SysUser sysUser = new SysUser();
sysUser.setAge(15);
sysUser.setGender("male");

sysUser.setId(12);
mapper.insert(sysUser);
}

}

运行这个test类,执行成功之后数据就插入进去了。

文章中的代码在github上:https://github.com/shang1989/quickMybatis/tree/spring

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: