上接入门案例(看之前的《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();
}
}
}