1.MyBatis占位符
MyBatis有两个占位符,分别是${}和# {} 。
我个人的理解${}占位符就是拼接意思将前后两部分拼接到一起。对于数据不做任何改变。 注意使用$占位符需要用@Param注解进行命名。
#{}占位符则是用来获取值的。 #{} 中名称是自定义类型的属性名,该属性有对应的 get 方法。如果没有get方法,那么会根据反射去获取该类型的值,如果找不到,那么报 ReflectionException 异常。(很常用)
下面是这两个占位符的代码。
$占位符 StudentDao接口:
//占位符的使用 使用$需要param命名 List<Student> zhanweifu(@Param("studentname") String name);
StudentDao.xml文件:
<!-- 占位符--> <select id="zhanweifu" resultType="com.liuhaiyang.domain.Student"> select * from student where name=${studentname} </select>
test测试类:
@Test//占位符 public void zhanweifu(){ SqlSession session = MybatisUtils.getSqlSession(); StudentDao dao=session.getMapper(StudentDao.class); List<Student> stu= dao.zhanweifu("'张三'"); //由于$属于+的方式连接字符串,所以需要加个'' 不加''会报异常 PersistenceException sql语句是:select * from student where name=张三 stu.forEach(s-> System.out.println(s)); session.close(); }
测试结果:
#{}占位符 平时使用的都是这个占位符,就不写代码了,重在理解。
2.MyBatis ${}占位符排序
这个是写着练手的。理解${}占位符。
StudentDao接口:
//占位符$排序 List<Student> zhanpai(@Param("name")String b, @Param("id") String a, @Param("tablename") String c);
StudentDao.xml配置文件
<!-- 占位符$排序 按照id降序排序--> <select id="zhanpai" resultType="com.liuhaiyang.domain.Student"> select * from ${tablename} where name=#{name} order by ${id} desc </select>
test测试类:
@Test public void zhanpai(){ SqlSession session =MybatisUtils.getSqlSession(); StudentDao dao=session.getMapper(StudentDao.class); List<Student> student=dao.zhanpai("张三","id","Student"); student.forEach(stu-> System.out.println(stu)); session.close(); }
运行截图:
3. MyBatis中一些重要对象的理解
1.Resources 类,顾名思义就是资源,用于读取资源文件(主配置信息)。其有很多方法通过加载并解析资源文件,返回不同类型的 IO 流对象。 一般用法
String config="mybatis.xml"; InputStream inputstream = Resources.getResourceAsStream(config);
2.SqlSessionFactoryBuilder类, 负责创建 SqlSessionFactory 对象。SqlSessionFactory 的创建,需要使用 SqlSessionFactoryBuilder 对象的 build() 方法。需要try-catch
private static SqlSessionFactory factory=null; factory = new SqlSessionFactoryBuilder().build(inputstream);
3.SqlSessionFactory接口(重要对象),SqlSessionFactory 接口对象是一个重量级对象(系统开销大的对象),是线程安全的,所以一个应用只需要一个该对象即可。创建 SqlSession 需要使用 SqlSessionFactory 接口的的 openSession()方法。作用是 SqlSession 的工厂,也即创建 SqlSession 的对象。 实现方法
public class DefaultSqlSessionFactory implements SqlSessionFactory { }
重要方法 openSession 用法:
public static SqlSession getSqlSession(){ private static SqlSessionFactory factory=null; SqlSession session=null; if(factory!=null){ /* openSession(true); 表示自动提交 openSession(false); 表示需要手动提交提交 openSession():同 openSession(false) */ session=factory.openSession(); } return session; }
4.SqlSession接口(重要对象)SqlSession 接口对象用于执行持久化操作。一个 SqlSession 对应着一次数据库会话,一次会话以 SqlSession 对象的创建开始,以 SqlSession 对象的关闭结束。SqlSession 接口对象是线程不安全的,所以每次数据库会话结束前,需要马上调用其 close() 方法,将其关闭。再次需要会话,再次创建。 SqlSession在方法内部创建,使用完毕后关闭。 SqlSession 的对象是通过 SqlSessionFactory 来获取的。实现方法:
public class DefaultSqlSession implements SqlSession { }
一些常用方法:
selectOne:执行sql语句,最多得到一行记录,多余1行则会抛出异常 selectList:执行sql语句,返回多行数据 selectMap:执行sql语句,得到一个Map结果 insert:执行insert语句 update:执行update语句 delete:执行delete语句 commit:提交事务 rollback:回滚事务
总结:其实MyBatis底层还是JDBC,只是将JDBC封装起来了,我们直接拿着用就是。