工具类方便操作
public class MybatisUtil {
private static SqlSessionFactory factory = null;
static {
try {
InputStream is = Resources.getResourceAsStream("mybatis.xml");
factory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SqlSession getSession() {
SqlSession session = null;
if(factory!=null) {
//openSession(true)的话就会打开自动提交事务
session = factory.openSession();
}
return session;
}
}
1、增删改(DML)操作
1.1、事务(Transaction)
事务是数据库操作的最小单元,有ACID的特性,应该保证一个事务中的sql语句要么同时成功,要么同时不成功
Mybatis中配置了事务管理器,type属性设置成JDBC,表示Mybatis采用了和原生JDBC相同的事务管理机制
在Mybatis执行的开始时,将自动提交功能关闭了,所以在执行DML操作时,需要手动提交事务
public void testIns() {
SqlSession session = MybatisUtil.getSession();
User u = new User();
u.setUsername("xiaoming");
u.setPassword("971224");
int num = session.insert("com.bjsxt.mapper.UserMapper.insUser", u);
if(num>0)
{
//提交事务
session.commit();
System.out.println("添加成功!");
}else {
//事务回滚
session.rollback();
System.out.println("添加失败!");
}
//关闭资源
session.close();
}
2、更新和删除数据
<update id="updUser" parameterType="user">
update t_user set username = #{username},password=#{password} where id=#{id}
</update>
@Test
public void testupd() {
SqlSession session = MybatisUtil.getSession();
User user = new User();
user.setId(3);
user.setUsername("wanger");
user.setPassword("123");
int update = session.update("com.bjsxt.mapper.UserMapper.updUser", user);
if(update>0) {
session.commit();
System.out.println("更新成功!");
}else {
session.rollback();
System.out.println("更新失败!");
}
session.close();
}
<delete id="delUser" parameterType="int">
delete from t_user where id=#{0}
</delete>
@Test
public void testdel() {
SqlSession session = MybatisUtil.getSession();
int num = session.delete("com.bjsxt.mapper.UserMapper.delUser", 7);
if(num>0)
{
//提交事务
session.commit();
System.out.println("删除成功!");
}else {
//事务回滚
session.rollback();
System.out.println("删除失败!");
}
//关闭资源
session.close();
}
2、接口绑定方案
在Mybatis中,提供了一套接口绑定方案,陈恒许愿可以提供一个接口,然后提供对应接口的一个mapper.xml文件,Mybatis会自动将接口和xml文件进行绑定,实际上就是Mybatis会更具接口和对应的xml文件创建接口的实现类,换言之,就是可以得到接口类型的对象,方便方法的调用
a)配置文件名与接口名一致
b)namespace的值要是包名+接口名(接口的全限定路径),不能随便写
c)方法的id要与接口中的方法名对应
d)mybatis.xml中的mapper标签不在写xml的路径而改成写接口的路径以前是resource = com/bjsxt/mapper/UserMapper.xml,现在改成 class=com.bjsxt.mapper.UserMapper,而为了简化操作,后面项目变大的话,不止有一个UserMapper,可能还会有其他的接口,所以为了方便操作,mappers标签下提 供了package标签
例:
e)其余要求不变
2.2、通过接口绑定解决多参数的传递
3、动态SQL
根据条件的不同,SQL语句也会随之动态的改变
3.1<if>
select * from t_user where 1=1
and username=#{username} and password=#{password} 3.2、where 用于管理where子句,有如下功能 a)如果没有条件,不会生成where关键字 b)如果有条件,会自动添加where关键字 c)如果第一个条件中有and,自动去除and select * from t_user
<where>
<!-- if用于条件判断
使用动态SQL时,接口里的参数必须取别名
test:用于设定判断条件,类似于java中if后括号里的条件
-->
<if test="username != null and username != ''">
and username=#{username}
</if>
<if test="password != null and password != ''">
and password=#{password}
</if>
</where>
3.3<choose><when><otherwise>
这是一套标签,同时使用,功能类似于switch...case...
<select id="sel" resultType="user">
select * from t_user
<where>
<!-- 只会执行一条,当有username的时候就不再执行下面的语句
当username没有,password有的时候只会执行paswword这一条,下面的不执行
都没有的话,执行最后一条
-->
<choose>
<when test="username != null and username != ''">
and username=#{username}
</when>
<when test="password != null and password != ''">
and password=#{password}
</when>
<otherwise>
and 1 = 1
</otherwise>
</choose>
</where>
</select>
3.4<set>
set标签作用在更新语句中,作用是如果username和password没有执行id后面的都好久回自动去除(只会去除,不会加,)
此处加一个id=#{id}的作用是,如果username和password都没有的话,没有id=#{id}的时候,sql语句会报错
<update id="updUser" parameterType="user" >
update t_user
<set>
id = #{id},
<if test="username != null and username != ''">
username=#{username},
</if>
<if test="password != null and password != ''">
password=#{password},
</if>
</set>
<where>
id = #{id}
</where>
</update>
3.5、<trim>
<update id="updUser" parameterType="user">
update t_user
<!--
prefix:前缀,向前面添加内容
prefixOverrides:从前面删除内容
suffix:后缀,向后面添加内容
suffixOverrides:向后面删除内容
-->
<trim prefix="set" prefixOverrides="" suffix="" suffixOverrides=",">
username = #{username},
</trim>
where id=#{id}
</update>
3.6、<bind>
<select id="selLike" resultType="user">
select * from t_user
<where>
<if test="username != null and username != ''">
<bind name="username" value="'%'+username+'%'"/>
and username like #{username}
</if>
</where>
</select>
3.7、<foreach>
一般用在查询中有in的时候
<select id="selIn" resultType="user" parameterType="list">
select
<include refid="mySql"/>
from t_user where id in
<!--
collection=传过来的集合的名字
item: 集合中遍历出来的每一项,引号内的item代表,取出来的每一项的名字叫item
index
open:( 以左括号开始
close:) 以右括号结束
separator:中间用什么来分割 ,
-->
<foreach collection="list" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</select>
3.8<include>
<mapper namespace="com.bjsxt.mapper.UserMapper">
id,username,password select from t_user