增加(insert)
<insert id="saveOil" parameterType="map">
insert into b_oil(id, oil_name, address, longitude, latitude, discount, explains, adddate)
values (#{id}, #{oil_name}, #{address}, #{longitude}, #{latitude}, #{discount}, #{explains}, now())
</insert>
删除(delete)
语法:
删除某个表的一条数据:
DELETE FROM [table_name] WHERE id = xxx;
- 如果没有指定 WHERE 子句,MySQL 表中的所有记录将被删除。
- 你可以在 WHERE 子句中指定任何条件
- 您可以在单个表中一次性删除记录。
删除某个表的多条数据:
delete from [table_name] where id in (12,13,14,15);
删除多个表的同一条数据:
DELETE [表1],[表2] from [表1] LEFT JOIN [表2] ON [表1].id=[表2].id WHERE [表1].id=25;
修改(update)
修改一个表的某个字段:
update [表名] set 字段名 = [要修改之后的值] where id = [某一行];
eg:修改ID为11的age字段
update t_user set age = 22 where id = 11;
修改一个表的多个字段信息:and
update [表名] set 字段名 = [要修改之后的值] and 字段名 = [要修改之后的值] where id = [某一行] and id = [某一行];
mybatis动态修改语句
<update id="updateOil" parameterType="map">
update b_oil
<trim prefix="set" suffixOverrides=",">
<if test="oil_name != null and oil_name != '' ">
oil_name = #{oil_name},
</if>
<if test="address != null">
address =# {address},
</if>
<if test="longitude != null">
longitude = #{longitude},
</if>
<if test="latitude != null">
latitude = #{latitude},
</if>
<if test="discount != null">
discount = #{discount},
</if>
<if test="explains != null">
explains = #{explains},
</if>
<if test="isdel != null and isdel != '' ">
isdel = #{isdel},
</if>
<if test="status != null ">
status = #{status}
</if>
</trim>
where id = #{id}
</update>
查询(select)
查询一个表的所有信息:
select * from [表名];
查询一个表的某个字段:
SELECT * FROM [表名] WHERE ID = #{id};
动态查询:o 为表 b_oil 的别名
<select id = "queryOilCount" parameterType = "map" resultType = "int">
select count(1)
from b_oil o
<where>
o.isdel = 0
<if test = "oil_name != null and oil_name !='' ">
and o.oil_name like concat ('%',#{oil_name},'%')
</if>
</where>
</select>
详细一点的(可以参考语句中的顺序)
<select id="queryOil" parameterType="map" resultMap="oilBean">
select
o.`id`,
o.`oil_name`,
o.`address`,
o.`longitude`,
o.`latitude`,
o.`discount`,
o.`explains`,
date_format (o.`adddate`, '%Y-%m-%d %H:%i') adddate,
o.status,
o.balance
from
b_oil o
<where>
o.isdel=0
<if test="oil_name !=null and oil_name !='' ">
and o.oil_name like concat('%',#{oil_name},'%')
</if>
<if test="address !=null and address !='' ">
and o.address like concat('%',#{address},'%')
</if>
<if test="status!=null and status!=''">
and o.status=#{status}
</if>
</where>
order by o.adddate desc
<if test="begin!=null">
limit #{begin},#{pageSize}
</if>
</select>
select元素属性信息
属性 | 描述 |
---|---|
id | 在命名空间中唯一的标识符,可以被用来引用这条语句。 |
parameterType | 将会传入这条语句的参数的类全限定名或别名。这个属性是可选的,因为 MyBatis 可以通过类型处理器(TypeHandler)推断出具体传入语句的参数,默认值为未设置(unset)。 |
resultType | 期望从这条语句中返回结果的类全限定名或别名。 注意,如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身的类型。 resultType 和 resultMap 之间只能同时使用一个。 |
resultMap | 对外部 resultMap 的命名引用。结果映射是 MyBatis 最强大的特性,如果你对其理解透彻,许多复杂的映射问题都能迎刃而解。 resultType 和 resultMap 之间只能同时使用一个。 |
flushCache | 将其设置为 true 后,只要语句被调用,都会导致本地缓存和二级缓存被清空,默认值:false。 |
useCache | 将其设置为 true 后,将会导致本条语句的结果被二级缓存缓存起来,默认值:对 select 元素为 true。 |
timeout | 这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数。默认值为未设置(unset)(依赖数据库驱动)。 |
fetchSize | 这是一个给驱动的建议值,尝试让驱动程序每次批量返回的结果行数等于这个设置值。 默认值为未设置(unset)(依赖驱动)。 |
statementType | 可选 STATEMENT,PREPARED 或 CALLABLE。这会让 MyBatis 分别使用 Statement,PreparedStatement 或 CallableStatement,默认值:PREPARED。 |
resultSetType | FORWARD_ONLY,SCROLL_SENSITIVE, SCROLL_INSENSITIVE 或 DEFAULT(等价于 unset) 中的一个,默认值为 unset (依赖数据库驱动)。 |
databaseId | 如果配置了数据库厂商标识(databaseIdProvider),MyBatis 会加载所有不带 databaseId 或匹配当前 databaseId 的语句;如果带和不带的语句都有,则不带的会被忽略。 |
resultOrdered | 这个设置仅针对嵌套结果 select 语句:如果为 true,将会假设包含了嵌套结果集或是分组,当返回一个主结果行时,就不会产生对前面结果集的引用。 这就使得在获取嵌套结果集的时候不至于内存不够用。默认值:false 。 |
resultSets | 这个设置仅适用于多结果集的情况。它将列出语句执行后返回的结果集并赋予每个结果集一个名称,多个名称之间以逗号分隔。 |