Mybatis 可以说是 ibatis 的升级版 ,MyBatis 提供了更为强大的功能,同时并没有损失其易用性,相反,在很多地方都借助于 JDK 的泛型和注解特性进行了简化
Java EE 框架 这个框架需要提供一个全局配置文件,用于指定程序正常运行所需的设置和参数信息。而针对常用的持久层框架而言(Hibernate、JPA、iBatis 等),则通常需要两个配置文件:
一类用于指定数据源、事务属性以及其他一些参数配置信息(通常是一个独立的文件,可以称之为全局配置文件);
iBatis 一般在命名为sqlMapConfig.xml 而Mybatis 就要给命名为 Configuration.xml
另一类则用于指定数据库表和程序之间的映射信息(可能不止一个文件,我们称之为映射文件)。
Mybatis 配置文件需要<properties></properties>
<!--properties标签-->
<properties resource="jdbconfig.properties"></properties>
<!--配置环境-->
<environments default=" ">
<!--配置mysql的环境,可以配置多个数据库-->
<environment id=" ">
<!--配置事务的类型-->
<transactionManager type="JDBC"></transactionManager>
<!--配置连接数据库的4个基本信息-->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
properties:
jdbc.username=
jdbc.password=
jdbc.url=
jdbc.driver=
创建sql以及写sql的时候也会有略微不同
ibatis
<sqlMap namespace="VehLog">
<typeAlias alias="VehLogResult" type="路径"/>
<!-- 用于select查询公用抽取的列,注意将blob与clob对象单独处理 -->
<sql id="vehLogColumns">
<![CDATA[
A.LSH as lsh,
A.CLRQ as clrq,
]]>
</sql>
<!-- 动态SQL -->
<sql id="vehLogDynamicWhere">
<dynamic prepend="WHERE">
<isNotEmpty prepend="AND" property="lsh">
A.LSH = #lsh#
</isNotEmpty>
<isNotEmpty prepend="AND" property="clrqString">
to_char(A.CLRQ,'yyyy-mm-dd') = #clrqString#
</isNotEmpty>
</dynamic>
</sql>
<!-- insert操作-->
<insert id="insert" parameterClass="路径" >
<![CDATA[
INSERT INTO VEH_LOG(
LSH,
CLRQ,
) VALUES (
#lsh:VARCHAR#,
#clrq:TIMESTAMP#,
)
]]>
</insert>
<!-- update操作-->
<update id="update" parameterClass="路径">
UPDATE VEH_LOG
SET LSH = #lsh#,
SET CLRQ = #clrq#
<isNotNull prepend="," property="ywlx">
YWLX = #ywlx:CHAR#
</isNotNull>
WHERE
LSH = #lsh#,
CLRQ = #clrq#
</update>
<!-- delete操作-->
<delete id="delete" parameterClass="java.lang.String">
<![CDATA[
DELETE FROM VEH_LOG A WHERE
A.LSH = #lsh#,
A.CLRQ = #clrq#
]]>
</delete>
<!-- 取所有数据 -->
<select id="getAll" parameterClass="java.util.Map" resultClass="VehLogResult">
SELECT <include refid="vehLogColumns" />
FROM VEH_LOG A
<include refid="vehLogDynamicWhere"/>
<isNotEmpty property="sortColumns">
ORDER BY $sortColumns$
</isNotEmpty>
</select>
<!-- 通过主键读取VehLog对象 -->
<select id="getById" parameterClass="java.lang.String" resultClass="VehLogResult">
SELECT <include refid="vehLogColumns" />
<![CDATA[
FROM VEH_LOG A
WHERE
A.LSH = #lsh#,
A.CLRQ = #clrq#
]]>
</select>
<!-- 按字段取数据 -->
<select id="getByProperty" parameterClass="java.util.Map" resultClass="VehLogResult">
SELECT <include refid="vehLogColumns" />
FROM VEH_LOG A
<include refid="vehLogDynamicWhere"/>
<isNotEmpty property="sortColumns">
ORDER BY $sortColumns$
</isNotEmpty>
</select>
<!-- 取总数 -->
<select id="count" parameterClass="java.util.Map" resultClass="long">
SELECT count(*) FROM VEH_LOG A
<include refid="vehLogDynamicWhere"/>
</select>
<!-- 分页查询 -->
<select id="pageSelect" parameterClass="java.util.Map" resultClass="VehLogResult">
SELECT <include refid="vehLogColumns" />
FROM VEH_LOG A
<include refid="vehLogDynamicWhere"/>
<isNotEmpty property="sortColumns">
ORDER BY $sortColumns$
</isNotEmpty>
</select>
<!-- 多记录查询 -->
<select id="selectInId" parameterClass="java.util.Map" resultClass="VehLogResult">
SELECT <include refid="vehLogColumns" />
FROM VEH_LOG A
WHERE A.LSH ,A.CLRQ IN
<iterate property="idsList" open="(" close=")" conjunction=",">
#idsList[]#
</iterate>
<isNotEmpty property="sortColumns">
ORDER BY $sortColumns$
</isNotEmpty>
</select>
<!-- 按字段delete操作-->
<delete id="deleteByProperty" parameterClass="java.util.Map" >
DELETE FROM VEH_LOG A <include refid="vehLogDynamicWhere"/>
</delete>
<!-- 业务工作量统计 -->
<select id="ywgzltjGroupbyGlbm" parameterClass="java.util.Map" resultClass="java.util.HashMap">
SELECT A.GLBM,COUNT(*) AS NUM
FROM VEH_LOG A
<include refid="vehLogDynamicWhere"/>
GROUP BY A.GLBM
ORDER BY A.GLBM ASC
</select>
</sqlMap>
mybatis
<?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="com.pxj.mapper.BusinessMapper">
<resultMap id="BaseResultMap" type="com.pxj.pojo.Business">
<!--
WARNING - @mbg.generated
-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="img_file_name" jdbcType="VARCHAR" property="imgFileName" />
<result column="title" jdbcType="VARCHAR" property="title" />
<result column="subtitle" jdbcType="VARCHAR" property="subtitle" />
<result column="price" jdbcType="DECIMAL" property="price" />
<result column="distance" jdbcType="INTEGER" property="distance" />
<result column="number" jdbcType="INTEGER" property="number" />
<result column="desc" jdbcType="VARCHAR" property="desc" />
<result column="city" jdbcType="VARCHAR" property="city" />
<result column="category" jdbcType="VARCHAR" property="category" />
<result column="star_total_num" jdbcType="INTEGER" property="starTotalNum" />
<result column="comment_total_num" jdbcType="INTEGER" property="commentTotalNum" />
</resultMap>
<select id="caixihuan" resultMap="BaseResultMap">
SELECT * FROM business WHERE city=#{city}
</select>
<select id="xiangqing" resultMap="BaseResultMap">
SELECT * FROM business WHERE id =#{id}
</select>
<select id="chaxun" resultMap="BaseResultMap">
SELECT * FROM business
<where>
<if test="keyword!=null and keyword!=''">
and title LIKE CONCAT('%',#{keyword},'%')
</if>
<if test="city!=null and city!=''">
AND city=#{city}
</if>
<if test="category!=null and category!='' and category!='all'">
AND category=#{category}
</if>
</where>
</select>
<select id="pingjia" resultType="map">
SELECT b.title,c.`comment`,m.phone,c.star,o.id FROM business b INNER JOIN orders o ON b.id=o.business_id
INNER JOIN member m ON o.member_id = m.id
INNER JOIN `comment` c ON o.id = c.orders_id
where b.id=#{id}
</select>
<select id="shList" resultType="com.pxj.pojo.Business">
SELECT * FROM business
<where>
<if test="title!=null and title!=''">
title LIKE CONCAT('%',#{title},'%')
</if>
</where>
</select>
<insert id="inst" parameterType="com.pxj.pojo.Business" useGeneratedKeys="true" keyProperty="id">
INSERT INTO business(id,img_file_name,title,subtitle,price,distance,number,`desc`,city,category,star_total_num,comment_total_num)
VALUES(null,#{imgFileName},#{title},#{subtitle},#{price},#{distance},#{number},#{desc},#{city},#{category},#{starTotalNum},#{commentTotalNum})
</insert>
<update id="upd" parameterType="com.pxj.pojo.Business">
UPDATE business SET img_file_name=#{imgFileName},title=#{title},subtitle=#{subtitle},price=#{price},distance=#{distance},number=#{number},
`desc`=#{desc},city=#{city},category=#{category},star_total_num=#{starTotalNum},comment_total_num=#{commentTotalNum} WHERE id=#{id}
</update>
<select id="upcha" resultType="com.pxj.pojo.Business">
SELECT * FROM business WHERE id=#{id}
</select>
<select id="lqbz" resultType="com.pxj.pojo.Business">
SELECT b.category FROM business b INNER JOIN dic d WHERE b.category=d.code AND d.`name`=#{name}
</select>
</mapper>
以上是个人在开发中jue得的不同之处
今天的文章mybatis和mybatisplus的区别_org.apache.ibatis分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/69566.html