0
点赞
收藏
分享

微信扫一扫

MyBatis-学习笔记-day02

水墨_青花 2022-10-09 阅读 67


parameterType(输入类型)

  传递简单的类型

  传递pojo对象

   Mybatis使用ognl表达式解析对象字段的值,#{}或者${}括号中的值为pojo属性名称。

  传递包装类

   包装类的应用场景,比方说是商品类,那么商品可能或包括一些其它的,颜色,形状,等

  比方说是这样的一个类QueryVo,里边包含了一个User类。这样的就叫做包装类

MyBatis-学习笔记-day02_xml

这样去传递

MyBatis-学习笔记-day02_xml_02

输出参数resulType

 基础数据类型

list

pojo

resultMap

   如果数据库中的字段名和实体类中的字段名不一致时,解决方案有,改数据库或者实体类,但是这是不可取的方案。 还有一种方案是取别名,在sql语句后边加上别名,能够结局问题。最后就是使用ResultMap了。

  关于resultMap,用起来的感觉就好像是hibernate的持久化类的映射文件,非常相似,这大概就是框架思想想通的地方

MyBatis-学习笔记-day02_xml_03

关于mybatis的sql灵活性体现

   动态sql-if标签的使用

Demo

MyBatis-学习笔记-day02_xml_04

sql-where标签的使用

  从上边的demo中,我们看到的是,where后边需要有一个 1=1 ,才能保证后边的 and 有效。那么使用where标签以后,就可以直接来了。

 where标签自动补上where关键字,同时处理多余的and

  需要注意一点,如果使用where标签,就不能手动的添加where关键字了,不然拼接出来,就会多一个where。

  使用方法很简单,只需要将if标签包起来就可以了。

  • sql-sql片段

先定义一个sql片段

MyBatis-学习笔记-day02_spring_05

使用也很简单:

MyBatis-学习笔记-day02_sql_06

foreach标签的使用

   重点是红框里边的

MyBatis-学习笔记-day02_sql_07

关联查询

 一对一关联查询 

   第一种方案使用resultType: 可以使用的方案是,实现原理很简单,就是表连接,这样需要有一个和连接表相对应的pojo(字段名相同)然后配合 resultType来使用。 但是这样做的方法的弊端是,面向对象的思想不够好。 (有公司在用,但是不推荐)

   第二种方案使用ResultMap: resultMap 就是先写一个resultMap 然后再引入。对于这个resultMap 我的感觉就是可以类比hibernate的映射文件,与hibernate不同的是,这里换了一个关联的标签<association> (这种更符合面向对象的思想)

MyBatis-学习笔记-day02_spring_08

一对多关联查询

MyBatis-学习笔记-day02_xml_09

一对多与一对一不同的地方有两点,一个是标签 collection,一个是属性 ofType。

mybatis和spring的整合

 先回顾一下之前学过的spring框架,spring框架的两个重要的内容就是 IOC 和AOP 的思想。

 首先IOC是控制反转,又叫依赖注入,它是用在容器管理上,每层对象之间的传递,是由spring来管理的。spring可以拿到对象的创建权。

  AOP面向切面编程思想,比方说最常用的事务的管理,比方说监控某一块代码性能的时候,比方日志管理。

整合思路

  1. SqlSessionFactory对象应该放到spring容器中作为单例存在。
  2. 传统dao的开发方式中,应该从spring容器中获得sqlsession对象。
  3. Mapper代理形式中,应该从spring容器中直接获得mapper的代理对象。
  4. 数据库的连接以及数据库连接池事务管理都交给spring容器来完成。

整合步骤

  1. 创建一个java工程。
  2. 导入jar包。(课前资料中mybatis与spring整合所有包)
  3. mybatis的配置文件sqlmapConfig.xml
  4. 编写Spring的配置文件
  5. 数据库连接及连接池
  6. sqlsessionFactory对象,
  7. 配置到spring容器中
  8. 编写Spring的配置文件
  9. 复制jdbc.properties配置文件到新工程
  10. 复制log4j.properties配置文件到新工程

mybatis推荐的是使用动态代理的方式进行开发,只需要有一个接口,有个映射文件,然后在applicationContexr.xml文件里边开启包扫描,就可以使用了。当有多个包需要开启扫描的时候,只需要逗号分隔开就行了

applicationContexr.xml 文件 里边的相关配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />

<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 连接池的最大数据库连接数 -->
<property name="maxActive" value="10" />
<!-- 最大空闲数 -->
<property name="maxIdle" value="5" />
</bean>

<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis核心配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
<!-- 开启别名包扫描 -->
<property name="typeAliasesPackage" value="com.angus.mybatis.pojo"></property>
</bean>

<!-- 动态代理配置方式:第一种 -->

<!-- <bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

<bean id="oneMapper" parent="baseMapper">
<property name="mapperInterface" value="com.angus.mybatis.mapper.UserMapper" />
</bean> -->

<!-- 动态代理,第二种方式:包扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="com.angus.mybatis.mapper"/>
</bean>

</beans>

逆向工程:替我们生成接口,生成pojo   这里先不做介绍。

 

举报

相关推荐

java学习笔记day02

Vue学习笔记_Day02

javaSE 笔记 day02

redis笔记-day02

MySql笔记Day02

jQuery 笔记day02

机器学习-Day02

JVM学习Day02

JAVA学习day02

0 条评论