导包,

配置mybatis的总配置文件: mybatis-config.xml,

 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE configuration
   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-config.dtd">
 <configuration>
     <!-- 引入数据库的信息的属性文件 -->
     <properties resource="db.properties"></properties>

     <typeAliases>
         <package name="model"/>
     </typeAliases>

     <environments default="hanqi">
         <environment id="hanqi">
             <!--
                 JDBC:
                 MANAGED:托管
              -->
             <transactionManager type="JDBC" />
             <!--
                 配置数据库源
                 POOLED: 连接池
                 UNPOOLED: 非连接池
                 JNDI: 使用应用服务器上的数据库连接
              -->
             <dataSource type="POOLED">
                 <property name="username" value="${jdbc.username}"/>
                 <property name="password" value="${jdbc.password}"/>
                 <property name="url" value="${jdbc.url}"/>
                 <property name="driver" value="${jdbc.driver}"/>
             </dataSource>
         </environment>

     </environments>

     <mappers>
         <!--
         <mapper resource="mapper/StudentMapper.xml" />
         <mapper class=""></mapper>
         <mapper url="f:/test/StudentMapper.xml"></mapper>
          -->
         <!--
         如果有多个,用包名,自动在包下面搜索
          -->
         <package name="com.hanqi.maya.mapper" />
     </mappers>

 </configuration>

新建每个实体类的接口和映射文件,并在xml映射文件中引入接口

 package mapper;

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

 import model.Student;

 public interface StudentMapper {
     public List<Student> selectAllStudent();

     public int insertStudent(Student s);

     public int updateStudent(Map m);

     public int deleteStudent(Map m);

 }
 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE mapper
   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="mapper.StudentMapper">

     <select id="selectAllStudent" resultType="Student">
         select * from student
     </select>

     <insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="sno" keyColumn="SNO">
         insert into student values(test1.nextval, #{name}, #{sex}, #{sbirthday}, #{sclass})
     </insert>

     <update id="updateStudent" parameterType="Map">
         update student s set s.sname=#{stuname},s.sbirthday=#{newDate}
         where s.sno=#{stusno}
     </update>

     <delete id="deleteStudent" parameterType="Map">
         delete student s where s.sno=#{stusno}
     </delete>

 </mapper>
namespace中如果是ID,可以随便写,需要唯一,如果是引入接口,需要接口和本文件名一致。

测试:

 package test;

 import static org.junit.Assert.*;

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

 import org.apache.ibatis.session.SqlSession;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import mapper.StudentMapper;
 import model.Student;
 import util.MyBatisUtil;

 public class JUTest {

     private SqlSession ss;
     //定义接口类,拿到接口
     private StudentMapper sm;

     @Before
     public void setUp() throws Exception {
         ss=MyBatisUtil.getSession();
         sm=ss.getMapper(StudentMapper.class);
     } 

     @After
     public void tearDown() throws Exception {
         MyBatisUtil.destory(ss);
     }

     @Test
     public void test() {
         List<Student> slist=sm.selectAllStudent();

         /*Student s=new Student(null,"name","sex",new Date(),95053);
         int a=sm.insertStudent(s);
         System.out.println(a);*/

         /*Map m=new HashMap();
         m.put("stusno", "101");
         m.put("stuname", "修改1");
         m.put("newDate",new Date());
         int a=sm.updateStudent(m);
         System.out.println(a);*/

         Map m1=new HashMap();
         m1.put("stusno", "s");
         int b=sm.deleteStudent(m1);

         for(Student s:slist){
             System.out.println(s);
         }

     }

 }

关联查询:

一对一查询(三种方式),

一对多查询

部门表P_Dept和员工表P_Emp

员工表中有部门编号

员工类中有部门属性

部门类中有员工集合属性

通过联合查询,查询员工时得到员工属性和部门,查询部门时通过联合查询得出员工列表

结构:

model包:

 packa1ge model;

 import java.util.Date;

 public class Emp {
     private int empno;
     private String ename;
     private String job;
     private int mgr;
     private Date hiredate;
     private int sal;
     private int comm;
     private int deptno;
     private int sex;
     private Dept dept;
     public Emp() {
         super();
         // TODO Auto-generated constructor stub
     }
     public Emp(int empno, String ename, String job, int mgr, Date hiredate, int sal, int comm, int deptno, int sex,
             Dept dept) {
         super();
         this.empno = empno;
         this.ename = ename;
         this.job = job;
         this.mgr = mgr;
         this.hiredate = hiredate;
         this.sal = sal;
         this.comm = comm;
         this.deptno = deptno;
         this.sex = sex;
         this.dept = dept;
     }
     public int getEmpno() {
         return empno;
     }
     public void setEmpno(int empno) {
         this.empno = empno;
     }
     public String getEname() {
         return ename;
     }
     public void setEname(String ename) {
         this.ename = ename;
     }
     public String getJob() {
         return job;
     }
     public void setJob(String job) {
         this.job = job;
     }
     public int getMgr() {
         return mgr;
     }
     public void setMgr(int mgr) {
         this.mgr = mgr;
     }
     public Date getHiredate() {
         return hiredate;
     }
     public void setHiredate(Date hiredate) {
         this.hiredate = hiredate;
     }
     public int getSal() {
         return sal;
     }
     public void setSal(int sal) {
         this.sal = sal;
     }
     public int getComm() {
         return comm;
     }
     public void setComm(int comm) {
         this.comm = comm;
     }
     public int getDeptno() {
         return deptno;
     }
     public void setDeptno(int deptno) {
         this.deptno = deptno;
     }
     public int getSex() {
         return sex;
     }
     public void setSex(int sex) {
         this.sex = sex;
     }
     public Dept getDept() {
         return dept;
     }
     public void setDept(Dept dept) {
         this.dept = dept;
     }
     @Override
     public String toString() {
         return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate
                 + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + ", sex=" + sex + ", dept=" + dept + "]";
     }

 }
 package model;

 import java.util.List;

 public class Dept {
     private int deptno;
     private String dname;
     private String loc;
     private List<Emp> elist;
     public Dept() {
         super();
         // TODO Auto-generated constructor stub
     }
     public Dept(int deptno, String dname, String loc) {
         super();
         this.deptno = deptno;
         this.dname = dname;
         this.loc = loc;
     }

     public Dept(int deptno, String dname, String loc, List<Emp> elist) {
         super();
         this.deptno = deptno;
         this.dname = dname;
         this.loc = loc;
         this.elist = elist;
     }

     public List<Emp> getElist() {
         return elist;
     }
     public void setElist(List<Emp> elist) {
         this.elist = elist;
     }
     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 + ", elist=" + elist + "]";
     }

 }

