配置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();
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值