MyBatis.3.CRUD
<?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的更多相关文章
- MyBatis:CRUD功能
在前面已经自动生成了mapper和pojo,接下来我们实现mybatis的CRUD功能,先新建service.controller层的方法. 这里的sid是一个开源的id生成类,写完后,我们还需要在启 ...
- Mybatis的CRUD案例
一.Mybatis增删改查案例 上一节<Mybatis入门和简单Demo>讲了如何Mybatis的由来,工作流程和一个简单的插入案例,本节主要继上一讲完整的展示Mybatis的CRUD操作 ...
- 【MyBatis】MyBatis实现CRUD操作
1.实现基本CRUD功能 使用MyBatis对数据完整的操作,也就是CRUD功能的实现.根据之前的内容,要想实现CRUD,只需要进行映射文件的配置. 范例:修改EmpMapper.xml文件,实现CR ...
- SpringBoot 整合 Mybatis 进行CRUD测试开发
今天来和大家分享下 Spring Boot 整合 MyBatis 的 CRUD 测试方法开发.因为 MyBaits 有两种开发形式,一种基于注解,一种基于 xml . SpringBoot配置文件也有 ...
- 基于mybatis的CRUD
u 基于Mybatis的CRUD u 掌握MyBatis的结果类型-resultMap和resultType u 掌握MyBatis的参数类型 u 掌握#和$两种语法 1 基于myb ...
- 05 Mybatis的CRUD操作和Mybatis连接池
1.CRUD的含义 CRUD是指在做计算处理时的增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delete)几个单词的首字母简写.主要被用在描述软件系统中 ...
- 03 Mybatis:05.使用Mybatis完成CRUD
mybatis框架:共四天 明确:我们在实际开发中,都是越简便越好,所以都是采用不写dao实现类的方式.不管使用XML还是注解配置. 第二天:mybatis基本使用 mybatis的单表crud操作 ...
- mybatis(CRUD)
3.mybatis(CRUD) 有了mybatis,我们要对数据库进行增删改查只需要操作接口和mapper.xml文件,然后进行测试就可以了. 实例代码如下: 接口 public interface ...
- MyBatis之CRUD
1 mybatis框架介绍 1.1回顾jdbc操作数据库的过程 1.2 mybatis开发步骤 A.提供一个SqlMapperConfig.xml(src目录下),该文件主要配置数据库连接,事务,二级 ...
- MyBatis Tutorial – CRUD Operations and Mapping Relationships – Part 1---- reference
http://www.javacodegeeks.com/2012/11/mybatis-tutorial-crud-operations-and-mapping-relationships-part ...
随机推荐
- netcore如何引用package?
netcore项目引用dll包,分如下三种: 1.引用网络包 从nuget获取,然后引入,使用命令:dotnet add package 包名 然后:dotnet restore 2.引用同解决方案的 ...
- C++指定位数小数输出
关键词:头文件<iomanip>,指令setw(x),fixed,setprecision(x). setw()这个指令也可以配合setfill('')用于对齐输出,详情见另一篇博客htt ...
- js传输txt文档内容
要求:实现修改text文档内容,即可将text修改内容传到页面显示: HTML: <!doctype html> <html lang="en"> < ...
- Vue 列表渲染及条件渲染实战
条件渲染 有时候我们要根据数据的情况,决定标签是否进行显示或者有其他动作.最常见的就是,表格渲染的时候,如果表格没有数据,就显示无数据.如果有数据就显示表格数据. Vue 帮我们提供了一个v-if的指 ...
- jsweb常用代码
<script> $(function (){ $.ajax({ url: 'https://test.com:8080/api/v1/users?query_not_auth=100&a ...
- Laravel路由除了根目录全报404错误
Route::get('hello',function(){ return 'Hello World!'; }); 在laravel/app/Http/routes.php下添加上面的语句,然后再浏览 ...
- NABC for Teamproject
“教育是一个社会发展的支柱, 你和我能看到并理解这个博客, 教育功不可没. 高等教育的形式并不是一成不变的, 高等教育一直在演进.”邹欣老师在博客上如此写道.为了迎合信息化时代的特色,网络上的知识传 ...
- 如何知道一个App的包名呢
包名(Package name)是Android系统中判断一个APP的唯一标识 记录我获取包名的几种方式 方法一:通过cmd命令,打开你要获取包名的APP 1.adb shell 2.dumpsys ...
- Structs2笔记①--structs的背景、structs2框架的意义、第一个helloworld
Struts2的背景 由出色稳定的框架struts1和webwork框架整合而来的 吸取了两大框架的优点 提高了开发的效率和规范性 更好的实现了MVC架构 解除了与servlet的强耦合性 使用str ...
- Fast Packet Processing - A Survey
笔记是边读边写的旁注,比较乱,没有整理就丢上来了. 可以说不仅要说fast packet process servey,也同时是一篇packet process的综述了.packet processi ...