15_使用注解开发

使用步骤

1.在接口中注解

public interface UserMapper {
    @Select("select * from mybatis.user")
    List<User> getUserList();
}

2.在mybatis-config.xml中绑定

<mappers>
        <mapper class="com.geng.dao.UserMapper"/>
</mappers>

3.测试

@Test
    public void test(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
    }

测试结果

本质:反射机制实现

底层:动态代理

注解实现增删查改

UserMapper.java

package com.geng.dao;

import com.geng.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {
    // 查询
    @Select("select * from mybatis.user")
    List<User> getUserList();
    // 根据id查询
    @Select("select * from mybatis.user where id=#{id}")
    User getUserById(@Param("id") int id);
    // 增加
    @Insert("insert into mybatis.user (id,name,pwd) values(#{id},#{name},#{pwd})")
    int addUser(User user);
    // 修改
    @Update("update mybatis.user set name=#{name}, pwd=#{pwd} where id = #{id}")
    int updateUser(User user);
    // 删除
    @Delete("delete from mybatis.user where id=#{id}")
    int deleteUser(@Param("id")int id);
}

UserDaoTest.java

import com.geng.dao.UserMapper;
import com.geng.pojo.User;
import com.geng.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class UserDaoTest {
    @Test
    public void selectTest(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    @Test
    public void selectByIdTest(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(2);
        System.out.println(user);
        sqlSession.close();
    }

    @Test
    public void insertTest(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.addUser(new User(1, "gengsj", "22222"));
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void updateTest(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateUser(new User(1, "doudou", "33333"));
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void deleteTest(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteUser(4);
        sqlSession.commit();
        sqlSession.close();

    }
}

2021.6.6 上海漕河泾

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值