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 的简单使用过程就结束了, 本人小白一枚, 很多信息也都来源于网络 -- 在此谢过默默贡献的开发童鞋们. 文中如有疑问欢迎留言, 拙文一篇, 希望对你有帮助. 谢谢.
来源: http://www.jianshu.com/p/b77145a502b5