一、springboot+springmvc+springdata jpa+mybatis+freemarker+druid项目配置

一、首先要新建一个springboot项目,如何创建项目,请看https://blog.csdn.net/IT_CREATE/article/details/86681538

 

二、导入相关联的包,即使你在创建springboot的时候没有选择相关联的包那些,也不要紧,只需要porm.xml引入下面这些就可以了

porm.xml配置文件:



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.19.RELEASE
		 
	
	com.ge
	telecom
	1.0
	telecomproject
	这是一个电信计费项目

	
		1.8
	

	

		
		
			com.alibaba
			druid
			1.0.11
		

		
		
			com.alibaba
			druid-spring-boot-starter
			1.1.0
		
 
		
		
			mysql
			mysql-connector-java
		
		
		
			org.springframework.boot
			spring-boot-configuration-processor
			true
		
		
		
			org.springframework.boot
			spring-boot-starter-aop
		

		
		
			org.springframework.boot
			spring-boot-starter-data-jpa
		
		
		
			org.springframework.boot
			spring-boot-starter-freemarker
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.3
		
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	


	
		
			spring-snapshots
			Spring Snapshots
			https://repo.spring.io/snapshot
			
				true
			
		
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
			
				false
			
		
	

	
		
			spring-snapshots
			Spring Snapshots
			https://repo.spring.io/snapshot
			
				true
			
		
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
			
				false
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


 

三、配置src/main/resources下的application.properties文件

server.context-path=/boots,表示用boots取代项目名,这个我们以后访问的路径就是http://localhost/boots/页面别名,后面要配置页面

#tomcat configure
server.port=80
server.session.timeout=1800
server.context-path=/boots
server.tomcat.uri-encoding=UTF-8

#springmvc configure
spring.mvc.view.prefix=/templates/
spring.mvc.view.suffix=.ftl
spring.freemarker.cache=true
spring.freemarker.charset=utf-8
spring.freemarker.allow-request-override=true
spring.freemarker.request-context-attribute=request

#logback configure
logging.config=classpath:logback.xml
logging.path=/logs

#datasource configure
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/crebas?useUnicode=true&characterEncoding=UTF8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=123456
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 now() from dual
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.filters=stat,wall,log4j
spring.datasource.logSlowSql=true

#mybatis configure
mybatis.type-aliases-package=com.ge.telecom.bean
mybatis.mapper-locations=classpath:mybatis/mapper/*mag/*sys/*Mapper.xml

########################################################
### Java Persistence Api
########################################################
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
#[org.hibernate.cfg.ImprovedNamingStrategy              #org.hibernate.cfg.DefaultNamingStrategy]
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

 

四、在src/main/resources下新建logback.xml日志记录文件进行配置



    

	
	  
          
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n
            
          
      

	
        ${LOG_PATH}/debug.log
        
            ${LOG_PATH}/debug-%d{yyyyMMdd}.log.%i
            
                50MB
            
            5
        
        
        	DEBUG
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n
            
        
    

    
        ${LOG_PATH}/info.log
        
            ${LOG_PATH}/info-%d{yyyyMMdd}.log.%i
            
                50MB
            
            5
        
        
        	INFO
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n
            
        
    
    
	
        ${LOG_PATH}/warn.log
        
            ${LOG_PATH}/warn-%d{yyyyMMdd}.log.%i
            
                50MB
            
            5
        
        
        	WARN
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n
            
        
    
	
    
        ${LOG_PATH}/error.log
        
            ${LOG_PATH}/error-%d{yyyyMMdd}.log.%i
            
            
                50MB
            
            5
        
        
        	ERROR
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n

            
        
    
    
    
		    
		
    	
        
        
        
    
    

 

五、在src/main/resources下的包com.xxx.xxxx下创建configure包,在该包下写一些配置类

5.1 新建ApplicationContextConfig.java配置类(主要用于配置业务层及以下,过滤掉controller层)

package com.ge.telecom.configure;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
//import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Controller;

/**
 * @Configuration 将该类声明为一个配置类,用来替代applicationContext.xml
 * @ComponentScan 开启容器的自动扫描, 并使用Filter排除一些不必要的组件
 * @author Administrator
 * 
 * 假如写了一些xml的配置文件,还可以使用@ImportResource({"classpath*:XXX.xml"})
 * 导入配置文件的内容
 */


