MyBatis本是apache的一个开源项目iBatis,2010年这个项目有Apache software foundation 迁移到了Google code,并改名MyBatis.2013年11月迁移到Github。iBatis是半ORM映射框架,它需要在数据库里手动建表,CURD操作时要自己写SQL语句,而Hibernate是全ORM映射框架,它只需要配置好文件,表会自动生成,CURD的SQL语句也是自动生成的,这是他们的主要区别。

MyBatis小巧,简单易学

MyBatis入门案例(综合)

1.1附加架包

1.2编写MyBatis配置文件 mybatis-comfig.xml(由于本人oracle数据库安装的问题端口号及数据库有所不同)

<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 别名的配置 Dept-->
<typeAliases>
<typeAlias type="cn.mybatis.entity.Dept" alias="Dept"/>
</typeAliases> <environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1522:orc" />
<property name="username" value="bxq" />
<property name="password" value="bxq" />
</dataSource>
</environment>
</environments>
<!--关联小配置-->
<mappers>
<mapper resource="cn/mybatis/entity/Dept.xml" />
<mapper resource="cn/mybatis/entity/Dept2.xml" />
</mappers> </configuration>

1.3编写Dept实体类

package cn.mybatis.entity;

public class Dept {
private Integer deptNo;//部门编号
private String deptName;//部门名称
private String deptCity;//部门所在地址
public String getDeptCity() {
return deptCity;
}
public void setDeptCity(String deptCity) {
this.deptCity = deptCity;
}
public Integer getDeptNo() {
return deptNo;
}
public void setDeptNo(Integer deptNo) {
this.deptNo = deptNo;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
@Override
public String toString(){
return "Dept [deptNo= " + deptNo +", deptName=" + deptName+",deptCity"+deptCity+"]";
}
}

1.4编写Dept.xml小配置文件

<?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="cn.mybatis.entity.Dept"> <resultMap type="Dept" id="resultMapper">
<result property="deptName" column="deptName"/>
<result property="deptNo" column="deptNo"/>
<result property="deptCity" column="deptCity"/>
</resultMap>
     <!--代替"*"的方法-->
<sql id="columns">
          <!--植入所需要的列名-->
deptNo,deptName,deptCity
</sql>
  <!--++++++++++++++++++++++++++resultMap 实现结果映射++++++++++++++++++++-->
<!-- 查询部门信息 resultMap 实现结果映射 -->
<select id="selectAllDeptMapper" resultMap="resultMapper">
select * from dept
</select>
<!-- 代替"*" 连接sql标签的id="columns"-->
<select id="selectAllDeptUseAlias" resultType="Dept">
select <include refid="columns"/> from dept
</select> <!-- +++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++ --> <!-- 1.1查询部门所有信息 -->
<select id="selectAllDept" resultType="Dept">
<!--查询所有部门信息 -->
<!-- SQL不区分大小写 -->
select * from dept
</select> <!-- 增加部门信息 -->
<insert id="insertDept" parameterType="Dept">
insert into dept values(#{deptNo},#{deptName},#{deptCity})
</insert> <!-- 删除信息 -->
<delete id="deleteDept" parameterType="Dept"> delete from dept where deptNo=#{deptNo}
</delete> <!-- 修改信息 -->
<update id="updateDept" parameterType="Dept"> update dept set deptName=#{deptName} where deptNo=#{deptNo}
</update> <!-- 模糊查询 -->
<select id="likeDept" parameterType="Dept" resultType="Dept"> select * from dept where deptName like '%${deptName}%'
</select>
</mapper>

1.5书写MyTest测试类

package cn.mybatis.Test;

import java.io.IOException;
import java.io.Reader;
import java.util.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test; import cn.mybatis.entity.Dept; public class MyTest { SqlSession session;
@Before
public void initData() throws IOException{
Reader reader=Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
session= factory.openSession();
}
@Test
public void testselectAllDept() throws IOException{
//在xml配置中的一个锁定唯一SQL的id
List<Dept> selectList = session.selectList("selectAllDept");
for (Dept dept : selectList) {
System.out.println(dept);
}
}
//模糊查詢
@Test
public void likeTest(){
Dept dept = new Dept();
dept.setDeptName("市场");
List<Dept> list = session.selectList("cn.mybatis.entity.Dept.likeDept",dept);
for (Dept item : list) {
System.out.println(item);
}
session.close();
}
//修改
@Test
public void updateTest(){
Dept dept = new Dept();
dept.setDeptNo(5);
dept.setDeptName("开发部");
int count = session.update("cn.mybatis.entity.Dept.updateDept",dept);
session.commit();
System.out.println(count+"update ok!!!");
session.close();
}
//删除
@Test
public void testdeleteDept() throws IOException{
Dept dept = new Dept();
dept.setDeptNo(8);
int count = session.delete("cn.mybatis.entity.Dept.deleteDept",dept);
session.commit();
System.out.println(count+"del ok!");
}
//增加
@Test
public void testinsertDept() throws IOException{
Dept dept = new Dept();
dept.setDeptNo(8);
dept.setDeptName("财务部1");
dept.setDeptCity("上海");
int count = session.insert("cn.mybatis.entity.Dept.insertDept",dept);
session.commit();
System.out.println(count+"insert ok!!!");
} /*
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* resultMap的使用
*/ @Test
public void testresultMap() throws IOException{
List<Dept> list = session.selectList("cn.mybatis.entity.Dept.selectAllDeptMapper");
for (Dept dept : list) {
System.out.println(dept);
}
session.close();
}
@Test
public void selectAllDeptUseAlias() throws IOException{
List<Dept> list = session.selectList("cn.mybatis.entity.Dept.selectAllDeptUseAlias");
for (Dept dept : list) {
System.out.println(dept);
}
session.close();
} /*
* 动态查询+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
@Test
public void TestdynamicSelect() throws IOException{
Dept dept = new Dept();
dept.setDeptName("市场部");
dept.setDeptNo(4);
dept.setDeptCity("北京");
List<Dept> list = session.selectList("cn.mybatis.dao.IDeptDao.dynamicSelect",dept);
for (Dept dept2 : list) {
System.out.println(dept2);
}
}
//动态修改
@Test
public void Testdynamicupdate() throws IOException{
Dept dept = new Dept();
dept.setDeptName("市场部1");
dept.setDeptNo(4);
dept.setDeptCity("北京");
int count = session.update("cn.mybatis.dao.IDeptDao.dynamicUpdate",dept);
System.out.println(count);
session.close();
}
}

由于测试方法过多我们简单的运行出来一二个看一下结果

查询:

删除:

2. 动态查询

2.1编写Dept2.xml

<?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="cn.mybatis.dao.IDeptDao">
<!-- 1.1查询部门所有信息 -->
<select id="selectDeptByNo" parameterType="int" resultType="Dept">
<!--查询所有部门信息 -->
<!-- SQL不区分大小写 -->
select * from dept where deptNo=#{deptNo}
</select> <select id="getMapper" resultType="Dept">
select * from dept
</select> <!-- 动态查询 -->
<select id="dynamicSelect" parameterType="Dept" resultType="Dept">
select * from dept
<where>
<if test="deptNo!=null">
and deptNo=#{deptNo}
</if>
<if test="deptName!=null">
and deptName=#{deptName}
</if>
<if test="deptCity!=null">
and deptCity=#{deptCity}
</if>
</where>
</select> <!-- 动态修改 -->
<select id="dynamicUpdate" parameterType="int" resultType="Dept">
update dept
<set>
<if test="deptNo!=null">
deptNo=#{deptNo},
</if>
<if test="deptName!=null">
deptName=#{deptName},
</if>
<if test="deptCity!=null">
deptCity=#{deptCity},
</if>
</set>
where deptNo=#{deptNo}
</select> </mapper>

2.3在1.5 书写MyTest测试类中可找到我们需要的测试类

/*
* 动态查询+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
@Test
public void TestdynamicSelect() throws IOException{
Dept dept = new Dept();
dept.setDeptName("市场部");
dept.setDeptNo(4);
dept.setDeptCity("北京");
List<Dept> list = session.selectList("cn.mybatis.dao.IDeptDao.dynamicSelect",dept);
for (Dept dept2 : list) {
System.out.println(dept2);
}
}
//动态修改
@Test
public void Testdynamicupdate() throws IOException{
Dept dept = new Dept();
dept.setDeptName("市场部1");
dept.setDeptNo(4);
dept.setDeptCity("北京");
int count = session.update("cn.mybatis.dao.IDeptDao.dynamicUpdate",dept);
System.out.println(count);
session.close();
}

2.4运行结果

动态查询

动态修改

 3.resultMap实现结果映射

3.1 先前在 1.4编写Dept.xml小配置文件中已经配置好了需要用到的条件。

 /*
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* resultMap的使用
*/ @Test
public void testresultMap() throws IOException{
List<Dept> list = session.selectList("cn.mybatis.entity.Dept.selectAllDeptMapper");
for (Dept dept : list) {
System.out.println(dept);
}
session.close();
}
@Test
public void selectAllDeptUseAlias() throws IOException{
List<Dept> list = session.selectList("cn.mybatis.entity.Dept.selectAllDeptUseAlias");
for (Dept dept : list) {
System.out.println(dept);
}
session.close();
}

3.2测试类 与1.5书写MyTest测试类中可见  

 /*
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* resultMap的使用
*/ @Test
public void testresultMap() throws IOException{
List<Dept> list = session.selectList("cn.mybatis.entity.Dept.selectAllDeptMapper");
for (Dept dept : list) {
System.out.println(dept);
}
session.close();
}
@Test
public void selectAllDeptUseAlias() throws IOException{
List<Dept> list = session.selectList("cn.mybatis.entity.Dept.selectAllDeptUseAlias");
for (Dept dept : list) {
System.out.println(dept);
}
session.close();
}

3.3测试结果

testresultMap();

selectAllDeptUseAlias();

4.session.getMapper()方法

4.1

创建IDeptDao接口

package cn.mybatis.dao;

import java.util.List;

import cn.mybatis.entity.Dept;
/**
* 接口
* @author xiaobai
*
*/
public interface IDeptDao {
public Dept selectDeptByNo(Integer deptNo); public List<Dept> getMapper();
}

 4.2编写MyTest2测试类

package cn.mybatis.Test;

import java.io.IOException;
import java.io.Reader;
import java.util.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test; import cn.mybatis.dao.IDeptDao;
import cn.mybatis.entity.Dept; public class MyTest2 {
SqlSession session;
@Before
public void initData() throws IOException{
Reader reader=Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
session= factory.openSession();
}
//实现getMapper接口
     //按条件查询
@Test
public void TestgetMapper(){
IDeptDao mapper = session.getMapper(IDeptDao.class);
Dept dept = mapper.selectDeptByNo(5);
System.out.println(dept.getDeptName());
session.close();
}
//查询全部信息
@Test
public void TestgetMapper1() throws Exception{
IDeptDao mapper = session.getMapper(IDeptDao.class);
List<Dept> list=mapper.getMapper();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}
} }

4.3测试结果:
查询全部信息

												

Mybatis(综合案例)的更多相关文章

  1. Mybatis高级:Mybatis注解开发单表操作,Mybatis注解开发多表操作,构建sql语句,综合案例学生管理系统使用接口注解方式优化

    知识点梳理 课堂讲义 一.Mybatis注解开发单表操作 *** 1.1 MyBatis的常用注解 之前我们在Mapper映射文件中编写的sql语句已经各种配置,其实是比较麻烦的 而这几年来注解开发越 ...

  2. spring基础:什么是框架,框架优势,spring优势,耦合内聚,什么是Ioc,IOC配置,set注入,第三方资源配置,综合案例spring整合mybatis实现

    知识点梳理 课堂讲义 1)Spring简介 1.1)什么是框架 源自于建筑学,隶属土木工程,后发展到软件工程领域 软件工程中框架的特点: 经过验证 具有一定功能 半成品 1.2)框架的优势 提高开发效 ...

  3. 企业级应用,如何实现服务化五(dubbo综合案例)

    这是企业级应用,如何实现服务化第五篇.在上一篇企业级应用,如何实现服务化四(基础环境准备)中.已经准备好了zookeeper注册中心,和dubbo管理控制台.这一篇通过一个综合案例,看一看在企业级应用 ...

  4. JQuery:JQuery基本语法,JQuery选择器,JQuery DOM,综合案例 复选框,综合案例 随机图片

    知识点梳理 课堂讲义 1.JQuery快速入门 1.1.JQuery介绍 jQuery 是一个 JavaScript 库. 框架:Mybatis (jar包) 大工具 插件:PageHelper (j ...

  5. MyBatis入门案例、增删改查

    一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...

  6. Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...

  7. Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...

  8. MyBatis入门案例 增删改查

    一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...

  9. JavaScript:综合案例-表单验证

    综合案例:表单验证 开发要求: 要求定义一个雇员信息的增加页面,例如页面名称为"emp_add.htmnl",而后在此页面中要提供有输入表单,此表单定义要求如下: .雇员编号:必须 ...

随机推荐

  1. 找不到方法:"!!0[] System.Array.Empty()".

    找不到方法:"!!0[] System.Array.Empty()". 这个原因是没装.net framework 4.6

  2. php中return的用法实例分析

    本文实例讲述了php中return的用法.分享给大家供大家参考.具体分析如下: 首先,它的意思就是返回;return()是语言结构而不是函数,仅在参数包含表达式时才需要用括号将其括起来.当返回一个变量 ...

  3. python征程1.2(初识python)

    1.操作符. 和其他绝大多数语言一样,Python的算术操作符以你熟悉的方式工作. “+,—,×,/,//,%,**,” 注意:python有两种除法操作符(1)单斜杠用以传统除法,(2)双斜杠用以浮 ...

  4. MongoDB常用操作--集合3

    1.更新集合中的文档,语法如下: db.collection.update(criteria,objNew,upsert,multi) 参数说明: criteria:用于设置查询条件的对象 objNe ...

  5. Hibernate(十)__缓存机制

    为什么需要缓存? 缓存的作用主要用来提高性能,可以简单的理解成一个Map: 使 用缓存涉及到三个操作:把数据放入缓存.从缓存中获取数据. 删除缓存中的无效数据. 从上图看出: 当我们去查询对象的时候, ...

  6. Atitit.异常处理 嵌套  冗长的解决方案

    Atitit.异常处理 嵌套  冗长的解决方案 1. 异常处理的需要改进的地方1 2. +异常设计的初衷是, 在程序中出现错误时, 由程序自己处理错误, 尽量不要以exit(0)这种粗暴的方式中止程序 ...

  7. 0x80040E14 Caused by Max Url Length bug

    We get a case when the customer access a SharePoint site, he meet an error on SharePoint. 0x80040E14 ...

  8. Android获取服务器Json字符串并显示在ListView上面

    已经好久没有更新博客,今天终于有新的东西可以记录了. 通过这次的任务学习到了以前没有注意到的知识点,真的有种书读百遍,其义自见的感觉.这次又重新认识了<Handler消息机制原理>.这次的 ...

  9. IOS开发基础知识--碎片32

    1:动画属性UIViewAnimationOptions说明 a:常规动画属性设置(可以同时选择多个进行设置) UIViewAnimationOptionLayoutSubviews:动画过程中保证子 ...

  10. ERROR ITMS-90682: Invalid Bundle - The asset catalog at 'Payload/XXXXX/Assets.car' can't contain 16-bit or P3 assets if the app supports iOS 9.3 or earlier.

    刚升级Xcode 8, 幺蛾子又出现了.提交的时候出了这个问题. BTW,感谢google.以下为解决方案:‘ 在 Xcode 8 中,当你资源文件中[含有16位图]或者[图片显示模式γ值为'P3'] ...