spring+springmvc+mybatis+oracle整合开发核心配置代码

1.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- 核心springmvc核心控制器 -->
    <servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
	
	<!-- POST编码过滤器 -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

2.mybatis.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>

  <!-- <environments default="oracle_developer">
     <environment id="mysql_developer">
        <transactionManager type="jdbc"></transactionManager>
        <dataSource type="pooled">
          <property name="driver" value="com.mysql.jdbc.Driver"></property>
          <property name="url" value="jdbc:mysql://localhost:3306/mybatis"></property>
          <property name="username" value="root"></property>
          <property name="password" value="123456"></property>  
        </dataSource>
     </environment>
     
     <environment id="oracle_developer">
        <transactionManager type="jdbc"></transactionManager>
        <dataSource type="pooled">
          <property name="driver" value="oracle.jdbc.driver.OracleDriver"></property>
          <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:mldn"></property>
          <property name="username" value="scott"></property>
          <property name="password" value="tiger"></property>  
        </dataSource>
     </environment>
  </environments>
     -->
    <mappers>
         <mapper resource="cn/itcast/entity/EmpMapper.xml"/>
    </mappers>

</configuration>

3.spring.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	 http://www.springframework.org/schema/beans/spring-beans.xsd
     	 http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
     	 http://www.springframework.org/schema/tx/spring-tx.xsd
     	 http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
     	 ">

	<!-- 配置C3P0连接池,目的:管理数据库连接 -->
	<bean id="comboPooledDataSourceID" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
		<property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:mldn"></property>
		<property name="user" value="scott"></property>
		<property name="password" value="tiger"></property>
	</bean>
	
	 <!-- 配置SqlSessionFactoryBean,目的:加载mybaits配置文件和映射文件,即替代原Mybatis工具类的作用 -->
	 <bean id="sqlSessionFactoryBeanID" class="org.mybatis.spring.SqlSessionFactoryBean">
	    <property name="configLocation" value="classpath:mybatis.xml"/>
	    <property name="dataSource" ref="comboPooledDataSourceID"/>
	 </bean>
	 
	 <!-- 配置Mybatis的事务管理器,即因为Mybatis底层用的是JDBC事务管事器,所以在这里依然配置JDBC事务管理器 -->
	 <bean id="dataSourceTransactionManagerID" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      		<property name="dataSource" ref="comboPooledDataSourceID"/>
     </bean>
	
	 <!-- 配置事务通知,即让哪些方法需要事务支持 -->
	 <tx:advice id="tx" transaction-manager="dataSourceTransactionManagerID">
	     <tx:attributes>
	         <tx:method name="*" propagation="REQUIRED"></tx:method>
	     </tx:attributes>
	 </tx:advice>
	
	 <!-- 配置事务切面,即让哪些包下的类需要事务 -->
	 <aop:config>
	    <aop:pointcut id="pointcut" expression="execution(* cn.itcast.dao.*.*(..))"></aop:pointcut>
	    <aop:advisor advice-ref="tx" pointcut-ref="pointcut"></aop:advisor>
	 </aop:config>
	
	 <!-- 注册EmpDao -->
	 <bean id="empDaoID" class="cn.itcast.dao.EmpDao">
	     <property name="sqlSessionFactory" ref="sqlSessionFactoryBeanID"></property>
	 </bean>
	 
	 <!-- 注册EmpService -->
	 <bean id="empServiceID" class="cn.itcast.service.EmpService">
	     <property name="empDao" ref="empDaoID"></property>
	 </bean>
	 
	 <!-- 注册EmpAction -->
	 <context:component-scan base-package="cn.itcast.action"></context:component-scan>
	 
	  <!-- 通知springioc容器这些注解的作用 -->
	  <context:annotation-config/>
	  
	  <!-- 视图解析器 -->
	  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	      <property name="prefix" value="/jsp/"/>
	      <property name="suffix" value=".jsp"/>
	  </bean>
</beans>     

4.EmpAction

package cn.itcast.action;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.itcast.entity.Emp;
import cn.itcast.service.EmpService;
//单例,控制器

@Controller
@RequestMapping(value="/emp")
public class EmpAction {
	
	private EmpService empService;
	@Resource(name="empServiceID")
	public void setEmpService(EmpService empService) {
		this.empService = empService;
	}
	//注册员工
	@RequestMapping(value="/register")
	public String register(Emp emp) throws Exception{
		//调用业务层
		empService.register(emp);
		return "success";
	}

}
5.register.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>员工注册</title>
  <body>
     <form action="${pageContext.request.contextPath }/emp/register.action" method="post">
        <table border="2" align="center">
          <tr>
             <th>编号</th>
             <td><input type="text" name="id"/></td>
          </tr>
          <tr>
				<th>姓名</th>
				<td><input type="text" name="name"></td>
			</tr>
			<tr>
				<th>薪水</th>
				<td><input type="text" name="sal"></td>
			</tr>
			<tr>
				<th>性别</th>
				<td>
					<input type="radio" name="sex" value="男"/>男
					<input type="radio" name="sex" value="女" checked/>女
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<input type="submit" value="注册"/>
				</td>
			</tr> 
        </table>
     </form>
  </body>
</html>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值