MyBatis系列目录--3. Mybatis注解

简介: 转载请注明出处哈:http://carlosfu.iteye.com/blog/2238662   1. PlayerDao注解方式实现 package com.sohu.tv.

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


 

1. PlayerDao注解方式实现

package com.sohu.tv.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Insert;
import com.sohu.tv.bean.Player;
/**
 * 注解方式实现PlayerDao
 * @author leifu
 * @Date 2015年7月28日
 * @Time 上午10:16:39
 */
public interface PlayerAnnotationDao {
    @Insert("insert into players(name, age) values(#{name}, #{age})")
    public int savePlayer(Player player);
  
    @Delete("delete from players where id=#{id}")
    public int deletePlayer(int id);
     
    @Update("update players set name=#{name},age=#{age} where id=#{id}")
    public int updatePlayer(Player player);
     
    @Select("select id,name,age from players where id=#{id}")
    public Player getPlayerById(int id);
     
    @Select("select id,name,age from players")
    public List<Player> selectAllPlayers();
}

 

2. mybatis-base.xml添加注解相关配置

<mappers>
    <mapper class="com.sohu.tv.mapper.PlayerAnnotationDao"/>
</mappers>

 

3. 单元测试:

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.PlayerAnnotationDao;
/**
 * mybatis-annotation实现方式配置
 * 
 * @author leifu
 * @Date 2015年7月28日
 * @Time 上午9:54:07
 */
public class PlayerMapperAnnotationTest extends BaseTest {
    private SqlSession sqlSession;
    @Before
    public void before() {
        sqlSession = sessionFactory.openSession(true);
    }
    @After
    public void after() {
        sqlSession.close();
    }
    @Test
    public void testGetPlayer() {
        PlayerAnnotationDao playerAnnotationDao = sqlSession.getMapper(PlayerAnnotationDao.class);
        Player player = playerAnnotationDao.getPlayerById(1);
        System.out.println(player);
    }
    @Test
    public void testInsertPlayer() {
        PlayerAnnotationDao playerAnnotationDao = sqlSession.getMapper(PlayerAnnotationDao.class);
        playerAnnotationDao.savePlayer(new Player(-1, "carlos", 34));
    }
    @Test
    public void testDeletePlayer() {
        PlayerAnnotationDao playerAnnotationDao = sqlSession.getMapper(PlayerAnnotationDao.class);
        playerAnnotationDao.deletePlayer(4);
    }
    @Test
    public void testUpdatePlayer() {
        PlayerAnnotationDao playerAnnotationDao = sqlSession.getMapper(PlayerAnnotationDao.class);
        playerAnnotationDao.updatePlayer(new Player(3, "ronaldo", 39));
    }
    @Test
    public void testSelectAllPlayers() {
        PlayerAnnotationDao playerAnnotationDao = sqlSession.getMapper(PlayerAnnotationDao.class);
        List<Player> playerList = playerAnnotationDao.selectAllPlayers();
        if (playerList != null && !playerList.isEmpty()) {
            System.out.println("playerList size: " + playerList.size());
            for (Player player : playerList) {
                System.out.println(player);
            }
        }
    }
}

  

相关文章
|
4月前
|
SQL Java 数据库连接
手写mybatis 注解版
手写mybatis 注解版
26 0
|
5月前
|
SQL XML Java
源码分析系列教程(08) - 手写MyBatis(注解版)
源码分析系列教程(08) - 手写MyBatis(注解版)
42 0
|
5月前
|
SQL XML Java
mybatis的注解开发之三种动态sql
mybatis的注解开发之三种动态sql
|
6月前
|
Java 数据库连接 数据库
MyBatis与Spring集成&常用注解以及AOP和PageHelper分页插件整合
MyBatis与Spring集成&常用注解以及AOP和PageHelper分页插件整合
52 0
|
4月前
|
XML Java 数据库连接
MyBatis深入探索:原生API与注解方式实现CRUD操作
MyBatis深入探索:原生API与注解方式实现CRUD操作
65 0
|
6月前
|
Java 数据库连接 数据库
mybatis-spring集成&数据库连接池&开启注解式开发
mybatis-spring集成&数据库连接池&开启注解式开发
|
9天前
|
SQL XML Java
【mybatis】第二篇:@Select注解中加入字段判断
【mybatis】第二篇:@Select注解中加入字段判断
|
4月前
|
XML Java 数据库连接
MyBatis--映射关系一对一和MyBatis--映射关系多对一 -都有基于xml和注解的教程
MyBatis--映射关系一对一和MyBatis--映射关系多对一 -都有基于xml和注解的教程
90 0
|
28天前
|
Java 数据库连接 网络安全
mybatis使用全注解的方式案例(包含一对多关系映射)
mybatis使用全注解的方式案例(包含一对多关系映射)
12 0
|
29天前
|
关系型数据库 Java 数据库连接
如何利用Mybatis-Plus自动生成代码(超详细注解)
如何利用Mybatis-Plus自动生成代码(超详细注解)
26 1