Mybatis实验三(注解)

Mybatis实验三(注解)

目录结构
在这里插入图片描述
以上重返代码在上个实验写过就不写了
https://blog.csdn.net/zhangjin2024/article/details/90511838

1.用注解就不用写 usermapper.xml文件了,第一sql的接口(不需要写实现类,实现方法是动态产生的)
UserMapper.java

package com.sxau.rjxy.mybatis_test3;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface UserMapper {
	@Insert("insert into users(name, age) values(#{name}, #{age})")
	public int insertUser(User user);

	@Delete("delete from users where id=#{id}")
	public int deleteUserById(int id);

	@Update("update users set name=#{name},age=#{age} where id=#{id}")
	public int updateUser(User user);

	@Select("select * from users where id=#{id}")
	public User getUserById(int id);

	@Select("select * from users")
	public List<User> getAllUser();
}


2.在config.xml中注册(注意是类)

<mappers>
        <mapper class="com.sxau.rjxy.mybatis_test3.UserMapper"/>
    </mappers>

3.测试类(名字最好不要写成Test,与junit test会重名)
Test1.java

package com.sxau.rjxy.mybatis_test3;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;

import db.utils.MybatisUtils;


public class Test1 {
 
	@Test
	public void testAdd() {
		SqlSessionFactory factory = MybatisUtils.getFactory();
		SqlSession session = factory.openSession(true);
		UserMapper umapper=session.getMapper(UserMapper.class);
		int add = umapper.insertUser(new User(-1, "注解", 21));
		// session.commit();
		System.out.println("add:" + add);
	}

}

4.结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值