1.sql映射器Mapper

MyBatis基于动态代理机制,让我们无需再编写Dao的实现。

传统Dao接口,现在名称统一以Mapper结尾,还有我们映射器配置文件要和映射器在同一个包。

1.1使用映射器步骤:

(1)根据需求,创建模型相关的Mapper接口

(2)编写映射文件

  a)*Mapper.xml的命名空间,必须和接口的“全限定名”一致

  b)定义sql标签的id,需要和“接口的方法”一致

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.itsource._01_mapper.TeacherMapper">
<!-- 查询 findAll()
resultType 返回类型
-->
<select id="findAll" resultType="Teacher">
select * from t_teacher
</select>
</mapper>

(3)测试

 @Test
public void findAll() {
SqlSession sqlSession = MyBatisUtils.INSTANCE.getSqlSession();
//MyBatis动态代理
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
mapper.findAll().forEach(t -> System.out.println(t));
}

2.高级查询

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.itsource._02_query.TeacherMapper">
<!-- 查询 findAll()
resultType 返回类型
-->
<select id="findAll" resultType="Teacher">
select * from t_teacher
</select>
<select id="queryAll" parameterType="teacherQuery" resultType="Teacher">
select * from t_teacher
<where>
<if test="name!=null">
and name like concat("%",#{name},"%")
</if>
<if test="minAge!=null">
and age &gt; #{minAge}
</if>
<if test="maxAge!=null">
and age &lt; #{maxAge}
</if>
<!-- 放入CDATA区域里面被xml忽略解析
<if test="maxAge != null">
<![CDATA[
and age < #{maxAge}
]]>
</if>-->
</where>
</select>
</mapper>

注意:

1.where:里面所有的条件如果都在前面加上and,并且最后会把第一个and替换为where

2.if 判断条件是否满足,如果是并且用and

3.模糊查询
  方案1:不能用#
         and ( name like %#{keywords}% or
password like %#{keywords}% )
  方案2:用$  sql注入
         and ( name like '%${keywords}%'
or password like '%${keywords}%' )
  方案3:用mysql中字符串拼接函数concat
         and ( name like
concat('%',#{keywords},'%') or password like '%${keywords}%' )

4.如果有特殊符号:

  方案1:转义符号,如“<”可以写成"&lt;",">"写成"&gt;"等等

  方案2:CDATA(上面的例子已经使用过)

3.ResultMap结果集映射

3.1为什么要使用结果映射

解决表字段名和对象属性名不一样的情况

关联对象查询,在mybatis不会默认查询出来,需要自己查询结果并且通过resultMap来配置

3.2关联映射分类

表与表之间的关系有:

一对一,一对多,多对一,多对多…

3.3关联映射处理方式

MyBatis提供两种方式处理我们关联对象,嵌套查询嵌套结果

嵌套结果:发送一条sql,查询所有的信息(本身+关联对象)

嵌套查询:发送1+n条sql

3.3.1多对一(一对一)——嵌套结果

 <!--嵌套结果(多对一),发送一条sql查询数据-->
<resultMap id="studentMap" type="student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="teacher" javaType="teacher">
<id property="id" column="tid"/>
<result property="name" column="tname"/>
<result property="age" column="tage"/>
</association>
</resultMap>
<select id="findAll" resultMap="studentMap">
select s.id,s.name,s.age,t.id tid,t.name tname,t.age tage
from t_student s join t_teacher t on s.teacher_id=t.id
</select>

3.3.2多对一(一对一)——嵌套查询

 <!--嵌套查询,发送1+n条sql查询数据-->
<resultMap id="studentMap" type="student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="teacher" javaType="teacher" column="teacher_id" select="queryByTeacherId"/>
</resultMap>
<select id="findAll" resultMap="studentMap">
select * from t_student
</select>
<select id="queryByTeacherId" parameterType="long" resultType="teacher">
select * from t_teacher where id=#{id}
</select>

3.3.3一对多(多对多)——嵌套结果

 <!--嵌套结果(一对多),发送一条sql查询数据-->
<resultMap id="teacherMap" type="teacher">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<collection property="students" javaType="arrayList" ofType="student">
<id property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="age" column="sage"/>
</collection>
</resultMap>
<select id="findAll" resultMap="teacherMap">
select t.id,t.name,t.age,s.id sid,s.name sname,s.age sage
from t_teacher t join t_student s on t.id=s.teacher_id
</select>

3.3.4一对多(多对多)——嵌套查询

 <!--嵌套查询,发送1+n条sql查询数据-->
<resultMap id="teacherMap" type="teacher">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<collection property="students" column="id" javaType="arrayList" ofType="student" select="queryStudent"/>
</resultMap>
<select id="findAll" resultMap="teacherMap">
select * from t_teacher
</select>
<select id="queryStudent" parameterType="long" resultType="student">
select * from t_student where teacher_id=#{id}
</select>

4.缓存

4.1 jpa

一级缓存(自带)命中条件:同一个EntityManagerFactory,同一个EntityManager,同一个OID

二级缓存(需要配置实现):同一个EntityManagerFactory,不同的EntityManager,同一个OID

4.2 MyBatis

一级缓存:属于sqlSession级别,同一个SqlSessionFactory,同一个SqlSession,同一个id

二级缓存:属于sqlSessionFactory,同一个SqlSessionFactory,不同的SqlSession,同一个id

序列化:把对象转换成二进制形式,方便传输(特别需要保存到磁盘或者硬盘,需要进行序列化)

反序列化:把二进制的数据转换成对象

5.SSM集成

Spring+SpringMvc+MyBatis

框架集成核心:如果你的项目中,用到了Spring框架,那么其他框架主要就是和Spring集成。

那么和Spring集成的核心思路:

  1.把当前框架的核心类交给Spring管理

  2.如果框架有事务,那么事务也要统一交给Spring管理

步骤:

(1)导入jar包:今天以导入jar的方式,不是maven

(2)配置文件

applicationContext.xml:

  jdbc.properties--->Datasource ---->SqlSessionFactory----扫描Mapper--->事务管理器-开启注解事务

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<context:component-scan base-package="cn.itsource.ssm.service"/>
<!--读取jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置连接池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--*Mapper.xml的位置-->
<property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*Mapper.xml"/>
<!--别名配置-->
<property name="typeAliasesPackage">
<value>
cn.itsource.ssm.domain
cn.itsource.ssm.query
</value>
</property>
</bean>
<!-- 处理mapper接口 spring会扫描包产生很多子类 注入到service层-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itsource.ssm.mapper"/>
</bean>
<!--事务配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

applicationContext-mvc.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描controller层-->
<context:component-scan base-package="cn.itsource.ssm.controller"/>
<!--静态资源放行-->
<mvc:default-servlet-handler/>
<!--mvc特有注解支持-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--spring核心控制器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--springMvc核心控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--编码过滤器-->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

(3)包的层次结构:

最新文章

  1. 如何在CentOS 7中禁止IPv6
  2. MaxMin搜索
  3. 按钮靠右css小结
  4. 激活Maven profile的几种方式
  5. 为IE单独写CSS的三种方法
  6. Asp.net 网站出现Service Unavailable 问题剖析
  7. Oracle前10条记录
  8. 加密算法 MD5/SHA1
  9. ●UOJ 131 [NOI2015] 品酒大会
  10. C#创建安装、卸载部署程序
  11. Java基础_0310:引用传递
  12. MySQL软件基本管理
  13. 2017-2018-2 20165314 实验三《 敏捷开发与XP实践》实验报告
  14. &lt;20190303&gt;大厂的风度,firmware每年更新!
  15. Linux Tomcat自启动
  16. [Selenium]当DOM结构里面有iFrame,iFrame里面是html,怎么send keys to 里面的body,怎么用Assert进行验证?
  17. 同一页面的不同Iframe获取数据
  18. Xcode模版生成文件头部注释
  19. curl:get,post 以及SoapClien访问webservice数据
  20. 《C++ Primer Plus》读书笔记之四—分支语句和逻辑操作符

热门文章

  1. call() 、 apply() 、bind()方法的作用和区别!
  2. 使用three.js创建大小不随着场景变化的文字
  3. jenkins System error
  4. react修改端口
  5. 大白话OSI七层协议
  6. C#中的委托和事件(二)
  7. Idea 2016 激活码
  8. 【已解决】解决IntelliJ IDEA控制台输出中文乱码问题
  9. 【解决】error pulling image configuration: Get https:// ...... x509: certificate has expired or is not yet valid
  10. cmd 窗口中运行 Java 程序