Spring , Spring mybatis 配制文件 模板

`### Spring , Spring mybatis 配制文件 模板

<?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:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!--定义数据配制文件地址-->
  <util:properties id="jdbc" location="classpath:jdbc.properties"/>
    <!-- 定义数据源 -->
    <bean id="ds" 
        class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="url" value="#{jdbc.url}"/>
        <property name="driverClassName" value="#{jdbc.driver}"/>
        <property name="username" value="#{jdbc.user}"/>
        <property name="password" value="#{jdbc.password}"/>
    </bean>    
    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.tarena"/>
    <!-- 开启MVC注解扫描 -->
    <mvc:annotation-driven/>
    
    <!-- 定义视图解析器ViewResolver -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"/> <!-- 前缀-->
        <property name="suffix" value=".jsp"/>  <!-- 后缀-->
    </bean>
    <!-- 处理系统异常 -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>   
           <!-- 异常类型 与 发生异常之后转到的访问地址 -->
                 <prop key="java.lang.Exception">main/error</prop>
            </props>
        </property>
    </bean>    
    <!-- -->
    <!-- 拦截器 -->
    <mvc:interceptors>
        <!-- 登录检查拦截器 -->
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <!--免拦截的请求路径-->
            <mvc:exclude-mapping path="/login/toLogin.do"/>
            <mvc:exclude-mapping path="/login/checkLogin.do"/>
               <!--拦截器类 --> 
            <bean class="com.tarena.web.LoginInterceptor"/> 
        </mvc:interceptor>
    </mvc:interceptors>
<!-- 整合JDBC ,如果编程时准备使用JdbcTemplate 完成DAO类数据查询功能则配制此处 -->
 <bean class="org.springframework.jdbc.core.JdbcTemplate">
     <property name="dataSource" ref="ds"/>
 </bean>
 
 <!-- session工厂 可根据映射文件自动生成DAO -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <property name="dataSource" ref="ds"  /><!--引用数据源-->
     <property name="mapperLocations"
         value="classpath:com/tarena/dao/*.xml" /> <!--此处配制映射文件路径-->
 </bean>
   <!--定义SqlSessionTemplate  如果编程时准备使用SqlSessionTemplate 完成DAO类数据查询功能则配制此处 -->
   <bean id="sqlSessionTemplate" 
       class="org.mybatis.spring.SqlSessionTemplate">
       <constructor-arg index="0" ref="sqlSessionFactory"/>
   </bean>

   <!-- 扫描指定包下带有注解@MyBatisRepository的接口 -->    
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.tarena.dao"   /> <!--扫描的包 -->
       <property name="annotationClass" 
           value="com.tarena.annotation.MyBatisRepository"  /> <!--注解类 -->
   </bean>
   
</beans>

映射文件配制模板


<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"      
 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.tarena.dao.DeptMapper">

<!-- 配制规则: 1,id 与 DAO接口中方法名一致 2,返回类型与 方法返回类型一致 3,参数类型与 方法参数类型一致 4,namespace 属性与DAO类一致 5,另外 若 实体类属性名与表字段名不一致则 考虑配制resultMap -->

<select id="findAll" 
       resultType="com.tarena.entity.Dept">
       select * from t_dept
   </select>
   
   <select id="findById"
       parameterType="int"
       resultType="com.tarena.entity.Dept">
       select * from t_dept where deptno=#{id}
   </select>
   
   <insert id="save"
       parameterType="com.tarena.entity.Dept">
       insert into t_dept values(
           dept_seq.nextval,
           #{dname},
           #{loc}
       )
   </insert>
   
   <update id="update"
       parameterType="com.tarena.entity.Dept">
       update t_dept set
           dname=#{dname},
           loc=#{loc}
       where deptno=#{deptno}
   </update>
   
   <delete id="delete"
       parameterType="int">
       delete from t_dept
       where deptno=#{id}
   </delete>
   
</mapper>`

转载于:https://my.oschina.net/dou2016/blog/704690

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值