八、持久层框架(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文件, ...
随机推荐
- 【译】第9节---EF Code First中数据注解
原文:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF Code-First ...
- FI 业务
f-02 post f-03 clear[account]-> f-04 post with clear fb70/f-22 f-32 clear[account]->f-28 post ...
- HDU 4825 Xor Sum(01字典树入门题)
http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...
- C++ 空字符('\0')和空格符(' ')
1.从字符串的长度:-->空字符的长度为0,空格符的长度为1. 2.虽然输出到屏幕是一样的,但是本质的ascii code 是不一样的,他们还是有区别的. #include<iostrea ...
- Centos7 linux下通过源码安装redis以及使用
下载redis安装包 wget http://download.redis.io/releases/redis-5.0.3.tar.gz 解压压缩包 tar -zxvf redis-.tar.gz y ...
- SQL关于WHERE 的计算次序
WHERE可包含任意数目的AND和OR操作符.允许两者结合以进行复杂 和高级的过滤. 但是OR和AND操作符是有先后次序的. 比如,原意是想找出 3班和5班年龄为21岁的同学,使用 :SELECT * ...
- Java String常用的两个方法
- Ajax请求导出Excel的问题
文章转载自: http://yuwenlin.iteye.com/blog/2275289 Ajax请求导出Excel的问题描述: 前端发起Ajax请求get或post,后台使用Poi生成excel文 ...
- 手工生成AWR报告方法记录
AWR(Automatic Workload Repository)报告是我们进行日常数据库性能评定.问题SQL发现的重要手段.熟练掌握AWR报告,是做好开发.运维DBA工作的重要基本功. AWR报告 ...
- appium --log-timestamp > appium.log
appium --log-timestamp > appium.log