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


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

 

 

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();
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM