MyBatis系列目录--2. Mybatis的简单CRUD(含操作视频)

转载请注明出处哈:http://carlosfu.iteye.com/blog/2238662


 

1. 定义sql映射xml文件:

 

playerMapper.xml
<?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.sohu.tv.mapper.PlayerDao">
    <select id="getPlayerById" parameterType="int" resultType="com.sohu.tv.bean.Player">
        select id,name,age from players where id=#{id}
    </select>
    <insert id="savePlayer" parameterType="com.sohu.tv.bean.Player">
        insert into players(name,age) values(#{name}, #{age});
    </insert>
    <delete id="deletePlayer" parameterType="int">
        delete from players where id=#{id}
    </delete>
    <update id="updatePlayer" parameterType="com.sohu.tv.bean.Player">
        update players set name=#{name},age=#{age} where id=#{id}
    </update>
    <select id="selectAllPlayers" resultType="com.sohu.tv.bean.Player">
        select id,name,age from players
    </select>
</mapper>

2. Dao

package com.sohu.tv.mapper;
import java.util.List;
import com.sohu.tv.bean.Player;
/**
 * 注解方式实现PlayerDao
 * 
 * @author leifu
 * @Date 2015年7月28日
 * @Time 上午10:16:39
 */
public interface PlayerDao {
  
    public int savePlayer(Player player);
   
    public int deletePlayer(int id);
      
    public int updatePlayer(Player player);
      
    public Player getPlayerById(int id);
      
    public List<Player> selectAllPlayers();
}
 

3. 单元测试:

package com.sohu.tv.test.base;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.BeforeClass;
/**
 * mybatis测试基类
 * 
 * @author leifu
 * @Date 2015年7月28日
 * @Time 上午10:59:03
 */
public class BaseTest {
    protected static SqlSessionFactory sessionFactory;
    /**
     * mybatis基础配置
     */
    protected final static String MYBATIS_CONF = "mybatis-base.xml";
    @BeforeClass
    public static void setUp() throws IOException {
        Reader reader = Resources.getResourceAsReader(MYBATIS_CONF);
        sessionFactory = new SqlSessionFactoryBuilder().build(reader);
    }
}
package com.sohu.tv.test.mapper;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.sohu.tv.bean.Player;
import com.sohu.tv.mapper.PlayerDao;
/**
 * mybatis-xml方式配置
 * 
 * @author leifu
 * @Date 2015年7月28日
 * @Time 上午9:54:07
 */
public class PlayerMapperXmlTestV2 extends BaseTest {
    private SqlSession sqlSession;
     
    @Before
    public void before() {
        sqlSession = sessionFactory.openSession(true);
    }
     
    @After
    public void after() {
        sqlSession.close();
    }
     
    @Test
    public void testGetPlayer() {
        PlayerDao playerDao = sqlSession.getMapper(PlayerDao.class);
        Player player = playerDao.getPlayerById(2);
        System.out.println(player);
    }
     
    @Test
    public void testInsertPlayer() {
        PlayerDao playerDao = sqlSession.getMapper(PlayerDao.class);
        playerDao.savePlayer(new Player(-1, "cr7", 30));
    }
     
    @Test
    public void testDeletePlayer() {
        PlayerDao playerDao = sqlSession.getMapper(PlayerDao.class);
        playerDao.deletePlayer(3);
    }
    @Test
    public void testUpdatePlayer() {
        PlayerDao playerDao = sqlSession.getMapper(PlayerDao.class);
        playerDao.updatePlayer(new Player(3, "cafu", 45));
    }
     
    @Test
    public void testSelectAllPlayers() {
        PlayerDao playerDao = sqlSession.getMapper(PlayerDao.class);
        List<Player> playerList = playerDao.selectAllPlayers();
        if (playerList != null && !playerList.isEmpty()) {
            System.out.println("playerList size: " + playerList.size());
            for (Player player : playerList) {
                System.out.println(player);
            }
        }
    }
}

4. 操作视频:

 

http://my.tv.sohu.com/us/201734495/81274865.shtml

上一篇:大数据小视角4:小议Lambda 与 Kappa 架构,不可变数据的计算探索


下一篇:MyBatis系列目录--6. Mybatis实用技巧