20、SSM框架-Mybatis接口编程方式实现增删改查 (2)

   前面一章已经搭建好了eclipse,mybatis,mysql的环境,并且实现了一个简单的查询。请注意,这种方式是用SqlSession实例来直接执行已映射的SQL语句:

[java] view plain copy print ?
  1. session.selectOne("com.mucfc.model.EmployeerMapper.findEmployeerByID"1);  
session.selectOne("com.mucfc.model.EmployeerMapper.findEmployeerByID", 1);
 
  

其实还有更简单的方法,而且是更好的方法,使用合理描述参数和SQL语句返回值的接口(比如EmployeerMapper.java),这样现在就可以至此那个更简单,更安全的代码,没有容易发生的字符串文字和转换的错误.下面是详细过程:

本文要改成以接口类来实现增删改查

整个工程目录 :

20、SSM框架-Mybatis接口编程方式实现增删改查 (2)_第1张图片

本文示例工程免费下载

1、首先还是mybatis的配置文件mybatis-config.xml

[html] view plain copy print ?
  1. xml version="1.0" encoding="UTF-8" ?>  
  2. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  3. "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  4.   
  5. <configuration>  
  6.       
  7.      <properties resource="mysql.properties" />   
  8.       
  9.     <typeAliases>  
  10.         <typeAlias alias="Employeer" type="com.mucfc.model.Employeer"  />  
  11.     typeAliases>  
  12.       
  13.     <environments default="development">  
  14.         <environment id="development">  
  15.             <transactionManager type="JDBC" />  
  16.             <dataSource type="POOLED">  
  17.           
  18.     <mappers>  
  19.         <mapper resource="com/mucfc/model/Employeer.xml" />  
  20.     mappers>  
  21. configuration>  




	
	  
	
	
		
	
	
	
		
			
			
		
			
				
				
				 
			
		
	
	
	
		
	

其中配置文件mysql.properties

[html] view plain copy print ?
  1. driver=com.mysql.jdbc.Driver  
  2. url=jdbc:mysql://localhost:3306/test?charset=utf8  
  3. username=root  
  4. password=christmas258@  
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?charset=utf8
username=root
password=christmas258@


2、数据表对应的类Employeer.JAVA

[java] view plain copy print ?
  1. package com.mucfc.model;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. /** 
  6.  * 雇员信息类 
  7.  *@author linbingwen 
  8.  *@time 2015.5.11 
  9.  */  
  10. public class Employeer {  
  11.     private Integer employeer_id;  
  12.     private String employeer_name;  
  13.     private Integer employeer_age ;  
  14.     private String employeer_department;  
  15.     private String employeer_worktype;  
  16.       
  17.     public Employeer() {  
  18.         super();  
  19.     }  
  20.     public Integer getEmployeer_id() {  
  21.         return employeer_id;  
  22.     }  
  23.     public void setEmployeer_id(Integer employeer_id) {  
  24.         this.employeer_id = employeer_id;  
  25.     }  
  26.     public String getEmployeer_name() {  
  27.         return employeer_name;  
  28.     }  
  29.     public void setEmployeer_name(String employeer_name) {  
  30.         this.employeer_name = employeer_name;  
  31.     }  
  32.     public Integer getEmployeer_age() {  
  33.         return employeer_age;  
  34.     }  
  35.     public void setEmployeer_age(Integer employeer_age) {  
  36.         this.employeer_age = employeer_age;  
  37.     }  
  38.     public String getEmployeer_department() {  
  39.         return employeer_department;  
  40.     }  
  41.     public void setEmployeer_department(String employeer_department) {  
  42.         this.employeer_department = employeer_department;  
  43.     }  
  44.     public String getEmployeer_worktype() {  
  45.         return employeer_worktype;  
  46.     }  
  47.     public void setEmployeer_worktype(String employeer_worktype) {  
  48.         this.employeer_worktype = employeer_worktype;  
  49.     }  
  50.     @Override  
  51.     public String toString() {  
  52.         return "Employeer [employeer_id=" + employeer_id + ", employeer_name="  
  53.                 + employeer_name + ", employeer_age=" + employeer_age  
  54.                 + ", employeer_department=" + employeer_department  
  55.                 + ", employeer_worktype=" + employeer_worktype + "]";  
  56.     }  
  57.       
  58.   
  59.   
  60. }  
