myBatis-CRUD
2023-12-26 19:03:15
上接入门案例(看之前的《mybatis基本介绍及入门案例》篇)
1、映射接口
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、映射文件
<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测试
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();
}
}
}
文章来源:https://blog.csdn.net/qq_36684207/article/details/135227859
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!