MyBatis框架详解

Mybatis框架图及各部分解释:

Mybatis架构图
		// 创建SqlSessionFactoryBuilder对象
		SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
		// 查找配置文件创建输入流
		InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 加载配置文件,创建SqlSessionFactory对象
		SqlSessionFactory	sqlSessionFactory = sfb.build(inputStream);
		// 创建SqlSession对象,包含对数据库访问的所有api
		SqlSession sqlSession = sqlSessionFactory.openSession();

		// 执行查询,参数一:要查询的statementId ,参数二:sql语句入参,要带上命名空间
		User user = sqlSession.selectOne("user.getUserById", 1);

        // 执行插入时,由于Mybatis默认不自动提交,需要手动提交事务
		sqlSession.insert("user.insertUser", user);
		// 提交事务
		sqlSession.commit();

		// 输出查询结果
		System.out.println(user);
		// 释放资源
		sqlSession.close();

 映射文件查询sql语句配置如下:

<!-- id:statementId
		 resultType:查询结果集的数据类型;如果要返回数据集合,只需设定为每一个元素的数据类型
		 parameterType:查询的入参
	-->
<select id="getUserById" parameterType="int" resultType="com.itheima.mybatis.pojo.User" >
		SELECT * FROM USER WHERE id = #{id1}
</select>
#{}相当于占位符,相当于jdbc的“?”
${value}字符串拼接指令 如:`%${}%`

映射文件插入操作sql语句(包含自增返回)配置如下:

<!-- useGeneratedKeys:标识插入使用自增id
		 keyProperty:与useGeneratedKeys配套使用,用于绑定主键接收的pojo属性
	 -->
	 <insert id="insertUserKey" parameterType="com.itheima.mybatis.pojo.User" useGeneratedKeys="true" keyProperty="id">
	 	<!-- selectKey:用于配置主键返回
	 		 keyProperty:要绑定的pojo属性
	 		 resultType:属性数据类型
	 		 order:指定什么时候执行,AFTER之后
	 	-->
		<!-- <selectKey keyProperty="id" resultType="int" order="AFTER">
			 SELECT LAST_INSERT_ID()
		</selectKey> -->
INSERT INTO USER
		            (`username`,
		             `birthday`,
		             `sex`,
		             `address`,
		             `uuid2`)
		VALUES (#{username},
		        #{birthday},
		        #{sex},
		        #{address},
		        #{uuid2});
	</insert>

修改用户配置:

<update id="updateUser" parameterType="com.itheima.mybatis.pojo.User">
		UPDATE USER SET username = #{username} WHERE id = #{id}
</update>

删除用户配置:

<delete id="deleteUser" parameterType="int">
		DELETE FROM `user` WHERE `id` = #{id1}
</delete>

Mybatis输入输出映射的类型:

输入类型:

  1. 传递简单的数据类型
    parameterType="简单类型"
  2. 传递pojo类
    parameterType="pojo" 通过ognl表达式获取pojo中的值:
                   (#{username},
                    #{birthday},
                    #{sex},
                    #{address},
                    #{uuid2});
  3. 传递包装pojo(对象中有对象)
    %${user.username}%
    //queryvo为包含user对象类的别名
    <select id="getUserByQueryVo" parameterType="queryvo" resultType="com.itheima.mybatis.pojo.User">
    		<!-- SELECT * FROM USER WHERE username LIKE #{name} -->
    		SELECT * FROM USER WHERE username LIKE '%${user.username}%'
    </select>

     

输出类型: 

  1. 输出简单类型
    <!-- 查询用户总记录数,演示返回简单类型 -->
    	<select id="getUserCount" resultType="int">
    		SELECT COUNT(1) FROM USER
    	</select>

     

  2. 输出pojo类型
  3. 输出pojo列表
  4. 输出Map对象
    <!-- resultMap入门
    		 type:映射成的pojo类型
    		 id:resultMap唯一标识
    	-->
    	<resultMap type="order" id="orderMap">
    		<!-- id标签用于绑定主键 -->
    		<!-- <id property="id" column="id"/> -->
    		
    		<!-- 使用result绑定普通字段 -->
    		<result property="userId" column="user_id"/>
    		<result property="number" column="number"/>
    		<result property="createtime" column="createtime"/>
    		<result property="note" column="note"/>
    	</resultMap>
    	<!-- 使用resultMap -->
    	<select id="getOrderListResultMap" resultMap="orderMap">
    		SELECT * FROM `order`
    	</select>

     

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值