package com.mucfc.model;

import java.io.Serializable;

/**
 * 雇员信息类
 *@author linbingwen
 *@time 2015.5.11
 */
public class Employeer {
	private Integer employeer_id;
	private String employeer_name;
	private Integer employeer_age ;
	private String employeer_department;
	private String employeer_worktype;
	
	public Employeer() {
		super();
	}
	public Integer getEmployeer_id() {
		return employeer_id;
	}
	public void setEmployeer_id(Integer employeer_id) {
		this.employeer_id = employeer_id;
	}
	public String getEmployeer_name() {
		return employeer_name;
	}
	public void setEmployeer_name(String employeer_name) {
		this.employeer_name = employeer_name;
	}
	public Integer getEmployeer_age() {
		return employeer_age;
	}
	public void setEmployeer_age(Integer employeer_age) {
		this.employeer_age = employeer_age;
	}
	public String getEmployeer_department() {
		return employeer_department;
	}
	public void setEmployeer_department(String employeer_department) {
		this.employeer_department = employeer_department;
	}
	public String getEmployeer_worktype() {
		return employeer_worktype;
	}
	public void setEmployeer_worktype(String employeer_worktype) {
		this.employeer_worktype = employeer_worktype;
	}
	@Override
	public String toString() {
		return "Employeer [employeer_id=" + employeer_id + ", employeer_name="
				+ employeer_name + ", employeer_age=" + employeer_age
				+ ", employeer_department=" + employeer_department
				+ ", employeer_worktype=" + employeer_worktype + "]";
	}
	


}
然后是SQL语句的配置Employeer.xml:

