[小白自学]MyBatis技术(二):CRUD(创建:Create, 读取:Read,更新:Update,删除: Delete)

1.通过Mybatis进行CRUD (创建:Create, 读取:Read,更新:Update,删除: Delete)一套操作,建立在上一个知识点[小白自学]MyBatis技术(一)的基础上进行。

 

修改Category.xml

<?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">
    
 	<!-- 表示命名空间是com.how2java.pojo,在后续调用sql语句的时候,会用到它 -->
    <mapper namespace="com.how2java.pojo">
    	<!--这条sql语句用id: listCategory 进行标示以供后续代码调用。resultType="Category" 表示返回的数据和Category关联起来,这里本应该使用的是 com.how2java.pojo.Category, 但是因为上一步配置了别名,所以直接使用Category就行了  -->
        <select id="listCategory" resultType="Category">
            select * from   category_     
        </select>      
        
        <!--  CRUD (创建:Create, 读取:Read,更新:Update,删除: Delete) -->
        <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>
        
    </mapper>

2.TestMybatis.java

package com.how2java;

import java.io.IOException;
import java.io.InputStream;
import java.util.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.how2java.pojo.Category;

public class TestMybatis {
	public static void main(String[] args) throws IOException {
		// 根据配置文件mybatis-config.xml得到sqlSessionFactory 。
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		// 然后再根据sqlSessionFactory 得到session
		SqlSession session = sqlSessionFactory.openSession();

		System.out.println("-------新增-------");
		Category c = new Category();
		c.setName("this is new");
		session.insert("addCategory", c);
		listAll(session);
		session.commit();		
		System.out.println("---------删除------");
		c.setId(1);
		session.delete("deleteCategory",c);
		listAll(session);
		System.out.println("---------根据id找name------");
		c= session.selectOne("getCategory",3);
		System.out.println("查询的3为:"+c.getName());
		System.out.println("---------根据id修改name------");
		c.setName("修改了的Category名稱");
        session.update("updateCategory",c);
        listAll(session);
		session.close();
	}
	
	/**
	 * 通过session的selectList方法,调用sql语句listCategory。listCategory这个就是在配置文件Category.xml中那条sql语句设置的id。
	 * 执行完毕之后,得到一个Category集合,遍历即可看到数据。
	 * 也可以写成 session.selectList("com.how2java.pojo.listCategory"); 效果一样
	 */
	private static void listAll(SqlSession session) {
		System.out.println("-------查询所有listAll()方法-------");
		List<Category> cs = session.selectList("listCategory");
		for (Category category : cs) {
			System.out.println(category.getName());
		}
	}
}

3.运行结果

-------新增-------
-------查询所有listAll()方法-------
11
category2
t3
4
5
6
this is new
this is new
this is new
---------删除------
-------查询所有listAll()方法-------
11
category2
t3
4
5
6
this is new
this is new
this is new
---------根据id找name------
查询的3为:t3
---------根据id修改name------
-------查询所有listAll()方法-------
11
category2
修改了的Category名稱
4
5
6
this is new
this is new
this is new

 

官方的Test

package com.how2java;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.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.how2java.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();
 
        listAll(session);

        session.commit();
        session.close();
 
    }

	private static void update(SqlSession session) {
		Category c= session.selectOne("getCategory",3);
        c.setName("修改了的Category名稱");
        session.update("updateCategory",c);
         
        listAll(session);
	}

	private static void get(SqlSession session) {
		Category c= session.selectOne("getCategory",3);
        
        System.out.println(c.getName());
	}

	private static void delete(SqlSession session) {
		Category c = new Category();
        c.setId(6);
        session.delete("deleteCategory",c);
        listAll(session);
	}

	private static void add(SqlSession session) {
		Category c = new Category();
        c.setName("新增加的Category");
        session.insert("addCategory",c);
         
        listAll(session);
	}
 
    private static void listAll(SqlSession session) {
        List<Category> cs = session.selectList("listCategory");
        for (Category c : cs) {
            System.out.println(c.getName());
        }
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一身正气z

打赏随心就好

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值