myBatis-CRUD

上接入门案例(看之前的《mybatis基本介绍及入门案例》篇)

1、映射接口


java 复制代码
public interface MonsterMapper {
    //添加方法

    public void addMonster(Monster monster);
    public void delMonster(Integer monster_id);
    public  void updateMonster(Monster monster);
    public Monster findById(Integer monster_id);
    public List<Monster> findAllMonster();
}

2、映射文件

XML 复制代码
<mapper namespace="com.bin.mybatis.mapper.MonsterMapper">
    <insert id="addMonster" parameterType="com.bin.mybatis.entity.Monster">
        INSERT INTO mybatis_monster_ (monster_id,age,birthday,email,gender,name,salary)
        VALUES (#{monster_id},#{age},#{birthday},#{email},#{gender},#{name},#{salary})
    </insert>
    <delete id="delMonster" parameterType="Integer">
        DELETE FROM mybatis_monster_ WHERE monster_id=#{monster_id}
    </delete>
    <update id="updateMonster" parameterType="com.bin.mybatis.entity.Monster">
        UPDATE mybatis_monster_ SET age=#{age},birthday=#{birthday},email=#{email},gender=#{gender},name=#{name},salary=#{salary}
        WHERE monster_id=#{monster_id}
    </update>
    <select id="findById" parameterType="Integer" resultType="com.bin.mybatis.entity.Monster">
        SELECT * FROM mybatis_monster_ WHERE monster_id=#{monster_id}
    </select>
    <select id="findAllMonster" resultType="com.bin.mybatis.entity.Monster">
        SELECT * FROM mybatis_monster_
    </select>
</mapper>

3、JUNIT测试

java 复制代码
import com.bin.mybatis.entity.Monster;
import com.bin.mybatis.mapper.MonsterMapper;
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.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class testMybatis {
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession session;
    private MonsterMapper monsterMapper;
    @Before
    public void init() throws Exception{
//        得到myBatis-config.xml,转换成一个inputStream
        InputStream inputStream = Resources.getResourceAsStream("myBatis-config.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //通过sqlSessionFactory对象获取一个回话
        session = sqlSessionFactory.openSession();
        //通过session获取到 monsterMapper这个接口对象
       monsterMapper = session.getMapper(MonsterMapper.class);

    }
    @Test
    public void testAdd() {
        Monster monster = new Monster();
        for(int i=0;i<10;i++) {
            monster.setAge(100+i);
            monster.setBirthday(new Date());
            monster.setEmail("123@qq.com");
            monster.setGender(1);
            monster.setSalary(8928.00);
            monster.setName("银角"+i);

            monsterMapper.addMonster(monster);
        }

    }
    @Test
    public void testDel() {
        monsterMapper.delMonster(1);
    }
    @Test
    public void testUpdate() {
        Monster monster = new Monster();
        monster.setMonster_id(2);
        monster.setAge(200);
        monster.setBirthday(new Date());
        monster.setEmail("123@qq.com");
        monster.setGender(1);
        monster.setSalary(8928.00);
        monster.setName("牛魔怪");
        monsterMapper.updateMonster(monster);
    }
    @Test
    public void testFindById(){
        Monster monster = new Monster();
        monster = monsterMapper.findById(11);
        System.out.println(monster);
    }
    @Test
    public void testFindAllMonster () {
        List<Monster> allMonster = monsterMapper.findAllMonster();
        for (Monster monster: allMonster){
            System.out.println(monster);
        }
    }
    @After
    public void destory() throws Exception{
        if (session != null) {
            session.commit();
            session.close();
        }
    }
}
相关推荐
百事老饼干3 分钟前
Java[面试题]-真实面试
java·开发语言·面试
customer0811 分钟前
【开源免费】基于SpringBoot+Vue.JS医院管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·开源·intellij-idea
2402_8575893621 分钟前
SpringBoot框架:作业管理技术新解
java·spring boot·后端
HBryce2425 分钟前
缓存-基础概念
java·缓存
一只爱打拳的程序猿39 分钟前
【Spring】更加简单的将对象存入Spring中并使用
java·后端·spring
杨荧41 分钟前
【JAVA毕业设计】基于Vue和SpringBoot的服装商城系统学科竞赛管理系统
java·开发语言·vue.js·spring boot·spring cloud·java-ee·kafka
minDuck43 分钟前
ruoyi-vue集成tianai-captcha验证码
java·前端·vue.js
为将者,自当识天晓地。1 小时前
c++多线程
java·开发语言
daqinzl1 小时前
java获取机器ip、mac
java·mac·ip