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);
            }
        }
    }
}

  

相关文章
|
5月前
|
SQL XML Java
【mybatis】第二篇:@Select注解中加入字段判断
【mybatis】第二篇:@Select注解中加入字段判断
|
7天前
|
SQL XML Java
mybatis复习02,简单的增删改查,@Param注解多个参数,resultType与resultMap的区别,#{}预编译参数
文章介绍了MyBatis的简单增删改查操作,包括创建数据表、实体类、配置文件、Mapper接口及其XML文件,并解释了`#{}`预编译参数和`@Param`注解的使用。同时,还涵盖了resultType与resultMap的区别,并提供了完整的代码实例和测试用例。
mybatis复习02,简单的增删改查,@Param注解多个参数,resultType与resultMap的区别,#{}预编译参数
|
20天前
|
Java 数据库连接 数据格式
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
IOC/DI配置管理DruidDataSource和properties、核心容器的创建、获取bean的方式、spring注解开发、注解开发管理第三方bean、Spring整合Mybatis和Junit
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
|
2月前
|
SQL Java 数据库
5、Mybatis-Plus 常用注解
这篇文章详细介绍了Mybatis-Plus中常用的注解,包括解决实体类与数据库表名不一致、字段不匹配的问题,主键生成策略的配置,以及逻辑删除的实现方法。
5、Mybatis-Plus 常用注解
|
2月前
|
SQL Java 数据库连接
后端框架的学习----mybatis框架(7、使用注解开发)
这篇文章讲述了如何使用MyBatis框架的注解方式进行开发,包括在接口上使用注解定义SQL语句,并通过动态代理实现对数据库的增删改查操作,同时强调了接口需要在核心配置文件中注册绑定。
|
4月前
|
Java 数据库连接 数据库
Springboot整合mybatis注解版(202005)
Springboot整合mybatis注解版(202005)
|
4月前
|
SQL Java 数据库连接
2万字实操案例之在Springboot框架下基于注解用Mybatis开发实现基础操作MySQL之预编译SQL主键返回增删改查
2万字实操案例之在Springboot框架下基于注解用Mybatis开发实现基础操作MySQL之预编译SQL主键返回增删改查
58 2
|
4月前
|
缓存 NoSQL Java
在 SSM 架构(Spring + SpringMVC + MyBatis)中,可以通过 Spring 的注解式缓存来实现 Redis 缓存功能
【6月更文挑战第18天】在SSM(Spring+SpringMVC+MyBatis)中集成Redis缓存,涉及以下步骤:添加Spring Boot的`spring-boot-starter-data-redis`依赖;配置Redis连接池(如JedisPoolConfig)和连接工厂;在Service层使用`@Cacheable`注解标记缓存方法,指定缓存名和键生成策略;最后,在主配置类启用缓存注解。通过这些步骤,可以利用Spring的注解实现Redis缓存。
69 2
|
3月前
|
数据库
MybatisPlus3---常用注解,驼峰转下滑线作为表明 cteateTime 数据表中的 cteate_time,@TableField,与数据库字段冲突要使用转义字符“`order`“,is
MybatisPlus3---常用注解,驼峰转下滑线作为表明 cteateTime 数据表中的 cteate_time,@TableField,与数据库字段冲突要使用转义字符“`order`“,is
|
3月前
|
Java 数据库连接 Maven
Private method ‘getVideoList()‘ is never used,mybatis必须指定Mapper文件和实体目录,在参考其他人写的代码,要认真分析别人的代码,不要丢失
Private method ‘getVideoList()‘ is never used,mybatis必须指定Mapper文件和实体目录,在参考其他人写的代码,要认真分析别人的代码,不要丢失