JdbcConfig配置文件模板
JdbcConfig
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
#parse("File Header.java")
public class ${NAME} {
@Value("${jdbc.driver}")
private String druid;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(druid);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource){
DataSourceTransactionManager d = new DataSourceTransactionManager();
d.setDataSource(dataSource);
return d;
}
}
MybatisConfig配置文件模板(Spring整合Mybatis)
MybatisConfig
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
#parse("File Header.java")
public class ${NAME} {
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean sf = new SqlSessionFactoryBean();
sf.setTypeAliasesPackage("cn.zqwl.pojo");
sf.setDataSource(dataSource);
return sf;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("cn.zqwl.mapper");
return msc;
}
}
SpringConfig配置文件模板
SpringConfig
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @Configuration
* 设置当前类为配置文件
*/
@Configuration
/**
* @ComponentScan
* 扫描包下带有特定注解的组件,方便为他们注册Spring管理的bean
* 除去Controller包下不扫描在Spring,在SpringMvcConfig已经扫描过了,
* Spring 和SpringMvcConfig各管各的
*/
@ComponentScan({"cn.zqwl.mapper","cn.zqwl.Server"})
/**
* @PropertySource
* 加载配置文件
*/
@PropertySource("jdbc.properties")
/**
* @Import
* 该注解用于导入配置文件
*/
@Import({JdbcConfig.class, MybatisConfig.class})
/**
* @EnableTransactionManagement
* 该注解用于开启事务
*/
@EnableTransactionManagement
public class ${NAME} {
}
SpringMVCConfig配置文件模板
SpringMVCConfig
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
* @Configuration
* 设置当前类为配置文件
*/
@Configuration
/**
* @ComponentScan
* 扫描包下带有特定注解的组件,告诉Spring框架将这些类识别为控制器类
* 只是扫描Controller包,其他包在Spring中扫描
* Spring 和SpringMvcConfig各管各的
*/
@ComponentScan({"cn.zqwl.Servlet"})
/**
* @EnableWebMvc
* 启用Spring Web MVC(Spring MVC)功能
* 这里是开启jason转换
*/
@EnableWebMvc
public class ${NAME} {
}
web配置文件
WebSpring
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class ${NAME} extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
*
* @return
* 放置Spring配置文件
*/
@Override
protected Class<?>[] getRootConfigClasses() {
//return new Class[]{SpringConfig.class};
return new Class[]{};
}
/**
*
* @return
* 放置SpringMVCConfig配置文件
*/
@Override
protected Class<?>[] getServletConfigClasses() {
//return new Class[]{SpringMvcConfig.class};
return new Class[]{};
}
/**
*
* @return
* 交给SpringMVC管理的资源路径
*/
@Override
protected String[] getServletMappings() {
//return new String[]{"/"};
return new String[]{};
}
}