@Configuration
@ComponentScan(basePackages= {"com.ge.telecom"},excludeFilters= {
		@ComponentScan.Filter(
				type=FilterType.ANNOTATION,value={Controller.class})})
public class ApplicationContextConfig {

	
	
}

5.2 新建DruidDataSourceConfig.java配置类(主要用于配置连接池)

package com.ge.telecom.configure;

import java.sql.SQLException;
import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

/**
 * @Configuration 该注解的作用:描述该JAVA类是一个配置类
 * @author Administrator
 *
 */
@Configuration
public class DruidDataSourceConfig {

	private Logger logger = LoggerFactory.getLogger(DruidDataSourceConfig.class);

    @Value("${spring.datasource.url}")
    private String dbUrl;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;

    @Value("${spring.datasource.initialSize}")
    private int initialSize;

    @Value("${spring.datasource.minIdle}")
    private int minIdle;

    @Value("${spring.datasource.maxActive}")
    private int maxActive;

    @Value("${spring.datasource.maxWait}")
    private int maxWait;

    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;

    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
    private int minEvictableIdleTimeMillis;

    @Value("${spring.datasource.validationQuery}")
    private String validationQuery;

    @Value("${spring.datasource.testWhileIdle}")
    private boolean testWhileIdle;

    @Value("${spring.datasource.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${spring.datasource.testOnReturn}")
    private boolean testOnReturn;

    @Value("${spring.datasource.filters}")
    private String filters;

    @Value("${spring.datasource.logSlowSql}")
    private String logSlowSql;

    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("loginUsername", username);
        reg.addInitParameter("loginPassword", password);
        reg.addInitParameter("logSlowSql", logSlowSql);
        return reg;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        return filterRegistrationBean;
    }

    @Bean
    public DataSource druidDataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        try {
            datasource.setFilters(filters);
        } catch (SQLException e) {
            logger.error("druid configuration initialization filter", e);
        }
        return datasource;
    }
	
	
	
}

5.3 新建SpringMVCConfig.java配置类(主要用于配置表现层,过滤掉业务层及以下,及一些视图。静态资源的配置或者拦截器、监听器、过滤器、转换器等)

package com.ge.telecom.configure;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
//import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import com.ge.telecom.converter.StringToDateConverter;
import com.ge.telecom.interceptor.LoginInterceptor;
import com.ge.telecom.listener.SsesionListener;

/**
 * SpringMVCConfig 配置类,用来替代springmvc-servlet.xml
 * @author Administrator
 * 
 * @EnableWebMvc 将该类定义为一个WEB层的配置类,需要extends WebMvcConfigurationSupport
 * 取代springboot框架针对WEBMVC的默认配置
 * 
 * 
 * WebMvcConfigurerAdapter 可以替代 @EnableWebMvc + WebMvcConfigurationSupport
 * 并且还支持我们自定义一些配置
 * 
 */
@Configuration
@ComponentScan(basePackages= {"com.ge.telecom"},excludeFilters= {
		@ComponentScan.Filter(
				type=FilterType.ANNOTATION,value={Service.class,Repository.class})})
public class SpringMVCConfig extends WebMvcConfigurerAdapter{
	
    @Resource
    private RequestMappingHandlerAdapter handlerAdapter;

	
	/**
	 * 注册url -- 页面之间的跳转关系
	 * 比较适合于:从登陆页面上,跳转到"忘记密码","如无账号,请注册"
	 * 从注册页面上,跳转到"已有账号,马上登录",
	 * 这个必须配置,把所有的页面都添加进去,才能访问这些页面,用registry.addViewController("/index").setViewName("main/index");
	 * setViewName()是页面在templates文件夹下的路径,addViewControlle()是给这个路径起了一个别名,及这个页面路径的别名
	 */
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		// TODO Auto-generated method stub
		//配置系统首页http://ip:port/项目名/
		
