Mybatis基础笔记

mybatis.xml:全局配置文件,配置了mybatis的运行环境等配置信息。
配置了映射文件: sql语句 * mapper.xml

SqlSessionFactory:会话工厂。通过全局配置文件加载生成
功能:创建会话 sqlSession

SqlSession;会话。作用:操作数据库CURD

Executor执行器:SqlSession通过执行器进行的操作数据库

mappedStatement 底层封装对象
作用:对数据库的操作过程进行封装,包括sql语句、输入参数、输出结果
输入参数的类型:java基本类型、自定义的pojo、hashMap
输出结果的类型:java基本类型、自定义的pojo、hashMap

两大对象
SqlSessionFactory:会话工厂
由全局配置文件加载:作用就是创建会话的
特点:一旦创建了会话工厂的实例,那么在应用程序的执行期都会存在。
我们应该把会话工厂设计为单一的实例  单例模式

SqlSession:会话
由会话工厂创建的,作用:操作数据库的
SqlSession是线程不安全的。Servlet?线程不安全的。单一的实例。解决方案:把会话声明成方法内部的局部变量

Mybatis.xml全局配置文件配置

<properties resource="db.properties"></properties>
<!-- 别名 -->
<typeAliases>
	<!-- type:给谁起别名 alias:自定义别名 -->
	<!-- <typeAlias type="com.hpe.pojo.User" alias="user"/> -->
	<!-- 批量起别名 name:指的是包,给每个包下的类起别名 别名是类名(首字母大小写都可以) -->
	<package name="com.hpe.pojo" />
</typeAliases>
<!-- 配置的运行环境default:默认要加载的环境 当mybatis与spring整合后,一下配置将废除不需要了 -->
<environments default="mysql">
	<environment id="mysql">
		<!-- 事务管理的配置 -->
		<transactionManager type="JDBC"></transactionManager>
		<!-- 数据库连接池 -->
		<dataSource type="POOLED">
			<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>
	<mapper resource="UserMapper.xml" />
</mappers>

映射文件配置
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:命名空间 作用:用于隔离sql -->
<mapper namespace="UserMapper">
<!-- 根据id查询用户的信息 id:用于标识映射文件的sql 称为statement的ID 唯一的,不可重复 parameterType:输入参数的类型 
	支持的类型:java基本类型、pojo、 Map #{}:给占位符设置值 接受参数里的参数信息 当参数类型为基本类型时,#{}里面的参数任意 或者是 
	#{value} resultType:返回结果集类型 支持的类型:java基本类型、pojo、 Map,指的是单条记录类型 -->
<select id="selectById" parameterType="int" resultType="user">
	select * from user where id = #{id}
</select>

<!-- 根据姓名模糊查询 ${}:字符串拼接,没有使用占位符,不安全,有sql注入的风险 如果参数类型为基本类型,则${}里面的参数只能是value 不推荐使用
	→ ${value} -->

<select id="selectByName" parameterType="String" resultType="user">
	select * from user where username like '%${value}%'
</select>
<!-- 新增用户 参数类型为自定义的pojo对象时,取值方式#{属性} keyProperty="id" useGeneratedKeys="true" 	获取新增数据的id -->
<insert id="addUser" parameterType="user"
	keyProperty="id" useGeneratedKeys="true">
	insert into
	user(username,birthday,sex,address)
	values(#{username},#{birthday},#{sex},#{address})
</insert>

<update id="updateUser" parameterType="user">
	update user set
	username=#{username},address=#{address} where id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
	delete from user where id = #{id}
</delete>

mapper动态代理开发
传统的持久层:dao接口 dao实现类
mybatis的持久层:dao接口 dao实现类 (获取会话、调用sqlsession方法进行操作数据库)mapper.xml映射文件
dao实现类的工作:代理他的工作
 演变成:mapper接口(同dao接口)mapper.xml 映射文件
mapper 动态代理开发的规范要求 :

  1. 映射文件mapper.xml的名字和接口的名字一致
  2. 映射文件的namespace要和接口的全路径一致
  3. 映射文件的statement的id要和接口的方法名一致
  4. 映射文件的输入参数类型和接口的输入参数类型一致
  5. 映射文件的返回值类型和接口的返回值类型一致
    mapper.xml详解
    parameterType:输入参数类型 java基本类型、pojo、hashMap
    hashmap通过#{map的key}设置值 如果没有这个key,不会报错,那么取值是null
    接口:
    public List selectByParams(@Param(“sex”) String sex, @Param(“addr”) String address);

多参数无需指定参数类型
1.使用#{index}来取值,从0开始
2.接口参数使用@Param,然后通过#{key}方式进行取值 就是把当前的参数封装到map中
select * from user where sex = #{0} and address = #{1}
select * from user where sex = #{sex} and address like ‘%${addr}%’
resultType:输出结果类型
java基本类型、pojo、hashMap
pojo属性和数据库字段名不匹配问题
解决办法一:给sql查询的结果起别名
select cardId as carid,name as caeName,userId as UserId form car
解决方法二:通过resultMap

id:主键 ,唯一标识
column:数据库表中的字段名
property:实体中的属性名
column和property是映射关系

普通结果
如果字段名和属性名一致,可以省略



resultMap解决映射复杂类型的pojo问题

当做一对多和一对一的时候, 如果字段名和属性名一致,不可以省略





collection:集合,用于一对多 property:属性名 -


普通结果



where
 <WHERE>会去除多余的and or
	<select id="selectIf" parameterType="user" 		resultType="user">
	select * from user
	<where>
		<if test="sex!=null and sex!=''">
			and sex = #{sex}
		</if>
		<if test="address!=null and address!=''">
			and address like '%${address}%'
		</if>
	</where>
</select>
set
<update id="updateUser" parameterType="user">
	update user
	<set>
		<if test="address!=null and address!=''">
			address = #{address},
		</if>
		<if test="username!=null and username!=''">
			username = #{username}
		</if>
		where id = #{id}
	</set>
</update>
foreach
collection:传递过来的参数,可以是list、array、map的key,还可以是pojo的属性
item:循环中的当前元素  
index:当前元素的下标
open:以什么开始
close:以什么结束
separator:用什么分割

<delete id="deleteByIds" parameterType="list">
	delete from user where id in 
	<foreach collection="list" item="id" open="(" close=")" separator=",">
		#{id}
	</foreach>
</delete>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值