版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27173485/article/details/88717903
一、数据库
略
二:使用IDEA 创建Spring boot 项目
2.1:选择Spring Initializ(Idea 使用spring assistant 插件可以实现)
下一步后:
点击下一步后,分别在Web选项中勾选Web,在Template Engines选项中勾选 Thymeleaf,在SQL选项中勾选MySQL和MyBatis。然后一直下一步到完成。
三、项目结构为:
四、Gradle文件配置如下:
plugins { id 'org.springframework.boot' version '2.1.3.RELEASE' id 'java' } apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.0' compileOnly 'org.projectlombok:lombok' runtimeOnly 'mysql:mysql-connector-java' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
application.properties 文件配置如下
spring: datasource: url: 'xxx' username: xx password: xx #### 以下配置项无需修改 #### #Fully qualified name of the JDBC driver. Auto-detected based on the URL by default. driver-class-name: com.mysql.jdbc.Driver #Fully qualified name of the connection pool implementation to use. By default, it is auto-detected from the classpath. type: com.zaxxer.hikari.HikariDataSource hikari: auto-commit: false # 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒 connection-timeout: 10000 # 设定连接校验的超时时间, 缺省:5秒 validation-timeout: 5000 connectionTestQuery: SELECT 1 leak-detection-threshold: 10000 # 一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟 idle-timeout: 600000 # 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟 # 建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';) # 当前数据库wait_timeout: 3600秒 max-lifetime: 1800000 # 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count) maximum-pool-size: 10 # 连接池中允许的最大空闲连接数。缺省值:10; minimum-idle: 2 pool-name: HikariConnPool #### MySQL Performance Tips https://github.com/brettwooldridge/HikariCP/wiki/MySQL-Configuration data-source-properties: cachePrepStmts: true prepStmtCacheSize: 250 prepStmtCacheSqlLimit: 2048 useServerPrepStmts: true useLocalSessionState: true rewriteBatchedStatements: true cacheResultSetMetadata: true cacheServerConfiguration: true elideSetAutoCommits: true maintainTimeStats: false
mybatis: mapper-locations: "classpath*:mybatis/**/*.xml"
五、在resources目录下创建mybatis-generator文件夹,并mybatis-generator文件夹中创建generatorConfig.xml文件文件
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> <!--id:必选,上下文id,用于在生成错误时提示--> <context id="mysql" targetRuntime="MyBatis3"> <!-- 生成的Java文件的编码 --> <property name="javaFileEncoding" value="UTF-8"/> <!--<property name="useMapperCommentGenerator" value="false"/>--> <plugin type="xx.common.plugins.LombokPlugin"> <property name="hasLombok" value="true"/> </plugin> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <!--<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>--> <property name="mappers" value="xx.common.base.BaseMapper"/> <property name="caseSensitive" value="true"/> <property name="forceAnnotation" value="true"/> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> </plugin> <!-- 对注释进行控制 --> <commentGenerator> <!-- suppressDate是去掉生成日期那行注释 --> <property name="suppressDate" value="true"/> <!-- suppressAllComments是去掉所有的注解 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--jdbc的数据库连接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="xxx" userId="xxx" password="xxx"> </jdbcConnection> <!-- java类型处理器 用于处理DB中的类型到Java中的类型,默认使用JavaTypeResolverDefaultImpl; 注意一点,默认会先尝试使用Integer,Long,Short等来对应DECIMAL和 NUMERIC数据类型; --> <javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl"> <!-- true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型 false:默认, scale>0;length>18:使用BigDecimal; scale=0;length[10,18]:使用Long; scale=0;length[5,9]:使用Integer; scale=0;length<5:使用Short; --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- java模型创建器,是必须要的元素 负责:1,key类(见context的defaultModelType);2,java类;3,查询类 targetPackage:生成的类要放的包,真实的包受enableSubPackages属性控制; targetProject:目标项目,指定一个存在的目录下,生成的内容会放到指定目录中,如果目录不存在,MBG不会自动建目录 --> <javaModelGenerator targetPackage="xxx.model" targetProject="src/main/java"> <!-- 是否允许子包,即targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="true"/> <!-- 是否对model添加 构造函数 --> <property name="constructorBased" value="false"/> <!-- 是否对类CHAR类型的列的数据进行trim操作 --> <property name="trimStrings" value="true"/> <!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 --> <property name="immutable" value="false"/> </javaModelGenerator> <!-- 生成SQL map的XML文件生成器, 注意,在Mybatis3之后,我们可以使用mapper.xml文件+Mapper接口(或者不用mapper接口), 或者只使用Mapper接口+Annotation, 所以,如果 javaClientGenerator配置中配置了需要生成XML的话,这个元素就必须配置 targetPackage/targetProject:同javaModelGenerator --> <sqlMapGenerator targetPackage="mybatis.generator" targetProject="src/main/resources"> <!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false --> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 对于mybatis来说,即生成Mapper接口,注意,如果没有配置该元素,那么默认不会生成Mapper接口 targetPackage/targetProject:同javaModelGenerator type:选择怎么生成mapper接口(在MyBatis3/MyBatis3Simple下): 1,ANNOTATEDMAPPER:会生成使用Mapper接口+Annotation的方式创建(SQL生成在annotation中),不会生成对应的XML; 2,MIXEDMAPPER:使用混合配置,会生成Mapper接口,并适当添加合适的Annotation,但是XML会生成在XML中; 3,XMLMAPPER:会生成Mapper接口,接口完全依赖XML; 注意,如果context是MyBatis3Simple:只支持ANNOTATEDMAPPER和XMLMAPPER --> <javaClientGenerator targetPackage="xxx.mapper.generator" type="XMLMAPPER" targetProject="src/main/java"> <!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false --> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 选择一个table来生成相关文件,可以有一个或多个table,必须要有table元素 tableName(必要):要生成对象的表名; domainObjectName 给表对应的 model 起名字 注意:大小写敏感问题。 --> <table tableName="md_area" domainObjectName="Area" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <!--用来修改表中某个列的属性,一个table元素中可以有多个columnOverride元素哈. property属性来指定列要生成的属性名称. --> <!--<columnOverride column="username" property="userName" />--> <columnRenamingRule searchString="^is_" replaceString=""/> </table> </context> </generatorConfiguration>
六、编写实体类、映射XML和Dao
七、编写实体类、运行自动生成生成