		//系统页面
		registry.addViewController("/index").setViewName("main/index");
		registry.addViewController("/404").setViewName("main/404");
		registry.addViewController("/login.action").setViewName("main/login");
		

	}
	
	/**
	 * 指定静态资源文件目录,必须配置
	 * 
	 * 还可以在application.properties使用
	 * spring.mvc.static-path-pattern=/static/**
	 * spring.resources.static-locations=classpath:/static/
	 * 
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// TODO Auto-generated method stub
		registry.addResourceHandler(new String[]{"/static/**","/resource/**"}).addResourceLocations("classpath:/static/");
	}
	
	/**
	 * 配置字符串转日期转换器,用到就配置,StringToDateConverter我自己写的,string也可直接插入数据库作为数据库的date、datetime
	 */
    @PostConstruct
    public void initEditableAvlidation() {

        ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer)handlerAdapter.getWebBindingInitializer();
        if(initializer.getConversionService()!=null) {
            GenericConversionService genericConversionService = (GenericConversionService)initializer.getConversionService();           
            genericConversionService.addConverter(new StringToDateConverter());
        }
    }
    
    //配置拦截器,用到就配,LoginInterceptor日志拦截器我自己写的
    @Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new LoginInterceptor());//配置拦截路径
		super.addInterceptors(registry);
	}

    //配置监听器,用到就配,SsesionListener是我自己写的
    @SuppressWarnings({ "rawtypes", "unchecked" })
	@Bean
    public ServletListenerRegistrationBean listenerRegist() {
        ServletListenerRegistrationBean srb = new ServletListenerRegistrationBean();
        srb.setListener(new SsesionListener());
        return srb;
    }
	
		
}

转换器、拦截器。监听器,这里就不贴出来了,这个不是这个基本配置主要的,这些东西你根据自己项目的要求自己写,自己配置。那么配置类视图路径后,访问地址就是http://localhost/boots/页面别名,比如:访问登录页面,就是http://localhost/boots/login.action

 

六、在src/main/resources下的包com.xxx.xxxx下创建filter包,在该包下写一些过滤器类,配置过滤器

新建CharacterEncodingFilter.java类,这个是个字符乱码过滤器,我认为还是很有必要的

package com.ge.telecom.filter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.annotation.Order;
/**
 * 中文乱码过滤器
 * 既可以处理POST乱码,又可以处理GET乱码、delete、update
 * @author 
 * @Order(1) 过滤器的执行顺序,值越小,越在前
 * 例外,过滤器需要在启动类,或者配置类身上 ,使用@ServletComponentScan完成过滤器的扫描
 */
@Order(1)
@WebFilter(urlPatterns="/*",filterName="encodingFilter",initParams= {
		@WebInitParam(name="encoding",value="utf-8"),
		@WebInitParam(name="forceEncoding",value="true")
})
public class CharacterEncodingFilter implements Filter {

	private String encoding;
	private boolean forceEncoding = false;

	public CharacterEncodingFilter() {
		// TODO Auto-generated constructor stub
	}

