idea springBoot+mybatis+Redis+Mysql(二)

idea springBoot+mybatis+Redis+Mysql(一)

目录结构

idea springBoot+mybatis+Redis+Mysql(二)_第1张图片

新建maven项目

file->newProject
idea springBoot+mybatis+Redis+Mysql(二)_第2张图片
idea springBoot+mybatis+Redis+Mysql(二)_第3张图片
idea springBoot+mybatis+Redis+Mysql(二)_第4张图片

添加项目必要包

修改pom.xml文件



    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.1.RELEASE
    
    4.0.0

    SpringBoot
    SpringBoot
    1.0-SNAPSHOT
    
        1.8
        2.8.0
        2.3.9
    
    
        
            org.springframework.boot
            spring-boot-starter-redis
            1.4.7.RELEASE
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
        
        
        
            org.mybatis
            mybatis
            3.4.2
        
        
        
            org.mybatis
            mybatis-spring
            1.3.1
        
        
        
            mysql
            mysql-connector-java
        
        
        
            com.alibaba
            druid
            1.0.27
        

        
        
            com.github.pagehelper
            pagehelper
            4.1.6
        
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
        
            com.google.code.gson
            gson
            ${gson.version}
        
        
        
            net.iharder
            base64
            ${net.iharder.base64.version}
        
        
            com.lowagie
            itext
            2.1.7
        
        
            org.apache.poi
            poi
            3.17
        
        
            commons-collections
            commons-collections
            3.2.1
        
        
            org.apache.shiro
            shiro-core
            1.4.0-RC2
        
        
            commons-io
            commons-io
            2.5
        
        
            org.freemarker
            freemarker
            2.3.25-incubating
        
        
            com.belerweb
            pinyin4j
            2.5.1
        
        
            com.aliyun.oss
            aliyun-sdk-oss
            2.5.0
        
        
            commons-fileupload
            commons-fileupload
            1.2.2
        
        
            com.esotericsoftware.kryo
            kryo
            2.24.0
        
        
            de.ruedigermoeller
            fst
            2.57
        
        
            com.aliyun
            aliyun-java-sdk-core
            3.2.2
        
        
            org.apache.poi
            poi-ooxml
            3.17
        
        
            com.aliyun
            aliyun-java-sdk-push
            3.0.0
        
        
            com.aliyun.mns
            aliyun-sdk-mns
            1.1.8
        
        
            org.quartz-scheduler
            quartz
            2.2.3
        
        
            com.aliyun
            aliyun-java-sdk-ram
            2.0.7
        
        
            com.alibaba
            fastjson
            1.2.3
        
        
            com.aliyun
            aliyun-java-sdk-sts
            2.1.6
        
        
            org.codehaus.jackson
            jackson-mapper-asl
            1.9.13
        
        
            junit
            junit
            test
        
        
            org.springframework
            spring-test
            4.1.4.RELEASE
        
        
            org.springframework.data
            spring-data-redis
            1.7.1.RELEASE
        
        
            redis.clients
            jedis
            2.8.2
        
    

    war
    

    
        
        SpringBoot
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
            
                org.apache.maven.plugins
                maven-war-plugin
                2.1.1
                
                    false
                
            
            
        
    

新建application.properties

server.port=8081 #访问端口号
server.context-path=/test #项目访问跟路径
druid.driver=com.mysql.jdbc.Driver 
druid.url=jdbc:mysql://0.0.0.0:3306/test #数据库地址
druid.username= #数据库用户名
druid.password= #数据库密码
druid.init-size=1
druid.min-idel=1
druid.max-active=5
druid.login.timeout.seconds=30
druid.query.timeout.seconds=30
druid.mappers=mappers/*/*.xml #mybatis sql文件扫描路径
mybatis.configuration.call-setters-on-nulls=true
mybatis.configuration.map-underscore-to-camel-case=true
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1 
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0

配置mybatis配置文件



    
        
        
        
        
        
        
        
    

    

        
        
        
    

    
        
            
            
        
    


配置log文件

log4j.rootLogger=info,ServerDailyRollingFile,stdout

log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd
log4j.appender.ServerDailyRollingFile.File=log/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

配置sql文件

!idea springBoot+mybatis+Redis+Mysql(二)_第5张图片




    
    
        testOther
    

    
    
        id,
        name
    
    
    
        #{id},
        #{name}
    
    
    
        insert into
        
        (
        
        ) values (
        
        )
    
    
    
        update
        
        set name = #{name}
        where
        id = #{id}
    
    
    
        delete from
        
        where
        id = #{id}
    
    
    
    
    




    

配置 Application

package com.Logos;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: TODO
 * @author: jxc
 * @date: 2019/6/18 7:47 PM
 * @version: V1.0
 * @Software: IntelliJ IDEA
 */
@SpringBootApplication
@RestController
@ServletComponentScan("com.Logos.configuration")
public class Application {
    @RequestMapping("/")
    public String index() {
        return "Hello Spring Boot";
    }

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

分页插件

/**
 * @Description: TODO
 * @author: jxc
 * @date: 2019/6/18 10:47 PM
 * @version: V1.0
 * @Software: IntelliJ IDEA
 */
@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class })})
public class PagePlugin implements Interceptor {

	private static String dialect = "";	//数据库方言
	private static String pageSqlId = ""; //mapper.xml中需要拦截的ID(正则匹配)
	
	public Object intercept(Invocation ivk) throws Throwable {
		if(ivk.getTarget() instanceof RoutingStatementHandler){
			RoutingStatementHandler statementHandler = (RoutingStatementHandler)ivk.getTarget();
			BaseStatementHandler delegate = (BaseStatementHandler) ReflectHelper.getValueByFieldName(statementHandler, "delegate");
			MappedStatement mappedStatement = (MappedStatement) ReflectHelper.getValueByFieldName(delegate, "mappedStatement");
			
			if(mappedStatement.getId().matches(pageSqlId)){ //拦截需要分页的SQL
				BoundSql boundSql = delegate.getBoundSql();
				Object parameterObject = boundSql.getParameterObject();//分页SQL