<?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="org.mybatis.example.dao.DeptMapper">
	<select id="selectOne" parameterType="int"
		resultType="org.mybatis.example.dao.Dept">
			select * from dept where deptno=#{id}
	</select>
	<insert id="insertDept" parameterType="org.mybatis.example.dao.Dept"
	useGeneratedKeys="true" keyProperty="dept">
		insert into dept(dname,loc)
		values(
			   #{dname,jdbcType=VARCHAR},
			   #{loc,jdbcType=VARCHAR})
	</insert>
	<update id="updateDept" parameterType="org.mybatis.example.dao.Dept">
		update dept set dname=#{dname},loc=#{loc}
					where deptno=#{deptno}
	</update>
	<delete id="deleteDept" parameterType="java.lang.Integer">
		delete from dept where deptno=#{id}
	</delete>
</mapper>

package org.mybatis.example.dao;

public interface DeptMapper {
	public Dept selectOne(int id);
	public int insertDept(Dept dept);
	public int updateDept(Dept dept);
	public int deleteDept(int id);
}

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;
import org.mybatis.example.dao.Dept;
import org.mybatis.example.dao.DeptMapper;

public class Test2 {
	public static void main(String[] args) throws IOException {
		String resource="mybatisConfig.xml";
		Reader reader=Resources.getResourceAsReader(resource);
		SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader);

		SqlSession session=sqlMapper.openSession();

		try {
			DeptMapper mapper=session.getMapper(DeptMapper.class);
			//1.查询
//			Dept dept=mapper.selectOne(2);
//			System.out.println(dept.getDname());
			//2.插入操作
			Dept dept=new Dept();
			dept.setDname("张沅湲");
			dept.setLoc("郑州");
			mapper.insertDept(dept);
			session.commit();//实现增加 修改 删除
			//3.更新操作;
//			Dept dept=new Dept();
//			dept.setDeptno(3);
//			dept.setDname("张沅湲");
//			dept.setLoc("湖南");
//			mapper.updateDept(dept);
//			session.commit();
			//4.删除操作;
//			int i=mapper.deleteDept(3);
//			session.commit();
//			System.out.println(i);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		}

	}
}

复杂的查询,在项目开发中,可以使用一个工具类来封装这些复杂的步骤:

package org.mybatis.example.dao;

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 SqlSessionFactoryUtil {
	private static SqlSessionFactory factory;
	public SqlSessionFactoryUtil() {}
	static{
		Reader reader=null;

		try {
			reader=Resources.getResourceAsReader("mybatisConfig.xml");
			factory=new SqlSessionFactoryBuilder().build(reader);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(reader!=null)reader.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	public static SqlSession getSqlSession(){
		return factory.openSession();
	}
}

DeptMapper接口的完全代码:

package org.mybatis.example.dao;

import java.util.List;

public interface DeptMapper {
	public Dept selectOne(int id);
	public int insertDept(Dept dept);
	public int updateDept(Dept dept);
	public int deleteDept(int id);
	public List<Dept>selectAllDepts();
	public List<Dept>selectByCriteria(String deptName);
}

1.查询所有部门信息参考代码;

<select id="selectAllDepts" resultType="org.mybatis.example.dao.Dept" >
		select deptno,dname,loc from dept
	</select>

测试代码;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.example.dao.Dept;
import org.mybatis.example.dao.DeptMapper;
import org.mybatis.example.dao.SqlSessionFactoryUtil;

public class Test3 {
	public static void main(String[] args) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		try {
			DeptMapper mapper=session.getMapper(DeptMapper.class);
			List<Dept>depts=mapper.selectAllDepts();
			for(Dept dept:depts){
				System.out.println("部门名称:"+dept.getDname());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
	}
}

2.按条件查询,映射文件做以下修改,新增条件为模糊查询方法:

<!-- 按条件查询 -->
	<select id="selectByCriteria" parameterType="java.lang.String"
		resultType="org.mybatis.example.dao.Dept">
		select deptno,dname,loc from dept
		where dname like '%${_parameter}%'
	</select>

测试代码

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.example.dao.Dept;
import org.mybatis.example.dao.DeptMapper;
import org.mybatis.example.dao.SqlSessionFactoryUtil;

//按照条件进行查询;
public class Test4 {
	public static void main(String[] args) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		try {
			DeptMapper mapper=session.getMapper(DeptMapper.class);
			List<Dept>depts=mapper.selectByCriteria("网络部");

			for(Dept dept:depts){
				System.out.println("部门名称:"+dept.getDname());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
	}
}

MyBatis.3.CRUD的更多相关文章

  1. MyBatis:CRUD功能

    在前面已经自动生成了mapper和pojo,接下来我们实现mybatis的CRUD功能,先新建service.controller层的方法. 这里的sid是一个开源的id生成类,写完后,我们还需要在启 ...

  2. Mybatis的CRUD案例

    一.Mybatis增删改查案例 上一节<Mybatis入门和简单Demo>讲了如何Mybatis的由来,工作流程和一个简单的插入案例,本节主要继上一讲完整的展示Mybatis的CRUD操作 ...

  3. 【MyBatis】MyBatis实现CRUD操作

    1.实现基本CRUD功能 使用MyBatis对数据完整的操作,也就是CRUD功能的实现.根据之前的内容,要想实现CRUD,只需要进行映射文件的配置. 范例:修改EmpMapper.xml文件,实现CR ...

  4. SpringBoot 整合 Mybatis 进行CRUD测试开发

    今天来和大家分享下 Spring Boot 整合 MyBatis 的 CRUD 测试方法开发.因为 MyBaits 有两种开发形式,一种基于注解,一种基于 xml . SpringBoot配置文件也有 ...

  5. 基于mybatis的CRUD

    u  基于Mybatis的CRUD u  掌握MyBatis的结果类型-resultMap和resultType u  掌握MyBatis的参数类型 u  掌握#和$两种语法 1      基于myb ...

  6. 05 Mybatis的CRUD操作和Mybatis连接池

    1.CRUD的含义 CRUD是指在做计算处理时的增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delete)几个单词的首字母简写.主要被用在描述软件系统中 ...

  7. 03 Mybatis:05.使用Mybatis完成CRUD

    mybatis框架:共四天 明确:我们在实际开发中,都是越简便越好,所以都是采用不写dao实现类的方式.不管使用XML还是注解配置. 第二天:mybatis基本使用 mybatis的单表crud操作 ...

  8. mybatis(CRUD)

    3.mybatis(CRUD) 有了mybatis,我们要对数据库进行增删改查只需要操作接口和mapper.xml文件,然后进行测试就可以了. 实例代码如下: 接口 public interface ...

  9. MyBatis之CRUD

    1 mybatis框架介绍 1.1回顾jdbc操作数据库的过程 1.2 mybatis开发步骤 A.提供一个SqlMapperConfig.xml(src目录下),该文件主要配置数据库连接,事务,二级 ...

  10. MyBatis Tutorial – CRUD Operations and Mapping Relationships – Part 1---- reference

    http://www.javacodegeeks.com/2012/11/mybatis-tutorial-crud-operations-and-mapping-relationships-part ...

随机推荐

  1. RAID卡的结构详解

    软件RAID的缺点如此之多,使人们不断地思考更多实现RAID的方法.既然软件缺点太多,那么用硬件实现如何呢? RAID卡就是一种利用独立硬件来实现RAID功能的方法.要在硬件上实现RAID功能,必须找 ...

  2. linux压缩相关

    tar命令 tar是打包,即把好多东西放在一个大文件里面,之后再压缩:当然也可以解包 tar的几个参数说明: -c 创建一个新的包 -x 将包里的文件还原出来 -t 显示包内文件的列表 -f 指定要处 ...

  3. 根据 WBS 列新 PID 数据

    之前写过关于 菜单树的. http://www.cnblogs.com/newsea/archive/2012/08/01/2618731.html 现在在写城市树. 结构: CREATE TABLE ...

  4. babel无法编译?

    ECMAScript 6(ES6)的发展或者说普及之快可以说是难以想象的,对很多人来说ECMAScript 5(ES5)都还普及呢.现代浏览器对ES6新特新或多或少的有些支持,但支持度不高,所以要想在 ...

  5. 慢吞吞的pip切换源

    http://blog.csdn.net/gz_liuyun/article/details/52778198

  6. Dao DaoImp

    DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...

  7. Final发布:文案+美工展示博客

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2476 小组介绍 组长:付佳 组员:张俊余 李文涛 孙赛佳 田良 于洋 段 ...

  8. Windows下Visual Studio2017之AI环境搭建

    本博客主要包含以下3点: AI简介及本博客主要目的 环境介绍及安装原因 搭建环境及检验是否安装成功 离线模型的训练 时间分配:   时间 时长(分钟) 收集资料+写博客 6.12 11:28-12:2 ...

  9. TFS任务预览

    不太熟悉TFS任务项的建立. 初步建立及按老师要求分配到个人的任务设置与时间安排如下: (长时间任务可由多人合作完成,具体根据情况迅速调整任务分配) 加上每人需要进行阅读前一小组的代码需要时间2*8= ...

  10. 超实用 2 ArrayList链表之 员工工资管理系统

    package ArrayList的小程序; import java.io.*; import java.util.*; public class kkk { /** * 作者:Mr.fan * 功能 ...