zoukankan      html  css  js  c++  java
  • (二)mybatis的CRUD

    1.CRUD

    userDao.xml

    <?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="learn.dao.userDao"><!--指向的是java包中的dao接口-->
        <!--配置查询所有
            findALL:UserDao的方法名称
            resultType:返回类型,查询完后要返回去哪里(即把结果集封装到哪里)-->
        <select id="findAll" resultType="learn.domain.User">
            select * from user ;
        </select>
    
        <!-- 保存用户 -->
        <insert id="saveUser" parameterType="learn.domain.User">/*插入参数的类型*/
            <!-- 配置插入操作后,获取插入数据的id */
            /* keyProperty=对应的实体类  keyColum=数据库中对应的列名 resultType结果集类型*/
            /*oredr表示在整个操作中的顺序*/-->
            <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="learn.domain.User">/*插入参数的类型*/
            update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id=#{id};
        </update>
    
        <!--删除用户-->
        <delete id="deleteUser" parameterType="int">/*由于传入的是Integer 所以parameterType为int/Integer/java.lang.Integer*/
            delete from user where id = #{id}/*由于方法只有一个Integer类型的参数,所以{}中只需要写清楚一个占位符即可,占位符的内容没有任何要求,叫什么都可以*/
        </delete>
    
        <!--根据id查询用户-->
        <select id="findById" parameterType="int" resultType="learn.domain.User">
            select * from user where id = #{uid}
        </select>
    
        <!--根据名称模糊查询-->
        <select id="findByName" parameterType="string" resultType="learn.domain.User">
            select * from user where username like #{name}
            <!--可以使用另一种方式来模糊查询-->
            <!--select * from user where username liske '%${value}%'-->
            <!--这种写法是固定的-->
    
        </select>
    
        <!--获取总用户数-->
        <select id="findTotal" resultType="int">
            select count(id) from user;
        </select>
    </mapper>
    

    --1.1两种模糊查询的对比

    --1.2使用实体类的包装对象作为查询条件

    解释,在执行完插入操作之后,再执行<selectKy>中的语句,查询到id

    因此如上图在执行保存方法之前输出的user对象是没有id的,执行之后user对象有id,这个id就是通过对象查询得到的

    1.3解决实体类属性名和表的列名不一致的情况

    解决方式1--起别名

    解决方式2--使用resultMap

    2.mybatis中编写dao的实现类(了解)

    教程链接p30-32 (https://www.bilibili.com/video/BV1mE411X7yp?p=32&spm_id_from=pageDriver)

    3.执行过程源码分析

    --3.1mybatis使用dao实现类的执行过程分析 p33-35

    教程链接 (https://www.bilibili.com/video/BV1mE411X7yp?p=34&spm_id_from=pageDriver)

    --3.2mybatis使用dao代理的执行过程分析p36-37

    教程链接 (https://www.bilibili.com/video/BV1mE411X7yp?p=36)

    4.properties标签的使用及细节

    • SqlMapConfig.xml
    <!--配置properties
    
            可以在标签内部配置连接数据库的信息.也可以通过属性引用外部配置文件信息
            resource属性引用:
                用于指定配置文件的位置,是按照类路径的写法来写,并且必须存在于内路径下
            url属性引用:是要求按照url的写法来写地址
                URL:Uniform Resource Locator 统一资源定位符,可以唯一标识一个资源的位置
                他的写法:协议+    主机+    端口+   URI
                     eg:http://localhost:8080/mybatisserver/frmo1Servlet
                     除了http协议还有file协议
                URI: Uniform Resource Identifier 统一资源标识符,他是在应用中可以唯一定位的资源
        -->
        <properties resource="jdbcConfig.properties"> <!--也可以通过url引用网络上的properties配置文件-->
    
        </properties>
    
    • jdbcConfig.properties
    jdbc.driver=com.mysql.jdbc.Driver
    
    jdbc.url=jdbc:mysql://localhost:3306/ssm?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=UTC
    jdbc.username=root
    jdbc.password=251091
    

    5.typeAliases标签和package标签

    <!--使用typeAliases配置别名,他只能配置domain了中类名的别名-->
    
        <typeAliases>
            <!--typeAlias用于配置别名,types属性指定的是实体类全限定类名,alias属性指定别名,当指定了别名就不再区分大小写()针对于使用别名的情况-->
            <!--<typeAlias type="learn.domain.User" alias="user"></typeAlias>-->
            <!-- 上述方法对于类多的情况下就比较麻烦-->
            <!--用于指定要配置别名的包,当指定后,该包下的实体类都会注册别名,并且类名就是别名不再区分大小写-->
            <package name="learn.domain"></package>
        </typeAliases>
    
  • 相关阅读:
    整数幂的求解
    非递归实现不重复序列的全排列(二)
    完整的将日期时间转换为汉字的代码
    如何得到某集合的所有子集合?
    再谈八皇后问题
    大数阶乘的计算(六)
    非递归实现不重复序列的全排列(一)
    非递归实现不重复序列的全排列(三)
    大数阶乘的计算(五)
    关于走楼梯的递归算法
  • 原文地址:https://www.cnblogs.com/psyduck/p/14393706.html
Copyright © 2011-2022 走看看