整体思路和http://www.cnblogs.com/mahuan2/p/5859921.html相同。

主要讲maven的pom.xml和一些配置变化,详细说明。

软件简介

Spring是一个流行的控制反转(IoC)和面向切面(AOP)的容器框架,在java webapp开发中使用广泛。http://projects.spring.io/spring-framework/

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。http://projects.spring.io/spring-boot/

MyBatis是一个基于Java的数据持久层框架,其原名是iBatis,在升级到3.0版本后,更名为MyBatis。https://github.com/mybatis/mybatis-3/

MyBatis Generator是一个MyBatis的代码生成器,通过配置,可自动生成数据操作接口、实体类以及mapper.xml文件。https://github.com/mybatis/generator

maven开发环境搭建

可以使用http://start.spring.io/初始化maven工程。

使用eclipseIDE,新建maven工程。

在pom.xml文件中,添加如下内容,引入相关jar。mybatis-generator版本是1.3.5。

<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.founder</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>first</name>
<description>Demo project for Spring Boot</description> <!-- lookup parent from repository -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath />
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator</artifactId>
<version>1.3.5</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>

工程目录结构

数据库建表,生成mybatis的代码

为了生成mybatis的代码,首先需要创建一个配置文件,告诉mybatis generator必须的变量。

配置文件保存在src/main/conf/build-mybatis.xml中。

具体配置信息参考官网http://www.mybatis.org/generator/index.html。

注意,配置文件中,添加了一个plugin,这是为生成分页操作添加的,具体内容,后面会讲解。

table中的tableName设置为%,意味着为mysql数据库中的所有表生成对应的代码文件。

mysql中表明使用“_”或者“-”分隔,自动生成的代码文件名中会去掉,并且其后面的字母会升级为大写。

指定好生成的代码文件的保存地址,共有三个。

<?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="C:\Users\mahuan\.m2\repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar" />
<context
id="MySQL2Tables"
targetRuntime="MyBatis3"
defaultModelType="flat">
<plugin type="com.founder.springboot.utils.mybatis.MySQLPaginationPlugin" />
<commentGenerator>
<property
name="suppressDate"
value="true" />
<property
name="suppressAllComments"
value="true" />
</commentGenerator>
<!--数据库链接地址账号密码 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://172.19.34.114:3306/founder_ali?useSSL=false"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver>
<property
name="forceBigDecimals"
value="false" />
</javaTypeResolver>
<!--生成Model类存放位置 -->
<javaModelGenerator
targetPackage="com.founder.springboot.model.database"
targetProject="src/main/java">
<property
name="enableSubPackages"
value="true" />
<property
name="trimStrings"
value="true" />
</javaModelGenerator>
<!--生成映射文件存放位置 -->
<sqlMapGenerator
targetPackage="com.founder.springboot.mapper.database"
targetProject="src/main/java">
<property
name="enableSubPackages"
value="true" />
</sqlMapGenerator>
<!--生成Dao类存放位置 -->
<javaClientGenerator
type="XMLMAPPER"
targetPackage="com.founder.springboot.mapper.database"
targetProject="src/main/java">
<property
name="enableSubPackages"
value="true" />
</javaClientGenerator>
<!--生成对应表及类名 -->
<table
tableName="%"
enableCountByExample="true"
enableUpdateByExample="true"
enableDeleteByExample="true"
enableSelectByExample="true"
selectByExampleQueryId="true">
<property
name="useActualColumnNames"
value="true" />
<!-- 需要插入时返回主键值,请将此属性打开,column值为主键的列明
<generatedKey column="taskId" sqlStatement="MySql" identity="true" />
-->
</table>
</context>
</generatorConfiguration>

编写代码生成脚本

