Idea文件模板(JdbcConfig、MybatisConfig、SpringConfig、SpringMVCConfig)

 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[]{};
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值