MyBatis的增删改查操作

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

  1. 添加语句

在映射文件中添加语句


	 
		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);















动态参数

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

//if拼接  sql语句要跟上 where 1 =1 
List selectStudentByIf(StudentBean student);



//不生成 1= 1  提高效率   自动在sql语句拼接的时候加上where  关键字
List selectStudentByWhere(StudentBean student);



//多选一
List selectStudentByChoose(StudentBean student);



List selectStudentByForeachArray(int[] ids);

	

List selectStudentByForeachList(List ids);



List selectStudentByForeachStudent(List students);



List selectStudentBySqlFragement();


 * from

//统计一张表的总数据条数  分页的总条数
int selectStudentCount();

	



你可能感兴趣的:(MyBatis的增删改查操作)