初学Spring boot + mybatis-plus 配置

pom.xml 文件

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

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-tomcat
			provided
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
				
		
		
			mysql
			mysql-connector-java
		
	
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.1.1
		
		
		
        
            com.baomidou
            mybatis-plus
            2.0.7
        
        
		
		
		
		
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		
		
		
		
       
	      
	    
	   
	   
 
  

application.properties 配置

#################    mysql配置    ###################
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=19930727

#################    tamcat配置     ###################
server.port=8080

#################   日志sql打印配置  ###################
logging.level.com.model.mapper=DEBUG

#################      视图配置      ###################
#spring mvc
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

logback.xml 配置




    
    logback
    
    
    
    
    
    
        
        
            [%d{yyyy-MM-dd HH:mm:ss}][%-5p] - %m%n 
        
    

    
    
        ${log.path}/spring-boot.log
        
            ${log.path}/spring-boot.%d{yyyy-MM-dd}.log
        
        
[%d{yyyy-MM-dd HH:mm:ss.SSS}][%-5p] %logger - %m%n
        
    

    
        
        
    



mybatis-puls 配置 


package com;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.MybatisConfiguration;
import com.baomidou.mybatisplus.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.enums.DBType;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;


/** 
 * MybatisPlus配置
 */
@Configuration
@MapperScan("com.model.mapper*")
@EnableConfigurationProperties(MybatisProperties.class)
public class MybatisPlusConfig {

	@Autowired 
	private Environment environment;
	
	private RelaxedPropertyResolver propertyResolver;  
	
	@Autowired
	private DataSource dataSource;
	
	@Autowired
	private MybatisProperties properties;
	
	@Autowired
	private ResourceLoader resourceLoader = new DefaultResourceLoader();
	
	@Autowired(required = false)
	private Interceptor[] interceptors;
	
	@Autowired(required = false)
	private DatabaseIdProvider databaseIdProvider;
	
	/**
     * 配置DataSource
     * @return
     * @throws SQLException
     */
    @Bean
    public DataSource druidDataSource() throws SQLException {
    	
    	this.propertyResolver = new RelaxedPropertyResolver(environment, "spring.datasource.");
    	DriverManagerDataSource datasource = new DriverManagerDataSource();
        datasource.setUrl(propertyResolver.getProperty("url"));
        datasource.setDriverClassName(propertyResolver.getProperty("driver-class-name"));
        datasource.setUsername(propertyResolver.getProperty("username"));
        datasource.setPassword(propertyResolver.getProperty("password"));
//        datasource.setInitialSize(Integer.valueOf(propertyResolver.getProperty("initial-size")));
//        datasource.setMinIdle(Integer.valueOf(propertyResolver.getProperty("min-idle")));
//        datasource.setMaxWait(Long.valueOf(propertyResolver.getProperty("max-wait")));
//        datasource.setMaxActive(Integer.valueOf(propertyResolver.getProperty("max-active")));
//        datasource.setMinEvictableIdleTimeMillis(Long.valueOf(propertyResolver.getProperty("min-evictable-idle-time-millis")));
//        try {
//            datasource.setFilters(propertyResolver.getProperty("filters"));
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } 
        return datasource;
    }

	
   /**
	* mybatis-plus分页插件
	*/
	@Bean
	public PaginationInterceptor paginationInterceptor() {
		PaginationInterceptor page = new PaginationInterceptor();
		page.setDialectType("mysql");
		return page;
	}
	/**
	* 这里全部使用mybatis-autoconfigure 已经自动加载的资源。不手动指定
	* 配置文件和mybatis-boot的配置文件同步
	* @return
	*/
	@Bean
	public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() {
		MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
		mybatisPlus.setDataSource(dataSource);
		mybatisPlus.setVfs(SpringBootVFS.class);
		if (StringUtils.hasText(this.properties.getConfigLocation())) {
			mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
		}
		mybatisPlus.setConfiguration(properties.getConfiguration());
		if (!ObjectUtils.isEmpty(this.interceptors)) {
			mybatisPlus.setPlugins(this.interceptors);
		}
		// MP 全局配置,更多内容进入类看注释
		GlobalConfiguration globalConfig = new GlobalConfiguration();
		globalConfig.setDbType(DBType.MYSQL.name());//数据库类型
		// ID 策略 AUTO->`0`("数据库ID自增") INPUT->`1`(用户输入ID") ID_WORKER->`2`("全局唯一ID") UUID->`3`("全局唯一ID")
		globalConfig.setIdType(2);
		globalConfig.setSqlInjector(new com.baomidou.mybatisplus.mapper.AutoSqlInjector());
		//MP 属性下划线 转 驼峰 , 如果原生配置 mc.setMapUnderscoreToCamelCase(true) 开启,该配置可以无。
		//globalConfig.setDbColumnUnderline(true);
		mybatisPlus.setGlobalConfig(globalConfig);
		MybatisConfiguration mc = new MybatisConfiguration();
		// 对于完全自定义的mapper需要加此项配置,才能实现下划线转驼峰
		//mc.setMapUnderscoreToCamelCase(true); 
		mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
		mybatisPlus.setConfiguration(mc);
		if (this.databaseIdProvider != null) {
			mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
		}
		if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
			mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
		}
		if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
			mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
		}
		if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
			mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
		}
		return mybatisPlus;
	}
	
}

导出war包配置


package com;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(DemoApplication.class);
	}
}


启动类


package com;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class DemoApplication {
	
	private static final Log log = LogFactory.getLog(DemoApplication.class);
	
	public static void main(String[] args) {

		log.info("【******************** 项目启动中 ********************】");
//		SpringApplication.run(DemoApplication.class, args);
		new SpringApplicationBuilder(DemoApplication.class).bannerMode(Mode.OFF).run(args);//关闭启动图案
		log.info("【******************** 项目启动成功 ********************】");
	}
	
}

替换启动图案只需要在 src/main/resources下添加banner.txt文件即可






 
 

你可能感兴趣的:(mybatis-plus,spring,boot)