1.创建Maven项目,项目名称mybatisdemo1,目录结构如图所示


2.pom.xml中配置内容如下


  4.0.0
  com.mycompany
  mybatisdemo1
  0.0.1-SNAPSHOT
  
  
  
  	
	
	    org.apache.logging.log4j
	    log4j-core
	    2.7
	
	
	
	
	    org.mybatis
	    mybatis
	    3.4.1
	
	
	
	
	    mysql
	    mysql-connector-java
	    5.1.39
	
	
	
	
	    junit
	    junit
	    4.11
	
	
	
  
  
  


3.在src/main/java目录创建实体类StudentBean,包名(com.mycompany.beans),目录结构如下

MyBatis-增删改查_第1张图片


4.实体类StudentBean的内容如下

package com.mycompany.beans;

public class StudentBean {
	private Integer sid;
	private String name;
	
	public StudentBean() {
		super();
	}
	
	public StudentBean(String name) {
		super();
		this.name = name;
	}
	
	public StudentBean(Integer sid, String name) {
		super();
		this.sid = sid;
		this.name = name;
	}

	public Integer getSid() {
		return sid;
	}
	public void setSid(Integer sid) {
		this.sid = sid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String toString() {
		return "StudentBean [sid="+sid+",name="+name+"]";
	}
}


5.在src/main/java下创建接口StudentMapper,包名(com.mycompany.mapper)目录结构如下


6.接口StudentMapperr的内容如下

package com.mycompany.mapper;

import java.util.List;

import com.mycompany.beans.StudentBean;

public interface StudentMapper {
	/**
	 * 新增用户
	 * @param student
	 * @return
	 * @throws Exception
	 */
	public int insertStudent(StudentBean student) throws Exception;
	
	/**
	 * 修改用户
	 * @param student
	 * @param sid
	 * @return
	 * @throws Exception
	 */
	public int updateStudent(StudentBean student) throws Exception;
	
	/**
	 * 删除用户
	 * @return
	 * @throws Exception
	 */
	public int deleteStudent(int sid) throws Exception;
	
	/**
	 * 根据ID查询学生信息
	 * @return
	 * @throws Exception
	 */
	public StudentBean selectStudentById(int sid) throws Exception;
	
	/**
	 * 查询所有的学生信息
	 * @return
	 * @throws Exception
	 */
	public List selectAllStudent() throws Exception;
}


7.在src/main/java下创建映射文件StudentMapper.xml,包名(com.mycompany.mapper),目录结构如下

MyBatis-增删改查_第2张图片


8.映射文件StudentMapper.xml的内容如下




	    
	
		INSERT INTO student (name) VALUES (#{name})
	
	
	
	
		UPDATE student SET name = #{name} WHERE sid = #{sid}
	
	
	
	
		DELETE FROM student WHERE sid=#{sid}
	
	
	
	
		SELECT * FROM student WHERE sid=#{sid}
	
	
	
	
		SELECT * FROM student
	
	
	
		
		
	


9.在src/main/java下创建工具类DBUtils,包名(com.mycompany.util)目录结构如下


10.工具类DBUtils的内容如下

package com.mycompany.util;

import java.io.IOException;
import java.io.Reader;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class DBUtils {
	public static SqlSessionFactory sessionFactory;
	
	static{
		try {
			Reader reader = Resources.getResourceAsReader("mybatis.cfg.xml");
			sessionFactory = new SqlSessionFactoryBuilder().build(reader);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static SqlSession getSession(){
		return sessionFactory.openSession();
	}
}


11.在src/main/resources下创建mysql.properties属性文件,目录结构如下

MyBatis-增删改查_第3张图片


12.mysql.properties属性文件内容如下

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=


13.在src/main/resources下创建mybatis配置文件mybatis.cfg.xml,目录结构如下

MyBatis-增删改查_第4张图片


14.配置文件mybatis.cfg.xml的内容如下




	
  	
  	
  	
  		
  	
  	
  	
    
		
			
            
            
            
            
            
            
            	
                
                
                
            
        
      
     
     
     	
     
     


15.在src/main/java下创建StudentService文件,包名(com.mycompany.service),目录结构如下

MyBatis-增删改查_第5张图片


16.StudentService文件的内容如下

package com.mycompany.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.mycompany.beans.StudentBean;
import com.mycompany.mapper.StudentMapper;
import com.mycompany.util.DBUtils;

public class StudentService {
	/**
	 * 添加学生信息
	 * @param student
	 */
	public void insertStudent(StudentBean student){
		SqlSession session = DBUtils.getSession();
		StudentMapper mapper = session.getMapper(StudentMapper.class);
		try {
			int result = mapper.insertStudent(student);
			System.out.println(result);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		}
	}
	
	/**
	 * 修改学生信息
	 * @param student
	 */
	public void updateStudent(StudentBean student){
		SqlSession session = DBUtils.getSession();
		StudentMapper mapper = session.getMapper(StudentMapper.class);
		try {
			int result = mapper.updateStudent(student);
			System.out.println(result);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		}
	}
	
	/**
	 * 删除学生信息
	 * @param sid
	 */
	public void deleteStudent(int sid){
		SqlSession session = DBUtils.getSession();
		StudentMapper mapper = session.getMapper(StudentMapper.class);
		try {
			int result = mapper.deleteStudent(sid);
			System.out.println(result);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		}
	}
	
	/**
	 * 根据ID查询学生信息
	 * @param sid
	 */
	public void selectStudentById(int sid){
		SqlSession session = DBUtils.getSession();
		StudentMapper mapper = session.getMapper(StudentMapper.class);
		try {
			StudentBean student = mapper.selectStudentById(sid);
			System.out.println(student);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		}
	}
	
	/**
	 * 查询所有学生信息
	 */
	public void selectAllStudent(){
		SqlSession session = DBUtils.getSession();
		StudentMapper mapper = session.getMapper(StudentMapper.class);
		try {
			List students = mapper.selectAllStudent();
			for (StudentBean studentBean : students) {
				System.out.println(studentBean);
			}
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		}
	}
}


17.在src/test/java下创建StudentServiceTest测试类,包名(com.mycompany.service)目录结构如下

MyBatis-增删改查_第6张图片


18.StudentServiceTest测试类的内容如下

package com.mycompany.service;

import org.junit.Test;

import com.mycompany.beans.StudentBean;

public class StudentServiceTest {

	/**
	 * 测试添加学生信息操作
	 */
	@Test
	public void insertStudentTest(){
		StudentService studentService = new StudentService();
		StudentBean student = new StudentBean("test insert");
		studentService.insertStudent(student);
	}
	
	/**
	 * 测试修改学生信息操作
	 */
	@Test
	public void updateStudent(){
		StudentService studentService = new StudentService();
		StudentBean studentBean = new StudentBean(5,"test update");
		studentService.updateStudent(studentBean);
	}
	
	/**
	 * 测试删除学生信息操作
	 */
	@Test
	public void deleteStudent(){
		StudentService studentService = new StudentService();
		studentService.deleteStudent(7);
	}
	
	/**
	 * 测试根据ID查询学生信息
	 */
	@Test
	public void selectStudentById(){
		StudentService studentService = new StudentService();
		studentService.selectStudentById(8);
	}
	
	/**
	 * 测试查询所有学生信息
	 */
	@Test
	public void selectAllStudent(){
		StudentService studentService = new StudentService();
		studentService.selectAllStudent();
	}
}

MyBatis-增删改查_第7张图片