之前没有接触过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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@192.168.7.10:1521:VD"/> 
<property name="username" value="joker"/>
<property name="password" value="joker"/>
</bean>
<!--配置SqlSessionFactoryBean,然后注入到sessionFactory中-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
<property name="dataSource" ref="dataSource"/>  
        <!-- <property name="mapperLocations">  
                  <list>  
表示在com.ezca下entity目录里所有以Mapper.xml结尾所有文件 
<value>classpath:org/ezca/autocount/dao/*Mapper.xml</value>
</list>  
</property>  --> 
<!--这里面有mybatis使用的方言和分页插件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>  
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">   
<property name="basePackage" value="org.ezca.autocount.dao"/>   
<property name="markerInterface" value="org.ezca.autocount.dao.CertRegionMapper"/>
</bean>
<!--================================事务相关控制=================================================-->  
<context:component-scan base-package="org.ezca"> 
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 由spring管理mybatis的事物 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource"></property>  
    </bean> 
    <!-- 开启事务注解驱动 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <!-因为某一个操作,不想使用mybatis进行映射,故直接注入jdbcTemplate->
     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

接下来,目录结构,为了看得仔细,我非常标准的将代码分成了entity,service,dao三层。

接下来,是service对mapper的注入:
package org.ezca.autocount.service;
import javax.annotation.Resource;
import org.ezca.autocount.dao.CertRegionMapper;
import org.ezca.autocount.entiy.CertRegion;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class CertRegionService {
@Resource
private CertRegionMapper certRegionMapper;
@Transactional
public void addCertRegion(CertRegion certRegion){
certRegionMapper.inserData(certRegion);
}
@Transactional
public void updateCertRegion(CertRegion certRegion){
certRegionMapper.updateData(certRegion);
}
}
这样,对于mapper来说,申明式的打开了事务,就可以将CertRegionService 作为一个bean注入到action中使用了。
接下来,重点来了:
certRegionMapper.java和certRegionMapper.xml,将mapper文件和mapper接口类放在同一个包内,就在SqlSessionFactoryBean可以不用配置mapperLocations,否则,还需要单独的配置,appper.xml的位置。

注意:certRegionMapper.java,它就是一个普通的接口,如果它只是在MapperScannerConfigurer的配置的basePackage下的一个接口,那么spring会注册一个certRegionMapper的bean供调用,而如果它被配置到了markerInterface下,那么你只能使用继承了这个接口的接口了。
package org.ezca.autocount.dao;
import org.ezca.autocount.entiy.CertRegion;
public interface CertRegionMapper extends CertRegionMapper1{
//用于新增对象
public void inserData(CertRegion certRegion);
//用于更新对象
public void updateData(CertRegion certRegion);
}
接下来是CertRegionMapper.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.ezca.autocount.dao.CertRegionMapper">
<!-- 如入参数为多个字符串,也可以不用写parameterType而直接传入一个hashMap,用 #{KEY}的方式取值,同时,mapperxml不支持<>符号,需要&lt,&gt,代替-->
<select id="inserData" parameterType="org.ezca.autocount.entiy.CertRegion">
insert into auoto_cert_region values (#{id,jdbcType=VARCHAR},#{certSn,jdbcType=VARCHAR},#{cert_region,jdbcType=VARCHAR},#{isuse,jdbcType=VARCHAR},to_date(#{cert_optime,jdbcType=VARCHAR},'YYYY-MM-DD HH24:MI:SS'))
</select>
<!--   这是一个对有时间戳类型和blob类型的类进行操作的配置其中在参数中的类中的属性分别对应为byte[]和Date类型,hasTSPCert为boolean 类型,在类的属性中为boolean 。
<update id="updateSignedData" parameterType="org.ezca.autotsp.entity.SignatureData">
  update vd_signed_data t
    set t.tspSigendData =
    #{tspSigendData,jdbcType=BLOB},
    t.tspSignDate = #{tspSignDate,jdbcType=TIMESTAMP},
t.tspsigned = #{tspSigned},
t.hasTSPCert =#{hasTSPCert}
where t.id = #{id}  
    </update>
    
     -->
</mapper>

到这里,就完成了整合。

Spring整合mybatis完整项目相关推荐

  1. spring整合mybatis(实现数据的增删改查)

    一.专业术语解释 1.spring:是分层的Java SE/EE应用full - stack轻量级开源框架,以IoC(控制反转)和AOP(面向切面编程)为内核,提供展现层spring MVC 和 sp ...

  2. spring整合mybatis(入门级简单教程1)--在spring中配置c3p0,并成功测试

    引子:spring整合mybatis.因为,我们看完(我就是这样的)spring和mybatis之后,本想自己写一个小小的项目,以便加深理解,但是我发现在spring中整合mybatis并不是一件容易 ...

  3. SSM之二(Spring整合Mybatis)

    项目与外界交互大概过程如下图: 一般过程是: 前端发送请求,查询数据.增加数据.修改数据.删除数据 中间件经过处理后,对数据发送请求 数据库返回数据,中间件再对数据处理 中间件响应前端请求 上一节关注 ...

  4. Spring整合Mybatis之注解方式,(注解整合Junit)

    Spring整合Mybatis之注解方式 我有一篇博客详细写了我自己使用xml的方法Spring整合MyBatis,现在我就把核心配置文件中的每个bean的配置使用注解的方式实现 注解整合MyBati ...

  5. Spring整合mybatis中的sqlSession是如何做到线程隔离的?

    转载自  Spring整合mybatis中的sqlSession是如何做到线程隔离的? 项目中常常使用mybatis配合spring进行数据库操作,但是我们知道,数据的操作是要求做到线程安全的,而且按 ...

  6. Spring 整合 Mybatis

    数据库环境 // 创建mybatis数据库 create database mybatis;use mybatis // 创建teacher表 create table teacher(id int ...

  7. 【Java从0到架构师】Spring - 整合 MyBatis

    整合 MyBatis 整合 MyBatis - 依赖 整合 MyBatis - 数据源 整合 MyBatis - SqlSessionFactoryBean 整合 MyBatis - MapperSc ...

  8. Spring整合Mybatis之DAO层、Service层开发

    3. Spring整合Mybatis编程DAO层开发 1. 项目引入相关依赖spring mybatis mysql mybatis-spring druid2. 编写spring.xml整合:spr ...

  9. Spring——Spring整合MyBatis

    文章目录: 1.写在前面 2.实现步骤 2.1 项目的大体框架 2.2 使用Navicat在数据库中创建一张表student2 2.3 在pom.xml文件中加入maven依赖 2.4 编写实体类St ...

最新文章

  1. MATLAB寻址访问按什么优先,matlab笔记
  2. MySQL show processlist说明
  3. 居然还有人在用 System.out.println打日志的吗?
  4. 第三篇——第二部分——第四文 配置SQL Server镜像——非域环境
  5. 史上最全的SpringCloud入门学习教程
  6. Oracle创建简单视图案例
  7. 企业的核心竞争力是什么
  8. cocos2d-x初探学习笔记(17)--瓦片地图集
  9. 大学生体测成绩判断c语言_体育改革瞄准高校,体测不过关可能真的毕不了业了...
  10. 给定一个年份,判断是不是闰年
  11. 背景提取算法——帧间差分法、背景差分法、ViBe算法、ViBe+算法
  12. 广告创意还是侮辱女性?全棉时代卸妆巾广告被骂上热搜......
  13. MySQL索引,MySQL中索引的限制?
  14. 蓝桥杯 ADV-70 算法提高 冒泡法排序
  15. 详解机器学习之感知机理论与实践
  16. typora下载安装步骤
  17. 计算机电源大小,常见电脑主板和电源尺寸
  18. html双人对战源码,双人对战五子棋游戏 综合运用HTML、CSS、JavaScript实现
  19. [深度学习 - 实战项目] yoloV5人脸侦测arcFace人脸识别silentFace静态活体检测
  20. Scratch(三十八):八大行星

热门文章

  1. 全概率公式、贝叶斯公式推导过程
  2. 已解决ValueError: Expected 2D array, got 1D array instead
  3. 类似爱库存S2B2C的电商源码
  4. 第18讲 译码与译码器
  5. 深度学习中感受野的概念
  6. 老电脑跑win7卡慢的解决办法
  7. 【概率论与数理统计(研究生课程)】知识点总结9(回归分析)
  8. 欧拉方法python代码实现
  9. henauOJ1057(走向人生巅峰)
  10. JavaWeb笔记之SSH(Struts2框架)