• mybatis crud基础


    修改

    1、接口

    2、配置文件

    一、基础crud

    1、select

    接口类

    User getUserById(int id);

    xml文件

        <!--id 方法名 resultType返回值类型 parameterType参数类型-->
        <select id="getUserById" resultType="com.wt.pojo.User" parameterType="int">
            select * from mybatis.user where id = #{id}
        </select>

    测试

      @Test
      public void getUsrByID(){
            SqlSession sqlSession = MyBatisUtil.getSession();
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            User user = userDao.getUserById(1);
            System.out.println(user);
            sqlSession.close();
      }

    2、insert

    接口类

    int insertUser(User user);

    xml文件

      <insert id="insertUser" parameterType="com.wt.pojo.User">
            insert into mybatis.user(name, pwd) values(#{name}, #{pwd})
        </insert>

    测试

      @Test
      public void InsertUser(){
       SqlSession sqlSession = MyBatisUtil.getSession();
       UserDao userDao = sqlSession.getMapper(UserDao.class);
       userDao.insertUser(new User("fun", "123"));
       sqlSession.commit();
       sqlSession.close();
      }

    3、update

    接口类

    int updateUser(User user);

    xml文件

      <update id="updateUser" parameterType="com.wt.pojo.User">
            update mybatis.user set name = #{name},  pwd = #{pwd}  where id = #{id} ;
        </update>

    测试

      @Test
      public void updateUser(){
            SqlSession sqlSession = MyBatisUtil.getSession();
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            userDao.updateUser(new User(1, "aishe", "123"));
            sqlSession.commit();
            sqlSession.close();
      }

    4、delete

    接口类(Mapper)

    int deleteUser(int id);

    xml

        <delete id="deleteUser" parameterType="int">
            delete from mybatis.user where id = #{id}
        </delete>

    测试

      @Test
      public void deleteUser(){
           SqlSession sqlSession = MyBatisUtil.getSession();
           UserDao userDao = sqlSession.getMapper(UserDao.class);
           userDao.deleteUser(2);
           sqlSession.commit();
           sqlSession.close();
      }

    注意:

    1、insert update delete commit

    2、insert update delete  方法类型 int

  • 相关阅读:
    关于浏览器cookie的详解
    浏览器渲染帧
    Vue3中关于diff的优化
    JS报错 Cannot set property 'display' of undefined问题
    vue选项卡 ,循环数据点击改变class,并初始循环中的第一个为选中状态class
    Nuxt中引入elementUI
    jq 获取节点 改变元素class的显示、隐藏
    vue监听滚动条
    Nuxt中引入iconfont图标
    vue导航点击切换 2.0
  • 原文地址:https://www.cnblogs.com/wt7018/p/13329593.html
Copyright © 2020-2023  润新知