idea+springboot2.0+maven+mybatis+mybatis generator自动生成代码

    最近尝试自己搭一主流的springboot框架,springboot全家桶相对于springmvc来说,少了很多的配置,并且内置tomcat,打包即可发布,适合轻量级系统开发,一下是配置的具体的过程。

1、jdk1.8

idea+springboot2.0+maven+mybatis+mybatis generator自动生成代码_第1张图片

2、填写GroupId和ArtifactId:

groupid:是项目组织唯一的标识符,对应着java包结构,是main里面java的目录结构

artifactid:对应着项目的名称,是项目根目录的鸣名称

注:在后面pom文件添加依赖时的标签,就是这两个。

jar、war的区别:jar包内项目编译后的.class文件,而war为包括.class,js,jsp,HTML等资源在内的项目压缩包。

version:项目版本号,其中含义网上均可搜到。

idea+springboot2.0+maven+mybatis+mybatis generator自动生成代码_第2张图片

3、添加依赖,顾名思义,选择所需功能,自动添加至pom中进行下载,之后点击next即可完成。

idea+springboot2.0+maven+mybatis+mybatis generator自动生成代码_第3张图片

4、基本结构如下图

idea+springboot2.0+maven+mybatis+mybatis generator自动生成代码_第4张图片

4.1项目启动类(顾名思义,可见代码中有main方法,idea自动添加main方法的启动项,可以进行启动,这里也是整个项目启动的起点),如下需添加相应注解

package com.liujx.springboot_mybtis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
//添加注解,对应项目中mapper(dao)的包路径
@MapperScan("com.liujx.springboot_maybtis.dao")
public class SpringbootMybtisApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootMybtisApplication.class, args);
	}
}

4.2  application.yml

为配置简洁,删掉了初始生成的application.properties文件,创建此文件(为项目启动时所加载的配置表,包括系统属性,环境变量,命令参数等)。

server:
  port: 8080


spring:
    datasource:
        name: mysql_test
        type: com.alibaba.druid.pool.DruidDataSource
        #druid相关配置
        druid:
          #监控统计拦截的filters
          filters: stat
          driver-class-name: com.mysql.jdbc.Driver
          #基本属性
          url: jdbc:mysql://192.168.1.239/test?useSSL=false&allowMultiQueries=true
          username: root
          password: root
          #配置初始化大小/最小/最大
          initial-size: 1
          min-idle: 1
          max-active: 20
          #获取连接等待超时时间
          max-wait: 60000
          #间隔多久进行一次检测,检测需要关闭的空闲连接
          time-between-eviction-runs-millis: 60000
          #一个连接在池中最小生存的时间
          min-evictable-idle-time-millis: 300000
          validation-query: SELECT 'x'
          test-while-idle: true
          test-on-borrow: false
          test-on-return: false
          #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
          pool-prepared-statements: false
          max-pool-prepared-statement-per-connection-size: 20
## 该配置节点为独立的节点,有很多同学容易将这个配置放在spring的节点下,导致配置无法被识别
mybatis:
  mapper-locations: classpath:mapper/*.xml  #注意:一定要对应mapper映射xml文件的所在路径
  type-aliases-package: com.winterchen.model  # 注意:对应实体类的路径

#pagehelper
pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql
    returnPageInfo: check

 

4.3pom.xml



	4.0.0

	com.liujx
	springboot_mybtis
	0.0.1-SNAPSHOT
	jar

	springboot_mybtis
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.5.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.2
		
		
			mysql
			mysql-connector-java
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			org.apache.commons
			commons-lang3
			3.4
		


		
			com.fasterxml.jackson.core
			jackson-core
		
		
			com.fasterxml.jackson.core
			jackson-databind
		
		
			com.fasterxml.jackson.datatype
			jackson-datatype-joda
		
		
			com.fasterxml.jackson.module
			jackson-module-parameter-names
		
		
		
			com.github.pagehelper
			pagehelper-spring-boot-starter
			1.2.5
		
		
		
			com.alibaba
			druid-spring-boot-starter
			1.1.9
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
			
			
				org.mybatis.generator
				mybatis-generator-maven-plugin
				1.3.2
				
					${basedir}/src/main/resources/generator/generatorConfig.xml
					true
					true
				
			
		
	


 

4.4配置mybatis generator自动生成代码

注意点:

(1)数据库驱动包:很多教程没有注明此jar来源,其实无需去下载,根据名称查询,在pom.xml 中添加依赖即可,地址即依赖下载的地址。

我是在这个网站https://mvnrepository.com查到的依赖:



    mysql
    mysql-connector-java
    5.1.30

(2)此工具需要生成dao层、modal(pojo实体)层、mapper.xml映射文件,其路径均在此处一一配置。

(3)配置需要生产的表单信息。
 




    
    
    
        
            
            
            
        
        
        
        
        
            
        
        
        
            
            
        
        
        
            
        
        
        
            
        
        
        

 

4.5自动生成代码

(1)点击run-Edit Configurations

这里写图片描述

(2)添加配置后运行

这里写图片描述

5.配置database信息

生成代码后,mapper.xml会出现波浪纹提示,此为系统无法对sql与数据库连接进行检验所致,对数据库进行连接,即可消除。

idea+springboot2.0+maven+mybatis+mybatis generator自动生成代码_第5张图片

你可能感兴趣的:(idea+springboot2.0+maven+mybatis+mybatis generator自动生成代码)