springboot mybaits mysql 整合

阅读更多

使用maven开发此demo。

一.使用idea创建一个maven项目,其中该注意的是:

1.勾选Create from archetype 选择列表中的maven-archetype-webapp,自动创建webapp目录


2.Properties中添加archetypeCatalog=internal .


4.创建好后,打开pom文件,等一会会自动生成.iml文件,这个文件制定了webapp的目录和moven的依赖包。


二:pom.xml文件



    4.0.0
    spring
    spring
    1.0.0-SNAPSHOT
    war
    spring
    
        org.springframework.boot
        spring-boot-starter-parent
        1.2.3.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            mysql
            mysql-connector-java
            5.1.34
        
        
            org.mybatis
            mybatis
            3.2.8
        
        
            org.mybatis
            mybatis-spring
            1.2.2
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            javax.servlet
            jstl
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
            provided
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
        
            org.slf4j
            slf4j-log4j12
            1.7.0
        
        
            org.slf4j
            slf4j-log4j12
            1.7.0
        
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        org.springframework
                        springloaded
                        1.2.0.RELEASE
                    
                
            
            
                org.apache.maven.plugins
                maven-deploy-plugin
                
                    true
                
            
        
    

三:properties文件。

数据源:

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.0.48:3306/activiti?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useOldAliasMetadataBehavior=true&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=123456

mvc配置:

server.tomcat.uri-encoding=UTF-8

server.port=8080
spring.jmx.enabled=false

# SPRING MVC
spring.mvc.locale=zh_CN
spring.mvc.date-format= yyyy-MM-dd
spring.view.prefix= /WEB-INF/pages/
spring.view.suffix= .jsp

四:application.class


package com.spring;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.MimeMappings;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.servlet.MultipartConfigElement;
import javax.sql.DataSource;

/**
 * Created by yuansb on 2016/10/19.
 */
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = "com.spring")
@MapperScan(basePackages = {"com.spring.mapper"})
@PropertySource({"classpath:datasource.properties"})
/*@EnableAsync*/
@EnableScheduling
public class Application extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("5MB");
        factory.setMaxRequestSize("5MB");
        return factory.createMultipartConfig();
    }

    @Bean
    public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                //container.setSessionTimeout(100, TimeUnit.MINUTES);
                MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
                mappings.add("woff", "application/x-font-woff");
                mappings.add("eot", "application/vnd.ms-fontobject");
                mappings.add("ttf", "application/x-font-ttf");
                container.setMimeMappings(mappings);
            }
        };
    }

    //手动把dataSource注入到sessionFactory,因为mybaits最新版本sqlTemplate没有自动注入sqlfactory,不加时报什么不能注入sqlTemplate。使用druid和多数据源不需要加这个。。
   //druid使用它还跑不起来。
    //sqlTemplate 就是dataSource没有注入进来。dataSource没有注入,一般是数据源配置不对,或者配置文件没有导入
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        return sessionFactory.getObject();
    }
}

 
 
五:配置日志
其中还包含,bean转换vo,map,json。excel操作,httpclient.md5。高精度算法等工具类
详细完整代码 :
http://download.csdn.net/detail/xiaoyao8903/9678836

加了一些工具类:

http://download.csdn.net/detail/xiaoyao8903/9679094









你可能感兴趣的:(springboot mybaits mysql 整合)