MyBatis学习笔记-初识

1、MyBatis原理图

MyBatis学习笔记-初识_第1张图片

     其中,对象就是所谓的封装的实体类;映射就是要对数据库进行相关操作的代码对应到数据库中,避免了平时编写调用数据库的一些“又臭又长”的代码;数据库不言而喻。

2、创建Java工程,并且创建相应的包

     本次创建的是Maven项目。所有包如下:

MyBatis学习笔记-初识_第2张图片

3、导入MyBatis所需的jar包

MyBatis学习笔记-初识_第3张图片

   如果创建的是Maven项目的话,可以在pom.xml中配置如下:


  4.0.0

  com.MyBatisTest
  MyBatisTest
  1.0
  jar

  MyBatisTest
  http://maven.apache.org

  
    UTF-8
  

  
    
      junit
      junit
      3.8.1
      test
    
    
	
	    javax.servlet
	    javax.servlet-api
	    3.1.0
	    provided
	
  
  
	
	    mysql
	    mysql-connector-java
	    5.1.47
	
	
	
	    com.google.code.gson
	    gson
	    2.8.2
	
	
	
	    commons-fileupload
	    commons-fileupload
	    1.3.1
	
	
	    org.hamcrest
	    hamcrest-core
	    1.3
	    test
	
	
	
	    org.apache.logging.log4j
	    log4j-core
	    2.3
	
	
	
	    org.apache.logging.log4j
	    log4j-slf4j-impl
	    2.3
	
	
	
	
	    org.slf4j
	    slf4j-api
	    1.7.25
	
	
	
	
	    org.mybatis
	    mybatis
	    3.4.6
	
	
	
	
	    javax.servlet
	    jstl
	    1.2
	
   

    junit
    junit
    4.10

  

4、添加MyBatis的配置文件(mybatis.cfg.xml)





	
	
	
		
	
	
     
        
            
            
            
            
            
            
            
            
                
                
                
                
            
        
    
    
    
    
        
        
        
    

5、创建实体类(本文中为Student.java)

package com.MyBatisTest.Bean;

public class Student {
	private int ID;
	private String Name;
	private int Age;
	
	public Student() {}
	public Student(int ID,String Name,int Age) {
		this.ID=ID;
		this.Name=Name;
		this.Age=Age;
	}
	public int getID() {
		return ID;
	}
	public void setID(int iD) {
		ID = iD;
	}
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	public int getAge() {
		return Age;
	}
	public void setAge(int age) {
		Age = age;
	}
	
	
}	

6、创建数据库增删改查方法接口(StidentMapper.java)

package com.MyBatisTest.Mapper;

import java.util.List;
import java.util.Map;

import com.MyBatisTest.Bean.Student;


public interface StudentMapper {
	/**
     * 插入信息
           */
    public int insertStudent(Student Student) throws Exception;
    /**
     * 更新信息1
     */
    public int updateStudent (Student Student,int id) throws Exception;
    /**
     * 更新信息2
     */
    public int updateStudent2 (Student student) throws Exception;
    /**
     * 更新信息3
     */
    public int updateStudent3 (Map map) throws Exception;
     /**
      *删除信息
      */
    public int deleteStudent(int id) throws Exception;
    /**
     * 按条件选择信息
     */
    public Student selectStudentById(int id) throws Exception;
     /**
      * 显示所有信息
      */
    public List selectAllStudent() throws Exception;
 

}

7、编写MyBatis的映射文件(StudentMapper.xml)





	
	
	
		insert into student(Name,Age) values(#{Name},#{Age})
	
	
	
	
		  update student set
		    Name = #{Name},
		    Age = #{Age}
		  where ID = #{id}
		
	
	
		  update student set
		    Name = #{Name},
		    Age = #{Age}
		  where ID = #{id}
		
		
	
	  delete from student whereID = #{id}
	
	
	
 	 
	

8、当以上所有文件都创建完成时,可以用JUint来进行单元测试运行

package com.MyBatisTest.MyBatisTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import com.MyBatisTest.Bean.Student;
import com.MyBatisTest.Mapper.StudentMapper;
import com.MyBatisTest.utils.DBTools;

public class MyBatisTest {
	@Test
	public void test01() {
		SqlSession session=DBTools.getSession();
		StudentMapper mapper=session.getMapper(StudentMapper.class);
		Student stu=new Student();
		stu.setName("testman0409");
		stu.setAge(999);
		try {
			mapper.insertStudent(stu);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	public void test02() {
		SqlSession session=DBTools.getSession();
		StudentMapper mapper=session.getMapper(StudentMapper.class);
		try {
			List list=mapper.selectAllStudent();
			System.out.println(list.size());
			for (Student student : list) {
				System.out.println(student.getName());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	public void test03() {
		SqlSession session=DBTools.getSession();
		StudentMapper mapper=session.getMapper(StudentMapper.class);
		try {
			Student student=mapper.selectStudentById(1);
			System.out.println(student.getName());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	public void test04() {
		SqlSession session=DBTools.getSession();
		StudentMapper mapper=session.getMapper(StudentMapper.class);
		try {
			Student stu=new Student();
			stu.setID(1);
			stu.setName("testman0409");
			stu.setAge(999);
			int result=mapper.updateStudent2(stu);
			System.out.println(result);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	public void test05() {
		SqlSession session=DBTools.getSession();
		StudentMapper mapper=session.getMapper(StudentMapper.class);
		try {
			Map stu=new HashMap();
			stu.put("id", 9);
			stu.put("uname", "lisi_new2");
			int result=mapper.updateStudent3(stu);
			System.out.println(result);
			
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	public void test06() {
		SqlSession session=DBTools.getSession();
		StudentMapper mapper=session.getMapper(StudentMapper.class);
		try {
		
			int result=mapper.deleteStudent(29);
			System.out.println(result);
			
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

    当运行test03方法时,结果如下:

MyBatis学习笔记-初识_第4张图片

9、附:本次数据库的Student表设计

MyBatis学习笔记-初识_第5张图片

总结

     以上所述就是MyBatis框架的入门知识,通过MyBatis框架可以完全替换JDBC,使得编码变的更加的简洁。而且MyBatis还是“半自动”的,可以直接编写所需要的SQL语句,让使用更加的灵活。

 

你可能感兴趣的:(框架知识积累)