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();
        }
    }
}
相关推荐
myNameGL7 分钟前
linux安装idea
java·ide·intellij-idea
青春男大9 分钟前
java栈--数据结构
java·开发语言·数据结构·学习·eclipse
HaiFan.1 小时前
SpringBoot 事务
java·数据库·spring boot·sql·mysql
我要学编程(ಥ_ಥ)1 小时前
一文详解“二叉树中的深搜“在算法中的应用
java·数据结构·算法·leetcode·深度优先
music0ant1 小时前
Idea 添加tomcat 并发布到tomcat
java·tomcat·intellij-idea
计算机徐师兄1 小时前
Java基于SSM框架的无中介租房系统小程序【附源码、文档】
java·微信小程序·小程序·无中介租房系统小程序·java无中介租房系统小程序·无中介租房微信小程序
源码哥_博纳软云1 小时前
JAVA智慧养老养老护理帮忙代办陪诊陪护小程序APP源码
java·开发语言·微信小程序·小程序·微信公众平台
忒可君2 小时前
C# winform 报错:类型“System.Int32”的对象无法转换为类型“System.Int16”。
java·开发语言
斌斌_____3 小时前
Spring Boot 配置文件的加载顺序
java·spring boot·后端
路在脚下@3 小时前
Spring如何处理循环依赖
java·后端·spring