使用注解方式配置sql语句,不需要写对应的UserMapper.xml

mybatis(4)--使用注解方式配置sql语句

 

 

package com.mapper;

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.SelectKey;
import org.apache.ibatis.annotations.Update;

import com.pojo.User;

//不需要写UserMapper的实现类
public interface UserMapper {
    // 用于返回该条数据插入后的id
    @SelectKey(statement = "select last_insert_id()", before = false, keyProperty = "id", resultType = Integer.class)
    @Insert("insert into t_user (`last_name`,`sex`) values(#{lastName},#{sex})")
    public int saveUser(User user);

    @Delete("delete from t_user where id=#{id}")
    public int deleteById(Integer id);

    @Update(value = "update t_user set last_name=#{lastName},sex=#{sex} where id=#{id}")
    public int updateUser(User user);

    @Select("select id,last_name lastName,sex from t_user where id=#{id}")
    public User queryUserById(Integer id);

    @Select("select id,last_name lastName,sex from t_user")
    public List<User> queryUsers();
}

 

相关文章:

  • 2022-12-23
  • 2022-01-05
  • 2021-06-03
  • 2021-08-27
  • 2021-11-09
  • 2022-12-23
  • 2021-11-09
猜你喜欢
  • 2021-02-07
  • 2021-09-27
  • 2021-08-11
  • 2022-01-28
  • 2022-12-23
  • 2022-12-23
  • 2022-02-01
相关资源
相似解决方案