[html] view plain copy print ?
  1. xml version="1.0" encoding="UTF-8" ?>  
  2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  3.  <mapper namespace="com.mucfc.mapper.EmployeerMapper">  
  4.    
  5.        
  6.      <select id="findEmployeerByID" parameterType="int" resultType="Employeer">  
  7.      select* from `t_employeer` where employeer_id =#{employeer_id}  
  8.      select>  
  9.        
  10.           
  11.          
  12.     <insert id="addEmployeer" parameterType="Employeer"  
  13.     useGeneratedKeys="true" keyProperty="employeer_id">    
  14.         insert into `t_employeer`(employeer_name,employeer_age,employeer_department,employeer_worktype)   
  15.         values(#{employeer_name},#{employeer_age},#{employeer_department},#{employeer_worktype})    
  16.     insert>    
  17.       
  18.        
  19.     <delete id="deleteEmployeer" parameterType="int">    
  20.         delete from `t_employeer` where employeer_id = #{employeer_id}    
  21.     delete>    
  22.       
  23.         
  24.     <update id="updateEmployeer" parameterType="Employeer">    
  25.         update t_employeer set employeer_name = #{employeer_name},employeer_age= #{employeer_age},employeer_department = #{employeer_department}  
  26.         ,employeer_worktype=#{employeer_worktype}  where employeer_id = #{employeer_id}    
  27.     update>    
  28.       
  29.  mapper>  


 
 
     
     
     
        
       
      
        insert into `t_employeer`(employeer_name,employeer_age,employeer_department,employeer_worktype) 
        values(#{employeer_name},#{employeer_age},#{employeer_department},#{employeer_worktype})  
      
    
     
      
        delete from `t_employeer` where employeer_id = #{employeer_id}  
      
    
      
      
        update t_employeer set employeer_name = #{employeer_name},employeer_age= #{employeer_age},employeer_department = #{employeer_department}
        ,employeer_worktype=#{employeer_worktype}  where employeer_id = #{employeer_id}  
      
    
 

注意com.mucfc.mapper.EmployeerMapper这个类是存在的,它是一个接口类,上一讲我们是使用 com.mucfc.model.EmployeerMapper,而上一讲的这个类是不存在的

4、SQL语句映射类EmployeerMapper.java

[java] view plain copy print ?
  1. package com.mucfc.mapper;  
  2.   
  3. import com.mucfc.model.Employeer;  
  4.   
  5. /** 
  6.  * SQL语句映射类 
  7.  * @author linbingwen 
  8.  * @time 2015.5.11 
  9.  */  
  10. public interface EmployeerMapper {  
  11.     /** 
  12.      * 注意要和Employeer.xml的方法名对应 
  13.      */  
  14.     public Employeer findEmployeerByID(int id);  
  15.   
  16.     /** 
  17.      * 注意要和Employeer.xml的方法名对应 
  18.      */  
  19.     public void addEmployeer(Employeer employeer);  
  20.   
  21.     /** 
  22.      * 注意要和Employeer.xml的方法名对应 
  23.      */  
  24.     public void deleteEmployeer(int id);  
  25.   
  26.     /** 
  27.      * 注意要和Employeer.xml的方法名对应 
  28.      */  
  29.     public void updateEmployeer(Employeer employeer);  
  30.   
  31. }  
package com.mucfc.mapper;

import com.mucfc.model.Employeer;

/**
 * SQL语句映射类
 * @author linbingwen
 * @time 2015.5.11
 */
public interface EmployeerMapper {
	/**
	 * 注意要和Employeer.xml的方法名对应
	 */
	public Employeer findEmployeerByID(int id);

	/**
	 * 注意要和Employeer.xml的方法名对应
	 */
	public void addEmployeer(Employeer employeer);

	/**
	 * 注意要和Employeer.xml的方法名对应
	 */
	public void deleteEmployeer(int id);

	/**
	 * 注意要和Employeer.xml的方法名对应
	 */
	public void updateEmployeer(Employeer employeer);

}
5、测试:

(1)增加命令

上一讲中的方式:

[java] view plain copy print ?
  1. /** 
  2.  * 增加 
  3.  */  
  4. public static void addEmployeer(Employeer employeer){  
  5.     SqlSession session = null;  
  6.     try {  
  7.         session = sqlSessionFactory.openSession();  
  8.          //返回值是记录条数    
  9.            int resultCount = session.insert("com.mucfc.model.EmployeerMapper.addEmployeer", employeer );    
  10.            System.out.printf("当前插入的employeer_id :%d    当前插入数据库中条数:%d " , employeer.getEmployeer_id() ,resultCount);  //获取插入对象的id    
  11.            System.out.println("");  
  12.            session.commit() ;         
  13.     } finally {  
  14.         session.close();  
  15.     }  
  16.       
  17. }  
	/**
	 * 增加
	 */
	public static void addEmployeer(Employeer employeer){
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			 //返回值是记录条数  
            int resultCount = session.insert("com.mucfc.model.EmployeerMapper.addEmployeer", employeer );  
            System.out.printf("当前插入的employeer_id :%d    当前插入数据库中条数:%d " , employeer.getEmployeer_id() ,resultCount);  //获取插入对象的id  
            System.out.println("");
            session.commit() ;  		
		} finally {
			session.close();
		}
		
	}


改成以接口编程的方式:

[java] view plain copy print ?
  1. /** 
  2.  * 增加 
  3.  */  
  4. public static void addEmployeer(Employeer employeer){  
  5.     SqlSession session = null;  
  6.     try {  
  7.         session = sqlSessionFactory.openSession();  
  8.         EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);  
  9.         employeerMapper.addEmployeer(employeer);  
  10.            session.commit() ;         
  11.     } finally {  
  12.         session.close();  
  13.     }  
  14.       
  15. }  
	/**
	 * 增加
	 */
	public static void addEmployeer(Employeer employeer){
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);
			employeerMapper.addEmployeer(employeer);
            session.commit() ;  		
		} finally {
			session.close();
		}
		
	}

(2)删除命令

上一讲中的方式:

