《Mybatis入门到精通》——3.动态SQL&多表映射

Bertha 。 2023-09-30 03:25 74阅读 0赞

Day_03

连接池、动态SQL;

#

连接池

(1)连接池:

在实际开发中都会使用连接池,因为它可以减少我们获取连接所消耗的时间;

(2)mybatis中的连接池:

mybatis连接池提供了3种配置方式;

配置的位置:主配置文件SqlMapConfig.xml中的dataSource标签,type属性就是表示采用何种连接池方式。

type属性的值,有3种:

  • POOLED:采用传统的javax.sql.DataSource规范中的连接池,mybatis中有针对规范的实现
  • UNPOOLED:采用传统的获取连接的方式,虽然也实现javax.sql.DataSource接口,但是并没有使用池的思想
  • JNDI:采用服务器提供的JNDI技术实现,来获取DataSource对象,不同的服务器所能拿到DataSource是不一样的

注意:如果不是web或者maven的war工程,是不能使用的;学习时使用的是tomcat服务器,采用的连接池就是dbcp连接池;

#

Mybatis事务

什么是事务?

  • 事务的四大特性ACID;
  • 不考虑隔离性会产生的3个问题;
  • 解决办法:四种隔离级别;

它是通过sqlsession对象的commit方法和rollback方法实现事务的提交和回滚,默认update和delete是不自动提交的,需要手动commit;

动态SQL

(1) if 标签

  1. <!-- 根据条件判断user的username是否为空,来动态的增加SQL条件语句 -->
  2. <select id="findByCondition" resultType="com.ming.domain.User" parameterType="com.ming.domain.User">
  3. <!-- 这里有个where1=1
  4. select * from user where 1=1
  5. <if test="username!=null">
  6. and username=#{username}
  7. </if>
  8. <if test="sex!=null">
  9. and sex=#{sex}
  10. </if>
  11. -->
  12. <!-- 写到<where>里面 不需要 where1=1 -->
  13. select * from user
  14. <where>
  15. <if test="username!=null">
  16. and username=#{username}
  17. </if>
  18. <if test="sex!=null">
  19. and sex=#{sex}
  20. </if>
  21. </where>
  22. </select>

测试:

  1. @Test
  2. public void findByCondition() {
  3. User u = new User();
  4. User u0 = new User();
  5. User u1 = new User();
  6. //username为空,会查询所有
  7. List<User> users0 = userDao.findByCondition(u0);
  8. users0.stream().forEach(System.out::println);
  9. //username不为空,会查询username=#{username}
  10. u.setUsername("老王");
  11. List<User> users = userDao.findByCondition(u);
  12. users.stream().forEach(System.out::println);
  13. //username和sex不为空,会查询username=#{username} sex=#{sex}
  14. u1.setUsername("老王");
  15. u1.setSex("女");
  16. List<User> users1 = userDao.findByCondition(u1);
  17. users1.stream().forEach(System.out::println);
  18. session.commit();
  19. }

(2) where和foreach标签
用于多个查询的sql:select * from user where id in(41, 42, 45);
通过一个类中传入集合的方法;

定义类:

  1. /**
  2. * @author A
  3. */
  4. public class QueryVo {
  5. //为测试OGNL #{user.username}
  6. public User getUser() {
  7. return user;
  8. }
  9. public void setUser(User user) {
  10. this.user = user;
  11. }
  12. private User user;
  13. //为测试动态SQL if标签
  14. private List<Integer> ids;
  15. public List<Integer> getIds() {
  16. return ids;
  17. }
  18. public void setIds(List<Integer> ids) {
  19. this.ids = ids;
  20. }
  21. }

定义接口方法

  1. /**
  2. * 根据QueryVo 中提供的id集合,查询用户信息 where id in (...)
  3. * @param vo
  4. * @return
  5. */
  6. List<User> findByIdGroup(QueryVo vo);

定义mapper.xml的SQL

  1. <!-- 条件查询的sql select * from user where id in(41,42,46) 条件:满足id在(...)中 通过一个 QueryVo类 传入集合的方法 -->
  2. <select id="findByIdGroup" resultType="com.ming.domain.User" parameterType="com.ming.domain.QueryVo">
  3. select * from user
  4. <!--<include refid="selectFrUser"></include>-->
  5. <where>
  6. <if test="ids != null and ids.size()>0">
  7. <!-- 这里有5个标签 collections open close item separator 注意 item要和下面的#{user.id}相对应 -->
  8. <foreach collection="ids" open="and id in (" close=")" item="user.id" separator=",">
  9. #{user.id}
  10. </foreach>
  11. </if>
  12. </where>
  13. </select>

测试:

  1. @Test
  2. public void findByIds(){
  3. QueryVo queryVo = new QueryVo();
  4. List<Integer> list = new ArrayList<Integer>();
  5. list.add(41);
  6. list.add(42);
  7. list.add(46);
  8. queryVo.setIds(list);
  9. List<User> users = userDao.findByIdGroup(queryVo);
  10. users.stream().forEach(System.out::println);
  11. //session.commit();
  12. }

(3) 复用SQL语句

对于经常重复使用的SQL语句,如SELECT * FROM user,很多

发表评论

表情:
评论列表 (有 0 条评论,74人围观)

还没有评论,来说两句吧...

相关阅读

    相关 MyBatis3SQL映射

    前言 前面学习了config.xml,下面就要进入MyBatis的核心SQL映射了,第一篇文章的时候,student.xml里面是这么写的: ![复制代码][copycod