Mybatis学习与使用

MyBatis
一、前言    

最近学习了经典框架Mybatis,为了能后更好的,有效的学习与使用,现做笔记如下:MyBatis 是一个可以自定义SQL、存储过程和高级映射的持久层框架。MyBatis 摒除了大部分的JDBC代码、手工设置参数和结果集重获。MyBatis 只使用简单的XML 和注解来配置和映射基本数据类型、Map 接口和POJO 到数据库记录。相对Hibernate和Apache OJB等“一站式”ORM解决方案而言,Mybatis 是一种“半自动化”的ORM实现。


二、工程目录


Mybatis学习与使用_第1张图片


三、代码


Dept.java:

package bean;

public class Dept {
	private int deptno;
	private String dname;
	private String loc;
	public Dept(){
		
	}
	public Dept(int deptno, String dname, String loc) {
		super();
		this.deptno = deptno;
		this.dname = dname;
		this.loc = loc;
	}
	public int  getDeptno() {
		return deptno;
	}
	public void setDeptno(int deptno) {
		this.deptno = deptno;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public String getLoc() {
		return loc;
	}
	public void setLoc(String loc) {
		this.loc = loc;
	}
	@Override
	public String toString() {
		return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc
				+ "]";
	}
	
}


Dept.xml:

  


	
		insert into T_dept values(#{deptno},#{dname},#{loc})
	
	
		update T_dept set  deptno=#{deptno},dname=#{dname},loc=#{loc} where deptno=#{deptno}
	
	
	    delete from T_dept where deptno=#{deptno}
	
	
	



DeptMapper.java:

package mapper;
import java.util.List;

import bean.Dept;

public interface DeptMapper {
	void addDept(Dept dept);
	void updateDept(Dept dept);
	void deleteDept(int detpno);
	Dept findDept(int deptno);
	List findAllDept();
}


Test.java:

package 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;

public class Test {
	public static void main(String[] args) {
		try {
			Reader reader=Resources.getResourceAsReader("SqlMapConfig.xml");
			SqlSessionFactoryBuilder sfb=new SqlSessionFactoryBuilder();
			SqlSessionFactory sf=sfb.build(reader);
			SqlSession session =sf.openSession();
			System.out.println(session);
			session.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


TestMapper.java:

package test;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import mapper.DeptMapper;

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

import bean.Dept;

public class TestMapper {
	
	public static void main(String[] args) {
		//findByPage(); // 分页查询
		try {
			Reader reader=Resources.getResourceAsReader("SqlMapConfig.xml");
			SqlSessionFactoryBuilder sfb=new SqlSessionFactoryBuilder();
			SqlSessionFactory sf=sfb.build(reader);
			SqlSession session =sf.openSession();
		    //通过接口创建实现类。进而通过接口引用   ,创建的时候应该是和Dept.xml的方法有关
			DeptMapper mapper=session.getMapper(mapper.DeptMapper.class);
			List lists=mapper.findAllDept();
			System.out.println(lists);
			session.commit();
			session.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void addDept(){
		try {
			Reader reader=Resources.getResourceAsReader("SqlMapConfig.xml");
			SqlSessionFactoryBuilder sfb=new SqlSessionFactoryBuilder();
			SqlSessionFactory sf=sfb.build(reader);
			SqlSession session =sf.openSession();
		
			Dept dept=new Dept(1002,"wtf","beijing");
			session.insert("addDept",dept);
			System.out.println("sucess");
			session.commit();
			session.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void updateDept(){
		try {
			Reader reader=Resources.getResourceAsReader("SqlMapConfig.xml");
			SqlSessionFactoryBuilder sfb=new SqlSessionFactoryBuilder();
			SqlSessionFactory sf=sfb.build(reader);
			SqlSession session =sf.openSession();
		
			Dept dept=new Dept(1002,"wtfxhs","beijing");
			session.update("updateDept",dept);
			System.out.println("sucess");
			session.commit();
			session.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void deleteDept(){
		try {
			Reader reader=Resources.getResourceAsReader("SqlMapConfig.xml");
			SqlSessionFactoryBuilder sfb=new SqlSessionFactoryBuilder();
			SqlSessionFactory sf=sfb.build(reader);
			SqlSession session =sf.openSession();
		
			int dept=1002;
			session.delete("deleteDept",dept);
			System.out.println("sucess");
			session.commit();
			session.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void findDept(){
		try {
			Reader reader=Resources.getResourceAsReader("SqlMapConfig.xml");
			SqlSessionFactoryBuilder sfb=new SqlSessionFactoryBuilder();
			SqlSessionFactory sf=sfb.build(reader);
			SqlSession session =sf.openSession();
		
			int dept=1003;
			Dept depts=session.selectOne("findDept",dept);
			System.out.println(depts);
			session.commit();
			session.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void findAllDept(){
		try {
			Reader reader=Resources.getResourceAsReader("SqlMapConfig.xml");
			SqlSessionFactoryBuilder sfb=new SqlSessionFactoryBuilder();
			SqlSessionFactory sf=sfb.build(reader);
			SqlSession session =sf.openSession();
			List lists=session.selectList("findAllDept");
			for(Dept dept :lists){
				
				System.out.println(dept);
			}
			session.commit();
			session.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	//分页查询
	public static void findByPage(){
		try {
			Reader reader=Resources.getResourceAsReader("SqlMapConfig.xml");
			SqlSessionFactoryBuilder sfb=new SqlSessionFactoryBuilder();
			SqlSessionFactory sf=sfb.build(reader);
			SqlSession session =sf.openSession();
			RowBounds r=new RowBounds(2, 3);
			
			List lists=session.selectList("findAllDept",null,r);
			for(Dept dept :lists){
				
				System.out.println(dept);
			}
			session.commit();
			session.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}


mabatis.sql:

select * from T_dept;



create table T_dept(
   deptno number(4) primary key,
   dname varchar2(20),
   loc varchar2(40)
);

insert into T_dept values(1001,'Obelia','haerbin');
insert into T_dept values(1002,'Obelia','haerbin');

insert into T_dept values(1004,'Obelia','haerbin');
insert into T_dept values(1005,'Obelia','haerbin');
insert into T_dept values(1006,'Obelia','haerbin');
insert into T_dept values(1007,'Obelia','haerbin');
insert into T_dept values(1008,'Obelia','haerbin');

update  T_dept set  deptno=1003,dname='helloworld',loc='nanjing' where deptno=1001;

delete  from T_dept where deptno=1003;


SqlMapConfig.xml:

  


	
		
			
			
				
				
				
				
			
		
	
	
		
	
 



四、截图

        运行Test.java,如下:   

    Mybatis学习与使用_第2张图片


       运行TestMapper.java,如下:

Mybatis学习与使用_第3张图片


 小细节:1、自定义映射

       Mybatis学习与使用_第4张图片

                2、注解配置

Mybatis学习与使用_第5张图片

Mybatis学习与使用_第6张图片

Mybatis学习与使用_第7张图片


    

联系邮箱:[email protected]

                                                                                                                                                                                                                                                                                                                                                                2017_09_20

你可能感兴趣的:(java)