八、持久层框架(MyBatis)
一、基于MyBatis的CRUD
1、首先是配置文件Category.xml修改
一次性修改配置文件Category.xml,提供CRUD对应的sql语句。
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.rog//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.demo.pojo">
<insert id="addCategory" parameter Type="Category">
insert into category_table (name) values (#{name})
</insert>
<delete id="deleteCategory" parameter Type="Category">
delete from category_table where id=#{id}
</delete>
<select id="getCategory" parameter Type="Category">
select * from category_table where id=#{id}
</select>
<update id="updateCategory" parameter Type="Category">
update category_table set name=#{name} where id=#{id}
</update>
<select id="listCategory" resultType="Category">
select * from category_table
</select>
</mapper>
2、增加(insert)
通过session.insert()方法调用addCategory对应的SQL语句
<insert id="addCategory" parameter Type="Category">
insert into category_table (name) values (#{name})
</insert>
Category c=new Category();
c.setName("插入一条Category数据");
session.insert("addCategory",c);
3、删除(delete)
通过session.delete()方法调用deleteCategory对应的SQL语句
<delete id="deleteCategory" parameterType="Category">
delete from category_table where id=#{id}
</delete>
package com.demo.java; import java.io.IOException;
import java.io.InputStream;
import java.uitl.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.demo.pojo.Category; public class TestMyBatis{
public static void main(String[] args) throws IOException{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder();.build(inputStream);
SqlSession session=sqlSessionFactory.openSession(); Category c=new Category();
c.setId(6);
session.delete("deleteCategory",c); listAll(session); session.commit();
session.close();
} private static void listAll(SqlSession session){
List<Category> list=session.selectList("listCategory");
for(Category c:cs){
System.out.println(c.getName());
}
} }
4、修改(update)
通过session.update()方法调用updateCategory对应的SQL语句
<update id="updateCategory" parameterType="Category">
update category_table set name=#{name} where id=#{id}
</update>
package com.demo.java; import java.io.IOException;
import java.io.InputStream;
import java.uitl.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.demo.pojo.Category; public class TestMyBatis{
public static void main(String[] args) throws IOException{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder();.build(inputStream);
SqlSession session=sqlSessionFactory.openSession(); Category c=session.selectOne("getCategory",3);
c.setName("修改了Category名称");
session.update("updateCategory",c); listAll(session); session.commit();
session.close();
} private static void listAll(SqlSession session){
List<Category> list=session.selectList("listCategory");
for(Category c:cs){
System.out.println(c.getName());
}
} }
5、模糊查询(like)
修改Category.xml,提供listCategoryByName查询语句
mysql的写法是如下:利用concat('%',#{0},'%')
<select id="listCategoryByName" parmaeterType="string" resultType="Category">
select * from category_table where name like concat('%',#{0},'%')
</select>
oracle的写法如下:'%'||#{0}||'%'
select * from category_table where name like '%'||#{0}||'%'
<?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="com.how2java.pojo">
<select id="listCategoryByName" parameterType="string" resultType="Category">
select * from category_ where name like concat('%',#{0},'%')
</select>
</mapper>
测试代码如下:
package com.demo.java; import java.io.IOException;
import java.io.InputStream;
import java.uitl.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.demo.pojo.Category; public class TestMyBatis{
public static void main(String[] args) throws IOException{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder();.build(inputStream);
SqlSession session=sqlSessionFactory.openSession(); List<Category> cs=session.selectList("listCategoryByName","hello");
for(Category c:cs){
system.out.println(c.getName());
} session.commit();
session.close();
} }
6、多条件查询
同样还是修改Category.xml配置文件
<select id="listCategoryByIdAndName" parameterType="map" resultType="Category">
select * from category_table where id>#{id} and name like concat('%',#{name},'%')
</select>
因为有多个参数,而selectList方法只能结构一个参数对象,所以需要把多个参数放在map里,然后把这个map对象作为参数传递进去
Map<String,Object> params=new HashMap<>():
params.put("id",3);
params.put("name","hello");
List<Category> cs=session.selectList("listCategoryByIdAndName",params);
package com.demo.java; import java.io.IOException;
import java.io.InputStream;
import java.uitl.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.demo.pojo.Category; public class TestMyBatis{
public static void main(String[] args) throws IOException{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder();.build(inputStream);
SqlSession session=sqlSessionFactory.openSession(); Map<String,Object> params=new HashMap<>();
params.put("id",3);
params.put("name","cat");
List<Category> cs=session.selectList("listCategoryByIdAndName",params);
for(Category c:cs){
system.out.println(c.getName());
} session.commit();
session.close();
} }
八、持久层框架(MyBatis)的更多相关文章
- 从零搭建springboot服务02-内嵌持久层框架Mybatis
愿历尽千帆,归来仍是少年 内嵌持久层框架Mybatis 1.所需依赖 <!-- Mysql驱动包 --> <dependency> <groupId>mysql&l ...
- Java数据持久层框架 MyBatis之背景知识二
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- java持久层框架mybatis如何防止sql注入
看到一篇很好的文章:http://www.jfox.info/ava-persistence-framework-mybatis-how-to-prevent-sql-injection sql注入大 ...
- Java数据持久层框架 MyBatis之背景知识三
摘录自:http://www.cnblogs.com/lcngu/p/5437281.html 对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.or ...
- Java数据持久层框架 MyBatis之API学习一(简介)
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- Java数据持久层框架 MyBatis之背景知识一
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- 开源顶级持久层框架——mybatis(ibatis)——day02
mybatis第二天 高级映射 查询缓存 和spring整合 课程复习: mybatis是什么? mybatis是一个持久层框架,mybatis ...
- Java持久层框架Mybatis入门
MyBatis是什么 MyBatis是Java的持久层框架,GitHub的star数高达15.8k,是Java技术栈中最热门的ORM框架之一.它支持自定义SQL.存储过程以及高级映射,可以通过XML或 ...
- 开源顶级持久层框架——mybatis(ibatis)——day01
mybatis-day01 1.对原生态jdbc程序中的问题总结 1.1环境 java环境:jdk eclipse:indigo ...
- 整合使用持久层框架mybatis 使用SqlSessionTemplate模板类与使用映射接口 对比
spring中整合使用mybatis的用法总结 一:在Spring配置Mybatis 第一步:将mybatis-spring类包添加 到项目的类库中 第二步:编写spring和持久层衔接的xml文件, ...
随机推荐
- mysql行转列(多行转一列)
场景 比如说一个订单对应多条数据,当状态(status)=1的时候, 数量(num)=25,当状态(status)=2的时候, 数量(num)=45,现在想用一条sql记录下不同状态对应的数量为多 ...
- gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now
[root@Gris- FMIS2600bak]# tar -zxvf /home/oradata/FMIS2600DMP.tar.gz gzip: stdin: not in gzip format ...
- python第二章(2)列表
names=["zhangyang","guyun","xiangpeng","leiming","xulia ...
- DAY3 数据类型与运算符
一.注释 代码注释分单行和多行注释, 单行注释用#,多行注释可以用三对双引号""" """ 注释用于解释某一行代码的作用,增加代码的可读性 ...
- dockerfile debian 和pip使用国内源
python官方镜像是基于debian的.国内使用时定制一下,加快下载速度. 1 debian本身使用国内源 dockfile中: #国内debian源 ADD sources.list /etc/a ...
- Codeforces 458C - Elections
458C - Elections 思路: 三分凹形函数极小值域 代码: #include<bits/stdc++.h> using namespace std; #define ll lo ...
- vue 上传单个图片自定义增加progress改良用户体验
<el-tab-pane label="开发商logo" name="first" style="position: relative;&quo ...
- tchart example
Random random = new Random(); // Color SeriesColor; int SeriesIndex=0; tChart1.Series.Clear(); Steem ...
- 雷林鹏分享:jQuery EasyUI 表单 - 表单验证
jQuery EasyUI 表单 - 表单验证 本教程将向您展示如何验证一个表单.easyui 框架提供一个 validatebox 插件来验证一个表单.在本教程中,我们将创建一个联系表单,并应用 v ...
- 雷林鹏分享:C# 方法
C# 方法 一个方法是把一些相关的语句组织在一起,用来执行一个任务的语句块.每一个 C# 程序至少有一个带有 Main 方法的类. 要使用一个方法,您需要: 定义方法 调用方法 C# 中定义方法 当定 ...