------Web. XML with Webinitializer.java configuration
Import Javax.servlet.ServletContext;
Import Org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
Import Ch.qos.logback.ext.spring.web.LogbackConfigListener;
public class Webinitializer extends Abstractannotationconfigdispatcherservletinitializer {
@Override
Protected class<?>[] Getrootconfigclasses () {
return new class<?>[] {applicationcontextconfig.class};
}
@Override
Protected class<?>[] Getservletconfigclasses () {
return new class<?>[] {webmvcconfig.class};
}
@Override
Protected string[] Getservletmappings () {
return new string[] {"/"};
}
@Override
protected void Registercontextloaderlistener (ServletContext servletcontext) {
Servletcontext.setinitparameter ("Logbackconfiglocation", "Classpath:logback.xml");
Servletcontext.addlistener (Logbackconfiglistener.class);
Super.registercontextloaderlistener (ServletContext);
}
}
------Webmvcconfig.java spring-servlet.xml with XML configuration
Import java.util.ArrayList;
Import java.util.List;
Import Org.springframework.context.annotation.ComponentScan;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.context.annotation.FilterType;
Import Org.springframework.http.MediaType;
Import Org.springframework.http.converter.HttpMessageConverter;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.web.bind.annotation.ControllerAdvice;
Import Org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
Import ORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;
Import Org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
Import Org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
Import Com.alibaba.fastjson.serializer.SerializerFeature;
Import Com.alibaba.fastjson.support.config.FastJsonConfig;
Import Com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
@Configuration
@EnableWebMvc
@ComponentScan (basepackages={"Com.fang.demo"}, Usedefaultfilters=false, includefilters={@ComponentScan. Filter ( Type=filtertype.annotation, Classes={controller.class, Controlleradvice.class})})
public class Webmvcconfig extends Webmvcconfigureradapter {
@Override
public void configuredefaultservlethandling (Defaultservlethandlerconfigurer configurer) {
Configurer.enable ();
}
@Override
public void Configureviewresolvers (Viewresolverregistry registry) {
Registry.jsp ("/", ". jsp");
}
@Override
public void Configuremessageconverters (list
Using the Fastjson tool
Fastjsonhttpmessageconverter fastconverter = new Fastjsonhttpmessageconverter ();
Fastjsonconfig fastjsonconfig = new Fastjsonconfig ();
Fastjsonconfig.setserializerfeatures (
Serializerfeature.prettyformat
);
Solve the problem of garbled characters
list<mediatype> fastmediatypes = new arraylist<mediatype> ();
Fastmediatypes.add (Mediatype.application_json_utf8);
Fastconverter.setfastjsonconfig (Fastjsonconfig);
Fastconverter.setsupportedmediatypes (fastmediatypes);
Converters.add (Fastconverter);
}
}
-------Datasourceconfig.java Data Source Configuration
Import Javax.sql.DataSource;
Import Org.springframework.beans.factory.annotation.Value;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.context.annotation.PropertySource;
Import Org.springframework.jdbc.datasource.DataSourceTransactionManager;
Import Org.springframework.transaction.PlatformTransactionManager;
Import org.springframework.transaction.annotation.EnableTransactionManagement;
Import Com.zaxxer.hikari.HikariConfig;
Import Com.zaxxer.hikari.HikariDataSource;
@Configuration
@EnableTransactionManagement
@PropertySource ({"Classpath:/config/jdbc.properties"})
public class Datasourceconfig {
@Value ("${jdbc.hikari.driverclassname}")
Private String Driverclassname;
@Value ("${jdbc.hikari.jdbcurl}")
Private String Jdbcurl;
@Value ("${jdbc.hikari.username}")
Private String username;
@Value ("${jdbc.hikari.password}")
private String password;
@Value ("${jdbc.hikari.readonly}")
Private Boolean readOnly;
@Value ("${jdbc.hikari.connectiontimeout}")
Private long connectiontimeout;
@Value ("${jdbc.hikari.idletimeout}")
Private long idleTimeout;
@Value ("${jdbc.hikari.maxlifetime}")
Private long maxlifetime;
@Value ("${jdbc.hikari.maximumpoolsize}")
private int maximumpoolsize;
@Value ("${jdbc.hikari.minimumidle}")
private int minimumidle;
@Value ("${jdbc.hikari.datasourceproperties.cacheprepstmts}")
Private Boolean Cacheprepstmts;
@Value ("${jdbc.hikari.datasourceproperties.prepstmtcachesize}")
private int prepstmtcachesize;
@Value ("${jdbc.hikari.datasourceproperties.prepstmtcachesqllimit}")
private int prepstmtcachesqllimit;
@Value ("${jdbc.hikari.datasourceproperties.useserverprepstmts}")
Private Boolean Useserverprepstmts;
@Bean (destroymethod= "Close")
Public DataSource DataSource () {
Hikariconfig config = new Hikariconfig ();
Config.setdriverclassname (Driverclassname);
Config.setjdbcurl (Jdbcurl);
Config.setusername (username);
Config.setpassword (password);
Config.setreadonly (readOnly);
Config.setconnectiontimeout (ConnectionTimeout);
Config.setidletimeout (idleTimeout);
Config.setmaxlifetime (Maxlifetime);
Config.setmaximumpoolsize (maximumpoolsize);
Config.setminimumidle (Minimumidle);
Config.adddatasourceproperty ("Cacheprepstmts", CACHEPREPSTMTS);
Config.adddatasourceproperty ("Prepstmtcachesize", prepstmtcachesize);
Config.adddatasourceproperty ("Prepstmtcachesqllimit", prepstmtcachesqllimit);
Config.adddatasourceproperty ("Useserverprepstmts", USESERVERPREPSTMTS);
Hikaridatasource ds = new Hikaridatasource (config);
return DS;
}
@Bean
Public Platformtransactionmanager TransactionManager (DataSource DataSource) {
return new Datasourcetransactionmanager (DataSource);
}
}
-------Applicationcontextconfig.java applicationcontext.xml with XML configuration
Import Javax.sql.DataSource;
Import Org.apache.ibatis.annotations.Mapper;
Import Org.apache.ibatis.session.SqlSessionFactory;
Import Org.mybatis.spring.SqlSessionFactoryBean;
Import Org.mybatis.spring.annotation.MapperScan;
Import Org.springframework.context.annotation.Bean;
Import Org.springframework.context.annotation.ComponentScan;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.context.annotation.EnableAspectJAutoProxy;
Import Org.springframework.context.annotation.FilterType;
Import Org.springframework.context.annotation.Import;
Import Org.springframework.core.io.support.PathMatchingResourcePatternResolver;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.web.bind.annotation.ControllerAdvice;
@Configuration
@ComponentScan (basepackages = {"Com.fang.demo"}, Excludefilters = {
@ComponentScan. Filter (type = filtertype.annotation, value = {controller.class, controlleradvice.class})})
@EnableAspectJAutoProxy
@Import ({datasourceconfig.class})
@MapperScan (basepackages= "Com.fang.demo", Annotationclass=mapper.class)
public class Applicationcontextconfig {
@Bean
Public sqlsessionfactory sqlsessionfactory (DataSource DataSource) throws Exception {
Sqlsessionfactorybean factory = new Sqlsessionfactorybean ();
Factory.setdatasource (DataSource);
Pathmatchingresourcepatternresolver resolver = new Pathmatchingresourcepatternresolver ();
Factory.setmapperlocations (Resolver.getresources ("Classpath:/mappers/*mapper.xml"));
return Factory.getobject ();
}
}
SpringMVC4 + Mybatis 0 Configuration grooming