zoukankan      html  css  js  c++  java
  • MyBatis的增删改查操作

      搭建好mybatis之后 进行对数据库的操作

      添加语句

      在映射文件中添加语句

      insert into student(name,age,score) values(#{name},#{age},#{score})

      映射文件要放在与接口一个目录下

      namespace:必须是对应接口的全限定名

      id :dao中的方法名字

      parameterType:传入的参数类型 可以省略

      添加语句后 获取主键的值 赋值给主键

      insert into student(name,age,score) values(#{name},#{age},#{score})

      select @@identity

      2.删除语句

      update student set name=#{name} ,age=#{age},score=#{score}

      where id = #{id}

      3.查询语句

      /**

      * 静态参数

      * @param student

      */

      void insertStudent(StudentBean student);

      List selectStudentAll();

      //根据姓名模糊查询

      List selectStudentByName(String name);

      //多参数查询 使用map作为方法参数

      List selectStuByMap1(Map map);

      List selectStuByMap2(Map map);

      List selectStuByParameters1(String name , int age);

      List selectStuByParameters2(String name , StudentBean student);

      List selectStuByParameters3(@Param("name") String name , @Param("age")int age);

      List selectStuByParameters4(@Param("name") String name , @Param("student") StudentBean student);

      select * from student

      select * from student where name like '%' #{name} '%'

      select * from student where name like '%' #{name} '%' and age>#{age}

      select * from student where name like '%' #{name} '%' and age > #{student.age}

      select * from student where name like '%' #{0} '%' and age > #{1}

      select * from student where name like '%' #{0} '%' and age > #{1.age}

      动态参数

      /**

      * 动态参数

      */

      //mybatis 动态参数类似域jstl《c:》

      //if拼接 sql语句要跟上 where 1 =1

      List selectStudentByIf(StudentBean student);

      select * from student where 1=1

      and name like '%' #{name} '%'

      and age > #{age}

      //不生成 1= 1 提高效率 自动在sql语句拼接的时候加上where 关键字

      List selectStudentByWhere(StudentBean student);

      select * from student

      and name like '%' #{name} '%'

      and age > #{age}

      //多选一无锡人流医院 http://www.bhnfkyy.com/

      List selectStudentByChoose(StudentBean student);

      select * from student

      where name like '%' #{name} '%'

      where age > #{age}

      where 1 = 2

      List selectStudentByForeachArray(int[] ids);

      select * from student

      where id in

      #{id}

      List selectStudentByForeachList(List ids);

      select * from student

      where id in

      #{id}

      List selectStudentByForeachStudent(List students);

      select * from student

      where id in

      #{student.id}

      List selectStudentBySqlFragement();

      select student

      * from

      //统计一张表的总数据条数 分页的总条数

      int selectStudentCount();

      select count(*) from student

  • 相关阅读:
    MondoDB 之 使用python对MongoDB排序 MondoDB -9
    MondoDB 之 使用python对MongoDB进行文档修改 MondoDB -8
    MondoDB 之 使用python对MongoDB进行查询文档 MondoDB -7
    MondoDB 之 使用python对MongoDB进行插入文档 MondoDB -6
    MondoDB 之 使用python操作MongoDB MondoDB -5
    MondoDB 之 $关键字 及 $修改器 $set $inc $push $pull $pop MondoDB -4
    MondoDB 之 数据类型 MondoDB -3
    MongoDB 之 操作增删改查 MongoDB
    简述常见数据库高可用方案
    关于MySql数据库误操作数据找回的办法
  • 原文地址:https://www.cnblogs.com/djw12333/p/11076608.html
Copyright © 2011-2022 走看看