Guest User

sp_config

a guest
Nov 14th, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1.  
  2. *************** DbConfig *****************
  3. package test.springmvccrud.config;
  4.  
  5. import javax.sql.DataSource;
  6.  
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.jdbc.core.JdbcTemplate;
  11. import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
  12. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  13.  
  14. @Configuration
  15. public class SpringDBConfig {
  16.  
  17. @Autowired
  18. DataSource dataSource;
  19.  
  20. @Bean
  21. public JdbcTemplate getJdbcTemplate(){
  22. return new JdbcTemplate(dataSource);
  23.  
  24. }
  25.  
  26.  
  27.  
  28. @Bean
  29. public DataSource dataSource() {
  30. DriverManagerDataSource ds = new DriverManagerDataSource();
  31. ds.setDriverClassName(com.mysql.jdbc.Driver.class.getName());
  32. ds.setUrl("jdbc:mysql://localhost:3306/studentinfo");
  33. ds.setUsername("root");
  34. ds.setPassword("root");
  35.  
  36. return ds;
  37. }
  38.  
  39. }
  40.  
  41.  
  42.  
  43. *************** DbConfig *****************
  44.  
  45. package test.springmvccrud.config;
  46.  
  47. import org.springframework.context.annotation.Bean;
  48. import org.springframework.context.annotation.ComponentScan;
  49. import org.springframework.context.annotation.Configuration;
  50. import org.springframework.context.support.ResourceBundleMessageSource;
  51. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  52. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  53. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  54. import org.springframework.web.servlet.view.InternalResourceViewResolver;
  55. import org.springframework.web.servlet.view.JstlView;
  56.  
  57. @EnableWebMvc
  58. @Configuration
  59. @ComponentScan({ "test.springmvccrud" })
  60. public class SpringWebConfig extends WebMvcConfigurerAdapter{
  61.  
  62. @Override
  63. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  64. registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
  65. }
  66.  
  67. @Bean
  68. public InternalResourceViewResolver viewResolver() {
  69. InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
  70. viewResolver.setViewClass(JstlView.class);
  71. viewResolver.setPrefix("/WEB-INF/view/");
  72. viewResolver.setSuffix(".jsp");
  73. return viewResolver;
  74. }
  75.  
  76. @Bean
  77. public ResourceBundleMessageSource messageSource() {
  78. ResourceBundleMessageSource rb = new ResourceBundleMessageSource();
  79. rb.setBasenames(new String[] { "messages/messages", "messages/validation" });
  80. return rb;
  81. }
  82.  
  83.  
  84.  
  85. }
Add Comment
Please, Sign In to add comment