mybatis学习(32):删除操作

本文主要介绍了在Mybatis中进行删除操作的详细步骤,包括涉及的目录结构、Mapper类、Mapper XML文件、Pojo类以及MybatisUtil工具类的使用。通过单元测试展示了具体的操作流程,并提供了项目的下载链接供读者实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

目录结构

com.geyao.mybatis.mapper

BlogMapper类

package com.geyao.mybatis.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;

import com.geyao.mybatis.pojo.Blog;

public interface BlogMapper {
	Blog selectBlog(Integer id);
	
	Blog selectBlog2(Integer id);
	
	List<Blog> selectBlogByTitle(String title);
	
	List<Blog> selectBlogByTitle2(String title);
	
	List<Blog> selectBlogBySort(String column);
	
	List<Blog> selectBlogByPage(int offset,int pagesize);
	
	List<Blog> selectBlogByPage1(@Param(value="offset")int offset,@Param(value="pagesize")int pagesize);
	
	List<Blog> selectBlogByPage2(Map<String, Object>map);
	
	int insertBlog(Blog blog);
	
	int insertBlogMysql(Blog blog);
	
	int updateBlog(Blog blog);
	
	int deleteBlogById(Integer id);
}

BlogMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)
 -->
<mapper namespace="com.geyao.mybatis.mapper.BlogMapper">
    <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复
    使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型
    resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回
    User类就是users表所对应的实体类
    -->
    <!-- 
        根据id查询得到一个user对象
     -->
     <resultMap type="Blog" id="blogResultMap">
     <id column="id" property="id" jdbcType="INTEGER"></id>
     	<result column="authod_id" property="authodId" jdbcType="INTEGER"/>
     </resultMap>
     
    <select id="selectBlog" parameterType="int"   resultType="Blog">
        select 
        id,
        title,
        authod_id as authodId,
        state,
        featured,
        style
         from Blog where id=#{id}
    </select> 
    
    <select id="selectBlog2" parameterType="int"  resultMap="blogResultMap">
          select *
         from Blog where id=#{id}
    </select>
    
    <select id="selectBlogByTitle" parameterType="String" resultMap="blogResultMap">
    	select * from Blog where title like #{title}
    </select>
    
    <select id="selectBlogByTitle2" parameterType="String" resultMap="blogResultMap">
    	select * from Blog where title like '${value}'
    </select>
    
     <select id="selectBlogBySort" parameterType="String" resultMap="blogResultMap">
    	select * from Blog order by ${value}
    </select>
    
      <select id="selectBlogByPage"  resultMap="blogResultMap">
    	select * from Blog limit #{0},#{1}
    </select>
    
     <select id="selectBlogByPage1"  resultMap="blogResultMap">
    	select * from Blog limit #{offset},#{pagesize}
    </select>
     <select id="selectBlogByPage2"  resultMap="blogResultMap">
    	select * from Blog limit #{offset},#{pagesize}
    </select>
    
     <insert id="insertBlog" parameterType="Blog" useGeneratedKeys="true" keyProperty="id">
    	INSERT INTO Blog(
    
    	title,
        authod_id,
        state,
        featured,
        style
    	)
    	VALUES(
    
    	#{title},
    	#{authodId},
    	#{state},
    	#{featured},
    	#{style}
    	)
    </insert>
     <insert id="insertBlogOracle" parameterType="Blog" >
     <selectKey resultType="java.lang.Integer" order="BEFORE" keyProperty="id">
     	select seq.nextval as id from dual
     </selectKey>
    	INSERT INTO Blog(
    
    	title,
        authod_id,
        state,
        featured,
        style
    	)
    	VALUES(
    
    	#{title},
    	#{authodId},
    	#{state},
    	#{featured},
    	#{style}
    	)
    </insert>
    
    
     <insert id="insertBlogMysql" parameterType="Blog" >
     <selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
     	SELECT LAST_INSERT_ID()
     </selectKey>
    	INSERT INTO Blog(
    
    	title,
        authod_id,
        state,
        featured,
        style
    	)
    	VALUES(
    
    	#{title},
    	#{authodId},
    	#{state},
    	#{featured},
    	#{style}
    	)
    </insert>
    <update id="updateBlog" parameterType="Blog" >
    UPDATE 
    Blog
    SET
    title = #{title},
   authod_id = #{authodId},
    state = #{state},
    featured = #{featured},
style= #{ style}
    WHERE id = #{id}
    </update>
    <delete id="deleteBlogById" parameterType="int">
    	delete from blog where id =#{id}
    </delete>
</mapper>

com.geyao.mybatis.pojo

Blog类

package com.geyao.mybatis.po
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

前端小歌谣

放弃很容易 但是坚持一定很酷

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

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

打赏作者

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

抵扣说明:

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

余额充值