MyBatis 核心文件配置 动态sql

MyBatis 配置文件

配置 config.properties 文件
driver=com.mysql.jdbc.Driver
url=数据库信息
#username=用户名
#password=用户密码
配置 SqlMapConfig.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
	<!-- 
		1)在 properties 元素体内指定的属性首先被读取。
		2)然后根据 properties 元素中的 resource 属性读取类路径下属性文件或根据 url 属性指定的路径读取属性文件,并覆盖已读取的同名属性。
		3)最后读取作为方法参数传递的属性,并覆盖已读取的同名属性。
			因此,通过方法参数传递的属性具有最高优先级,resource/url 属性中指定的配置文件次之,最低优先级的是 properties 属性中指定的属性。 
	 -->
	<properties resource="config.properties">
		<!-- <property name="driver" value="com.mysql.jdbc.Driver" />
		<property name="url" value="数据库信息“ /> -->
		<property name="username" value="用户账号"/>
  		<property name="password" value="用户密码"/>
	</properties>
	
	<!-- 配置开启mybatis驼峰命名规则 -->
	<settings>
	    <setting name="mapUnderscoreToCamelCase" value="true"/>
	</settings>

	<!-- 配置实例类 xml resultType="alias" -->	
	<typeAliases>
		<!-- <typeAlias type="com.hm.user.entity.UserInfo" alias="UserInfo"/> -->
		<typeAlias type="com.hm.user.entity.UserInfo" />
	</typeAliases>
	<environments default="mysql">
		<environment id="mysql">
			<!--配置事务的类型 -->
			<transactionManager type="JDBC"></transactionManager>
			<dataSource type="POOLED">
				<property name="driver" value="${driver}"/>
				<property name="url" value="${url}"/>
				<property name="username" value="${username}"/>
				<property name="password" value="${password}"/>
			</dataSource>
		</environment>
	</environments>

	<mappers>
		<mapper resource="*.xml 文件路径" />
	</mappers>
</configuration>
sql 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="com.hm.user.dao.IUserMapperDao">

	<!--
       1.#{},预编译的方式preparedstatement,使用占位符替换,防止sql注入,一个参数的时候,任意参数名可以接收
       2.${},普通的Statement,字符串直接拼接,不可以防止sql注入,一个参数的时候,必须使用${value}接收参数
       注意:
		  	#{} 只是替换?,相当于PreparedStatement使用占位符去替换参数,可以防止sql注入。
			${} 是进行字符串拼接,相当于sql语句中的Statement,使用字符串去拼接sql;$可以是sql中的任一部分传入到Statement中,不能防止sql注入。

			使用${} 去取出参数值信息,需要使用${value}
			#{} 只是表示占位,与参数的名字无关,如果只有一个参数,会自动对应。
     -->
    <select id="queryUserByTableName" resultType="com.zpc.mybatis.pojo.User">
        select * from ${tableName}
    </select>
    
	<select id="selectById" resultType="com.hm.user.entity.UserInfo">
		select 
			uid, dr01u, dr02u, dr04u, dr05u, dr06u, DATE_FORMAT(dr96u,'%Y-%m-%d') AS dr96u
		from dr_userinfo LIMIT 1
	</select>
	
	<select id="selectListData" parameterType="Map" resultType="com.hm.user.entity.UserInfo">
		select 
			uid, dr01u, dr02u, dr04u, dr05u, dr06u, DATE_FORMAT(dr96u,'%Y-%m-%d') AS dr96u
		from dr_userinfo 
		<where>
			and dr99u=0
			<!-- <if test="userName != null and userName.trim() != ''">
				and	dr01u = #{userName}
			</if> -->
			<choose>
				<when test="dr04u != null and dr04u.trim() != ''">
					and dr04u = #{dr04u}
				</when>
				<when test="dr05u != null and dr05u.trim() != ''">
					and dr05u = #{dr05u}
				</when>
				<otherwise>
					and	dr01u = '孟琬'
				</otherwise>
			</choose>
		</where>
	</select>
	
	<update id="updateData" parameterType="com.hm.user.entity.TestBo">
		update dr_test 
		<!-- set 
			title = #{title},
			love = #{love},
			price = #{price},
			date = NOW(),
			money = #{money}
		where id='19916142406778880' -->
		<trim prefix="set" suffixOverrides=",">
			<if test="title != null and title.trim() != ''">
				title = #{title}, 
			</if>
			<if test="love != null and love.trim() != ''">
				love = #{love},
			</if>
			<if test="price != null and price.trim() != ''">
				price = #{price},
			</if>
			<!-- <if test="money != null"> -->
				money = #{money},
			<!-- </if> -->
			date = NOW(),
		</trim>
		<where>
			and id='19916142406778880'
		</where>
	</update>
	
	<select id="selectByTest" resultType="com.hm.user.entity.TestBo">
		select * from dr_test 
		<where>
			and id = '19916142406778880'
		</where>
		limit 1
	</select>
	
	<select id="selectTestListData" parameterType="Map" resultType="com.hm.user.entity.TestBo">
		select * from dr_test
		<where>
			and id in 
			<foreach collection="arr" index="index" item="a" open="(" close=")" separator=",">
				#{a}
			</foreach>
		</where>
	</select>
