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

posted @ 2019-06-24 13:48  tiana_Z  阅读(137)  评论(0编辑  收藏  举报