[java] view plain copy print ?
  1. /** 
  2.  * 删除 
  3.  *  
  4.  */  
  5. public static void deleteEmployeer(int id){  
  6.     SqlSession session = null;  
  7.     try {  
  8.         session = sqlSessionFactory.openSession();  
  9.          //返回值是记录条数    
  10.          int resultCount=session.delete("com.mucfc.model.EmployeerMapper.deleteEmployeer",id);   
  11.           System.out.println("当前删除数据库中条数: "+resultCount);  //获取插入对象的id    
  12.            session.commit() ;         
  13.     } finally {  
  14.         session.close();  
  15.     }  
  16. }  
	/**
	 * 删除
	 * 
	 */
	public static void deleteEmployeer(int id){
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			 //返回值是记录条数  
			 int resultCount=session.delete("com.mucfc.model.EmployeerMapper.deleteEmployeer",id); 
			  System.out.println("当前删除数据库中条数: "+resultCount);  //获取插入对象的id  
            session.commit() ;  		
		} finally {
			session.close();
		}
	}


改成以接口编程的方式:

[java] view plain copy print ?
  1. /** 
  2.  * 删除 
  3.  *  
  4.  */  
  5. public static void deleteEmployeer(int id){  
  6.     SqlSession session = null;  
  7.     try {  
  8.         session = sqlSessionFactory.openSession();  
  9.         EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);  
  10.         employeerMapper.deleteEmployeer(id);  
  11.            session.commit() ;         
  12.     } finally {  
  13.         session.close();  
  14.     }  
  15. }  
	/**
	 * 删除
	 * 
	 */
	public static void deleteEmployeer(int id){
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);
			employeerMapper.deleteEmployeer(id);
            session.commit() ;  		
		} finally {
			session.close();
		}
	}


(3)查找命令

上一讲中的方式:

[java] view plain copy print ?
  1. /** 
  2.  * 查找 
  3.  */  
  4. public static void findEmployeerById(int id) {  
  5.     SqlSession session = null;  
  6.     try {  
  7.         session = sqlSessionFactory.openSession();  
  8.         Employeer employeer = (Employeer) session.selectOne(  
  9.                 "com.mucfc.model.EmployeerMapper.findEmployeerByID"1);  
  10.         if (employeer == null)  
  11.             System.out.println("null");  
  12.         else  
  13.             System.out.println(employeer);  
  14.     } finally {  
  15.         session.close();  
  16.     }  
  17. }  
	/**
	 * 查找
	 */
	public static void findEmployeerById(int id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			Employeer employeer = (Employeer) session.selectOne(
					"com.mucfc.model.EmployeerMapper.findEmployeerByID", 1);
			if (employeer == null)
				System.out.println("null");
			else
				System.out.println(employeer);
		} finally {
			session.close();
		}
	}


改成以接口编程的方式:

[java] view plain copy print ?
  1. /** 
  2.      * 查找 
  3.      */  
  4.     public static void findEmployeerById(int id) {  
  5.         SqlSession session = null;  
  6.         try {  
  7.             session = sqlSessionFactory.openSession();  
  8.             EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);  
  9.             Employeer employeer=employeerMapper.findEmployeerByID(id);  
  10.             if (employeer == null)  
  11.                 System.out.println("null");  
  12.             else  
  13.                 System.out.println(employeer);  
  14.         } finally {  
  15.             session.close();  
  16.         }  
  17.     }  
/**
	 * 查找
	 */
	public static void findEmployeerById(int id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);
			Employeer employeer=employeerMapper.findEmployeerByID(id);
			if (employeer == null)
				System.out.println("null");
			else
				System.out.println(employeer);
		} finally {
			session.close();
		}
	}

(4)修改命令

上一讲中的方式:

[java] view plain copy print ?
  1. /** 
  2.  * 更改 
  3.  */  
  4. public static void updateEmployeer(Employeer employeer){  
  5.     SqlSession session = null;  
  6.     try {  
  7.         session = sqlSessionFactory.openSession();   
  8.            session.update("com.mucfc.model.EmployeerMapper.updateEmployeer",employeer);      
  9.            session.commit() ;         
  10.     } finally {  
  11.         session.close();  
  12.     }  
  13.       
  14. }  
	/**
	 * 更改
	 */
	public static void updateEmployeer(Employeer employeer){
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession(); 
            session.update("com.mucfc.model.EmployeerMapper.updateEmployeer",employeer);    
            session.commit() ;  		
		} finally {
			session.close();
		}
		
	}


改成以接口编程的方式:

