美文网首页
spring boot 集成mybatis-plus

spring boot 集成mybatis-plus

作者: 周六不算加班 | 来源:发表于2018-06-01 20:25 被阅读148次

    一、创建maven项目

    二、在pom添加spring boot相关的jar包

    1、完善maven项目
        添加<packaging>jar</packaging>
            <name>Demo</name>
            <description>Demo project for Spring Boot</description>
            
    2、实现spring boot
    
        2.1、基础jar包引入
            实现spring boot有多种形式,一种是继承父类中的spring boot 
            如下:
            <parent>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.0.2.RELEASE</version>
                <relativePath />
            </parent>
            还有一种是直接添加:
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>2.0.2.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
            
        2.2、配置UTF-8编码格式
            <properties>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
                <java.version>1.8</java.version>
            </properties> 
        
        2.3、spring boot jar包引入
            <dependencies>
                <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>
            </dependencies>
        以上配置完以后基础的框架已经配置好了。 
    

    三、pom文件集成mybaties-plus jar包

    1、加入mysql包
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>      
    
                 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
         </dependency>
    
    
    2、加入mybaties-plus jar包
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatisplus-spring-boot-starter</artifactId>
            <version>1.0.5</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        
        spring boot如果是2.0版本以上的mybatis-plus建议用最新版本的目前我在用的是2.3版本亲测可用。
        如果是低版本的会出现版本出现出图问题。
    
    3、加入生成代码工具的jar 包
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>
    
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity-engine-core -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>
    

    四、 pom文件配置部署环境
    <build>
    <resources>
    <resource>
    <directory>src/main/java</directory>
    <filtering>false</filtering>
    <includes>
    <include>*/.xml</include>
    </includes>
    </resource>

    <resource>
    <directory>src/main/resources</directory>
    </resource>
    </resources>

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

    五、配置文件中设置

    1、服务设置
        server.port=8080
        
    2、#DRUID POOL 配置
        spring.datasource.initialSize=5  
        spring.datasource.minIdle=5  
        spring.datasource.maxActive=20  
        spring.datasource.maxWait=60000  
        spring.datasource.timeBetweenEvictionRunsMillis=60000  
        spring.datasource.minEvictableIdleTimeMillis=300000  
        spring.datasource.validationQuery=SELECT 1 FROM DUAL  
        spring.datasource.testWhileIdle=true  
        spring.datasource.testOnBorrow=false  
        spring.datasource.testOnReturn=false  
    
        spring.datasource.poolPreparedStatements=true  
        spring.datasource.maxPoolPreparedStatementPerConnectionSize=20  
    
        spring.datasource.filters=stat,wall,log4j  
        spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    
    3、#Default DataBase Source
        spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
        spring.datasource.url=
        spring.datasource.username=
        spring.datasource.password=
        spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    4、#mybatis-plus
        在resource路径下的mybatis文件夹
        mybatis-plus.mapper-locations=classpath\:mybatis/*Mapper.xml
        扫描model的文件
        mybatis-plus.typeAliasesPackage=com.java.model
        
        mybatis-plus.global-config.id-type=2
        mybatis-plus.global-config.field-strategy=2
        mybatis-plus.global-config.auto-set-db-type=true
        mybatis-plus.global-config.db-column-underline=true
        mybatis-plus.global-config.refresh-mapper=true
        mybatis-plus.configuration.map-underscore-to-camel-case=true
        mybatis-plus.configuration.cache-enabled=false
    
       5、#log4j
               log4j.rootLogger=info,ServerDailyRollingFile,stdout
    
               log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
               log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd
               log4j.appender.ServerDailyRollingFile.File=D://test/test.log
               log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout
               log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d{yyyy-MM-dd         HH:mm:ss} [%t] %-5p [%c] - %m%n
               log4j.appender.ServerDailyRollingFile.Append=true
    
               log4j.appender.stdout=org.apache.log4j.ConsoleAppender
               log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
               log4j.appender.stdout.layout.ConversionPattern=%d yyyy-MM-dd HH:mm:ss %p [%c] %m%n
    

    六、代码生成器
    请参考官网

    七、代码配置mybatis-plus

    @Configuration
    @MapperScan("com.java.mapper*")
    public class MybatisPlusConfig {
    
        /**
         * mybatis-plus分页插件<br>
         * 文档:http://mp.baomidou.com<br>
         */
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            paginationInterceptor.setDialectType(DBType.MYSQL.getDb());
            return paginationInterceptor;
        }
    }
    

    相关文章

      网友评论

          本文标题:spring boot 集成mybatis-plus

          本文链接:https://www.haomeiwen.com/subject/kgnksftx.html