Mybatis学习(二):增删查改

<?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.cn.bean">
      
        <select id="selectAll" resultType="Category">
            select * from category     
        </select>
        <insert id="addCategory" parameterType="Category">
        	insert into category(id,name) values(#{id},#{name})
        </insert>
        <delete id="deleteCategory" parameterType="Category">
        	delete from category where id = #{id}
        </delete>
        <select id="selectCategory" parameterType="int" resultType="Category">
        	select * from category where id = #{id}
        </select>
        <!-- 跟新一个Category -->
        <update id="updateCategory" parameterType="Category">
        	update category set name=#{name} where id = #{id}
        </update>
        
    </mapper>
package com.cn;

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.cn.bean.Category;

public class TestCategory {
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测试
	Category cg=new Category();
	cg.setName("new Category!!!");
	session.insert("addCategory", cg);
	//增删改需要提交
	session.commit();
	
	cg.setId(100);
	cg.setName("手动设置自增项");
	session.insert("addCategory", cg);
	session.commit();
	
	//手动设置自增项值后,从设置的自增项下一个开始
	Category newcate=new Category();
	newcate.setName("手动设置id后自增项从何处开始");
	session.insert("addCategory", newcate);
	session.commit();
	//列出结果
	listAll(session);
	
	System.out.println("-----查询测试-----");
	//查询
	Category select1=session.selectOne("selectCategory", 1);
	Category select6=session.selectOne("selectCategory",6);
	System.out.println("select1:"+select1.getId()+" "+select1.getName());
	//查询结果为空会返回空指针
//	System.out.println("select6:"+select6.getId()+" "+select6.getName());
	
	Category update=new Category();
	//更新测试
	System.out.println("-----更新测试-----");
	update.setName("更新后的值");
	//更新不存在的ID
	session.update("updateCategory", update);
	session.commit();
	
	//查看结果
	listAll(session);
	//删除
	System.out.println("-----删除测试-----");
	session.delete("deleteCategory", 1);
	session.commit();
	//删除不存在的
	session.delete("deleteCategory",6);//不执行任何操作
	listAll(session);
	session.close();
}
public static void listAll(SqlSession session)
{
	//列出所有Category
	System.out.println("-----ALL-----");
		List<Category> cs=session.selectList("selectAll");
		for(Category c:cs)
		{
			System.out.println(c.getId()+"   "+c.getName());
		}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值