Mybatis的CRUD

1.修改配置文件Category.xml,提供CRUD对应的sql语句。

 <?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="mybatis.pojo">
<insert id="addCategory" parameterType="Category">
insert into category ( name ) values (#{name})
</insert> <delete id="deleteCategory" parameterType="Category">
delete from category where id= #{id}
</delete> <select id="getCategory" parameterType="_int" resultType="Category">
select * from category where id= #{id}
</select> <update id="updateCategory" parameterType="Category">
update category set name=#{name} where id=#{id}
</update>
<select id="listCategory" resultType="Category">
select * from category
</select>
</mapper>

2.增加(C)

向数据库中插入10条数据

     private void Myinset() throws IOException {
create();
for (int i = 0; i < 10; i++) {
Category category = new Category();
category.setName("xiaomi" + i);
session.insert("addCategory", category);
}
listAll(session);
close();
}

通过session.insert调用addCategory对应的SQL语句,addCategory对应的插入sql语句,#{name}会自动获取category对象的name属性值

 <insert id="addCategory" parameterType="Category">
insert into category ( name ) values (#{name})
</insert>

3.删除(D)

删除id=6的对象

     private void Mydelete() throws IOException {
create();
Category category = new Category();
category.setId(6);
session.delete("deleteCategory", category);
listAll(session);
close();
}

通过session.delete调用deleteCategory对应的SQL语句,deleteCategory对应删除sql语句,#{id}会获取category对象的id。

 <delete id="deleteCategory" parameterType="Category">
delete from category where id= #{id}
</delete>

4.更新(U)

通过session.update进行修改。updateCategory对应的sql语句,#{name}和#{id}获取category对象的name和id。

     <update id="updateCategory" parameterType="Category">
update category set name=#{name} where id=#{id}
</update>
     private void Myupdate() throws IOException {
create();
Category category = session.selectOne("getCategory", 3);
category.setName("修改了的Category名字");
session.update("updateCategory", category);
listAll(session); }

5.查询(R)

<1>查询所有

     private void Allquery() throws IOException {
create();
listAll(session);
close();
}

对应的sql查询语句:

     <select id="listCategory" resultType="Category">
select * from category
</select>

<2>根据id查询

通过session.selectOne获取id=3的记录,Category category= session.selectOne("getCategory",3),getCategory对应的sql语句:

     <select id="getCategory" parameterType="_int" resultType="Category">
select * from category where id= #{id}
</select>
     private void Myquery() throws IOException {
create();
Category category = session.selectOne("getCategory", 3);
System.out.println(category.getName());
close();
}

testCRUD.java

 package mybatis.test;

 import java.io.IOException;
import java.io.InputStream;
import java.util.List; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import mybatis.pojo.Category; public class testCRUD { private SqlSession session;
private InputStream inputStream;
private SqlSessionFactory sqlSessionFactory; public testCRUD() throws IOException {
// TODO Auto-generated constructor stub
String resource = "mybatis-config.xml";
this.inputStream = org.apache.ibatis.io.Resources.getResourceAsStream(resource);
this.sqlSessionFactory = new SqlSessionFactoryBuilder().build(this.inputStream);
} public void create() throws IOException {
session = sqlSessionFactory.openSession();
} public void close() {
session.commit();
session.close();
} // 1.增加
private void Myinset() throws IOException {
create();
for (int i = 0; i < 10; i++) {
Category category = new Category();
category.setName("xiaomi" + i);
session.insert("addCategory", category);
}
listAll(session);
close();
} // 2.删除
private void Mydelete() throws IOException {
create();
Category category = new Category();
category.setId(6);
session.delete("deleteCategory", category);
listAll(session);
close();
} // 3.查询
private void Myquery() throws IOException {
create();
Category category = session.selectOne("getCategory", 3);
System.out.println(category.getName());
close();
} // 4.更新
private void Myupdate() throws IOException {
create();
Category category = session.selectOne("getCategory", 3);
category.setName("修改了的Category名字");
session.update("updateCategory", category);
listAll(session); } // 5.查询所有
private void Allquery() throws IOException {
create();
listAll(session);
close();
} // 列出所有的对象名字
private void listAll(SqlSession session) {
List<Category> cs = session.selectList("listCategory");
for (Category c : cs) {
System.out.println(c.getName());
}
} public static void main(String[] args) throws IOException {
testCRUD testCRUD = new testCRUD();
// testCRUD.Myinset();
// testCRUD.Mydelete();
// testCRUD.Myquery();
testCRUD.Myupdate();
// testCRUD.Allquery();
}
}

笔记51 Mybatis快速入门(二)的更多相关文章

  1. MyBatis学习笔记(一)——MyBatis快速入门

    转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4261895.html 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优 ...

  2. 笔记56 Mybatis快速入门(七)

    相关概念介绍(二) 6.一级缓存 <1>在一个session里查询相同id的数据 package mybatis.annotation; import java.io.IOExceptio ...

  3. 笔记50 Mybatis快速入门(一)

    一.Mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...

  4. 笔记52 Mybatis快速入门(三)

    一.更多查询 1.模糊查询 修改Category.xml,提供listCategoryByName查询语句select * from category where name like concat(' ...

  5. mybatis快速入门(二)

    这次接着上次写增删改查吧. 现将上节的方法改造一下,改造测试类. package cn.my.test; import java.io.IOException; import java.io.Inpu ...

  6. 笔记55 Mybatis快速入门(六)

    相关概念介绍(一) 1.日志 有时候需要打印日志,知道mybatis执行了什么样的SQL语句,以便进行调试.这时,就需要开启日志,而mybatis自身是没有带日志的,使用的都是第三方日志,这里介绍如何 ...

  7. 笔记54 Mybatis快速入门(五)

    Mybatis中注解的使用 1.XML方式的CRUD 新增加接口CategoryMapper ,并在接口中声明的方法上,加上注解对比配置文件Category.xml,其实就是把SQL语句从XML挪到了 ...

  8. 笔记53 Mybatis快速入门(四)

    动态SQL 1.if 假设需要对Product执行两条sql语句,一个是查询所有,一个是根据名称模糊查询.那么按照现在的方式,必须提供两条sql语句:listProduct和listProductBy ...

  9. MyBatis学习总结(一)——MyBatis快速入门

    一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...

随机推荐

  1. JavaScript翻转字符串方法

    先把字符串转化成数组String.prototype.split(),再借助数组的reverse方法翻转数组顺序(Array.prototype.reverse()),然后把数组转化成字符串. 使用的 ...

  2. Android开发环境部署:JDK+Android Studio

    1. 刚开始接触Android开发,首先需要为你的电脑安装java JDK(Java开发工具包),不管是用Eclipse还是Android Studio都需要只吃Java语言运行吧. 官网:Oracl ...

  3. 运行连接Oracle数据库时,Idea报错: Error : java 不支持发行版本5

    按照上面的截图步骤,一步步往下走,再运行程序时就不会报错了. 原文链接:https://blog.csdn.net/qq_22076345/article/details/82392236 感谢原文作 ...

  4. Delphi 方法:overload、override、virtual、dynamic、abstract

    1.overload 在Pascal语法规则中,同一个UNIT里是不能存在两个同名的函数的,例如: function func(): Boolean; function func(const x: C ...

  5. Vue学习笔记【3】——Vue指令之v-bind的三种用法

    直接使用指令v-bind 使用简化指令: 在绑定的时候,拼接绑定内容::title="btnTitle + ', 这是追加的内容'" <!DOCTYPE html> & ...

  6. 完爆 Best Fit,看阿里如何优化 Sigma 在线调度策略节约亿级成本

    摘要:2018 年“双 11”的交易额又达到了一个历史新高度 2135 亿.相比十年前,我们的交易额增长了 360 多倍,而交易峰值增长了 1200 多倍.相对应的,系统数呈现爆发式增长.系统在支撑“ ...

  7. 【LeetCode 23】合并K个排序链表

    题目链接 [题解] 会归并排序吧? 就把这K个链表当成是K个数字就好. 然后做归并排序. 因为归并排序的时候本来就会有这么一个过程. [l..mid]和[mid+1..r]这两段区间都是有序的了已经. ...

  8. 【LeetCode 17】电话号码的字母组合

    题目链接 [题解] 用回溯法搞一下. 一搞就有~ 注意输入空串的时候别返回那个空串.. [代码] class Solution { public: string dic[10]; vector< ...

  9. tarjan求强连通+缩点——cf1248E

    这题好像是DEF里最水的,, /* 建图:如果a认识b,那么从a->b连一条边,将点分成两个集合A,B,没有从A->B的边 求出强连通分量,再造一张新图,新图中任取一个的出度为0的点作为集 ...

  10. NX二次开发-Block UI C++界面关于 在Block UI中UF_initialize();和UF_terminate();的使用

    关于 在Block UI中UF_initialize();和UF_terminate();的使用 用Block UI作NX二次开发的时候,不需要在使用UFUN函数的时候加UF_initialize() ...