mapper包:

 package mapper;

 import java.util.List;

 import model.Dept;
 import model.Emp;

 public interface DeptMapper {
     Dept selectDeptByDeptno(Integer id);
 }
 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE mapper
   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="mapper.DeptMapper">

     <resultMap type="Dept" id="abc">
         <collection property="elist" column="deptno"
             select="mapper.EmpMapper.selectEmpByDeptno" />
     </resultMap>

     <select id="selectDeptByDeptno" parameterType="Integer" resultMap="abc">
         select * from p_dept d where d.deptno=#{deptno}
     </select>

 </mapper>
 package mapper;

 import java.util.List;

 import model.Emp;

 public interface EmpMapper {
     //查询所有信息
     List<Emp> selectAllEmp();
     // 根据部门id查询员工
     List<Emp> selectEmpByDeptno(Integer deptno);
 }
 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE mapper
   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="mapper.EmpMapper">

     <!-- 对象及联 -->
     <resultMap type="Emp" id="empList">
         <id property="empno" column="empno" />
         <result property="ename" column="ename" />
         <result property="job" column="job" />
         <result property="mgr" column="mgr" />
         <result property="hiredate" column="hiredate" />
         <result property="sal" column="sal" />
         <result property="comm" column="comm" />
         <result property="deptno" column="deptno" />
         <result property="sex" column="sex"/>
         <result property="dept.deptno" column="deptno"/>
         <result property="dept.dname" column="dname"/>
         <result property="dept.loc" column="loc"/>
     </resultMap>

     <select id="selectAllEmp" resultMap="empList">
         select * from p_Emp e left join p_Dept d  on d.deptno=e.deptno

     </select>

     <!-- 关联 -->
     <!-- <resultMap type="Emp" id="empList1">
         <id property="empno" column="empno" />
         <result property="ename" column="ename" />
         <result property="job" column="job" />
         <result property="mgr" column="mgr" />
         <result property="hiredate" column="hiredate" />
         <result property="sal" column="sal" />
         <result property="comm" column="comm" />
         <result property="deptno" column="deptno" />
         <result property="sex" column="sex"/>
         <association property="dept" resultMap="deptlist"></association>
     </resultMap>
     <resultMap type="Dept" id="deptlist">
         <result property="deptno" column="deptno"/>
         <result property="dname" column="dname"/>
         <result property="loc" column="loc"/>
     </resultMap>

     <select id="selectAllEmp" resultMap="empList1">
         select * from p_Emp e left join p_Dept d  on d.deptno=e.deptno
     </select> -->
     <!-- 关联查询 -->
 <!--     <resultMap type="Emp" id="empList3">
         <association property="dept" column="deptno"
             select="mapper.DeptMapper.selectDeptById" />
     </resultMap>

     <select id="selectAllEmp" resultMap="empList3">
         select * from p_EMP t
     </select> -->
     <!-- 一对多 -->
     <select id="selectEmpByDeptno" parameterType="Integer" resultType="Emp">
         SELECT * FROM p_emp e WHERE e.deptno=#{deptno}
     </select>

 </mapper>

