笔记51 Mybatis快速入门(二)
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快速入门(二)的更多相关文章
- MyBatis学习笔记(一)——MyBatis快速入门
转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4261895.html 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优 ...
- 笔记56 Mybatis快速入门(七)
相关概念介绍(二) 6.一级缓存 <1>在一个session里查询相同id的数据 package mybatis.annotation; import java.io.IOExceptio ...
- 笔记50 Mybatis快速入门(一)
一.Mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...
- 笔记52 Mybatis快速入门(三)
一.更多查询 1.模糊查询 修改Category.xml,提供listCategoryByName查询语句select * from category where name like concat(' ...
- mybatis快速入门(二)
这次接着上次写增删改查吧. 现将上节的方法改造一下,改造测试类. package cn.my.test; import java.io.IOException; import java.io.Inpu ...
- 笔记55 Mybatis快速入门(六)
相关概念介绍(一) 1.日志 有时候需要打印日志,知道mybatis执行了什么样的SQL语句,以便进行调试.这时,就需要开启日志,而mybatis自身是没有带日志的,使用的都是第三方日志,这里介绍如何 ...
- 笔记54 Mybatis快速入门(五)
Mybatis中注解的使用 1.XML方式的CRUD 新增加接口CategoryMapper ,并在接口中声明的方法上,加上注解对比配置文件Category.xml,其实就是把SQL语句从XML挪到了 ...
- 笔记53 Mybatis快速入门(四)
动态SQL 1.if 假设需要对Product执行两条sql语句,一个是查询所有,一个是根据名称模糊查询.那么按照现在的方式,必须提供两条sql语句:listProduct和listProductBy ...
- MyBatis学习总结(一)——MyBatis快速入门
一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
随机推荐
- 十次艳遇单例设计模式(Singleton Pattern)
1.引言 单例设计模式(Singleton Pattern)是最简单且常见的设计模式之一,在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访 ...
- Java中的session详解
一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...
- 配置基于python的VIM环境
配置基于python的VIM环境 安装插件管理工具 为防止过多插件管理的麻烦,首先安装vim的插件管理工具Vundle.vundle本身的github软件已经有相关的中文文档,地址如下: vundle ...
- 【JS学习】慕课网8-17编程练习 网页的返回与跳转
编程练习 制作一个跳转提示页面: 要求: 1. 如果打开该页面后,如果不做任何操作则5秒后自动跳转到一个新的地址,如慕课网主页. 2. 如果点击“返回”按钮则返回前一个页面. 代码如下: 需要注意的是 ...
- JavaScript常见设计模式梳理
单例模式 单例模式,顾名思义就是保证每个类都只有一个实例对象. 其实现思路很简单,先判断实例是否存在,如果不存在则创建新的实例返回,如果存在则直接返回该实例. 策略模式 策略模式可以理解为:封装多个可 ...
- Spring data JPA 快速入门
1需求 向客户中插入一条数据 如果使用Jpa框架可以不用先建表 可以使用框架生成表 2 实现步骤 a 创建工程 使用maven管理工程 <properties> ...
- javascript的简洁的写法
每种语言都有它特别的地方,对于JavaScript来说,使用var就可以声明任意类型的变量,这门脚本语言看起来很简单,然而想要写出优雅的代码却是需要不断积累经验的.本文利列举了JavaScript初学 ...
- RichView
TRichView中文文档 TRichView 是Delphi/C++Builder 控件,主要用于显示.编辑和打印超文本文档. 新版本解决多个兼容性问题,更新了字符串标签.剪贴板.RTF和DB组件 ...
- 剑指offer——数组中出现次数超过一半的数字(c++)
题目描述数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2.如 ...
- 【系统安全性】四、认证Authentication
四.认证Authentication 1.为什么要认证 对请求.数据进行认证,判断伪造的数据 HTTP请求很脆弱,抓包软件很强大,容易伪造身份,非法获取数据 2.摘要认证 对象:客户端参数.服务端响应 ...