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

时间:2022-03-27 01:11:17

使用注解方式配置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();
}