SSM框架学习之Mybatis学习以及资料整理

SSM框架

mybatis框架

解决数据的持久化问题的框架

Spring MVC框架

解决 WEB 层问题的 MVC 框架

Spring框架

解决技术整合问题的框架

知识点:

1、什么是框架?
它是我们软件开发中的一套解决方案,不同的框架解决的是不同的问题。
使用框架的好处:
框架封装了很多的细节,使开发者可以使用极简的方式实现功能。大大提高开发效率。
2、三层架构
表现层:
是用于展示数据的
业务层:
是处理业务需求
持久层:
是和数据库交互的

三层架构中SSM框架的作用点(范围)
在这里插入图片描述

Mybatis中运用了那些知识点:
1、动态代理——优势:在不修改源码的基础上对已有的方法进行增强
2、设计模式之构建者模式——优势:把对象的创建细节隐藏,使使用者直接调用方法即可拿到对象
3、设计模式之工厂模式——优势:解耦(降低类之间的依赖关系)

Mybatis环境搭建中几个比较重要的配置文件
1、SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration  
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- mybatis的主配置文件 -->
<configuration>
	<!-- 配置文件的地址 -->
	<properties
		url="file:\\\...\jdbcConfig.properties"></properties>
	<!-- 配置别名 -->
	<typeAliases>
		<package name="com.gk.domain" />
	</typeAliases>
	<environments default="mysql">
	<!-- 配置mql环境 -->
	<environment id="mysql">
		<!-- 配置实务类型 -->
			<transactionManager type="JDBC"></transactionManager>
			<!-- 配置数据源(连接池) -->
			<dataSource type="POOLED">
			<!-- 配置连接数据库的4个基本信息 -->
				<property name="driver" value="${jdbc.driver}" />
				<property name="url" value="${jdbc.url}" />
				<property name="username" value="${jdbc.username}" />
				<property name="password" value="${jdbc.password}" />
			</dataSource>
		</environment>
	</environments>
	<!-- 配置映射配置文件 -->
	<mappers>
		<package name="com.gk.dao" />
	</mappers>
</configuration>

2、jdbcConfig.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/当前数据库
jdbc.username=数据库权限名称
jdbc.password=数据库权限密码

3、log4j.properties

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

4、UserDao.xml 可有可无看是否为注解开发,还是配置文件来开发
注意:resultMap中的result标签的property属性对应的是实体类的属性,column属性对应的是数据库中的字段。

<?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="com.gk.dao.IUserDao">
	<resultMap type="user" id="userAccountMap">
		<id property="id" column="id"></id>
		<result property="username" column="username"></result>
		<result property="address" column="address"></result>
		<result property="sex" column="sex"></result>
		<result property="birthday" column="birthday"></result>
		<!-- 配置user对象中accounts集合的映射 -->
			<collection property="accounts" ofType="account"><!-- ofType意思是集合中元素的类型 -->
			<id property="id" column="aid"></id>
			<result property="uid" column="uid"></result>
			<result property="money" column="money"></result>
			</collection>
	</resultMap>
<select id="findAll" resultMap="userAccountMap">
 	select *,a.id as aid from user u left join account a on u.id = a.uid;
 </select>
 
 <select id="findById" parameterType="java.lang.Integer" resultType="com.gk.domain.User">
 	select * from user where id=#{id};
 </select>
</mapper>
自定义mybatis中的图解

在这里插入图片描述

selectList中PreparedStatement对象的执行方法

execute:它能执行CRUD中的任意一种语句,它的返回值是一个boolean类型,表示是否有结果集。有则为true,没有则为false。
executeQuery:它只能执行CUD语句,查询语句无法执行,他的返回值是影响数据库记录的
excuteUpdate:它只能执行SELECT语句,无法执行增删改。执行结果封装的结果集ResultSet对象

模糊查询两种配置方式(#{}与 ${}的区别):
#{}表示一个占位符号
通过#{}可以实现 preparedStatement 向占位符中设置值,自动进行 java 类型和 jdbc 类型转换,
#{}可以有效防止 sql 注入。 #{}可以接收简单类型值或 pojo 属性值。 如果 parameterType 传输单个简单类型值,#{} 括号中可以是 value 或其它名称。

select * from user where username like #{username}

${} 表示拼接 sql 串
通过 ${}可以将 parameterType 传入的内容拼接在 sql 中且不进行 jdbc 类型转换, $ { } 可以接收简
单类型值或 pojo 属性值,如果 parameterType 传输单个简单类型值,${}括号中只能是 value。

select * from user where username like '%${value}%'
为什么在CUD过程中必须使用sqlSession.commit()提交事务?

主要原因就是在连接池中取出的连接,都会将调用 connection.setAutoCommit(false)方法,这样我们
就必须使用 sqlSession.commit()方法,相当于使用了 JDBC 中的 connection.commit()方法实现事务提交。
也可以用sqlSession.openSession(true)设置为自动提交。

动态sql语句

<select id="findByUser" resultType="user" parameterType="user">
select * from user where 1=1
<if test="username!=null and username != '' ">
and username like #{username}
</if> <if test="address != null">
and address like #{address}
</if>
</select>
注意:<if>标签的 test 属性中写的是对象的属性名,如果是包装类的对象要使用 OGNL 表达式的写法。
另外要注意 where 1=1 的作用!可以用<where>标签代替where 1=1;
Mybatis中简化编写的SQL片段

Sql 中可将重复的 sql 提取出来,使用时用 include 引用即可,最终达到 sql 重用的目的。

< !-- 抽取重复的语句代码片段 -->
< sql id="defaultSql" >
select * from user
< /sql >
<!-- 根据 id 查询 --> 
<select id="findById" resultType="UsEr" parameterType="int">
<include refid="defaultSql"></include>
where id = #{uid}
</select>

延迟加载

定义:就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据。延迟加载也称懒加载。

好处:先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要比关联查询多张表速度要快。
坏处:
因为只有当需要用到数据时,才会进行数据库查询,这样在大批量数据查询时,因为查询工作也要消耗时间,所以可能造成用户等待时间变长,造成用户体验下降。

Mybatis的一级缓存

一级缓存是 SqlSession 级别的缓存,只要 SqlSession 没有 flush 或 close,它就存在。
当调用 SqlSession 的修改,添加,删除,commit(),close()等方法时,就会清空一级缓存。

在这里插入图片描述

Mybatis的二级缓存

二级缓存是 mapper 映射级别的缓存,多个 SqlSession 去操作同一个 Mapper 映射的 sql 语句,多个SqlSession 可以共用二级缓存,二级缓存是跨 SqlSession 的。

Mybatis注解开发

@Insert:实现新增
@Update:实现更新
@Delete:实现删除
@Select:实现查询
@Result:实现结果集封装
@Results:可以与@Result 一起使用,封装多个结果集
@ResultMap:实现引用@Results 定义的封装
@One:实现一对一结果集封装
@Many:实现一对多结果集封装
@SelectProvider: 实现动态 SQL 映射
@CacheNamespace:实现注解二级缓存的使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值