[java] view plain copy print ?
  1. /** 
  2.  * 更改 
  3.  */  
  4. public static void updateEmployeer(Employeer employeer){  
  5.     SqlSession session = null;  
  6.     try {  
  7.         session = sqlSessionFactory.openSession();   
  8.         EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);  
  9.         employeerMapper.updateEmployeer(employeer);  
  10.            session.commit() ;         
  11.     } finally {  
  12.         session.close();  
  13.     }  
  14.       
  15. }  
	/**
	 * 更改
	 */
	public static void updateEmployeer(Employeer employeer){
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession(); 
			EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);
			employeerMapper.updateEmployeer(employeer);
            session.commit() ;  		
		} finally {
			session.close();
		}
		
	}

这是一个完整的测试:

[java] view plain copy print ?
  1. package com.mucfc.test;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.Reader;  
  5.   
  6. import org.apache.ibatis.io.Resources;  
  7. import org.apache.ibatis.session.SqlSession;  
  8. import org.apache.ibatis.session.SqlSessionFactory;  
  9. import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  10.   
  11. import com.mucfc.mapper.EmployeerMapper;  
  12. import com.mucfc.model.Employeer;  
  13.   
  14. public class MybatisTest {  
  15.     private static SqlSessionFactory sqlSessionFactory;  
  16.     private static Reader reader;  
  17.     static {  
  18.         try {  
  19.             reader = Resources.getResourceAsReader("mybatis-config.xml");  
  20.             sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);  
  21.         } catch (Exception e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.     }  
  25.   
  26.     /** 
  27.      * 查找 
  28.      */  
  29.     public static void findEmployeerById(int id) {  
  30.         SqlSession session = null;  
  31.         try {  
  32.             session = sqlSessionFactory.openSession();  
  33.             EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);  
  34.             Employeer employeer=employeerMapper.findEmployeerByID(id);  
  35.             if (employeer == null)  
  36.                 System.out.println("null");  
  37.             else  
  38.                 System.out.println(employeer);  
  39.         } finally {  
  40.             session.close();  
  41.         }  
  42.     }  
  43.     /** 
  44.      * 增加 
  45.      */  
  46.     public static void addEmployeer(Employeer employeer){  
  47.         SqlSession session = null;  
  48.         try {  
  49.             session = sqlSessionFactory.openSession();  
  50.             EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);  
  51.             employeerMapper.addEmployeer(employeer);  
  52.             session.commit() ;            
  53.         } finally {  
  54.             session.close();  
  55.         }  
  56.           
  57.     }  
  58.     /** 
  59.      * 删除 
  60.      *  
  61.      */  
  62.     public static void deleteEmployeer(int id){  
  63.         SqlSession session = null;  
  64.         try {  
  65.             session = sqlSessionFactory.openSession();  
  66.             EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);  
  67.             employeerMapper.deleteEmployeer(id);  
  68.             session.commit() ;            
  69.         } finally {  
  70.             session.close();  
  71.         }  
  72.     }  
  73.     /** 
  74.      * 更改 
  75.      */  
  76.     public static void updateEmployeer(Employeer employeer){  
  77.         SqlSession session = null;  
  78.         try {  
  79.             session = sqlSessionFactory.openSession();   
  80.             EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);  
  81.             employeerMapper.updateEmployeer(employeer);  
  82.             session.commit() ;            
  83.         } finally {  
  84.             session.close();  
  85.         }  
  86.           
  87.     }  
  88.   
  89.     public static void main(String[] args) {                              
  90.         Employeer employeer1=new Employeer();  
  91.         employeer1.setEmployeer_name("李四");  
  92.         employeer1.setEmployeer_age(23);  
  93.         employeer1.setEmployeer_department("产品一部");  
  94.         employeer1.setEmployeer_worktype("开发工程师");  
  95.           
  96.         Employeer employeer2=new Employeer();  
  97.         employeer2.setEmployeer_name("张三");  
  98.         employeer2.setEmployeer_age(30);  
  99.         employeer2.setEmployeer_department("产品二部");  
  100.         employeer2.setEmployeer_worktype("测试工程师");  
  101.           
  102.         Employeer employeer3=new Employeer();  
  103.         employeer3.setEmployeer_name("小王");  
  104.         employeer3.setEmployeer_age(22);  
  105.         employeer3.setEmployeer_department("产品三部");  
  106.         employeer3.setEmployeer_worktype("数据分析师");  
  107.           
  108.           
  109.         Employeer employeer4=new Employeer();  
  110.         employeer4.setEmployeer_name("明明");  
  111.         employeer4.setEmployeer_age(22);  
  112.         employeer4.setEmployeer_department("财会部");  
  113.         employeer4.setEmployeer_worktype("财务人员");  
  114.       
  115.         //插入  
  116.         addEmployeer(employeer1);  
  117.         addEmployeer(employeer2);  
  118.         addEmployeer(employeer3);  
  119.         addEmployeer(employeer4);  
  120.         findEmployeerById(26);  
  121.         deleteEmployeer(27);  
  122.           
  123.         //更改  
  124.         employeer2.setEmployeer_id(28);  
  125.         employeer2.setEmployeer_age(21);  
  126.         employeer2.setEmployeer_department("产品四部");  
  127.         updateEmployeer(employeer2);  
  128.   
  129.     }  
  130.   
  131. }  
