/**
* 配置事物管理器
* @param dataSource
* @return
*/
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public JdbcTemplate jdbcTemplate (DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
config.properties 文件在 resources 目录下
#数据库配置
db.url=jdbc:mysql://192.168.182.135:3306/bank
db.driver=com.mysql.jdbc.Driver
db.user=root
db.password=123456
#数据库连接池配置
#连接池中保留的最小连接数
pool.minPoolSize=5
#连接池中保留的最大连接数
pool.maxPoolSize=30
#获取连接超时时间
pool.checkoutTimeout=1000
配置 servlet.xml, 由WebConfig类实现
Thymeleaf 模板配置也在下面
package com.liulu.bank.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import java.nio.charset.StandardCharsets;
/**
* User : liulu
* Date : 2016-10-7 15:16
*/
@Configuration
@EnableWebMvc // 启用 SpringMVC ,相当于 xml中的 <mvc:annotation-driven/>
@ComponentScan(basePackages = {"com.liulu.bank.controller", "com.liulu.lit"},
includeFilters = @ComponentScan.Filter(classes = Controller.class),
useDefaultFilters = false)
public class WebConfig extends WebMvcConfigurerAdapter {
/**
* 设置由 web容器处理静态资源 ,相当于 xml中的<mvc:default-servlet-handler/>
*/
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
/**
* 下面三个bean 配置 Thymeleaf 模板
* @return
*/
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding(String.valueOf(StandardCharsets.UTF_8));
return templateResolver;
}
@Bean
public TemplateEngine templateEngine(SpringResourceTemplateResolver templateResolver) {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
return templateEngine;
}
@Bean
public ViewResolver viewResolver(TemplateEngine templateEngine) {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine);
viewResolver.setCharacterEncoding(String.valueOf(StandardCharsets.UTF_8));
return viewResolver;
}
}
Spring MVC整合Freemarker基于注解方式
Spring + Spring MVC + Ibatis + Velocity 框架搭建