dao
package dao;
import model.Student;
import java.util.List;
public interface StudentMapper {
List<Student> selectAllStudent();
int insertStudent(Student student);
int updateStudent(Student student);
int deleteById(int id);
}
mapple.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.StudentMapper">
<select id="selectAllStudent" resultType="student">
select * from `student`
</select>
<insert id="insertStudent" parameterType="student">
insert into `student`(`name`,`age`)
values(#{name},#{age})
<selectKey resultType="int" keyColumn="id" keyProperty="id" order="AFTER">
select last_insert_id();
</selectKey>
</insert>
<update id="updateStudent" parameterType="student">
update `student` set `name` = #{name},`age` = #{age}
where `id` = #{id}
</update>
<delete id="deleteById" parameterType="int">
delete from `student` where `id` = #{id}
</delete>
</mapper>
测试
import dao.StudentMapper;
import model.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
public class AppTest {
SqlSessionFactory factory = null;
@Before
public void prepare() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
factory = new SqlSessionFactoryBuilder().build(inputStream);
inputStream.close();
}
@Test
public void test(){
SqlSession session= factory.openSession();
try{
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
List<Student> list = studentMapper.selectAllStudent();
Iterator<Student> iterator = list.iterator();
while(iterator.hasNext()){
Student student = iterator.next();
System.out.println(student);
}
}finally{
session.commit();
session.close();
}
}
@Test
public void testInsert(){
SqlSession sqlSession = factory.openSession();
try{
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student = new Student("young",20);
int affectedRows = studentMapper.insertStudent(student);
if(affectedRows==1){
System.out.println("插入成功!");
System.out.println(student);//查看从数据库返回id填充student的id属性
}else{
System.out.println("插入失败!");
}
}finally {
sqlSession.commit();
sqlSession.close();
}
}
@Test
public void testUpdate(){
SqlSession sqlSession = factory.openSession();
try{
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student = new Student(10000,"Tom",21);
int affectedRows = studentMapper.updateStudent(student);
if(affectedRows != 0){
System.out.println("更新成功!");
System.out.println(student);
}else{
System.out.println("更新失败");
}
}finally {
sqlSession.commit();
sqlSession.close();
}
}
@Test
public void testDelete(){
SqlSession sqlSession = factory.openSession();
try{
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
int id = 10000;
int affectedRows = studentMapper.deleteById(id);
if(affectedRows == 1){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}finally {
sqlSession.commit();
sqlSession.close();
}
}
}
注意
- 插入、删除、修改要获取数据库中受影响的记录数,以便验证操作是否成功
- 插入一条数据最好返回从数据库返回生成的ID来给model的id赋值,以便后续使用