• MyBatis(五)、CRUD操作与重要参数及标签


    1.CRUD操作

    <?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.bittergourd.mybatis.dao.UserDao">
    <!-- 配置查询所有 id是对应接口的方法名 sql语句建议不要写*号 -->
    <select id="findAll" resultType="com.bittergourd.mybatis.domain.User">
            select * from user
        </select>
        <insert id="saveUser"
                parameterType="com.bittergourd.mybatis.domain.User">
            <!-- 返回插入数据 的id  /*这种注释不对  如果不是原生get方法,需要写get后的名称*/ -->
            <selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
                select last_insert_id();
            </selectKey>
            insert into user(username,address,sex,birthday)
            values(#{username},#{address},#{sex},#{birthday})
        </insert>
        <update id="updateUser"
                parameterType="com.bittergourd.mybatis.domain.User">
            update user set
            username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id={#id};
        </update>
        <delete id="deleteUser" parameterType="int">
            delete from user where id=#{id} /*只有一个参数时只需要有一个占位符即可*/
        </delete>
    
        <!-- 查询一个-->
        <select id="findById" parameterType="int"
                resultType="com.bittergourd.mybatis.domain.User">
            select * from user where id=#{abc};
        </select>
    
        <!-- 根据名称模糊查询 -->
        <select id="findByName" parameterType="string"
                resultType="com.bittergourd.mybatis.domain.User">
            <!-- 调用方法的时候再提供百分号%% preparestatement-->
            select * from user where username like #{username};
            <!-- 下面也可以 value固定写法 不推荐 因为是再statement上处理-->
            <!-- select * from user where username like '%${value}%'; -->
        </select>
        
        <!-- 聚合函数 -->
        <select id="findTotal" resultType="Int">
            select count(id) from user ;
        </select>
    </mapper>
    

    保存并得到保存后的id

    2.parameterType(输入类型)

    1.传递简单类型

    2.传递pojo对象

    Mybatis使用ognl表达式解析对象字段的值,#{} 或者 ${} 括号中的值为pojo属性的名称

    ognl表达式:

    通过对象的取值方法来获取数据。写法上get省略

    Object Graphic Navigation Language

    对象 图 导航 语言

    • 类中: user.getUsername()
    • OGNL表达式写法: user.username

    通常包装多个对象到一个对象里

    3.resultType(输出结果)

    username数据库字段与userName对象字段可以对应

    因为mysql在windows系统下不区分大小写,在linux系统下严格区分,所以也会报错

    之前进行查询的前提是

    对象的字段与数据库的字段保持一致

    只有这样才能从数据库查出结果封装
    进pojo对象。

    如果对象的字段与数据库的字段不一致
    • 1.让sql语句里面别名为对象的字段
    • 2.使用resultMap让值一一对应
      【开发效率提升,执行效率降低】
       <resultMap id="userMap"
                   type="com.bittergourd.mybatis.domain.User">
            <!-- 主键字段的对应 -->
            <id property="userId" column="id"></id>
            <!-- 非主键字段-->
            <result property="userName" column="username"></result>
        </resultMap>
    
    

    重要标签

    知识点

    • properties的url与resources [注意本地路径也是一种url]
    • typeAliases与package
    • mappers与package

    引出问题:

    答案:
    parameterType都在Mybatis里面注册过了[所以在windows下数据库不区分大小写]

    resultType如果也要这么办可以使用typeAliases与package [实体类别名]

    mapper的package是规定了

        <mappers>
            <!--<mapper resource="com/itheima/dao/IUserDao.xml"></mapper>-->
            <!-- package标签是用于指定dao接口所在的包,当指定了之后就不需要在写mapper以及resource或者class了 -->
            <package name="com.bittergourd.mybatis.dao"></package>
        </mappers>
    
    完整配置:
    <?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">
    <configuration>
        <!-- 配置properties
            可以在标签内部配置连接数据库的信息。也可以通过属性引用外部配置文件信息
            resource属性: 常用的
                用于指定配置文件的位置,是按照类路径的写法来写,并且必须存在于类路径下。
            url属性:
                是要求按照Url的写法来写地址
                URL:Uniform Resource Locator 统一资源定位符。它是可以唯一标识一个资源的位置。
                它的写法:
                    http://localhost:8080/mybatisserver/demo1Servlet
                    协议      主机     端口       URI
    
                URI:Uniform Resource Identifier 统一资源标识符。它是在应用中可以唯一定位一个资源的。
        -->
        <properties url="file:///D:/IdeaProjects/day02_eesy_01mybatisCRUD/src/main/resources/jdbcConfig.properties">
            <!-- <property name="driver" value="com.mysql.jdbc.Driver"></property>
             <property name="url" value="jdbc:mysql://localhost:3306/eesy_mybatis"></property>
             <property name="username" value="root"></property>
             <property name="password" value="1234"></property>-->
        </properties>
    
        <!--使用typeAliases配置别名,它只能配置domain中类的别名 -->
        <typeAliases>
            <!--typeAlias用于配置别名。
            type属性指定的是实体类全限定类名。alias属性指定别名,当指定了别名就不再区分大小写
            <typeAlias type="com.itheima.domain.User" alias="user"></typeAlias>-->
    
            <!-- 用于指定要配置别名的包,当指定之后,该包下的实体类都会注册别名,并且类名就是别名,不再区分大小写-->
            <package name="com.bittergourd.mybatis.domain"></package>
        </typeAliases>
    
        <!--配置环境-->
        <environments default="mysql">
            <!-- 配置mysql的环境-->
            <environment id="mysql">
                <!-- 配置事务 -->
                <transactionManager type="JDBC"></transactionManager>
    
                <!--配置连接池-->
                <dataSource type="POOLED">
                    <property name="driver" value="${jdbc.driver}"></property>
                    <property name="url" value="${jdbc.url}"></property>
                    <property name="username" value="${jdbc.username}"></property>
                    <property name="password" value="${jdbc.password}"></property>
                </dataSource>
            </environment>
        </environments>
        <!-- 配置映射文件的位置 -->
        <mappers>
            <!--<mapper resource="com/itheima/dao/IUserDao.xml"></mapper>-->
            <!-- package标签是用于指定dao接口所在的包,当指定了之后就不需要在写mapper以及resource或者class了 -->
            <package name="com.bittergourd.mybatis.dao"></package>
        </mappers>
    </configuration>
    

    4.if、where [ 条件查询 ]

        <!--  根据条件查询  -->
        <select id="findUserByCondition"
                resultType="com.bittergourd.mybatis.domain.User"
                parameterType="user">
            select * from user where 1=1
            <if test="username != null">
                and username = #{username}
            </if>
            <if test="sex != null">
                and sex = #{sex}
            </if>
        </select>
    
        <!--  根据条件查询  -->
        <select id="findUserByCondition2"
                resultType="com.bittergourd.mybatis.domain.User"
                parameterType="user">
            select * from user
            <where>
                <if test="username != null">
                    and username = #{username}
                </if>
                <if test="sex != null">
                    and sex = #{sex}
                </if>
            </where>
        </select>
    

    5.in条件查询与sql的复用

        <sql id="defaultSql">
            select * from user
            <!-- mybatis里面的;可写可不写,但是这种写法不能写,因为后面还有语句 -->
        </sql>
        <!--  根据条件查询  -->
        <select id="findUserByCondition3"
                resultType="com.bittergourd.mybatis.domain.User"
                parameterType="queryvo">
    --         select * from user
            <include refid="defaultSql"></include>
            <where>
                <if test="username != null">
                    <foreach collection="ids" open="and id in (" close=")"
                             item="uid" separator=",">
                        <!-- 需要与item保持一致即可 -->
                        #{uid}
                    </foreach>
                </if>
            </where>
        </select>
    
  • 相关阅读:
    [PDF]阅读、注释最佳软件
    [CentOS 7]挂载ntfs格式U盘
    如何更改键盘按键---KeyTweak?
    ssh 文件上传、文件目录上传和下载
    centos7安装Anaconda(Anaconda3-2020.02-Linux-x86_64)与基本命令使用
    Ubuntu 下SVN常用操作
    程序员常用docker命令
    numpy&pandas
    Deep Learning with pytorch笔记(第三章)
    pytorch中的ReflectionPad2d
  • 原文地址:https://www.cnblogs.com/biturd/p/12623139.html
Copyright © 2020-2023  润新知