码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot Mybatis整合

时间:2018-06-20 12:54:27      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:UI   1.2   character   scope   sources   指标   class   for   spl   

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

特点

1. 创建独立的Spring应用程序
2. 嵌入的Tomcat,无需部署WAR文件
3. 简化Maven配置
4. 自动配置Spring
5. 提供生产就绪型功能,如指标,健康检查和外部配置
6. 绝对没有代码生成和对XML没有要求配置 

环境搭建

用IDEA创建一个新项目File-->New Project-->Spring Initializr

技术分享图片

Next-->填写Group和Artifact

技术分享图片

Next-->选择项目需要的依赖

技术分享图片技术分享图片

 

Next-->填写完项目名称和选择项目位置之后点击Finish

技术分享图片

等待编译完成,运行,出现如下图说明搭建成功

技术分享图片

完善一下目录结构:

技术分享图片

创建一个Hello World Controller作为项目的入口

1 @Controller
2 @EnableAutoConfiguration
3 public class HelloWorld {
4     @RequestMapping("/")
5     @ResponseBody
6     String home(){
7         return "Hello World!";
8     }
9 }

运行http://localhost:8080/出现

技术分享图片

JDBC连接数据库

项目不使用application.properties文件 而使用更加简洁的application.yml文件: 
将原有的resource文件夹下的application.properties文件删除,创建一个新的application.yml配置文件, 
文件的内容如下:

技术分享图片
server:
    port: 9091
spring:
    datasource:
        url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&characterEncoding=UTF-8
        password: root
        username: root
        driver-class-name: com.mysql.jdbc.Driver
        type: com.alibaba.druid.pool.DruidDataSource
mybatis:
    type-aliases-package: com.example.demo.model
    mapper-locations: classpath:mappers/*.xml
application.yml

pom.xml

技术分享图片
<?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.example</groupId>
    <artifactId>springbootdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springbootdemo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.8</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--Mybatis逆向生成 -->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

        </plugins>
    </build>


</project>
pom.xml

使用mybatis generator 自动生成代码:/src/main/resources/generator/generatorConfig.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>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/test?useSSL=false&amp;characterEncoding=UTF-8"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <!-- 实体类生成位置 -->
        <javaModelGenerator targetPackage="com.example.demo.model" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- Mappers 文件生成位置 -->
        <sqlMapGenerator targetPackage="mappers"  targetProject=".\src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 接口生成位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper"  targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 生成表目录 -->

        <table tableName="ie_green_indexsystemtable" schema="ie_green_indexsystemtable" domainObjectName="GreenIndexSystem" enableUpdateByExample="false" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByPrimaryKey="false"
        enableDeleteByPrimaryKey="false" enableInsert="false">
        </table>
    </context>
</generatorConfiguration>
generatorConfig.xml

Mybatisgenerator

技术分享图片
public class MybatisGenerator {
    public static void main(String[] args) throws Exception{
        generator("D:\\springbootdemo\\src\\main\\resources\\generator\\generatorConfig.xml");
    }

    public static void generator(String path) throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File(path);
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
}
MybatisGenerator

运行Mybatisgenerator

技术分享图片

 

Spring Boot Mybatis整合

标签:UI   1.2   character   scope   sources   指标   class   for   spl   

原文地址:https://www.cnblogs.com/zqyw/p/9200543.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!