publicinterfaceIUserDao{/**
* 查询所有用户
* @return
*/@Select("select * from user")@Results(id="userMap",
value={@Result(id=true,column="id",property="userId"),@Result(column="username",property="userName"),@Result(column="sex",property="userSex"),@Result(column="address",property="userAddress"),@Result(column="birthday",property="userBirthday")})
List<User>findAll();/**
* 根据 id 查询一个用户
* @param userId
* @return
*/@Select("select * from user where id = #{uid} ")@ResultMap("userMap")
User findById(Integer userId);/**
* 保存操作
* @param user
* @return
*/@Insert("insert into user(username,sex,birthday,address)values(#{username},#{sex},#{birthday},#{address})")intsaveUser(User user);/**
* 更新操作
* @param user
* @return
*/@Update("update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id =#{id} ")intupdateUser(User user);/**
* 删除用户
* @param userId
* @return
*/@Delete("delete from user where id = #{uid} ")intdeleteUser(Integer userId);/**
* 查询使用聚合函数
* @return
*/@Select("select count(*) from user ")intfindTotal();/**
* 模糊查询
* @param name
* @return
*/@Select("select * from user where username like #{username} ")
List<User>findByName(String name);}
编写 SqlMapConfig 配置文件
<!-- 配置映射信息 --><mappers><!-- 配置 dao 接口的位置,它有两种方式
第一种:使用 mapper 标签配置 class 属性
第二种:使用 package 标签,直接指定 dao 接口所在的包
--><packagename="cn.myp666.dao"/></mappers>
publicinterfaceIUserDao{/**
* 查询所有用户
* @return
*/@Select("select * from user")@Results(id="userMap",
value={@Result(id=true,column="id",property="userId"),@Result(column="username",property="userName"),@Result(column="sex",property="userSex"),@Result(column="address",property="userAddress"),@Result(column="birthday",property="userBirthday")})
List<User>findAll();/**
* 根据 id 查询一个用户
* @param userId
* @return
*/@Select("select * from user where id = #{uid} ")@ResultMap("userMap")
User findById(Integer userId);}