导包,

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

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!-- 引入数据库的信息的属性文件 -->
  7. <properties resource="db.properties"></properties>
  8.  
  9. <typeAliases>
  10. <package name="model"/>
  11. </typeAliases>
  12.  
  13. <environments default="hanqi">
  14. <environment id="hanqi">
  15. <!--
  16. JDBC:
  17. MANAGED:托管
  18. -->
  19. <transactionManager type="JDBC" />
  20. <!--
  21. 配置数据库源
  22. POOLED: 连接池
  23. UNPOOLED: 非连接池
  24. JNDI: 使用应用服务器上的数据库连接
  25. -->
  26. <dataSource type="POOLED">
  27. <property name="username" value="${jdbc.username}"/>
  28. <property name="password" value="${jdbc.password}"/>
  29. <property name="url" value="${jdbc.url}"/>
  30. <property name="driver" value="${jdbc.driver}"/>
  31. </dataSource>
  32. </environment>
  33.  
  34. </environments>
  35.  
  36. <mappers>
  37. <!--
  38. <mapper resource="mapper/StudentMapper.xml" />
  39. <mapper class=""></mapper>
  40. <mapper url="f:/test/StudentMapper.xml"></mapper>
  41. -->
  42. <!--
  43. 如果有多个,用包名,自动在包下面搜索
  44. -->
  45. <package name="com.hanqi.maya.mapper" />
  46. </mappers>
  47.  
  48. </configuration>

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

  1. package mapper;
  2.  
  3. import java.util.List;
  4. import java.util.Map;
  5.  
  6. import model.Student;
  7.  
  8. public interface StudentMapper {
  9. public List<Student> selectAllStudent();
  10.  
  11. public int insertStudent(Student s);
  12.  
  13. public int updateStudent(Map m);
  14.  
  15. public int deleteStudent(Map m);
  16.  
  17. }
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="mapper.StudentMapper">
  6.  
  7. <select id="selectAllStudent" resultType="Student">
  8. select * from student
  9. </select>
  10.  
  11. <insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="sno" keyColumn="SNO">
  12. insert into student values(test1.nextval, #{name}, #{sex}, #{sbirthday}, #{sclass})
  13. </insert>
  14.  
  15. <update id="updateStudent" parameterType="Map">
  16. update student s set s.sname=#{stuname},s.sbirthday=#{newDate}
  17. where s.sno=#{stusno}
  18. </update>
  19.  
  20. <delete id="deleteStudent" parameterType="Map">
  21. delete student s where s.sno=#{stusno}
  22. </delete>
  23.  
  24. </mapper>
  1. namespace中如果是ID,可以随便写,需要唯一,如果是引入接口,需要接口和本文件名一致。

测试:

  1. package test;
  2.  
  3. import static org.junit.Assert.*;
  4.  
  5. import java.util.Date;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9.  
  10. import org.apache.ibatis.session.SqlSession;
  11. import org.junit.After;
  12. import org.junit.Before;
  13. import org.junit.Test;
  14. import mapper.StudentMapper;
  15. import model.Student;
  16. import util.MyBatisUtil;
  17.  
  18. public class JUTest {
  19.  
  20. private SqlSession ss;
  21. //定义接口类,拿到接口
  22. private StudentMapper sm;
  23.  
  24. @Before
  25. public void setUp() throws Exception {
  26. ss=MyBatisUtil.getSession();
  27. sm=ss.getMapper(StudentMapper.class);
  28. }
  29.  
  30. @After
  31. public void tearDown() throws Exception {
  32. MyBatisUtil.destory(ss);
  33. }
  34.  
  35. @Test
  36. public void test() {
  37. List<Student> slist=sm.selectAllStudent();
  38.  
  39. /*Student s=new Student(null,"name","sex",new Date(),95053);
  40. int a=sm.insertStudent(s);
  41. System.out.println(a);*/
  42.  
  43. /*Map m=new HashMap();
  44. m.put("stusno", "101");
  45. m.put("stuname", "修改1");
  46. m.put("newDate",new Date());
  47. int a=sm.updateStudent(m);
  48. System.out.println(a);*/
  49.  
  50. Map m1=new HashMap();
  51. m1.put("stusno", "s");
  52. int b=sm.deleteStudent(m1);
  53.  
  54. for(Student s:slist){
  55. System.out.println(s);
  56. }
  57.  
  58. }
  59.  
  60. }

关联查询:

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

一对多查询

部门表P_Dept和员工表P_Emp

员工表中有部门编号

员工类中有部门属性

部门类中有员工集合属性

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

结构:

model包:

  1. packa1ge model;
  2.  
  3. import java.util.Date;
  4.  
  5. public class Emp {
  6. private int empno;
  7. private String ename;
  8. private String job;
  9. private int mgr;
  10. private Date hiredate;
  11. private int sal;
  12. private int comm;
  13. private int deptno;
  14. private int sex;
  15. private Dept dept;
  16. public Emp() {
  17. super();
  18. // TODO Auto-generated constructor stub
  19. }
  20. public Emp(int empno, String ename, String job, int mgr, Date hiredate, int sal, int comm, int deptno, int sex,
  21. Dept dept) {
  22. super();
  23. this.empno = empno;
  24. this.ename = ename;
  25. this.job = job;
  26. this.mgr = mgr;
  27. this.hiredate = hiredate;
  28. this.sal = sal;
  29. this.comm = comm;
  30. this.deptno = deptno;
  31. this.sex = sex;
  32. this.dept = dept;
  33. }
  34. public int getEmpno() {
  35. return empno;
  36. }
  37. public void setEmpno(int empno) {
  38. this.empno = empno;
  39. }
  40. public String getEname() {
  41. return ename;
  42. }
  43. public void setEname(String ename) {
  44. this.ename = ename;
  45. }
  46. public String getJob() {
  47. return job;
  48. }
  49. public void setJob(String job) {
  50. this.job = job;
  51. }
  52. public int getMgr() {
  53. return mgr;
  54. }
  55. public void setMgr(int mgr) {
  56. this.mgr = mgr;
  57. }
  58. public Date getHiredate() {
  59. return hiredate;
  60. }
  61. public void setHiredate(Date hiredate) {
  62. this.hiredate = hiredate;
  63. }
  64. public int getSal() {
  65. return sal;
  66. }
  67. public void setSal(int sal) {
  68. this.sal = sal;
  69. }
  70. public int getComm() {
  71. return comm;
  72. }
  73. public void setComm(int comm) {
  74. this.comm = comm;
  75. }
  76. public int getDeptno() {
  77. return deptno;
  78. }
  79. public void setDeptno(int deptno) {
  80. this.deptno = deptno;
  81. }
  82. public int getSex() {
  83. return sex;
  84. }
  85. public void setSex(int sex) {
  86. this.sex = sex;
  87. }
  88. public Dept getDept() {
  89. return dept;
  90. }
  91. public void setDept(Dept dept) {
  92. this.dept = dept;
  93. }
  94. @Override
  95. public String toString() {
  96. return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate
  97. + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + ", sex=" + sex + ", dept=" + dept + "]";
  98. }
  99.  
  100. }
  1. package model;
  2.  
  3. import java.util.List;
  4.  
  5. public class Dept {
  6. private int deptno;
  7. private String dname;
  8. private String loc;
  9. private List<Emp> elist;
  10. public Dept() {
  11. super();
  12. // TODO Auto-generated constructor stub
  13. }
  14. public Dept(int deptno, String dname, String loc) {
  15. super();
  16. this.deptno = deptno;
  17. this.dname = dname;
  18. this.loc = loc;
  19. }
  20.  
  21. public Dept(int deptno, String dname, String loc, List<Emp> elist) {
  22. super();
  23. this.deptno = deptno;
  24. this.dname = dname;
  25. this.loc = loc;
  26. this.elist = elist;
  27. }
  28.  
  29. public List<Emp> getElist() {
  30. return elist;
  31. }
  32. public void setElist(List<Emp> elist) {
  33. this.elist = elist;
  34. }
  35. public int getDeptno() {
  36. return deptno;
  37. }
  38. public void setDeptno(int deptno) {
  39. this.deptno = deptno;
  40. }
  41. public String getDname() {
  42. return dname;
  43. }
  44. public void setDname(String dname) {
  45. this.dname = dname;
  46. }
  47. public String getLoc() {
  48. return loc;
  49. }
  50. public void setLoc(String loc) {
  51. this.loc = loc;
  52. }
  53. @Override
  54. public String toString() {
  55. return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + ", elist=" + elist + "]";
  56. }
  57.  
  58. }

mapper包:

  1. package mapper;
  2.  
  3. import java.util.List;
  4.  
  5. import model.Dept;
  6. import model.Emp;
  7.  
  8. public interface DeptMapper {
  9. Dept selectDeptByDeptno(Integer id);
  10. }
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="mapper.DeptMapper">
  6.  
  7. <resultMap type="Dept" id="abc">
  8. <collection property="elist" column="deptno"
  9. select="mapper.EmpMapper.selectEmpByDeptno" />
  10. </resultMap>
  11.  
  12. <select id="selectDeptByDeptno" parameterType="Integer" resultMap="abc">
  13. select * from p_dept d where d.deptno=#{deptno}
  14. </select>
  15.  
  16. </mapper>
  1. package mapper;
  2.  
  3. import java.util.List;
  4.  
  5. import model.Emp;
  6.  
  7. public interface EmpMapper {
  8. //查询所有信息
  9. List<Emp> selectAllEmp();
  10. // 根据部门id查询员工
  11. List<Emp> selectEmpByDeptno(Integer deptno);
  12. }
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="mapper.EmpMapper">
  6.  
  7. <!-- 对象及联 -->
  8. <resultMap type="Emp" id="empList">
  9. <id property="empno" column="empno" />
  10. <result property="ename" column="ename" />
  11. <result property="job" column="job" />
  12. <result property="mgr" column="mgr" />
  13. <result property="hiredate" column="hiredate" />
  14. <result property="sal" column="sal" />
  15. <result property="comm" column="comm" />
  16. <result property="deptno" column="deptno" />
  17. <result property="sex" column="sex"/>
  18. <result property="dept.deptno" column="deptno"/>
  19. <result property="dept.dname" column="dname"/>
  20. <result property="dept.loc" column="loc"/>
  21. </resultMap>
  22.  
  23. <select id="selectAllEmp" resultMap="empList">
  24. select * from p_Emp e left join p_Dept d on d.deptno=e.deptno
  25.  
  26. </select>
  27.  
  28. <!-- 关联 -->
  29. <!-- <resultMap type="Emp" id="empList1">
  30. <id property="empno" column="empno" />
  31. <result property="ename" column="ename" />
  32. <result property="job" column="job" />
  33. <result property="mgr" column="mgr" />
  34. <result property="hiredate" column="hiredate" />
  35. <result property="sal" column="sal" />
  36. <result property="comm" column="comm" />
  37. <result property="deptno" column="deptno" />
  38. <result property="sex" column="sex"/>
  39. <association property="dept" resultMap="deptlist"></association>
  40. </resultMap>
  41. <resultMap type="Dept" id="deptlist">
  42. <result property="deptno" column="deptno"/>
  43. <result property="dname" column="dname"/>
  44. <result property="loc" column="loc"/>
  45. </resultMap>
  46.  
  47. <select id="selectAllEmp" resultMap="empList1">
  48. select * from p_Emp e left join p_Dept d on d.deptno=e.deptno
  49. </select> -->
  50. <!-- 关联查询 -->
  51. <!-- <resultMap type="Emp" id="empList3">
  52. <association property="dept" column="deptno"
  53. select="mapper.DeptMapper.selectDeptById" />
  54. </resultMap>
  55.  
  56. <select id="selectAllEmp" resultMap="empList3">
  57. select * from p_EMP t
  58. </select> -->
  59. <!-- 一对多 -->
  60. <select id="selectEmpByDeptno" parameterType="Integer" resultType="Emp">
  61. SELECT * FROM p_emp e WHERE e.deptno=#{deptno}
  62. </select>
  63.  
  64. </mapper>

测试:

  1. package test;
  2.  
  3. import static org.junit.Assert.*;
  4.  
  5. import java.util.List;
  6.  
  7. import org.apache.ibatis.session.SqlSession;
  8. import org.junit.After;
  9. import org.junit.Before;
  10. import org.junit.Test;
  11.  
  12. import mapper.DeptMapper;
  13. import mapper.EmpMapper;
  14. import model.Dept;
  15. import model.Emp;
  16. import util.MyBatisUtil;
  17.  
  18. public class JUTest {
  19. private SqlSession ss;
  20. private EmpMapper em;
  21. private DeptMapper dm;
  22.  
  23. @Before
  24. public void setUp() throws Exception {
  25. ss = MyBatisUtil.getSession();
  26. em = ss.getMapper(EmpMapper.class);
  27. dm = ss.getMapper(DeptMapper.class);
  28. }
  29.  
  30. @After
  31. public void tearDown() throws Exception {
  32. MyBatisUtil.destory(ss);
  33. }
  34.  
  35. @Test
  36. public void test() {
  37. /*List<Emp> slist=em.selectAllEmp();
  38. for(Emp e:slist){
  39. System.out.println(e);
  40. }*/
  41.  
  42. /*List<Emp> elist = em.selectEmpByDeptno(10);
  43. System.out.println(elist);*/
  44.  
  45. Dept d = dm.selectDeptByDeptno(10);
  46. System.out.println(d);
  47. }
  48.  
  49. }

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. Ta-lib函数功能列表

    import tkinter as tk from tkinter import ttk import matplotlib.pyplot as plt import numpy as np impo ...

  2. 浅酌iOS 11兼容性

    WeTest导读 苹果在WWDC2017大会,公布了最新的iOS 11,系统新特性肯定是让不少果粉充满期待.在网上已能看到不少关于iOS 11的体验文章,那么iOS 11的新特性会对APP产生什么兼容 ...

  3. 斜率DP hdu 3507

    Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique, he s ...

  4. python中的collections.namedtuple

    简介 collections.namedtuple是一个工厂方法,它可以动态的创建一个继承tuple的子类.跟tuple相比,返回的子类可以使用名称来访问元素. 使用方法 用一个例子来介绍: > ...

  5. vue.js之获取当前点击对象(其实是套着vue的原生javascript吧,笑😊)

    转载请注明出处:http://www.cnblogs.com/meng1314-shuai/p/7455575.html 熟悉jquery的小伙伴应该都知道jquery获取当前点击对象是有多么的粗暴, ...

  6. Django编写RESTful API(五):添加超链接提高模型间的关联性

    前言 在第四篇中,加入了用户模型,以及相关的认证和权限的功能.但是我们在使用的时候,会发现在访问http://127.0.0.1:8000/users/时看到的用户列表,不能够直接点击某个链接然后查看 ...

  7. Struts2使用自定义拦截器导致Action注入参数丢失、url参数

    写struts2项目时发现前台超链接中的参数无法传到action, 所有带有传递参数的均无法正常使用了,在Action中所有的参数无法被注入. 后来经过debug发现其中的页面都要先经过拦截器,而后再 ...

  8. python爬虫之一---------豆瓣妹子图

    #-*- coding:utf-8 -*- __author__ = "carry" import urllib import urllib2 from bs4 import Be ...

  9. Android studio 安装的安装一些问题

    在国内如何更新android sdk? 由于众所周知的某些原因,我们无法直接连接android sdk的更新服务更新sdk,所以可以通过国内的ftp站点把常用的sdk组件如android platfo ...

  10. 正则表达式 提取<A>标签

    功能用途 主要实现了提取html代码中的a标签和url地址. 示例代码 Regex regex = new Regex("href\\s*=\\s*(?:\"(?<1> ...