iBatis的CRUD操作详细总结
昨天晚上看了一下关于iBatis的一个讲解的视频,讲的和我的这个简单的总结差不多....
思考了一下还是把主要操作都总结一下吧,当然这里也不是全的,知识简单的CRUD。。。
首先我觉得持久层的操作主要就是这几个:
public interface IPersonDao {
//添加
public boolean addPerson(Person person);
//更新
public boolean updatePerson(Person person);
//删除 根据ID删除, 批量删除
public boolean deletePersonById(int id);
public boolean deleteAll(List<Integer> ids);
//查询,根据ID, name模糊查询, 查询所有, 多条件查询,分页查询
public Person queryById(int id);
public List<Person> queryByName(String name);
public List<Person> queryAllPerson();
public List<Person> queryPersons(Person person);
public List<Person> queryPage(Map<String, Object> info);
}
然后我们就一一实现吧。
Person类:
public class Person {
private int id;
private String name;
private int age;
//省去了getter setter
}
Person.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="Person">
<!-- 实体类路径和名 这里得到person是大小写不敏感的 -->
<typeAlias alias="person" type="com.gbx.po.Person" /> <!-- SQL语句 --> <insert id="addPerson" parameterClass="person" >
<!-- 利用自增字段来填充主键 -->
<selectKey resultClass="int" keyProperty="id">
select LAST_INSERT_ID() as id
</selectKey>
insert into t_person(id, name, age)
values(#id#, #name#, #age#)
</insert> <update id="updatePerson" parameterClass="person">
update t_person
set
id=#id#,
name=#name#,
age=#age#
where
id=#id#
</update>
<delete id="deletePerson" parameterClass="int">
delete from t_person
where
id=#id#
</delete> <delete id="deleteAllPerson" parameterClass="List">
delete from t_person
where
id in
(<iterate conjunction=",">
#[]#
</iterate>)
</delete> <select id="queryPersonById" parameterClass="int" resultClass="person">
select id, name, age
from t_person
where
id=#id#
</select> <select id="queryPersonByName" parameterClass="String" resultClass="person">
select id, name, age
from t_person
where
name like '%$name$%'
</select>
<select id="queryAllPerson" resultClass="person" >
select id, name, age
from t_person
</select> <select id="queryPersons" resultClass="person" parameterClass="person" >
select id, name, age
from t_person
<dynamic prepend="where">
<isNotEqual prepend="and" property="id" compareValue="0">
id=#id#
</isNotEqual>
<isNotNull prepend="and" property="name">
name like '%$name$%'
</isNotNull>
<isGreaterEqual prepend="and" property="age" compareValue="0">
age>#age#
</isGreaterEqual>
</dynamic> </select> <select id="queryPage" parameterClass="java.util.Map" resultClass="person">
select id, name, age
from t_person
<dynamic prepend="where">
<isNotEqual prepend="and" property="id" compareValue="0">
id=#id#
</isNotEqual>
<isNotNull prepend="and" property="name">
name like '%$name$%'
</isNotNull>
<isNotEqual prepend="and" property="age" compareValue="0">
age>=#age#
</isNotEqual>
</dynamic>
limit #begin#, #pageSize#
</select> </sqlMap>
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig>
<!-- 引入资源 -->
<properties resource="SqlMap.properties"/>
<!-- 配置数据库连接信息 -->
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}" />
<property name="JDBC.ConnectionURL" value="${url}" />
<property name="JDBC.Username" value="${username}" />
<property name="JDBC.Password" value="${password}" />
</dataSource>
</transactionManager> <sqlMap resource="com/gbx/po/Person.xml"/> </sqlMapConfig>
PersonDaoImp
package com.gbx.dao; import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.gbx.po.Person;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class PersonDaoImp implements IPersonDao{ private static SqlMapClient client = null;
static{
try {
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
client = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
public boolean addPerson(Person person) {
System.out.println("添加前 " + person.getId());
try {
client.insert("addPerson", person);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("添加后: " + person.getId());
return true;
} public boolean updatePerson(Person person) {
try {
client.update("updatePerson", person);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("更新成功....");
return true;
} public boolean deletePersonById(int id) {
try {
client.delete("deletePerson", id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("删除成功....");
return true;
} public boolean deleteAll(List<Integer> ids) {
try {
client.delete("deleteAllPerson", ids);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("删除成功....");
return true;
} public Person queryById(int id) {
Person p = null;
try {
p = (Person) client.queryForObject("queryPersonById", id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return p;
} @SuppressWarnings("unchecked")
public List<Person> queryByName(String name) {
List<Person> persons = null;
try {
persons = client.queryForList("queryPersonByName", name);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return persons;
} @SuppressWarnings({ "unchecked", "deprecation" })
public List<Person> queryAllPerson() {
List<Person> persons = null;
try { persons = client.queryForList("queryAllPerson");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return persons;
} @SuppressWarnings("unchecked")
public List<Person> queryPersons(Person person) {
List<Person> persons = null;
try {
persons = client.queryForList("queryPersons", person);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return persons;
} @SuppressWarnings("unchecked")
public List<Person> queryPage(Map<String, Object> info) {
List<Person> persons = null;
try {
persons = client.queryForList("queryPage", info);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return persons;
} public static void main(String[] args) {
IPersonDao dao = new PersonDaoImp();
//添加
// Person person = new Person();
// person.setName("小明");
// person.setAge(1);
// dao.addPerson(person); //更新
// Person person = new Person();
// person.setId(18);
// person.setName("小明123");
// person.setAge(1);
// dao.updatePerson(person); //删除
// dao.deletePersonById(18);
//批量删除
// List<Integer> ids = new ArrayList<Integer>();
// ids.add(3); ids.add(4); ids.add(5);
// dao.deleteAll(ids);
// System.out.println(ids); //ID查询
// Person person = new Person();
// person = dao.queryById(19);
// System.out.println("查询到的个人信息为:" + person.getId() + " " + person.getAge() + " " + person.getName()); //姓名模糊查询
// List<Person> persons = dao.queryByName("小米");
// for (Person p : persons) {
// System.out.println("查询到的个人信息为:" + p.getId() + " " + p.getAge() + " " + p.getName());
// } //查询所有
// List<Person> persons = dao.queryAllPerson();
// for (Person p : persons) {
// System.out.println("查询到的个人信息为:" + p.getId() + " " + p.getAge() + " " + p.getName());
// } //多条件查询
// Person person = new Person();
// person.setId(0);
// person.setName("小");
// person.setAge(20);
// List<Person> persons = dao.queryPersons(person);
// for (Person p : persons) {
// System.out.println("查询到的个人信息为:" + p.getId() + " " + p.getAge() + " " + p.getName());
// } //利用Map实现模糊的分页查询
Map<String, Object> info = new HashMap<String, Object>();
info.put("begin", 0);
info.put("pageSize", 100);
info.put("id", 0);
info.put("name", "小");
info.put("age", 20); List<Person> persons = dao.queryPage(info);
for (Person p : persons) {
System.out.println("查询到的个人信息为:" + p.getId() + " " + p.getAge() + " " + p.getName());
}
}
}
iBatis的CRUD操作详细总结的更多相关文章
- 一步步学Mybatis-实现单表情况下的CRUD操作 (3)
今天这一章要紧接上一讲中的东西,本章中创建基于单表操作的CRUD与GetList操作,此示例中以Visitor表为范例,为了创建一点测试数据我们先弄个Add方法吧 继续在上次的IVisitorOper ...
- Springboot整合Mybatis实现级联一对多CRUD操作
在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...
- Spring Boot整合Mybatis完成级联一对多CRUD操作
在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...
- 【翻译】MongoDB指南/CRUD操作(四)
[原文地址]https://docs.mongodb.com/manual/ CRUD操作(四) 1 查询方案(Query Plans) MongoDB 查询优化程序处理查询并且针对给定可利用的索引选 ...
- 使用MyBatis对表执行CRUD操作
一.使用MyBatis对表执行CRUD操作——基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: <?xml version="1.0&quo ...
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作(转载)
本文转载自:http://www.cnblogs.com/jpf-java/p/6013540.html 上一篇博文MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybati ...
- MyBatis入门学习教程-使用MyBatis对表执行CRUD操作
上一篇MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对use ...
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
一.使用MyBatis对表执行CRUD操作--基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: 1 <?xml version="1.0&q ...
- 【JAVA使用XPath、DOM4J解析XML文件,实现对XML文件的CRUD操作】
一.简介 1.使用XPath可以快速精确定位指定的节点,以实现对XML文件的CRUD操作. 2.去网上下载一个“XPath帮助文档”,以便于查看语法等详细信息,最好是那种有很多实例的那种. 3.学习X ...
随机推荐
- 使用webpy创建一个简单的restful风格的webservice应用
下载:wget http://webpy.org/static/web.py-0.38.tar.gz解压并进入web.py-0.38文件夹安装:easy_install web.py 这是一个如何使用 ...
- soapUI-property Transfer
1.1.1 Property Transfer 创建或双击现有的Property-Transfer TestStep将打开以下窗口: 左侧的列表显示了此TestStep中配置的传输,添加和管理所需的 ...
- 第一章SpringBoot入门
一.简介 SpringBoot来简化Spring应用的开发,约定大于配置,去繁从简,just run就能创建一个独立的产品级别的应用. 背景: j2EE笨重的开发方法,繁多的配置,低下的开发效率,复杂 ...
- MFC CFile类读写文件详解
CFile类提供了对文件进行打开,关闭,读,写,删除,重命名以及获取文件信息等文件操作的基本功能,足以处理任意类型的文件操作. 一个读写文件的例子: 文件I/O 虽然使用CArchive类内建的序列化 ...
- DIV内容垂直居中
css垂直居中属性设置vertical-align: middle对div不起作用,例如: <!DOCTYPE html> <html lang="zh-CN"& ...
- Linux服务器---安装bind
安装bind 1.安装bind软件,需要安装3 个bind.bind-chroot.bind-util [root@localhost pub]# yum install -y bind bind-c ...
- 2018年星际争霸AI挑战赛–三星与FB获冠亚军,中科院自动化所夺得季军
雷锋网 AI 科技评论消息,2018 年 11 月 13-17 日,AAAI 人工智能与交互式数字娱乐大会 (AI for Interactive Digital Entertainment) 在阿尔 ...
- ELK+Kafka学习笔记之FileBeat日志合并配置输出到kafka集群
filebeat.prospectors: - type: log #日志输出类型 enabled: true ...
- Linux下Tomcat端口、进程以及防火墙设置
Linux下Tomcat端口.进程以及防火墙设置 1,查看tomcat进程: #ps -aux | grep tomcat(或者ps -ef | grep tomcat都行) 可以看到现在运行着两个 ...
- JavaScript 获取和修改 内联样式
JavaScript 获取和修改 内联样式 版权声明:未经授权,严禁转载分享! 元素的样式 HTML 元素的 style 属性返回一个 CSSStyleDeclaration 类型的对象. Style ...