	public void destroy() {
		// TODO Auto-generated method stub
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		String method = req.getMethod();
		if (forceEncoding) {
			res.setCharacterEncoding(encoding);
		}
		
		if ("GET".equals(method.toUpperCase())) {
			HttpServletRequest wrapper = new EncodingHttpServletRequestWrapper(req, encoding);
			chain.doFilter(wrapper, res);
		} else {
			req.setCharacterEncoding(encoding);
			chain.doFilter(req, res);
		}
	}

	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub
		encoding = config.getInitParameter("encoding");
		forceEncoding = Boolean.valueOf(config.getInitParameter("forceEncoding"));
	}

	private static class EncodingHttpServletRequestWrapper extends HttpServletRequestWrapper {

		private HttpServletRequest request;
		private String encoding;

		public EncodingHttpServletRequestWrapper(HttpServletRequest request, String encoding) {
			super(request);
			// TODO Auto-generated constructor stub
			this.request = request;
			this.encoding = encoding;
		}

		@Override
		public String getQueryString() {
			// TODO Auto-generated method stub
			String content = request.getQueryString();

			if (content != null) {
				try {
					content = URLDecoder.decode(content, encoding);
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return content;
		}
		
		@Override
		public Map getParameterMap() {
			// TODO Auto-generated method stub
			String content = getQueryString();
			//userName=阿斯蒂芬&loginName=1223&pwd=&gender=0&income=2342342342&marry=true&birthday=&hobby=0&hobby=1
			Map params = new HashMap();
			// 处理多值的情况
			Map> multiValues = new HashMap>();
			if (content != null) {
				String[] tem = content.split("&");
				for (String str : tem) {
					String[] kvs = str.split("=");// userName=撒旦法
					// 需要处理一个提交项有多个值的情况发生
					if (params.containsKey(kvs[0])) {
						// 需要处理checkbox的多值情况,例如:ck=111&ck=222&ck=333
						List valuesList;
						if (multiValues.containsKey(kvs[0])) {// 如果多值集合中已经包含某个键
							valuesList = multiValues.get(kvs[0]);
							if (kvs.length >= 2) {// ck=111
								valuesList.add(kvs[1]);
							} else {
								valuesList.add("");// ck=
							}
						} else {// 如果多值集合中尚未包含某个键
							valuesList = new ArrayList();
							valuesList.add(params.get(kvs[0])[0]);// 初始加入
							if (kvs.length >= 2) {// ck=111
								valuesList.add(kvs[1]);
							} else {// ck=
								valuesList.add("");
							}
							multiValues.put(kvs[0], valuesList);
						}
					} else {
						if (kvs.length >= 2) {// userName=撒旦法
							params.put(kvs[0], new String[] { kvs[1] });
						} else {// userName=
							params.put(kvs[0], new String[] { "" });
						}
					}
				} // for循环结束

				// --------------将多值情况,同样添加到params集合中去-------------
				if (multiValues != null && !multiValues.isEmpty()) {
					Iterator its = multiValues.keySet().iterator();
					while (its.hasNext()) {
						String key = (String) its.next();
						List strs = multiValues.get(key);
						int size = strs.size();// 获得值的个数
						String[] arrays = new String[size];
						for (int i = 0; i < size; i++) {
							arrays[i] = strs.get(i);
						}
						params.put(key, arrays);// 将多值的情况也处理到Map集合中去
					}
				}
			}
			return params;
		}

		@Override
		public String getParameter(String name) {
			// TODO Auto-generated method stub
			Map params = getParameterMap();
			String val = "";
			if (params != null && params.containsKey(name)) {
				val = params.get(name)[0];
			}
			return val;
		}

		@Override
		public Enumeration getParameterNames() {
			// TODO Auto-generated method stub
			return Collections.enumeration(getParameterMap().keySet());
		}

		@Override
		public String[] getParameterValues(String name) {
			// TODO Auto-generated method stub
			return (String[]) getParameterMap().get(name);
		}
	}

}

 

七、启动类TelecomprojectApplication的配置

package com.ge.telecom;

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

import com.ge.telecom.configure.ApplicationContextConfig;


@SpringBootApplication
@MapperScan({"com.ge.telecom.*.*.mapper"})
@ServletComponentScan
public class TelecomprojectApplication {

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

}

因为springdata jpa不需要扫面包,但是mybatis需要去扫描相应的映射的包,所以要加上@MapperScan注解

 

基本配置也就是这样,作为项目,这个就可以启动和使用springdata jpa和mybatis了,也可以做前后端交互了。如何使用我在下一章节介绍,因为使用有一点小规范。

 

下面几章节我会介绍一些操作日志和登录日志如何书写,涉及到一些切面,拦截器、监听器等。

 

二、配置了双持久层框架(springdata jpa+mybatis)后包结构及遵循的一些规范

https://blog.csdn.net/IT_CREATE/article/details/88117298

你可能感兴趣的:(一、springboot+springmvc+springdata jpa+mybatis+freemarker+druid项目配置)