快速复制一个maven模块步骤
复制粘贴一个maven模块
删除taget和iml文件
打开pom.xml文件 将artifactId名称改为当前maven项目文件夹的名称的
Mybatis
动态sql
<sql id=""></sql> 可定义动态代码块 通过<include refid=""></include>引用 <where></where> 语义与where一样 通过<if test=""></if>有选择的执行sql语句,每个sql前语句都通过and连接
<sql id="allColumn">
id , username, birthday, sex, address
</sql>
<select id="selectByCondition" parameterType="user" resultType="user">
select
<include refid="allColumn"></include>
from users
<where>
<if test="userName != null and userName != ''">
and username like concat('%',#{userName},'%')
</if>
<if test="birthday != null">
and birthday = birthday
</if>
<if test="sex != null and sex !=''">
and sex = sex
</if>
<if test="address != null and address != ''">
and address like concat('%',#{address},'%')
</if>
</where>
</select>
<set></set>语义与set一样 通过<if test=""></if>有选择的执行sql语句,每个sql语句后都通过,连接
<update id="updateSet" parameterType="user">
update users
<set>
<if test="userName != null and userName != ''">
username = #{userName},
</if>
<if test="birthday != null ">
birthday = #{birthday},
</if>
<if test="sex != null and sex != ''">
sex = #{sex},
</if>
<if test="address != null and address != ''">
address = #{address},
</if>
</set>
where id = #{id}
</update>
<foreach collection="" item="" separator="" close="" open=""></foreach>
<select id="selectByIds" resultType="user">
select
<include refid="allColumn"></include>
from users where id in
<foreach collection="array" separator="," item="id" open="(" close=")">
#{id}
</foreach>
</select>
MyBatis注意事项
select * from users where id=2 or id=6 or id=4表示查询id为246的语句
select * from users where id in (2,4,6)
不能使用 select * from
查询语句中有返回值要指定resultType的类型 增删改不用指定
构建不带主键的构造方法
sql查询语句
select column1,column2 from table where column1 = ?
select column1,column2 from table where column1 = #{}
select username,password from table_user where username = #{username}
sql增加语句
insert into table (column1,column2) values (value1,value2);
insert into table (column1,column2) values (#{},#{});
insert into table_user (username,password) values (#{username},#{password});
sql更新语句
update table set column1 = value1,column2 = value2 where column1 = ?;
update table set column1 = value1,column2 = value2 where column1 = #{};
update table_user set username = zhangsan,password = 123 where username = #{username};
sql删除语句
delete from table where column = ?;
delete from table where column = #{};
delete from table_user where username = #{username};