配置spring+springMVC+mybatis+oracle

項目結構:

1. applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:context="http://www.springframework.org/schema/context" 
		xmlns:p="http://www.springframework.org/schema/p"
		xmlns:aop="http://www.springframework.org/schema/aop" 
		xmlns:tx="http://www.springframework.org/schema/tx"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
							http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
							http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
							http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
							http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
							
	<!-- 開啓掃包 -->
	<context:component-scan base-package="com.spring.mvc" />
	<!-- 讀取properties文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	
	<!-- 配置數據源 -->
	<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="maxActive" value="10" />
		<property name="maxIdle" value="5" />
	</bean>
	
	<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置mybatis配置文件位置 -->
		<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
		<!-- 數據源 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- mapper代理(掃包方式配置代理)
		 該配置可以實現在service層使用mapper接口對象
	 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.spring.mvc.mapper"></property>
	</bean>
	
	<!-- 事務管理器 -->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 事務註解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
</beans>
	

2. springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		xmlns:p="http://www.springframework.org/schema/p"
		xmlns:context="http://www.springframework.org/schema/context"
		xmlns:mvc="http://www.springframework.org/schema/mvc"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        					http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        					http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<!-- 註解掃描 -->	   					   
	<context:component-scan base-package="com.spring.mvc" />
	
	<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->
	
	<!-- 配置mvc:
		包含RequestMappingHandlerMapping和RequestMappingHandlerAdapter
	 -->
	<mvc:annotation-driven />
	
	<!-- 配置前綴、後綴 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
</beans>

3. sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 該文件可只要文件頭,內容爲空 -->
<configuration>
    <!-- 配置簡名 -->
	<typeAliases>
		<package name="com.spring.mvc.entity"/>
	</typeAliases>
</configuration>

4. db.properties

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc.username=scott
jdbc.password=302030

5. Controller.java

@Controller
public class HelloWorldController {
	
	private EmpService empService;
	
	@Autowired
	public void setEmpService(EmpService empService) {
		this.empService = empService;
	}

}

6. Mapper

package com.spring.mvc.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Select;

import com.spring.mvc.entity.Emp;

public interface EmpMapper {

	@Select("SELECT * FROM EMP")
	List<Emp> findAll();
	
}

7. ServiceImpl

@Service
public class EmpServiceImpl implements EmpService {

	@Autowired
	private EmpMapper empMapper;
	
	@Override
	public List<Emp> findAll() {
		return empMapper.findAll();
	}
	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章