package com.mucfc.test;

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;

import com.mucfc.mapper.EmployeerMapper;
import com.mucfc.model.Employeer;

public class MybatisTest {
	private static SqlSessionFactory sqlSessionFactory;
	private static Reader reader;
	static {
		try {
			reader = Resources.getResourceAsReader("mybatis-config.xml");
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 查找
	 */
	public static void findEmployeerById(int id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);
			Employeer employeer=employeerMapper.findEmployeerByID(id);
			if (employeer == null)
				System.out.println("null");
			else
				System.out.println(employeer);
		} finally {
			session.close();
		}
	}
	/**
	 * 增加
	 */
	public static void addEmployeer(Employeer employeer){
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);
			employeerMapper.addEmployeer(employeer);
            session.commit() ;  		
		} finally {
			session.close();
		}
		
	}
	/**
	 * 删除
	 * 
	 */
	public static void deleteEmployeer(int id){
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);
			employeerMapper.deleteEmployeer(id);
            session.commit() ;  		
		} finally {
			session.close();
		}
	}
	/**
	 * 更改
	 */
	public static void updateEmployeer(Employeer employeer){
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession(); 
			EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class);
			employeerMapper.updateEmployeer(employeer);
            session.commit() ;  		
		} finally {
			session.close();
		}
		
	}

	public static void main(String[] args) {							
		Employeer employeer1=new Employeer();
		employeer1.setEmployeer_name("李四");
		employeer1.setEmployeer_age(23);
		employeer1.setEmployeer_department("产品一部");
		employeer1.setEmployeer_worktype("开发工程师");
		
		Employeer employeer2=new Employeer();
		employeer2.setEmployeer_name("张三");
		employeer2.setEmployeer_age(30);
		employeer2.setEmployeer_department("产品二部");
		employeer2.setEmployeer_worktype("测试工程师");
		
		Employeer employeer3=new Employeer();
		employeer3.setEmployeer_name("小王");
		employeer3.setEmployeer_age(22);
		employeer3.setEmployeer_department("产品三部");
		employeer3.setEmployeer_worktype("数据分析师");
		
		
		Employeer employeer4=new Employeer();
		employeer4.setEmployeer_name("明明");
		employeer4.setEmployeer_age(22);
		employeer4.setEmployeer_department("财会部");
		employeer4.setEmployeer_worktype("财务人员");
	
		//插入
     	addEmployeer(employeer1);
     	addEmployeer(employeer2);
     	addEmployeer(employeer3);
     	addEmployeer(employeer4);
     	findEmployeerById(26);
     	deleteEmployeer(27);
		
		//更改
		employeer2.setEmployeer_id(28);
		employeer2.setEmployeer_age(21);
		employeer2.setEmployeer_department("产品四部");
		updateEmployeer(employeer2);

	}

}

测试结果:

插入了4条数据,ID为25~28

然后27号被删除

29号被修改

20、SSM框架-Mybatis接口编程方式实现增删改查 (2)_第2张图片

本文示例工程免费下载

你可能感兴趣的:(SSM框架)