Spring4.1.5+Mybatis3.2.8基于java config整合实现(Maven)

喜欢用新版,不喜欢xml配置,所以就有了这个东西…除了Maven的pom.xml和Mybatis的mapper是xml的以外,没有xml配置。

所用到的东西:Eclipse JavaEE Luna Service Release 2 (4.4.2),内置Maven。Tomcat 7.0.54、JDK 7u71、Spring 4.1.5、Mybatis 3.2.8


步骤:

一、创建Maven项目

二、修改pom.xml,引入相关包

三、通过Mybatis Generator生成mapper相关文件并引入

四、创建Config相关配置类,实现无xml配置


前三步网上都很容易找到相关的文章,暂时省略。

目前只说第四步,网上绝大部分都是使用传统xml配置的,所以采用java config方式之后,不停的出错,最后终于解决了…

1、CommonInitializer 实现 WebApplicationInitializer,采用java config的第一步

@Order(1)
public class CommonInitializer implements WebApplicationInitializer {
	@Override
	public void onStartup(ServletContext container) {
		AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
		rootContext.register(AppConfig.class);
		container.addListener(new ContextLoaderListener(rootContext));

		AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
		dispatcherContext.register(DispatcherConfig.class);

		ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
		dispatcher.setLoadOnStartup(1);
		dispatcher.addMapping("/");
	}
}

2、AppConfig类,用来设置数据库、扫描mapper等。dbconfig.properties为数据库连接配置,放在resources目录下

@Configuration
@ComponentScan(basePackages = "com.eien.web.service")
@PropertySource("classpath:dbconfig.properties")
@MapperScan("com.eien.dao")
public class AppConfig {

	@Autowired
	Environment env;

	@Bean
	public DataSource dataSource() throws SQLException {
		BasicDataSource dSource = new BasicDataSource();
		dSource.setDriverClassName(env.getProperty("driverClassName"));
		dSource.setUrl(env.getProperty("jdbc_url"));
		dSource.setUsername(env.getProperty("jdbc_username"));
		dSource.setPassword(env.getProperty("jdbc_password"));
		dSource.setInitialSize(0);
		dSource.setMaxActive(20);
		dSource.setMaxIdle(20);
		dSource.setMinIdle(0);
		dSource.setMaxWait(60000);
		return dSource;
	}

	@Bean
	public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
		SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(dataSource());
		sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/eien/mapping/*.xml"));
		return sessionFactory;
	}

	@Bean
	public DataSourceTransactionManager txManager() throws SQLException {
		return new DataSourceTransactionManager(dataSource());
	}
}

3、DispatcherConfig,代替Spring MVC中的设置


@Configuration
@ComponentScan(basePackages = "com.eien.web.controller")
public class DispatcherConfig {

	@Bean
	ViewResolver viewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix("WEB-INF/jsp/");
		resolver.setSuffix(".jsp");
		return resolver;
	}
}

4、pom.xml中需要配置plugin,否则maven install的时候会提示没有web.xml

<build>
		<finalName>maventest</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

最重要的就是这几点配置,后续再详细补充…




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值