</mapper>

MyBatis

1)配置mybatis-config.xml 全局的配置文件 (1、数据源,2、外部的mapper)
2)创建SqlSessionFactory
3)通过SqlSessionFactory创建SqlSession对象
4)通过SqlSession操作数据库 CRUD
5)调用session.commit()提交事务
6)调用session.close()关闭会话

insert
insert 的几个属性说明:
id:唯一标识,随便写,在同一个命名空间下保持唯一,使用动态代理之后要求和方法名保持一致
parameterType:参数的类型,使用动态代理之后和方法的参数类型一致
useGeneratedKeys:开启主键回写
keyColumn:指定数据库的主键
keyProperty:主键对应的pojo属性名
标签内部:具体的sql语句。
update
id属性:当前名称空间下的statement的唯一标识(必须属性);
parameterType:传入的参数类型,可以省略。
标签内部:具体的sql语句。
delete
delete 的几个属性说明:
id属性:当前名称空间下的statement的唯一标识(必须属性);
parameterType:传入的参数类型,可以省略。
标签内部:具体的sql语句。
sql片段
<sql id="sql唯一标识"></sql>
<include refId="sql唯一标识" />

动态sql

if 语句
<!-- 判断字段是否为null 或者为空 若是则不执行 否则执行 -->
select * from dr_table where isDelete = 0
<if test="title!=null and title.trim()!=''">
      and title = #{title}
</if>
choose when otherwise 语句
	<!-- 
		1.一旦有条件成立的when,后续的when则不会执行
  	  	2.当所有的when都不执行时,才会执行otherwise
     -->
select * from dr_table where isDelete = 0
<choose>
	  <when test="title!=null and title.trim()!=''">
         and title=#{title}
     </when>
     <when test="love!=null">
         and love= #{love}
     </when>
     <otherwise>
         and money='100'
     </otherwise>
 </choose>
where 和set 语句
<!--
	如果多出一个and,会自动去除,
	如果缺少and或者多出多个and则会报错
-->
select * from dr_table where isDelete = 0
<where>
	<if test="title!=null and title.trim()!=''">
        and title=#{title}
 	</if>
   <if test="love!=null">
       and love= #{love}
   </if>
</where>
<update id="唯一标识" parameterType="entity 路径">
    UPDATE tb_table
    <trim prefix="set" suffixOverrides=",">
        <if test="title!=null">title= #{title},</if>
        <if test="love!=null">love= #{love},</if>
        <if test="money!=null">money= #{money},</if>
        updated = now(),
    </trim>
    WHERE
    (id = #{id});
</update>
foreach 语句
select * from dr_table where id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
        #{id}
</foreach>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值