电商平台学习笔记(四)——Spring配置Mybatis简化DAO层省略Mybatis核心配置文件

2023-11-05

今天看到电商视频教程第三天的时候,惊奇的发现,这个电商平台一共有16张表!加上MyBatis的模板Bean,一共32个Bean!如下图:
想到这里,如果有Mybatis的核心配置文件:mybatis-config.xml!仅对每一JavaBean使用<typeAliase>标签就得使用32个,使用<mapper>标签也得使用16个来包括对应的每一张表的配置文件!这种工作量是很大的!(关键还没有技术含量,只是不断的重复使用Ctrl+C和Ctrl+V组合键)。我们肯定不想做这种重复性且毫无技术含量的工作(以前写代码时的确做了不少! Spring配置Mybatis简化DAO层省略Mybatis核心配置文件 - 享受孤独 - 享受孤独的博客 )!所以没办法,自己也做了一个Demo去尝试另外一种省事的 配置方式!别不说,直接上代码:
Demo主要目录结构(src下只是在web.xml中配置):
    Spring配置Mybatis简化DAO层省略Mybatis核心配置文件 - 享受孤独 - 享受孤独的博客

  Spring注解配置文件annotation.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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!-- Spring扫包 @Service .... --> <context:component-scan base-package="cn.ilxy.service"/> </beans>

Spring数据库jdbc配置文件:jdbc.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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!-- 配置C3P0 --> <!-- c3p0连接池 DBCP c3p0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 连接池中的最大连接数 --> <property name="maxPoolSize" value="40" /> <!-- 连接池中的最小连接数 --> <property name="minPoolSize" value="3"></property> <!-- 初始化连接池中的 连接数,取值 在 minPoolSize 和 maxPoolSize 之间,default:3 --> <property name="initialPoolSize" value="1" /> <!-- 最大空闲时间,60s内该连接没有被使用则被丢弃,若为0 永不丢弃.default:0 --> <property name="maxIdleTime" value="60" /> <!-- 当连接数不够时,每次同时创建多少个连接 --> <property name="acquireIncrement" value="1" /> <!-- 每60s检查连接池中的所有空间连接,如果没有被使用,就被放弃, default:0 --> <property name="idleConnectionTestPeriod" value="60" /> </bean> </beans>

Spring整合Mybatis配置文件:mybatis.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd "> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:cn/ilxy/dao/*.xml"/> <property name="typeAliasesPackage" value="cn.ilxy.bean"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.ilxy.dao"/> </bean> </beans>

Spring加载jdbc连接数据库信息文件:property.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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!-- jdbc的配置:使用这种方式list标签能写多个配置文件 --> <value>classpath:properties/jdbc.properties</value> </list> </property> </bean> </beans>

Spring事务支持配置文件:transaction.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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <aop:aspectj-autoproxy /> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>

数据库相关信息文件:jdbc.peoperties

jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/new_babasport?useUnicode=true&amp;characterEncoding=UTF-8&amp;allowMultiQueries=true jdbc.username=root jdbc.password=950808

TestDBDao.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="cn.ilxy.dao.TestDBDAO"> <resultMap type="TestTb" id="testTb"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> <result column="birthday" jdbcType="VARCHAR" property="birthday" /> </resultMap> <insert id="testSave" parameterType="TestTb" useGeneratedKeys="true"> INSERT test_tb(name , birthday) VALUES(#{name} , #{birthday}) </insert> </mapper>

TestTb.java代码:

package cn.ilxy.bean; public class TestTb { private int id; private String name; private String birthday; /**************省略了get和set方法****************/ }

TestDBDAO.java代码:

package cn.ilxy.dao; import cn.ilxy.bean.TestTb; public interface TestDBDAO { public void testSave(TestTb testTb); }

TestDbService.java代码:

package cn.ilxy.service; import cn.ilxy.bean.TestTb; public interface TestDbService { public void testDbSave(TestTb testTb) ; }

TestDbServiceImpl.java代码:

package cn.ilxy.service; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import cn.ilxy.bean.TestTb; import cn.ilxy.dao.TestDBDAO; @Service @Transactional public class TestDbServiceImpl implements TestDbService{ @Resource private TestDBDAO dao; public void testDbSave(TestTb testTb) { dao.testSave(testTb); } }

好了,到这里,主要的文件源码都在这里了!经过测试,证明是没有问题的!
不过在配置这个的过程中,也是蛮曲折的!主要是一直要这个异常,如下:

Caused by: java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Wrong namespace. Expected 'cn.ilxy.dao.TestDBDao' but found 'cn.ilxy.bean.TestTb'. at org.mybatis.spring.mapper.MapperFactoryBean.checkDaoConfig(MapperFactoryBean.java:83) at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ... 40 more Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Wrong namespace. Expected 'cn.ilxy.dao.TestDBDao' but found 'cn.ilxy.bean.TestTb'. at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:120) at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:92) at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.loadXmlResource(MapperAnnotationBuilder.java:167) at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parse(MapperAnnotationBuilder.java:118) at org.apache.ibatis.binding.MapperRegistry.addMapper(MapperRegistry.java:72) at org.apache.ibatis.session.Configuration.addMapper(Configuration.java:685) at org.mybatis.spring.mapper.MapperFactoryBean.checkDaoConfig(MapperFactoryBean.java:80) ... 43 more Caused by: org.apache.ibatis.builder.BuilderException: Wrong namespace. Expected 'cn.ilxy.dao.TestDBDao' but found 'cn.ilxy.bean.TestTb'. at org.apache.ibatis.builder.MapperBuilderAssistant.setCurrentNamespace(MapperBuilderAssistant.java:78) at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:112) ... 49 more

