一、基于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)的更多相关文章

  1. 从零搭建springboot服务02-内嵌持久层框架Mybatis

    愿历尽千帆,归来仍是少年 内嵌持久层框架Mybatis 1.所需依赖 <!-- Mysql驱动包 --> <dependency> <groupId>mysql&l ...

  2. Java数据持久层框架 MyBatis之背景知识二

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  3. java持久层框架mybatis如何防止sql注入

    看到一篇很好的文章:http://www.jfox.info/ava-persistence-framework-mybatis-how-to-prevent-sql-injection sql注入大 ...

  4. Java数据持久层框架 MyBatis之背景知识三

    摘录自:http://www.cnblogs.com/lcngu/p/5437281.html 对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.or ...

  5. Java数据持久层框架 MyBatis之API学习一(简介)

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  6. Java数据持久层框架 MyBatis之背景知识一

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  7. 开源顶级持久层框架——mybatis(ibatis)——day02

    mybatis第二天    高级映射 查询缓存 和spring整合          课程复习:         mybatis是什么?         mybatis是一个持久层框架,mybatis ...

  8. Java持久层框架Mybatis入门

    MyBatis是什么 MyBatis是Java的持久层框架,GitHub的star数高达15.8k,是Java技术栈中最热门的ORM框架之一.它支持自定义SQL.存储过程以及高级映射,可以通过XML或 ...

  9. 开源顶级持久层框架——mybatis(ibatis)——day01

    mybatis-day01     1.对原生态jdbc程序中的问题总结         1.1环境             java环境:jdk             eclipse:indigo ...

  10. 整合使用持久层框架mybatis 使用SqlSessionTemplate模板类与使用映射接口 对比

    spring中整合使用mybatis的用法总结 一:在Spring配置Mybatis 第一步:将mybatis-spring类包添加 到项目的类库中 第二步:编写spring和持久层衔接的xml文件, ...

随机推荐

  1. Common-io,FileUtils工具类的使用

    package Cristin.Common.File; import org.apache.commons.io.FileUtils; import org.apache.commons.io.fi ...

  2. 用Github做一个静态网页(GithubPages)

    一.新建一个仓库(new). 二.命名Repository name为:(名字).github.io(一定要有.github.io). 三.勾选Initialize this repository w ...

  3. @Value("#{}")与@Value("${}")的区别以及用法

    package com.ieou.capsule_basic_info.util; import org.springframework.beans.factory.annotation.Value; ...

  4. Spring boot2.0 与 2.0以前版本 跨域配置的区别

    一·简介 spring boot升级到2.0后发现继承WebMvcConfigurerAdapter实现跨域过时了,那我们就紧随潮流. 二·全局配置 2.0以前 支持跨域请求代码: import or ...

  5. Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA

    解决方法: 如果安装的是GPU版本 如果你有一个GPU,你不应该关心AVX的支持,因为大多数昂贵的操作将被分派到一个GPU设备上(除非明确地设置).在这种情况下,您可以简单地忽略此警告: import ...

  6. python shelve模块

    #coding=utf- import shelve f = shelve.open("shelve_test") f['info'] = "alex" f[, ...

  7. HDUOJ 不容易系列之(4)——考新郎

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2049 一开始我的想法就是使用错排公式,先使用全排列从N对中选出M对,然后再使用错排对选出的M对进行错排计 ...

  8. anaconda3 安装opencv3.4.2 cuda9.2 mint19(ubuntu 18.04)

    从opencv1的时代,编译这玩意就不是太轻松.之前都是在win下.2.x时代,开始用cmake GUI,选vs版本,x86 x64 各种依赖库选项,debug release,... 现在3.4了, ...

  9. Lua面向对象之三:其它一些尝试

    1.尝试一:子类对象调用被覆盖了的父类函数 根据元表设置流程,我们只有将父类元表找到就能调用父类的方法了 ①在子类Circle中增加一个调用父类方法的函数 --调用父类被子类覆盖了的name方法 fu ...

  10. Golang的session管理器

    对于一些需要对用户进行管理(比如验证操作的权限等)的站点来说,session管理器是必不可少的.下面实现了一个线程安全的简单session管理类.生产环境:golang1.4.2+win7x64gol ...