springboot+mybatis+maven的配置过程,废话不多说,直接看过程。
1、新建maven项目,+mysql+mybatis等框架
2、resouce目录下新增目录-generator,目录下新增文件-generatorConfig.xml
3、 然后pom文件加入依赖-注1个dependency和1个plugin:如下
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<plugin>
<!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>---/resources/generator/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
<configurationFile>这个配置项要写generatorConfig.xml的路径。
4、generator.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>
<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
<classPathEntry location="/code/aptp/web/src/main/java/lib/mysql-connector-java-5.1.29-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/test"
userId="root" password="">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 实体类生成的位置-->
<javaModelGenerator targetPackage="autopay.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- *Mapper.xml 文件的位置 -->
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- Mapper 接口文件的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="autopay.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="t_project" domainObjectName="ProjectManage" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<!--<table tableName="t_api_manage" domainObjectName="ApiManage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
<!--<table tableName="t_case" domainObjectName="CaseManage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
</context>
</generatorConfiguration>
***
<table 只写本次要生成的sql。已经生成或者不用的注释掉。
5、application.properties--项目的配置文件配置数据库和mybatis的
mapper路径(不然mapper会扫不到)
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.poolSize=5
spring.datasource.maxPoolSize=20
mybatis.mapper-locations=classpath:mapping/*.xml
mybatis.type-aliases-package=autopay.model
6、maven-plugins选择generator的插件,点击,就可以生成sql语句
或者在run/debug的配置中,新增maven-generator的配置,命令:mybaits-generator:generate -e 也行
image.png
到此为止是整个配置过程,允许后,可以看下项目目录,mapper、dao实体、xml文件都已经生成了。
7、来看下生成的xml文件-也就是有sql语句的文件,一般默认会生成insert和insertSelective两个。这俩有啥区别呢?如果有非必填的插入行,后者更方便
-具体的内容不copy了,因为是自动生成了,这里主要说下常见的几个坑。
- ID自增的,配置如下:--当然语句关于ID部分也要删掉
<insert id="insert" parameterType="autopay.model.ApiManage" useGeneratedKeys="true" keyProperty="id">
<selectKey keyProperty="id" resultType="INTEGER">
select LAST_INSERT_ID()
</selectKey>
- 关于resultType="INTEGER",mybatis是米有int类型的!
- resultType如要要返回实体类型,这里直接赋值实体类名
那怎么调用呢。也很简单。正常写就行了。贴个test代码
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApiManageMapperTest{
@Resource
ApiManageMapper apiManageMapper;
ApiManage apiManage;
@Test
public void findApilists(){
System.out.println("");
}
@Test
public void insert() {
apiManage = new ApiManage();
// apiManage.setXX--代码省略
apiManageMapper.insertSelective(apiManage);
}
}
mybatis的简单使用过程就结束了,本人小白一枚,很多信息也都来源于网络--在此谢过默默贡献的开发童鞋们。文中如有疑问欢迎留言,拙文一篇,希望对你有帮助。谢谢。