zoukankan      html  css  js  c++  java
  • MyBatis学习(二)代码实战

    一、项目依赖

    本项目是基于mybatis3.4.6版本实现的,用到的jar包如下

    二、项目结构解析

    三、配置文件解析

     四、mapper文件解析

    <?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="pojo.User"><!--这里直接写实体类的路径可以保证唯一性-->
        <!--结果集映射-->
        <resultMap id="BaseResultMap" type="pojo.User">
            <result column="ID" property="id" jdbcType="VARCHAR"/>
            <result column="NAME" property="name" jdbcType="VARCHAR"/>
            <result column="PHONE" property="phone" jdbcType="VARCHAR"/>
            <result column="EMAIL" property="email" jdbcType="VARCHAR"/>
            <result column="QQ" property="qq" jdbcType="VARCHAR"/>
            <result column="COMPANY" property="company" jdbcType="VARCHAR"/>
            <result column="PROFESSION" property="profession" jdbcType="VARCHAR"/>
        </resultMap>
    
        <!--list查询-->
        <select id="listUser" resultMap="BaseResultMap">
            select * from  T_USER
        </select>
    
        <!--新增-->
        <insert id="addUser" parameterType="pojo.User">
            insert into T_USER (ID, NAME, PHONE,EMAIL,QQ,COMPANY,PROFESSION)
            values
            (#{id},#{name},#{phone},#{email},#{qq},#{company},#{profession})
        </insert>
    
        <!--删除-->
        <delete id="deleteUser" parameterType="java.lang.String">
            delete from T_USER where ID = #{id}
        </delete>
    
        <!--单条查询-->
        <select id="getUser" parameterType="java.lang.String" resultType="pojo.User">
            select * from T_USER where id= #{id}
        </select>
    
        <!--更新-->
        <update id="updateUser" parameterType="pojo.User">
            update T_USER
            <set>
                <if test="name != null">
                    NAME = #{name,jdbcType=VARCHAR},
                </if>
                <if test="phone != null">
                    PHONE = #{phone,jdbcType=VARCHAR},
                </if>
                <if test="email != null">
                    EMAIL = #{email,jdbcType=TIMESTAMP},
                </if>
                <if test="qq != null">
                    QQ = #{qq,jdbcType=TIMESTAMP},
                </if>
                <if test="company != null">
                    COMPANY = #{company,jdbcType=VARCHAR},
                </if>
                <if test="profession != null">
                    PROFESSION = #{profession,jdbcType=VARCHAR},
                </if>
            </set>
            where id=#{id}
        </update>
    </mapper>

    参数解析

    • parameterType:要求输入参数的类型
    • resultType:输出的类型
    • resultMap:输出的结果对象,应用与复杂数据的场景,可将查询到的数据映射到一个可自定义的数据集中去。

    小结

    parameterType

      就是用来在 SQL 映射文件中指定输入参数类型的,可以指定为基本数据类型(如 int、float 等)、包装数据类型(如 String、Interger 等)以及用户自己编写的 JavaBean 封装类

    resultType

      resultType 是用来指定数据库返回的信息对应的 Java 的数据类型。

    #{}

      在传统的 JDBC 的编程中,占位符用 “?” 来表示,然后再加载 SQL 之前按照 “?” 的位置设置参数。而 “#{}” 在 MyBatis 中也代表一种占位符,该符号接受输入参数,在大括号中编写参数名称来接受对应参数。当 “#{}” 接受简单类型时可以用 value 或者其他任意名称来获取。

    ${}

      在 SQL 配置中,有时候需要拼接 SQL 语句(例如模糊查询时),用 “#{}” 是无法达到目的的。在 MyBatis 中,“${}” 代表一个 “拼接符号” ,可以在原有 SQL 语句上拼接新的符合 SQL 语法的语句。使用 “${}” 拼接符号拼接 SQL ,会引起 SQL 注入,所以一般不建议使用 “${}”。

    PS: <select> 标签对中 SQL 语句的 “${}” 符号,表示拼接 SQL 串,将接受的参数内容不加任何修饰地拼接在 SQL 中,“${}” 符号中的内容也只能是对应的变量。(例子:%${goodsNameLike}%,%要写在外面)

    MyBatis 使用场景

      通过上面的入门程序,不难看出在进行 MyBatis 开发时,我们的大部分精力都放在了 SQL 映射文件上。在对 SQL 优化要求比较高,或是项目需求或业务经常变动时,我们使用MyBatis。

    MyBatis 的特点

      就是以 SQL 语句为核心的不完全的 ORM(关系型映射)框架。与 Hibernate 相比,Hibernate 的学习成本比较高,而 SQL 语句并不需要开发人员完成,只需要调用相关 API 即可。这对于开发效率是一个优势,但是缺点是没办法对 SQL 语句进行优化和修改。而 MyBatis 虽然需要开发人员自己配置 SQL 语句,MyBatis 来实现映射关系,但是这样的项目可以适应经常变化的项目需求。

    五、测试类解析

    //1、根据 mybatis-config.xml 配置的信息得到 sqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    //2、然后根据 sqlSessionFactory 得到 session
    SqlSession session = sqlSessionFactory.openSession();
    //3、这里调用session对象的相关方法调用 sql 语句来进行数据库交互,此处举查询单条记录的例子
    User user = session.selectOne("getUser", "BF0001");
    System.out.println("查询T_USER表数据===开始===");
    System.out.println("查询到的记录为:" + user.toString());
    System.out.println("查询T_USER表数据===结束===");
    //4、提交修改
    session.commit();
    //5、关闭 session
  • 相关阅读:
    POJ 1401 Factorial
    POJ 2407 Relatives(欧拉函数)
    POJ 1730 Perfect Pth Powers(唯一分解定理)
    POJ 2262 Goldbach's Conjecture(Eratosthenes筛法)
    POJ 2551 Ones
    POJ 1163 The Triangle
    POJ 3356 AGTC
    POJ 2192 Zipper
    POJ 1080 Human Gene Functions
    POJ 1159 Palindrome(最长公共子序列)
  • 原文地址:https://www.cnblogs.com/riches/p/11648133.html
Copyright © 2011-2022 走看看