Mybatis学习与使用

MyBatis
一、前言    

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


二、工程目录




三、代码


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:

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" 
	"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="mapper.DeptMapper">
	<insert id="addDept" parameterType="bean.Dept">
		insert into T_dept values(#{deptno},#{dname},#{loc})
	</insert>
	<update id="updateDept" parameterType="bean.Dept">
		update T_dept set  deptno=#{deptno},dname=#{dname},loc=#{loc} where deptno=#{deptno}
	</update>
	<delete id="deleteDept" parameterType="int">
	    delete from T_dept where deptno=#{deptno}
	</delete>
	<select id="findDept" parameterType="int" resultType="bean.Dept">
	    select * from T_dept where deptno=#{deptno}
	</select>
	<select id="findAllDept" resultType="bean.Dept">
		select * from T_dept
	</select>

</mapper>


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<Dept> 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<Dept> 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<Dept> 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<Dept> 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:

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" 
	"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
	<environments default="environment">
		<environment id="environment">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
				<property name="url"
					value="jdbc:oracle:thin:@localhost:1521:orcl" />
				<property name="username" value="scott" />
				<property name="password" value="xhs12302" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="bean/Dept.xml"/>
	</mappers>
</configuration> 



四、截图

        运行Test.java,如下:   

    


       运行TestMapper.java,如下:



 小细节:1、自定义映射

       

                2、注解配置





    

联系邮箱:xhsgg12302@outlook.com

                                                                                                                                                                                                                                                                                                                                                                2017_09_20

转载于:https://www.cnblogs.com/Obelia/p/7581710.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值