您现在的位置是:首页 > 文章详情

初学者Mybatis的初级使用

日期:2018-11-18点击:339

前言:本文章主要分三点内容对于Mybatis的初级使用进行教学,我们因为只是为了实现功能,而不是去了解底层封装原理,因此,我们只需要了解,Mybatis只是用来后端管理sql 管理dao层的框架即可!
第一点:导框架包。
第二点:配置文件
第三点:对于dao接口实现的xml文件进行编写(管理sql)

正文


第一点:
因为我使用的是Maven的工程模式来进行系统的开发,因此我在导jar包的过程不需要去直接下载jar放到工程,然后再add build...而我因为使用Maven只需要在jar网上复制他的配置文件,Maven就可以直接帮我导下来使用:
在工程名.web中找到pom.xml中,找到意思是所有,在这个的范围内加入调用mybatis的架包:
 <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- MyBatis相关依赖 Ends -->

这样就完成了第一步。
第二点:去需要使用这个框架的工程里面写配置文件,然后就可以使用
配置文件的书写:
在resource目录下建立mybatis-config.xml文件(文件名随意啦),这是个是mybatis的主配置文件。里面主要配置property,typeAlias,typeHandler,environment,mapper等。
我的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <!-- 加载相关资源文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:oracle.properties</value> <value>classpath:redis.properties</value> </list> </property> <property name="fileEncoding" value="UTF-8"></property> </bean> <!-- 开启注解扫描 --> <context:annotation-config/> <!-- 自动扫描的包名 --> <context:component-scan base-package="org.mvtts"> <!-- 不控制 Controller注解,即与 Controller分离 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 配置数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.oracle.driverClassName}"/> <property name="url" value="${jdbc.oracle.url}"/> <property name="username" value="${jdbc.oracle.username}"/> <property name="password" value="${jdbc.oracle.password}"/> <property name="maxActive" value="${jdbc.oracle.maxActive}"/> <property name="initialSize" value="${jdbc.oracle.initialSize}"/> <property name="maxWait" value="60000"/> <property name="minIdle" value="5"/> <property name="removeAbandoned" value="true"/> <property name="removeAbandonedTimeout" value="180"/> <property name="connectionProperties" value="config.decrypt=true"/> </bean> <!-- SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据库连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 加载MyBatis的全局配置文件 --> <property name="mapperLocations"> <list> <value>classpath:**/dao/**/*Mapper.xml</value> </list> </property> </bean> <!-- Mapper扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 --> <!-- 对**.dao包内进行扫描 --> <property name="basePackage" value="**.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 事务管理器,对MyBatis操作数据库事务控制,Spring使用jdbc的事务控制类 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" scope="singleton"> <!-- 数据源dataSource在applicationContext-mybatis.xml中配置了 --> <property name="dataSource" ref="dataSource"/> </bean> <!-- 注解的事务管理@Transactional --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> 

最后就是写dao里面接口实现的xml
下面是我写的xml
<?xml version="1.0" encoding="UTF-8" ?>


<id column="EVS_ID" property="evsId" jdbcType="DECIMAL" /> <result column="EVS_USE_ID" property="evsUseId" jdbcType="DECIMAL" /> <result column="EVS_ORTHER_ID" property="evsOrtherId" jdbcType="DECIMAL" /> <result column="EVS_CONTENT" property="evsContent" jdbcType="VARCHAR" /> <result column="EVS_STA_ID" property="evsStaId" jdbcType="DECIMAL" /> <result column="EVS_MEMO" property="evsMemo" jdbcType="VARCHAR" /> <result column="EVS_CREATE_TIME" property="evsCreateTime"/> <result column="EVS_UPDATE_TIME" property="evsUpdateTime" /> <result column="EVS" property="evs" />



<result column="EVS_ID" property="evsId" jdbcType="DECIMAL" /> <result column="EVS_USE_ID" property="evsUseId" jdbcType="DECIMAL" /> <result column="EVS_ORTHER_ID" property="evsOrtherId" jdbcType="DECIMAL" /> <result column="EVS_CONTENT" property="evsContent" jdbcType="VARCHAR" /> <result column="EVS_STA_ID" property="evsStaId" jdbcType="DECIMAL" /> <result column="EVS_MEMO" property="evsMemo" jdbcType="VARCHAR" /> <result column="EVS_CREATE_TIME" property="evsCreateTime"/> <result column="EVS_UPDATE_TIME" property="evsUpdateTime" /> <result column="EVS" property="evs" /> <result column="SAP_NAME" property="sapName"/> <result column="SAP_ID" property="sapId" />



 SELECT SEQ_EVA_SCHOOL.NEXTVAL FROM dual </selectKey> INSERT INTO "EVALUATE_SCHOOL" ( "EVS_ID", "EVS_USE_ID", "EVS_ORTHER_ID", "EVS_CONTENT", "EVS", "EVS_STA_ID", "EVS_MEMO", "EVS_CREATE_TIME", "EVS_UPDATE_TIME" ) VALUES ( #{evsId}, #{evsUseId}, #{evsOrtherId}, #{evsContent}, #{evs}, 1, '', SYSDATE, SYSDATE )


 SELECT A.*, B.SAP_NAME, B.SAP_ID FROM EVALUATE_SCHOOL A INNER JOIN STUDENT_APPLY B ON B.SAP_STU_ID = A .EVS_USE_ID WHERE A .EVS_ORTHER_ID = #{driId}

原文链接:https://yq.aliyun.com/articles/670915
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章