Mybatis基础之增删改查(CRUD)

1、配置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">

    <mapper namespace="com.how2java.pojo">
        <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>
        <select id="listCategory" resultType="Category">
            select * from   category_     
        </select>    
    </mapper>

2、增加
测试类TestMybatis

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.setName("新增加的Category");
        session.insert("addCategory",c);

        listAll(session);

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

    }

    private static void listAll(SqlSession session) {
        List<Category> cs = session.selectList("listCategory");
        for (Category c : cs) {
            System.out.println(c.getName());
        }
    }
}
 对应:
<insert id="addCategory" parameterType="Category" >
            insert into category_ ( name ) values (#{name})   
    </insert>

3、删除

        Category c = new Category();
        c.setId(6);
        session.delete("deleteCategory",c);

        listAll(session);
   对应:
<delete id="deleteCategory" parameterType="Category" >
            delete from category_ where id= #{id}  
        </delete>

4、获取

        Category c= session.selectOne("getCategory",3);
        System.out.println(c.getName());
     对应:
<select id="getCategory" parameterType="_int" resultType="Category">
            select * from   category_  where id= #{id}   
</select>

5、修改

        Category c= session.selectOne("getCategory",3);
        c.setName("修改了的Category名稱");
        session.update("updateCategory",c);

        listAll(session);
对应:
<update id="updateCategory" parameterType="Category" >
    update category_ set name=#{name} where id=#{id}   
</update>

6、查询所有

listAll(session);

<select id="listCategory" resultType="Category">
    select * from   category_     
</select>

7、模糊查询
Category.xml

<select id="listCategoryByName"  parameterType="string" resultType="Category">
     select * from   category_  where name like concat('%',#{0},'%')
        </select> 

TestMyBatis类

List<Category> cs = session.selectList("listCategoryByName","cat");
        for (Category c : cs) {
            System.out.println(c.getName());
        }

8、多条件查询

<select id="listCategoryByIdAndName"  parameterType="map" resultType="Category">
    select * from   category_  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", "cat");

        List<Category> cs = session.selectList("listCategoryByIdAndName",params);
        for (Category c : cs) {
            System.out.println(c.getName());
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值