MyBatis的框架配置与使用2


MyBatis的框架配置与使用2





原创

2017年12月24日22:36:32

        <ul class="article_tags clearfix csdn-tracking-statistics tracking-click" data-mod="popu_377" style="display: none;">
            <li class="tit"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">标签:</font></font></li>








  小编上一次写了MyBatis的查询,这一次接着后面的增加改。

  首先我们新建一个项目,建好后如下图所示(具体细节步骤请看我的上篇文章):
这里写图片描述
  删除增:

    typeMapper.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.t.dao.typeMapper">
    <select id="selectType" resultType="com.t.entity.typeEntity">
        select * from type
        <if test="name!=null and name!=''">
            where typename like concat('%',#{name},'%')
        </if>
    </select>
    <!-- 删除 -->
    <delete id="deleteType">
        delete from type where tid = #{tid}
    </delete>
    <!-- 新增 -->
    <insert id="addType" parameterType="com.t.entity.typeEntity">
        insert into type values(null,#{typename},#{ftid})
    </insert>
    <!-- 修改 -->
    <update id="updateType"  parameterType="com.t.entity.typeEntity">
        update type set where tid=#{tid} typename=#{typename},ftid=#{ftid}
    </update>
</mapper>
 
 
  • 1
  • 2
  • 3
  • 4
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

    typeMapper.java代码如下所示:

package com.t.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.t.entity.typeEntity;
public interface typeMapper {
    //查询
    public List<typeEntity> selectType(@Param("name") String name);
    //删除
    public int deleteType(@Param("tid")int tid);
    //新增
    public int addType(typeEntity t);
    //修改
    public int updateType(typeEntity t);
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

  数据库如下图所示:
这里写图片描述
  测试代码如下所示:

package com.t.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.t.dao.typeMapper;
import com.t.entity.typeEntity;
import com.t.util.MyBatisUtil;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        //查询
        List<typeEntity> tlist = MyBatisUtil.getSqlSession().getMapper(typeMapper.class).selectType("");
        for (typeEntity t : tlist) {
            System.out.println(t.getTypename());
        }
        //删除
        //获取Session
        SqlSession session = MyBatisUtil.getSqlSession();
        //调用删除接口
        int del =session.getMapper(typeMapper.class).deleteType(4);
        //如果del>0就提交事务,小于0就回滚事务
        if(del>0){
            session.commit();
            System.out.println("删除成功");
        }else{
            session.rollback();
        }
        //新增
        //创建一个新的对象
        typeEntity t = new typeEntity();
        //赋值
        t.setFtid(2);
        t.setTypename("aaa");
        int add = session.getMapper(typeMapper.class).addType(t);
        //如果add>0就提交事务,小于0就回滚事务
        if(add>0){
            session.commit();
            System.out.println("新增成功");
        }else{
            session.rollback();
        }
        //修改
        //创建一个新的对象
        typeEntity t1 = new typeEntity();
        //赋值
        t1.setTid(1);
        t1.setFtid(10);
        t1.setTypename("小编");
        int upd = session.getMapper(typeMapper.class).updType(t1);
        //如果upd>0就提交事务,小于0就回滚事务
        if(upd>0){
            session.commit();
            System.out.println("修改成功");
        }else{
            session.rollback();
        }
        MyBatisUtil.closeSqlSession(session);
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 三十
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

  运行效果:

把TID为4的记录删除,新增了一行记录,修改了TID为1的记录。
  运行后数据库如下图所示:
这里写图片描述

  这就是一个简单的控制台的增删了。





$(".MathJax").remove();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值