首先解释一下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.propertiesdriver=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 blocke.printStackTrace();}}//Cpublic  void insertPerson(Person person) throws SQLException {sqlMapClient.insert("insertPerson", person);}//Rpublic Person queryPersonById(int id) throws SQLException {return (Person)sqlMapClient.queryForObject("queryPersonById", id);}@SuppressWarnings("unchecked")public List<Person> queryALl() throws SQLException {return sqlMapClient.queryForList("queryAll");}//Upublic void updatePersonById(Person person) throws SQLException {sqlMapClient.update("updatePersonById", person);}//Dpublic 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 blocke.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 blocke.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 blocke.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 blocke.printStackTrace();}*///Dtry {new PersonDao().deletePersonById(3);System.out.println("删除成功。。");} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}

  

转载于:https://www.cnblogs.com/E-star/p/3392699.html

iBatis的基本配置+CRUD操作相关推荐

  1. 用于MyBatis CRUD操作的Spring MVC 3控制器

    到目前为止,我们已经为域类" User "创建了CRUD数据库服务,并且还将MyBatis配置与Spring Configuration文件集成在一起. 接下来,我们将使用Spri ...

  2. 二、CRUD操作以及配置解析

    二.CRUD操作以及配置解析 1.在原来基础上加上增加.删除和更改 在UserDao的接口中 /*** 根据id查询用户*/User getUserById(int id);/*** 增加一个用户*/ ...

  3. MyBatis02:CRUD操作和配置解析

    MyBatis02:CRUD操作和配置解析 CRUD select 根据id查询用户 接口方法 public interface UserMapper {//查询所有用户List<User> ...

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

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

  5. mybatis mysql crud_Mybatis实现CRUD操作

    Mybatis实现CRUD操作 导入相关依赖 创建实体类User 创建Mybatis主配置文件:SqlMapConfig.xml 创建IUserDao接口. 创建于接口对应的映射配置文件:IUserD ...

  6. mybatis crud_MyBatis教程– CRUD操作和映射关系–第1部分

    mybatis crud CRUD操作 MyBatis是一个SQL Mapper工具,与直接使用JDBC相比,它极大地简化了数据库编程. 步骤1:创建一个Maven项目并配置MyBatis依赖项. & ...

  7. MyBatis教程– CRUD操作和映射关系–第1部分

    CRUD操作 MyBatis是一个SQL Mapper工具,与直接使用JDBC相比,它极大地简化了数据库编程. 步骤1:创建一个Maven项目并配置MyBatis依赖项. <project xm ...

  8. MyBatis学习存档(4)——进行CRUD操作

    使用MyBatis进行数据库的CRUD操作有2种方式:一种如之前所说的接口+xml,而另一种是通过对接口上的方法加注解(@Select @Insert @Delete @Update) 但是通常情况下 ...

  9. Ibator生成iBATIS配置文件 DO及DAO操作记录

    Ibator是iBATIS的代码发生器, Ibator可以生成一个数据库中的一个表(或多个表)的DAO层.DO层及符合iBATIS规范的配置,它减少了我们编写配置文件.创建DO及DAO的工作量,并且可 ...

最新文章

  1. 【python】使用python脚本将CelebA中图片按照 list_attr_celeba.txt 中属性处理(删除、复制、移动)
  2. java 支持 shards 的jar_Hibernate Shards 数据的水平、垂直切割(一)- Hibernate测试环境...
  3. 空调系统故障类型与故障案例集
  4. OpenCV立体声匹配 stereo matching将L和R图像转换为视差和点云的实例(附完整代码)
  5. hibernate 中id生成策略
  6. bs4爬取的时候有两个标签相同_PYTHON爬取数据储存到excel
  7. [Java] 蓝桥杯ADV-155 算法提高 上帝造题五分钟
  8. 2021,我的年终总结......
  9. Atitit.git的存储结构and 追踪
  10. windows下 gcc 下载及使用指南
  11. obs点开始推流显示无法连接服务器,前沿科技资讯:OBS Studio推流连接失败如何办 OBS推流失败的正确解决方法...
  12. 好好说话之Tcache Attack(1):tcache基础与tcache poisoning
  13. 采用FPGA开发高清相机sensorISP芯片要点分析
  14. “飞天”就是一个操作系统,最重要的功能就是资源管理;这套系统简单说就是把所有资源抽象成一台计算机,并通过互联网提供计算服务。...
  15. Android 磁场传感器 地磁倾角计算 SensorManager.getInclination方法
  16. 【游戏开发实战】Unity快速搭建体素风格关卡地图(Tile3D | 我的世界 | Voxel | 场景 | 编辑器)
  17. i春秋-2016-2017年信息安全竞赛 Web writeup 补题 By Assassin
  18. ONLYOFFICE文档V7.2现已发布————插件市场、实时查看器、连写、全新表单字段、UI 更新等
  19. STM32WL无线Lora
  20. typeorm中文网【TS】你们要的TypeORM中文文档Ta来了

热门文章

  1. python 检测直线 交点_Python+OpenCV图像处理——实现直线检测
  2. Exchange 日志/存储路径
  3. 云原生时代,Java 的危与机
  4. 消息中间件学习总结(20)——主流MQ比较及MQ常见使用场景总结
  5. 信贷系统学习总结(2)——现金贷之借贷模式与前端产品
  6. Java基础学习总结(90)——Java单元测试技巧
  7. 儒林外史每回概括简短10字_冬至祝福语简短10字左右 ,冬至祝福语简短精悍一句话...
  8. 用docker swarm 实现集群
  9. 网友建站经验——(转载)
  10. 灵活控制 Hibernate 的日志或 SQL 输出,以便于诊断