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