package com.founder.springboot.utils.mybatis;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback; public class MyBatisGeneratorTool {
public static void main(String[] args) {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
String genCfg = "build-mybatis.xml";
File configFile = new File(MyBatisGeneratorTool.class.getResource(genCfg).getFile());
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
try {
config = cp.parseConfiguration(configFile);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = null;
try {
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
try {
myBatisGenerator.generate(null);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

分页插件代码

package com.founder.ebd.util.mybatis;

import java.util.List;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.Plugin;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement; public class MySQLPaginationPlugin extends PluginAdapter { @Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
// add field, getter, setter for limit clause
addLimit(topLevelClass, introspectedTable, "limitStart");
addLimit(topLevelClass, introspectedTable, "count");
// add the method that get the only Criteria
addCriteriaGetter(topLevelClass, introspectedTable);
return super.modelExampleClassGenerated(topLevelClass, introspectedTable);
} @Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$
isNotNullElement.addAttribute(new Attribute("test", "limitStart != null and limitStart >= 0")); //$NON-NLS-1$ //$NON-NLS-2$
isNotNullElement.addElement(new TextElement("limit ${limitStart} , ${count}"));
element.addElement(isNotNullElement);
return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
} @Override
public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$
isNotNullElement.addAttribute(new Attribute("test", "limitStart != null and limitStart >= 0")); //$NON-NLS-1$ //$NON-NLS-2$
isNotNullElement.addElement(new TextElement("limit ${limitStart} , ${count}"));
element.addElement(isNotNullElement);
return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
} @Override
public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
IntrospectedTable introspectedTable, Plugin.ModelClassType modelClassType) { return super.modelGetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, modelClassType);
} private void addLimit(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) {
CommentGenerator commentGenerator = context.getCommentGenerator();
Field field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
field.setType(FullyQualifiedJavaType.getIntInstance());
field.setName(name);
field.setInitializationString("-1");
commentGenerator.addFieldComment(field, introspectedTable);
topLevelClass.addField(field);
char c = name.charAt(0);
String camel = Character.toUpperCase(c) + name.substring(1);
Method method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("set" + camel);
method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), name));
method.addBodyLine("this." + name + "=" + name + ";");
commentGenerator.addGeneralMethodComment(method, introspectedTable);
topLevelClass.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(FullyQualifiedJavaType.getIntInstance());
method.setName("get" + camel);
method.addBodyLine("return " + name + ";");
commentGenerator.addGeneralMethodComment(method, introspectedTable);
topLevelClass.addMethod(method);
} private void addCriteriaGetter(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
CommentGenerator commentGenerator = context.getCommentGenerator();
Method method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("getCriteria");
method.setReturnType(new FullyQualifiedJavaType("Criteria"));
method.addBodyLine("if (oredCriteria.size() != 0) {return oredCriteria.get(0);}");
method.addBodyLine("Criteria criteria = createCriteriaInternal();");
method.addBodyLine("oredCriteria.add(criteria);");
method.addBodyLine("return criteria;");
commentGenerator.addGeneralMethodComment(method, introspectedTable);
topLevelClass.addMethod(method);
} @Override
public boolean validate(List<String> arg0) {
// TODO Auto-generated method stub
return true;
}
}

SpringBoot配置

在src/main/resources/application.properties中增加mybatis配置和jdbc配置。表示mybatis的配置文件未知,以及mapper的xml文件位置。

mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:com/founder/springboot/mapper/database/*.xml spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://172.19.34.114:3306/founder_ali?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 123456

在application启动类中,增加注解,表示何处扫描mapper接口。

package com.founder.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan("com.founder.springboot.mapper.database")
public class FirstApplication { public static void main(String[] args) {
SpringApplication.run(FirstApplication.class, args);
}
}

至此,mybatis配置完成。

springboot application.properties配置可参考官方文档

测试生成的代码

ExampleService

package com.founder.springboot.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.founder.springboot.mapper.database.SysUserMapper;
import com.founder.springboot.model.database.SysUser;
import com.founder.springboot.model.database.SysUserExample; @Service
public class ExampleService { @Autowired
SysUserMapper sysUser; public List<SysUser> get() {
SysUserExample example = new SysUserExample();
example.setLimitStart(0);
example.setCount(10);
return sysUser.selectByExample(example);
}
}

ExampleController

package com.founder.springboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.founder.springboot.model.database.SysUser;
import com.founder.springboot.service.ExampleService; @RestController
public class ExampleController { @Autowired
private ExampleService service; @RequestMapping(value = "/get", produces = "application/json; charset=UTF-8")
public List<SysUser> get() {
return service.get();
}
}

测试类

package com.founder.springboot.controller;

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.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import com.founder.springboot.FirstApplication; @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = FirstApplication.class)
@WebAppConfiguration
public class ExampleControllerTest {
@Autowired
private ExampleController controller; @Test
public void get() throws Exception {
System.out.println(controller.get());
}
}

SpringBoot打包运行

使用maven命令

mvn clean package

可将springboot工程,打包为一个可运行的jar包。

注意:mapper的xml文件在src/main/java目录中,maven默认是不编译的,导致jar中缺少xml配置文件。需修改pom.xml配置,初始pom.xml已修改。

springboot和mybatis集成,自动生成model、mapper,增加mybatis分页功能的更多相关文章

  1. Java MyBatis逆向工程,自动生成pojo,mapper

    生成xml文件,文件名generator.xml <?xml version="1.0" encoding="UTF-8"?><!DOCTYP ...

  2. Mybatis Generator自动生成的mapper只有insert方法

    – Mybatis Generator 生成的mapper只有insert方法 – 首先检查generatorConfig.xml中table项中的属性 enableSelectByPrimaryKe ...

  3. 使用Generator 自动生成 model mapper mapping 文件

    1.下载包 地址http://download.csdn.net/detail/u012909091/7206091 2.下载完成解压文件到任意目录 3.删除下mybatis-generator-co ...

  4. MyBatis 使用Generator自动生成Model , Dao, mapper

    最近   我新建了一 个maven 项目,使用的是spring + springmvc + mybatis框架. 听说Mybatis可以自动生成model和mapper以及dao层,我就从网上查了查资 ...

  5. Springboot 系列(十一)使用 Mybatis(自动生成插件) 访问数据库

    1. Springboot mybatis 介绍 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数获取 ...

  6. IDEA Maven Mybatis generator 自动生成代码(实例讲解)(转)

    IDEA Maven Mybatis generator 自动生成代码(实例讲解) MyBatis Generator • 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的 ...

  7. Springboot mybatis generate 自动生成实体类和Mapper

    https://github.com/JasmineQian/SpringDemo_2019/tree/master/mybatis Springboot让java开发变得方便,Springboot中 ...

  8. mybatis自动生成model、dao及对应的mapper.xml文件

    背景: 日常开发中,如果新建表,手动敲写model.dao和对应的mapper.xml文件,费时费力且容易出错, 所以采用mybatis自动生成model.dao及对应的mapper.xml文件.代码 ...

  9. SpringBoot 添加mybatis generator 自动生成代码插件

    自动生成数据层代码,提高开发效率 1.pom添加插件,并指定配置文件路径 <!-- mybatis generator 自动生成代码插件 --> <plugin> <gr ...

  10. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

随机推荐

  1. scrapy 爬取天猫商品信息

    spider # -*- coding: utf-8 -*- from urllib.parse import urlencode import requests import scrapy impo ...

  2. luoguP2982 [USACO10FEB]慢下来Slowing down

    https://www.luogu.org/problemnew/show/P2982 这题你写个树剖当然可以做,但是我们还有一种更简单的方法,使用 dfs 序 + 树状数组即可 考虑一只牛到了自己的 ...

  3. 洛谷P3356 火星探险问题(费用流)

    传送门 和深海机器人问题差不多……看到有的大佬是用dp过的,强无敌…… 考虑一下,把每一个点拆点,分别是$A_i$和$B_i$,连一条容量为$inf$,费用为$0$的边,表示可以随便走.如果有石头,再 ...

  4. 自动备份数据库crond

    #!/bin/bash # export and backup the abgent_web database.sql mysqldump -uuser -ppassword ltden_db --s ...

  5. CI框架部署后访问出现404

    昨天新配置了一个PHP集成开发环境,安装完后,把项目放到Apache服务器的www目录下,发现只能打开首页,其他页面全部无法打开,当时比较纳闷,以为是服务器没有配置好,测试了一下,发现环境配置没有问题 ...

  6. c语言数据结构学习心得——队列

    队列 只允许在一端进行插入,在另一端进行删除的线性表 队头(Front):允许删除的一端(队首) 队尾(Rear):允许插入的一端 FIFO:先进先出 不要求从数组首位开始存储队列 #define M ...

  7. 搭建git服务器(临时服务器,命令行形式,针对2到5人左右,轻量)

    服务端配置 ############################################################################################## ...

  8. C++_IO与文件2-用cout进行输出

    C++将输出流看作是字节流,在程序中,很多数据被组织成比字节更大的单位. 例如int类型由16位或者32位的二进制值表示:double值由64位的二进制数据表示: 但是在将字节流发送给屏幕时,希望每个 ...

  9. AJAX概述和简单使用

    一.ajax概述: asynchronous javascript and xml ,用于异步的向服务器发出请求,接收数据的 一种技术. 在整个过程中:页面无刷新,不打断用户的操作: 按需要获取数据, ...

  10. 【字符串】【hash】【倍增】洛谷 P3502 [POI2010]CHO-Hamsters 题解

        这是一道字符串建模+图论的问题. 题目描述 Byteasar breeds hamsters. Each hamster has a unique name, consisting of lo ...