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

简介: 转载请注明出处哈:http://carlosfu.iteye.com/blog/2238662   1. 定义sql映射xml文件:   playerMapper.xml select id,name,age from ...

转载请注明出处哈: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

相关文章
|
12月前
|
XML Java 数据库连接
二、搭建MyBatis采用xml方式,验证CRUD(增删改查操作)
二、搭建MyBatis采用xml方式,验证CRUD(增删改查操作)
373 21
|
XML Java 数据库连接
【MyBatis】MyBatis操作数据库(一)
【MyBatis】MyBatis操作数据库(一)
195 1
|
SQL 存储 Java
基于MyBatis的增删改查操作
基于MyBatis的增删改查操作
108 1
|
SQL Java 数据库连接
MyBatis-Plus:简化 CRUD 操作的艺术
MyBatis-Plus 是一个基于 MyBatis 的增强工具,它旨在简化 MyBatis 的使用,提高开发效率。
622 1
MyBatis-Plus:简化 CRUD 操作的艺术
|
缓存 Java 数据库连接
我们后端程序员不是操作MyBatis的CRUD Boy
大家好,我是南哥。一个对Java程序员进阶成长颇有研究的人,今天我们接着新的一篇Java进阶指南。为啥都戏称后端是CRUD Boy?难道就因为天天怼着数据库CRUD吗?要我说,是这个岗位的位置要的就是你CRUD,你不得不CRUD。哪有公司天天能给你搭建高并发、高可用、大数据框架的活呢,一条业务线总要成长吧,慢慢成熟了就要装修工来缝缝补补、美化美化,也就是CRUD的活。不能妄自菲薄CRUD Boy,我们是后端工程师。今天来指南下操作数据库之MyBatis框架。
371 3
我们后端程序员不是操作MyBatis的CRUD Boy
|
SQL Java 数据库连接
Mybatis方式完成CRUD操作
Mybatis方式完成CRUD操作
211 0
|
SQL 缓存 Java
使用MyBatis优化Java持久层操作
使用MyBatis优化Java持久层操作
MybatisPlus-标准CRUD制作,新增boolean save(T t),删除 ~ delete(int id),修改 ~ update(T t),根据id查询,T getById....
MybatisPlus-标准CRUD制作,新增boolean save(T t),删除 ~ delete(int id),修改 ~ update(T t),根据id查询,T getById....
|
SQL XML Java
【MyBatis】 MyBatis框架下的高效数据操作:深入理解增删查改(CRUD)
【MyBatis】 MyBatis框架下的高效数据操作:深入理解增删查改(CRUD)
259 1
|
SQL 缓存 Java
使用MyBatis优化Java持久层操作
使用MyBatis优化Java持久层操作