美文网首页Java面试@IT·互联网JavaEE 学习专题
mybatis实战教程(三)-实现数据增删改查(CRUD)

mybatis实战教程(三)-实现数据增删改查(CRUD)

作者: StrongZhao | 来源:发表于2017-05-10 23:51 被阅读274次

  前面我们讲了如何搭建mybatis开发环境以及采用接口式编程,基于接口式编程的基础上来讲一下如何通过mybatis来实现数据的增删改查操作。步骤如下:

操作步骤

1.在ActivityDao接口中分别定义insert、destroy、update、selectById四种方法。代码如下:

package com.zhq.dao;

import java.util.List;

import com.zhq.pojo.Activity;

public interface ActivityDao {
    
    List<Activity> selectByAll();
    Activity selectById(int id);
    void insert(Activity activity);
    void update(Activity activity);
    void destroy(int id);
}

2.在ActivityMappper配置文件中分别增加以下代码:

<select id="selectById" parameterType="int" resultType="Activity">
   select * from activity where id = #{id};
</select>
    
<update id="update" parameterType="Activity">
   update activity set title=#{title},description=#{note},imgPath=#{imgPath},start_time=#{startTime},end_time=#{endTime};   
</update>
    
<delete id="destroy" parameterType="int">
    delete from activity where id = #{id};s
</delete>
    
<insert id="insert" parameterType="Activity">
   insert into activity set title=#{title},description=#{note},imgPath=#{imgPath},start_time=#{startTime},end_time=#{endTime};
</insert>

最终效果如下:

<?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.zhq.dao.ActivityDao">
    <select id="selectAll" resultType="Activity">
        select * from activity;
    </select>
    <select id="selectById" parameterType="int" resultType="Activity">
        select * from activity where id = #{id};
    </select>
    
    <update id="update" parameterType="Activity">
        update activity set title=#{title},description=#{note},imgPath=#{imgPath},start_time=#{startTime},end_time=#{endTime};  
    </update>
    
    <delete id="destroy" parameterType="int">
        delete from activity where id = #{id};
    </delete>
    
    <insert id="insert" parameterType="Activity">
        insert into activity set title=#{title},description=#{note},imgPath=#{imgPath},start_time=#{startTime},end_time=#{endTime};
    </insert>
</mapper>

ps:mapper、select、update、delete、insert标签属性设置请参考:mybatisXML映射文件

3.在ActivityTest测试类中增加四个测试方法:insert()、update()、delete()、select()。代码如下:

package com.zhq.test;

import java.io.IOException;
import java.io.Reader;
import java.util.Date;

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 org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.zhq.dao.ActivityDao;
import com.zhq.pojo.Activity;

public class ActivityTest {
    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;

    
    @Before
    public void setUp() throws Exception {
        try {
            reader = Resources.getResourceAsReader("mybatis_config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void select() {
        SqlSession session = sqlSessionFactory.openSession();
//      Activity activity = session.selectOne("com.zhq.dao.ActivityDao.selectById",10);
        ActivityDao activityDao = session.getMapper(ActivityDao.class);
        Activity activity = activityDao.selectById(10);
        session.commit();
        System.out.println(activity);
        Assert.assertNotEquals("查找成功", "爱美丽", activity.getTitle());
        session.close();
    }
    
    @Test
    public void insert(){
        SqlSession session = sqlSessionFactory.openSession();
        ActivityDao activityDao = session.getMapper(ActivityDao.class);
        Activity activity = new Activity();
        activity.setTitle("耐克大促销");
        activity.setNote("耐克大促销");
        activity.setImgPath("www.zhq.com/91");
        activity.setStartTime(new Date());
        activity.setEndTime(new Date());
        activityDao.insert(activity);;
        session.commit();
        session.close();
    }
    
    @Test
    public void update(){
        SqlSession session = sqlSessionFactory.openSession();
        ActivityDao activityDao = session.getMapper(ActivityDao.class);
        Activity activity = activityDao.selectById(10);
        activity.setId(10);
        activity.setTitle("爱打游戏");
        activity.setNote("爱打游戏");
        activity.setStartTime(new Date());
        activity.setEndTime(new Date());
        activityDao.update(activity);
        session.commit();
        session.close();
    }
    
    @Test
    public void delete(){
        SqlSession session = sqlSessionFactory.openSession();
        ActivityDao activityDao = session.getMapper(ActivityDao.class);
        activityDao.destroy(10);;
        session.commit();
        session.close();
    }

}

4.运行测试代码一路绿灯完成

附源码

代码地址:https://github.com/DerryZhao/mybatisLearn

相关文章

网友评论

  • 尉昌达:日期类型的字段这样都查不出来的吧,全为null,大哥
    StrongZhao:@尉昌达 你说呢?代码拿下来自己跑一下不就知道了么
    尉昌达:@StrongZhao 用事实说话啊,你有试过吗
    StrongZhao:@尉昌达 看我SQL语句都有设置默认值

本文标题:mybatis实战教程(三)-实现数据增删改查(CRUD)

本文链接:https://www.haomeiwen.com/subject/jeentxtx.html