13-注解增删改查

public interface UserMapper {

//方法存在多个参数,所有的参数前面必须加上@Param()注解

@Select(“select * from user where id =#{id}”)

User getUserById(@Param(“id”) int id);

@Insert(“insert into user(id,name,pwd) values(#{id},#{name},#{password})”)

int addUser(User user);

@Update(“update user set name=#{name},pwd=#{password} where id=#{id}”)

int updateUser(User user);

@Delete(“delete from user where id=#{uid}”)

int deleteUser(@Param(“uid”)int id);

}

测试

import com.tian.dao.UserMapper;

import com.tian.pojo.User;

import com.tian.utils.MybatisUtils;

import 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 org.apache.ibatis.session.SqlSession;

import org.junit.Test;

import java.util.HashMap;

import java.util.List;

public class UserMapperTest {

@Test

public void test1(){

SqlSession sqlSession = MybatisUtils.getSqlSession();

UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

User userById = userMapper.getUserById(1);

System.out.println(userById);

//关闭sqlsession

sqlSession.close();

}

@Test

public void test2(){

SqlSession sqlSession = MybatisUtils.getSqlSession();

UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

int i = userMapper.addUser(new User(6, “he”, “456”));

System.out.println(i);

sqlSession.close();

}

@Test

public void test3(){

SqlSession sqlSession = MybatisUtils.getSqlSession();

UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

userMapper.updateUser(new User(6, “hel”, “456”));

sqlSession.close();

}

@Test

public void test4(){

SqlSession sqlSession = MybatisUtils.getSqlSession();

UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

userMapper.deleteUser(6);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值