最近再了解一些springMVC写法,我知道现在最新的注释写法,但是现在很多厂子都还在用些配置文件.xml来进行配置这种写法。
我最近熟悉的注释写法,但是还是要看看xml写法的。
SpringConfig代码:
package com.daitu.config; import org.springframework.context.annotation.*; @Configuration @ComponentScan({"com.daitu.service"}) @PropertySource("classpath:/jdbc.properties") @Import({JdbcConfig.class, MybatisConfig.class}) public class SpringConfig { }
SpringMvconfig代码:
package com.daitu.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; //创建springmvc的配置文件,加载controller对应的bean @Configuration @ComponentScan("com.daitu.controller") @EnableWebMvc public class SpringMvcConfig { }
ServletConfig代码:
package com.daitu.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[]{SpringConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{SpringMvcConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } }
JdbcConfig代码:
package com.daitu.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import javax.sql.DataSource; public class JdbcConfig { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driver); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } }
MybatisConfig代码:
package com.daitu.config; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.context.annotation.Bean; import javax.sql.DataSource; public class MybatisConfig { @Bean public SqlSessionFactoryBean sessionFactoryBean(DataSource dataSource) { SqlSessionFactoryBean factoryBean =new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setTypeAliasesPackage("com.daitu.domain"); return factoryBean; } @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer msc =new MapperScannerConfigurer(); msc.setBasePackage("com.daitu.dao"); return msc; } }
标签:springMVC,com,daitu,dataSource,org,import,写法,class From: https://www.cnblogs.com/daitu66/p/17638103.html