测试:

 package test;

 import static org.junit.Assert.*;

 import java.util.List;

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

 import mapper.DeptMapper;
 import mapper.EmpMapper;
 import model.Dept;
 import model.Emp;
 import util.MyBatisUtil;

 public class JUTest {
     private SqlSession ss;
     private EmpMapper em;
     private DeptMapper dm;

     @Before
     public void setUp() throws Exception {
         ss = MyBatisUtil.getSession();
         em = ss.getMapper(EmpMapper.class);
         dm = ss.getMapper(DeptMapper.class);
     }

     @After
     public void tearDown() throws Exception {
         MyBatisUtil.destory(ss);
     }

     @Test
     public void test() {
         /*List<Emp> slist=em.selectAllEmp();
         for(Emp e:slist){
             System.out.println(e);
         }*/

         /*List<Emp> elist = em.selectEmpByDeptno(10);
         System.out.println(elist);*/

         Dept d = dm.selectDeptByDeptno(10);
         System.out.println(d);
     }

 }

MyBatis框架(二)的更多相关文章

  1. Mybatis框架二:增删改查

    这里是搭建框架和准备数据: http://www.cnblogs.com/xuyiqing/p/8600888.html 实现增删改查功能: 测试类: package junit; import ja ...

  2. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(2 配置spring-dao和测试)

    用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 搭建目录环境和依赖) 四:在\resources\spring 下面 ...

  3. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 构建目录环境和依赖)

    引言:在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一   的基础上 继续进行项目搭建 该部分的主要目的是测通MyBatis 及Spring-dao ...

  4. OSGI企业应用开发(九)整合Spring和Mybatis框架(二)

    上篇文章中,我们完成了在OSGI应用中整合Spring和Mybatis框架的准备工作,本节我们继续Spring和Mybatis框架的整合. 一.解决OSGI整合Spring中的Placeholder问 ...

  5. (转)MyBatis框架的学习(二)——MyBatis架构与入门

    http://blog.csdn.net/yerenyuan_pku/article/details/71699515 MyBatis框架的架构 MyBatis框架的架构如下图: 下面作简要概述: S ...

  6. java实现微信支付宝等多个支付平台合一的二维码支付(maven+spring springmvc mybatis框架)

    首先申明,本人实现微信支付宝等支付平台合多为一的二维码支付,并且实现有效时间内支付有效,本人采用的框架是spring springmvc mybatis 框架,maven管理.其实如果支付,不需要my ...

  7. Java框架之MyBatis框架(二)

    Mybatis框架是相对于优化dao层的框架,其有效的减少了频繁的连接数据库(在配置文件xml中进行配置),将sql语句与java代码进行分离(写在XXXXmapper.xml文件中,一个表对应一个x ...

  8. JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

  9. 【mybatis annotation】数据层框架应用--Mybatis(二) 基于注解实现数据的CRUD

    使用MyBatis框架进行持久层开发 MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架. MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索. MyBa ...

  10. SpringMVC框架二:SpringMVC与MyBatis整合

    下面整合SpringMVC和MyBatis框架,并做一个小案例 创建数据库springmvc,并创建两张表,加入一些数据: 两张表:商品表,用户表 CREATE DATABASE springmvc; ...

随机推荐

  1. JS中的函数、BOM和DOM操作

     一.JS中的函数 [关于注释] /** [文档注释]:开头两个*.写在函数上方,在调用函数时可以看到文档上方的描述信息. */   // 单行注释 /* 多行注释 */ 1.函数的声明及调用 (1) ...

  2. Dynamics 365中部分账号使用系统明显缓慢怎么办?先这么干!

    摘要: 本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复263或者20170828可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyon ...

  3. Markdown 简单指北

    Markdown is intended to be as easy-to-read and easy-to-write as is feasible. Markdown 简介 Markdown是一种 ...

  4. python绘图:matplotlib和pandas的应用

    在进行数据分析时,绘图是必不可少的模式探索方式.用Python进行数据分析时,matplotlib和pandas是最常用到的两个库.1.matplotlib库的应用准备工作如下:打开ipython,输 ...

  5. Linux上Oracle自动启停方案

    环境 CentOS 6 x86_64, Oracle 11g R2   方案 Oracle在$ORACLE_HOME/bin目录下提供了dbstart和dbshut两个脚本来启动和停止Oracle.d ...

  6. 有哪些关于 Python 的技术博客?

    Python是一种动态解释型的编程语言,它可以在Windows.UNIX.MAC等多种操作系统以及Java..NET开发平台上使用.不过包含的内容很多,加上各种标准库.拓展库,乱花渐欲迷人眼.因此如何 ...

  7. java常见面试题(二)

    1.java集合类 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).JavaSDK不提供直接继承自Collect ...

  8. 寻找Harris、Shi-Tomasi和亚像素角点

    Harris.Shi-Tomasi和亚像素角点都是角点,隶属于特征点这个大类(特征点可以分为边缘.角点.斑点). 一.Harris角点检测是一种直接基于灰度图像的角点提取算法,稳定性较高,但是也可能出 ...

  9. error: The requested URL returned error: 401 Unauthorized while accessing

    我遇到的其中一个问题. 问题描述: 在git push -u origin master是,提示“error: The requested URL returned error: 401 Unauth ...

  10. Mac Intellij Debug 模式

    前言:本文的核心是记录debug的使用,与具体的操作系统无关. 1.什么是Debug Debug是程序的一种运行模式.用来发掘程序的走向,以及程序在运行过程中参数的变化. 2.Debug的作用 deb ...