Mybatis增删改查

参考 增删改查

简单的CRUD可以用根据mybatis提供的注解来实现,不需要针对StudentMapper 接口去编写具体的实现类代码,这个具体的实现类由MyBatis帮我们动态构建出来,我们只需要直接拿来使用即可

1.@Select注解 实现简单的查询

import java.util.List;
import org.apache.ibatis.annotations.Select;

public interface StudentMapper {
	 
	@Select("select * from Student")
	List<Student> selectAll();

	@Select("SELECT * FROM Student where studentName = #{name}")
	Student selectByStudentName(String name);
}

2.@Delete注解 实现删除

import org.apache.ibatis.annotations.Delete;

public interface StudentMapper {

	@Delete("delete from Student")
	void deleteAll();
}

3.xml 实现插入

在StudentMapper定义方法insertBatch()

import java.util.List;

public interface StudentMapper {
	
	void insertBatch(List<Student> students);
}

在StudentMapper.xml文件中添加如下sql 

<insert id="insertBatch" parameterType="com.demo.model.Student" >
    insert into Student (StudentId, StudentName, Remark)
    values 
    <foreach collection="list" item="item" index="index" separator=",">    
    (#{item.Studentid,jdbcType=VARCHAR}, #{item.Studentname,jdbcType=NVARCHAR}, #{item.remark,jdbcType=VARCHAR})      
    </foreach>     
</insert>

 

posted on 2020-07-22 14:18  dreamstar  阅读(63)  评论(0编辑  收藏  举报