zoukankan      html  css  js  c++  java
  • springboot学习(一)

    最近想学习springboot所以在网上找了很多文章参考怎么构建springboot项目以及集成mybatis

      集成mybatis的部分参考了这两篇文章

      https://blog.csdn.net/travellersY/article/details/78620247?utm_source=blogxgwz12

      https://blog.csdn.net/sufu1065/article/details/82905892?utm_source=blogxgwz21

    1.首先通过idea创建springboot项目

    2.构建项目文件夹

     

    3.添加依赖

    <plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
    <executions>
    <execution>
    <id>Generate MyBatis Artifacts</id>
    <phase>deploy</phase>
    <goals>
    <goal>generate</goal>
    </goals>
    </execution>
    </executions>
    <configuration>
    <!-- generator 工具配置文件的位置 -->
    <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
    <verbose>true</verbose>
    <overwrite>true</overwrite>
    </configuration>
    <dependencies>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
    </dependency>
    <dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.7</version>
    </dependency>
    </dependencies>
    </plugin>

    4.配置文件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>
    <!-- 引入配置文件 -->
    <properties resource="mybatis-generator/mybatisGeneratorinit.properties"/>

    <!-- 一个数据库一个context -->
    <!--defaultModelType="flat" 大数据字段,不分表 -->
    <context id="MysqlTables" targetRuntime="MyBatis3Simple" defaultModelType="flat">
    <!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;
    一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖 -->
    <property name="autoDelimitKeywords" value="true" />
    <!-- 生成的Java文件的编码 -->
    <property name="javaFileEncoding" value="utf-8" />
    <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->
    <property name="beginningDelimiter" value="`" />
    <property name="endingDelimiter" value="`" />

    <!-- 格式化java代码 -->
    <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
    <!-- 格式化XML代码 -->
    <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
    <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

    <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />

    <!-- 注释 -->
    <commentGenerator >
    <property name="suppressAllComments" value="false"/><!-- 是否取消注释 -->
    <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
    </commentGenerator>

    <!-- jdbc连接 -->
    <jdbcConnection driverClass="${jdbc_driver}" connectionURL="${jdbc_url}" userId="${jdbc_user}" password="${jdbc_password}" />
    <!-- 类型转换 -->
    <javaTypeResolver>
    <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
    <property name="forceBigDecimals" value="false"/>
    </javaTypeResolver>

    <!-- 生成实体类地址 -->
    <javaModelGenerator targetPackage="com.zhh.model" targetProject="${project}" >
    <property name="enableSubPackages" value="false"/>
    <property name="trimStrings" value="true"/>
    </javaModelGenerator>
    <!-- 生成mapxml文件 -->
    <sqlMapGenerator targetPackage="mappers" targetProject="${resources}" >
    <property name="enableSubPackages" value="false" />
    </sqlMapGenerator>
    <!-- 生成mapxml对应client,也就是接口dao -->
    <javaClientGenerator targetPackage="com.zhh.dao" targetProject="${project}" type="XMLMAPPER" >
    <property name="enableSubPackages" value="false" />
    </javaClientGenerator>
    <!-- table可以有多个,每个数据库中的表都可以写一个table,tableName表示要匹配的数据库表,也可以在tableName属性中通过使用%通配符来匹配所有数据库表,只有匹配的表才会自动生成文件 -->
    <table tableName="student" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
    <property name="useActualColumnNames" value="false" />
    <!-- 数据库表主键 -->
    <generatedKey column="id" sqlStatement="Mysql" identity="true" />
    </table>
    </context>
    </generatorConfiguration>
    配置文件mybatisGeneratorinit.properties
    #Mybatis Generator configuration
    #dao类和实体类的位置
    project =src/main/java
    #mapper文件的位置
    resources=src/main/resources
    #根据数据库中的表生成对应的pojo类、dao、mapper
    jdbc_driver =com.mysql.jdbc.Driver
    jdbc_url=jdbc:mysql://localhost:3306/test
    jdbc_user=root
    jdbc_password=root
    使用插件

    这里右键maven build...

    生成成功后

    修改Service

       

    添加StudentServiceImpl

    运行之后路径

     http://localhost:8080/student

    效果



    这里还有一个好用的idea辅助插件Free MyBatis plugin
    安装步骤
        点击file
        setting
        

    输入插件名字

    Free MyBatis plugin安装

    效果

    
    
    可以点击箭头直接从dao层去到xml对应的方法


  • 相关阅读:
    终端解析 规格严格
    用HQ时,出现了一个Bug 规格严格
    GC这篇文章介绍得很基本 规格严格
    DB2协议研究 规格严格
    JVM monitoring 规格严格
    很有趣的GC内容 规格严格
    在linux内核中修改TCP MSS值 规格严格
    最近为项目写了一个调试模块,由此想到的一些内容 规格严格
    Java中国象棋博弈程序探秘[3]——着法合法性判断
    NetBeans 时事通讯(刊号 # 10 Jun 03, 2008)
  • 原文地址:https://www.cnblogs.com/StudyZhh/p/9835675.html
Copyright © 2011-2022 走看看