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());
		}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章