首先解释一下CRUD的含义:CRUD是指在做计算处理时的增加(Create)、查询(Retrieve)(重新得到数据)、更新(Update)和删除(Delete) 基本的数据库操作

创建工程iBatisDemo

1:首先要导入关于iBatis的jar包,以及连接数据库的jar包(我用的是MySQL

2: 创建表t_person, 建立实体类Person

create table t_person(
id int primary key auto_increment,
name varchar(50),
age int
);

  

public class Person {
private int id;
private String name;
private int age; ......
}

  

3:创建总的XML配置文件

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/Person.xml"/> </sqlMapConfig>

  

l, SqlMap.properties

SqlMap.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=1

  

  

4: 穿件实体类的XML配置文件 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"> <!-- 实体类路径和类名 -->
<typeAlias type="com.gbx.Person" alias="person"/>
<!-- 数据库实体类的映射 -->
<resultMap id="personsResult" class="person" >
<result property="id" column="id" />
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap> <!-- SQL语句 --> <insert id="insertPerson" parameterClass="person">
insert into t_person(
id,
name,
age
)
values(
#id#,
#name#,
#age#
)
</insert> <select id="queryPersonById" parameterClass="int" resultClass="person">
select id, name, age from t_person where id = #id#
</select> <select id="queryAll" resultMap="personsResult">
select id, name, age from t_person;
</select>
<update id="updatePersonById" parameterClass="person">
update t_person set name = #name#, age = #age# where id = #id#
</update>
<delete id="deletePersonById" parameterClass="int">
delete from t_person where id = #id#
</delete> </sqlMap>

  

5: 写测试类

package com.gbx.dao;

import java.awt.Font;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List; import javax.swing.JOptionPane; import com.gbx.Person;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class PersonDao {
private static SqlMapClient sqlMapClient = null;
static {
try {
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//C
public void insertPerson(Person person) throws SQLException {
sqlMapClient.insert("insertPerson", person);
}
//R
public Person queryPersonById(int id) throws SQLException {
return (Person)sqlMapClient.queryForObject("queryPersonById", id);
}
@SuppressWarnings("unchecked")
public List<Person> queryALl() throws SQLException {
return sqlMapClient.queryForList("queryAll");
}
//U
public void updatePersonById(Person person) throws SQLException {
sqlMapClient.update("updatePersonById", person);
}
//D
public void deletePersonById(int id) throws SQLException {
sqlMapClient.delete("deletePersonById", id);
} public static void main(String args[]) {
//C
/*Person person = new Person();
person.setId(3);
person.setName("小米3");
person.setAge(57);
try {
new PersonDao().insertPerson(person);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuilder br = new StringBuilder();
br.append("添加成功!往数据中添加了如下数据:\n");
br.append("编号 " + "姓名 " + "年龄 \t\n ");
br.append(person.getId() + " " + person.getName() + " " + person.getAge() + " \n"); JOptionPane.getRootFrame().setFont(new Font("Arial", Font.BOLD, 20));
JOptionPane.showMessageDialog(null, br.toString());*/ //R
/*Person person;
try {
person = new PersonDao().queryPersonById(1);
StringBuilder br = new StringBuilder();
br.append("查询成功!往数据中添加了如下数据:\n");
br.append("编号 " + "姓名 " + "年龄 \t\n ");
br.append(person.getId() + " " + person.getName() + " " + person.getAge() + " \n"); JOptionPane.getRootFrame().setFont(new Font("Arial", Font.BOLD, 20));
JOptionPane.showMessageDialog(null, br.toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
List<Person> persons = new PersonDao().queryALl(); for (Person p : persons) {
System.out.println(p.getId() + " " +p.getName() + " " + p.getAge());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/ //U
/*Person p = new Person();
p.setName("哈哈");
p.setAge(100);
p.setId(1);//表示修改1号
try {
new PersonDao().updatePersonById(p);
System.out.println("修改成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/ //D
try {
new PersonDao().deletePersonById(3);
System.out.println("删除成功。。");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}

  

iBatis的基本配置+CRUD操作的更多相关文章

  1. mybatis(二)执行CRUD操作的两种方式配置和注解

    一.使用MyBatis对表执行CRUD操作——基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: <?xml version="1.0&quo ...

  2. hibernate入门-基本配置及简单的crud操作

    框架来说主要是需要写大量的配置文件,hibernate相比mybatis来说更强大,移植性更好: 1.类和数据库的映射配置:配置文件命名一般--类名.hbm.xml (user.hbm.xml),与实 ...

  3. 数据库CRUD操作以及MyBatis的配置使用

    • 业务字段设计 • 数据库创建 • CRUD操作 • MyBatis集成 • 注解和XML定义 • ViewObject和DateTool • 首页开发     • 业务字段设计 实体: name: ...

  4. 使用MyBatis对表执行CRUD操作

    一.使用MyBatis对表执行CRUD操作——基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: <?xml version="1.0&quo ...

  5. MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作(转载)

    本文转载自:http://www.cnblogs.com/jpf-java/p/6013540.html 上一篇博文MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybati ...

  6. MyBatis入门学习教程-使用MyBatis对表执行CRUD操作

    上一篇MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对use ...

  7. MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作

    一.使用MyBatis对表执行CRUD操作--基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: 1 <?xml version="1.0&q ...

  8. MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作

    上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对u ...

  9. MyBatis学习总结_02_使用MyBatis对表执行CRUD操作

    一.使用MyBatis对表执行CRUD操作——基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: 1 <?xml version="1.0&q ...

随机推荐

  1. react native 淘宝镜像

    终端命令  open 打开 .npmrc 插入一行代码 registry=https://registry.npm.taobao.org

  2. linux文件系统软链接硬链接

    引子 目前,UNIX的文件系统有很多种实现,例如UFS(基于BSD的UNIX文件系统).ext3.ext4.ZFS和Reiserfs等等. 不论哪一种文件系统,总是需要存储数据.硬盘的最小存储单位是扇 ...

  3. OO第四次阶段性总结

    测试与正确性论证的效果差异及优缺点 测试实际上就是对程序进行的一种黑箱测试,利用各种各样的测试样例去检验程序是否能够给出正确的结果.其中的单元测试则是将整体的测试拆分成单元来进行,但其仍然躲不开黑箱测 ...

  4. SQLServer 重启服务后,自增1的标识列一次增长了1000(转自博问)

    sql2012:我重启了下sql服务,然后自增列Id居然一下子跳了100,怎么回事啊?(之前的数据Id为1,我重启服务后,第二条数据Id就变成1001了),我自增是1,求大神帮忙啊 SQLServer ...

  5. c#获取指定时区的日期

    1.首先将服务器的时间转化为utc时间,然后转换成指定时区的日期 public DateTime GetSpecificZoneNowDate(string zoneName = "Chin ...

  6. 171. Excel Sheet Column Number(简单数学题)

    Given a column title as appear in an Excel sheet, return its corresponding column number. For exampl ...

  7. EOJ Monthly 2018.11 猜价格 (模拟)

    分三种情况: 1.k=1.此时每次都说反话,反着二分即可. 2.1<k <= n.那么在前n次问答中一定会出现一次错误,通过不断输出1找出那个错误发生的位置(若回答是>那这就是错误) ...

  8. Visual Studio 2013旗舰版KEY

    Visual Studio 2013旗舰版KEY:BWG7X-J98B3-W34RT-33B3R-JVYW9

  9. linux常用命令:head 命令

    head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 1.命令格式: hea ...

  10. python函数里面,一个*是可变参数的元祖,两个*是可变参数的字典

    python的函数中,有时会有类似*args,**keys这样的参数,代表的是可变参数,一个*表示元祖,两个*表示字典,就是说这个函数可以接受任何类型的参数,都不会报错,有些函数为了提高可用性,会加这 ...