MyBatis注释配置:Annotation注释的使用

@select,@insert,@update,@delete可以完成常见的CURD SQL语句的映射.
1..UserMapper.java
package org.fkit.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.fkit.domain.User;
public interface UserMapper
{
    @Insert("INSERT INTO tb_user (USERNAME,LOGINNAME,PASSWORD,PHONE,ADDRESS) VALUES(#{username},#{loginname},#{password},#{phone},#{address})")
    @Options(useGeneratedKeys=true,keyProperty="id")
    int saveUser(User user);
    
    @Delete("DELETE FROM tb_user WHERE ID=#{id}")
    int removeUser(@Param("id") Integer id);
    
    @Update("UPDATE tb_user SET USERNAME=#{username},LOGINNAME=#{loginname},PASSWORD=#{password},PHONE=#{phone},ADDRESS=#{address} WHERE ID=#{id}")
    void modifyUser(User user);
    
    @Select("SELECT * FROM  tb_user WHERE ID=#{id}")
    @Results({
        @Result(id=true,column="ID",property="id"),
        @Result(column="USERNAME",property="username"),
        @Result(column="LOGINNAME",property="loginname"),
        @Result(column="PASSWORD",property="password"),
        @Result(column="PHONE",property="phone"),
        @Result(column="ADDRESS",property="address")
    })
    User selectUserById(Integer id);
    @Select("SELECT * FROM tb_user")
    @Results({
        @Result(id=true,column="ID",property="id"),
        @Result(column="USERNAME",property="username"),
        @Result(column="LOGINNAME",property="loginname"),
        @Result(column="PASSWORD",property="password"),
        @Result(column="PHONE",property="phone"),
        @Result(column="ADDRESS",property="address")
    })
    List<User> selectAllUser();
}
在UserMapper.java中定义了saveUser,removeUser,modifyUser,selectUserById和selectAllUser5个方法,分别对应插入,更新,删除,根据id查询用户和查询所有用户5个数据库操作,并使用Annotation注释代替之前的XML配置,Annotation配置中的SQL语句和XML配置中的SQL语句用法一致,此处不再赘述.
2.使用saveUser()
    @GetMapping("/mysql/insert")
    public void insert()
    {
        //获得SqlSession实例
        SqlSession sqlSession = FKSqlSessionFactory.getSqlSession();
        //获得Mapper接口代理对象
        UserMapper em = sqlSession.getMapper(UserMapper.class);
        //user
        User user = new User();
        user.setLoginname("login abc");
        user.setUsername("haha");
        user.setPassword("123456");
        user.setPhone("911");
        user.setAddress("china");
        //插入
        int result = em.saveUser(user);
        System.out.println(result);
        //commit
        sqlSession.commit();
        sqlSession.close();
    }
3.输出
16:27:33,319 DEBUG saveUser:159 - ==>  Preparing: INSERT INTO tb_user (USERNAME,LOGINNAME,PASSWORD,PHONE,ADDRESS) VALUES(?,?,?,?,?)
16:27:33,352 DEBUG saveUser:159 - ==> Parameters: haha(String), login abc(String), 123456(String), 911(String), china(String)
16:27:33,359 DEBUG saveUser:159 - <==    Updates: 1
1