这个异常(最后面,加粗部分)意思是说,错误的命名空间,本来该命名空间(namespace)应该是填 cn.ilxy.dao.TestDBDao, 但是实际上是 cn.ilxy.bean.TestTb !后来照着视频再看一遍才知道答案!在 TestDBDao.xml文件中的 namespace属性我填错了(填成了 cn.ilxy.bean.TestTb,有核心配置文件时可以填这个 )!后来改成 cn.ilxy.dao.TestDBDao就能正常运行了!
之后在百度上百度了一下,才明白其中真正的原因!这个namespace属性一定要写成表对应的接口,并且接口中的方法一定要和增删改查语句的id对应:即没对应的话,最终执行的操作并不是你预期的操作,不存在的话则直接报异常(我做过测试了:将接口中的唯一方法改成了testAdd,报错信息如下: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.ilxy.dao.TestDBDAO.testAdd )!这里我就有一个大胆的猜测:Spring的底层实现机制使用了反射!
果然,网上百度了一下MapperScannerConfigurer的实现原理!看了别人的博客发现:Spring在底层的确是根据以上的两项信息,利用JDK的代理!继承实现InvokeHandler接口并注入了SqlsessionFactory)而帮我们产生了代理动态代理类,我们才能只定义接口而不需要去实现该接口了!在这里,我们不得不感叹Spring的神奇之处了!我也是深深的体会到了编程之美!
另附上另外一篇文章的地址 !http://blog.csdn.net/hupanfeng/article/details/21454847!感谢这位大神,通过他写的博客证实了我的猜测!
今天也是花了很长时间才解决那个Bug,觉得很有价值所以写这篇博客记录下来,分享给大家!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

电商平台学习笔记(四)——Spring配置Mybatis简化DAO层省略Mybatis核心配置文件 的相关文章

随机推荐

  • Inno Setup 系列之添加管理员权限

    需求 Inno Setup打包的程序安装完成后运行失败 这个是因为权限不够 打包的应用需要管理员权限 解决 1 在 Setup 节点添加 PrivilegesRequired admin 2 在Inno Setup的安装目录下有个Setup
  • Python机器学习从零开始(三)数据准备

    目录 1 数据预处理 1 1调整数据尺度 1 2正态化数据 1 3标准化数据 1 4二值数据 2 数据特征选定 2 1单变量特征选定 2 2递归特征消除 2 3数据降维 2 4特征重要性 总结 特征选择时困难耗时的 也需要对需求的理解和专业
  • 微信小程序 多选项目 checkbox & checkbox-group 组件

    完整微信小程序 Java后端 技术贴目录清单页面 必看 多项选择器 checkbox group 内部由多个 checkbox组成 checkbox多项选择器组件 和HTML中的多选框是一样的 用来进行多项选择 属性 类型 默认值 必填 说
  • 一次K8S实操相关的实用笔记,希望能帮助大家填点坑

    前言 前段时间做了些 K8S 理论上的储备 也在测试的 K8S 集群上做了些简单的测试 虽然感觉收获颇丰 但是纸上得来终觉浅 没有经过生产环境的捶打和磨练 还是没办法掌握这种技术的精华 学到的东西更多的也只是流于表面 无法深入其中 正巧最近
  • 自学Android开发 AES加密

    目录 一 问题 二 解决源码 一 问题 在Java 后端发来的AES加密数据时 发现Android APP不能解密 而且Android 端 加密同样的数据返回的结果居然不一致 所以我在网上查询后 发现在SecureRandom在产生安全随机
  • Android Spider Charles - 夜神模拟器证书安装App抓包

    文章目录 前言 一 软件安装 1 Openssl安装 1 1下载安装 1 2配置环境变量 1 3查看openssl版本 输入命令 openssl version 2 夜神模拟器安装 1 1 下载安装 1 2工具准备 MT管理器 3 Char
  • NLP预处理之分词工具的使用(jieba,hanlp)

    文章目录 一 jieba简介 二 jieba的使用 1 精确模式分词 2 全模式分词 3 搜索引擎模式分词 4 使用用户自定义分词 三 hanlp简介 四 hanlp的使用 1 使用hanlp进行中文分词 2 使用hanlp进行英文分词 五
  • Python3学习之数字

    Python 数字数据类型用于存储数值 数据类型是不允许改变的 这就意味着如果改变数字数据类型的值 将重新分配内存空间 以下实例在变量赋值时 Number 对象将被创建 var1 1 var2 10 可以使用del语句删除一些数字对象的引用
  • msfvenom常用参数

    Kali中的 msfvenom 取代了msfpayload和msfencode 常用于生成后门木马 msfpayload是MSF攻击荷载生成器 用于生成shellcode和可执行代码 msfencode是MSF编码器 一 msfvenom
  • 【socket】listen函数及backlog参数

    man listen显示 LISTEN 2 Linux Programmer s Manual LISTEN 2 NAME listen listen for connections on a socket SYNOPSIS include
  • 华为OD机试 - 阿里巴巴找黄金宝箱(II)(Java & JS & Python)

    题目描述 一贫如洗的樵夫阿里巴巴在去砍柴的路上 无意中发现了强盗集团的藏宝地 藏宝地有编号从0 N的箱子 每个箱子上面贴有箱子中藏有金币的数量 从金币数量中选出一个数字集合 并销毁贴有这些数字的每个箱子 如果能销毁一半及以上的箱子 则返回这
  • 第十八课,立方体贴图(加载天空盒)

    原理我在这里不再过多叙述 主要从代码的运行方向来解读立方体贴图 添加天空盒顶点 float skyboxVertices positions 1 0f 1 0f 1 0f 1 0f 1 0f 1 0f 1 0f 1 0f 1 0f 1 0f
  • mysql--部门表员工表练习题

    部门表 create table dept deptno int primary key auto increment 部门编号 dname varchar 14 部门名字 loc varchar 13 地址 insert into dep
  • java核心技术卷一学习(一)

    1 java的微型版Java Micro Edition可以适用于嵌入式编程 2 0X是十六进制 0是8进制 0B是2进制 字母大小写均可 3 java 8种基本类型4中整形 byte 1字节 short 2字节 int 4字节 long
  • 曝光:超级实习生们的训练不一般

    有同学反映在面试华为时 总共四面 到技术面的时候被毙掉了 而且短时间内不能进行二次投递 这就非常的痛苦 虽然现在网上有很多的面试资料 其中不乏一些干货精华 但是只收藏不会用或者只会背八股文 面试时会非常被动 在面试中 动手编程能力是最重要的
  • vue3加载element-plus,修改端口

    环境 最新的node js 最新的vue 安装element plus npm i element plus save legacy peer deps 加载element plus 之前的vue2的方法不适用 import Element
  • 外包三年半,人废了一半

    如果不是女朋友和我提分手 我估计现在还没醒悟 大专生 18年通过校招进入湖南某软件公司 干了3年多的CRUD 今年年初 感觉自己不能够在这样下去了 长时间呆在一个舒适的环境会让一个人堕落 而我已经在一个企业干了3年的CRUD 已经让我变得不
  • VUE+Element UI 展示json数据 [vue-json-viewer]

    1 安装组件vue json viewer npm install vue json viewer save 2 在main js中引入并使用vue json viewer import JsonViewer from vue json v
  • linux下安装git-lfs的两种方法

    一 方法一 推荐 首先安装git lfs ubuntu版 sudo apt get install git lfs centeros版 sudo yum install git lfs 然后验证安装成功 git lfs install 若显
  • 电商平台学习笔记(四)——Spring配置Mybatis简化DAO层省略Mybatis核心配置文件

    今天看到电商视频教程第三天的时候 惊奇的发现 这个电商平台一共有16张表 加上MyBatis的模板Bean 一共32个Bean 如下图 想到这里 如果有Mybatis的核心配置文件 mybatis config xml 仅对每一JavaBe