基础增删改查
package cn.xm.exam.mapper.common.custom;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
public interface MessageCustomMapper {
@Insert(" insert into message values (#{messageid,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{idcode,jdbcType=CHAR}, #{sex,jdbcType=VARCHAR}, #{birthday,jdbcType=DATE}, #{emptype,jdbcType=VARCHAR},#{isdispose,jdbcType=VARCHAR})")
public int insertIntoByAnnotation(Message message) throws SQLException;
@Select("select * from message where messageid = #{messageId}")
public Message getMessageById(@Param("messageId")String messageId)throws SQLException;
@Update("update message set name=#{name} where messageid=#{messageid}")
public int updateMessageById(Message message)throws SQLException;
@Select("select * from message where messageid = #{messageId}")
public Map getMessageMapById(@Param("messageId")String messageId)throws SQLException;
@Delete("delete from message where messageid = #{messageId}")
public int deleteByMessageId(@Param("messageId")String messageid)throws SQLException;
}
高级注解-provider
@SelectProvider
@SelectProvider(type = EmployeeDynalSqlProvider.class , method = "selectWithParam")
List<Employee> selectWithParam(Map<String , Object> param);
@InsertProvider
@InsertProvider(type = SellerBynalSqlProvider.class,method = "insertSeller")
void save(Sellers seller);
@UpdateProvider
@UpdateProvider(type = SellerBynalSqlProvider.class, method = "updateSeller")
void update(Sellers seller);
@DeleteProvider
@DeleteProvider(type = EmployeeDynalSqlProvider.class , method = "deleteEmployee")
void deleteEmployee(Map<String , Object> param);
具体用法例如
public class UserProvider {
/**
* udpate
* @param UserDO userDO
* @return
*/
public String updateSQL(final UserDO userDO) {
return new SQL() {
{
UPDATE("t_user");
SET("gmt_modified = now()");
if (userDO.getUserName() != null) {
SET("user_name = #{userName}");
}
WHERE("id = #{id}}");
}
}.toString();
}
}
很复杂,这种情况还是xml好用
高级注解-一对n
@One
@Select(" select * from stuffs where id = #{id} ")
@Results(
value = {
@Result(column = "name",property = "name"),
@Result(column = "little_type",property = "littleTypes",
//one指示我们,查询出来的结果只有一个。
one = @One(
//select = sql语句 select="com.qingyunqifu.dao.LittleTypeDao.findLittleTypeById",
//及时加载
fetchType = FetchType.EAGER))
}
)
Stuffs findStuffById(Integer id);
@Many
@Select(" select * from sellers where id=#{id} ")
@Results({
@Result(id = true, column = "id", property = "id"),
@Result(column = "name", property = "name"),
@Result(column = "manager_name", property = "managerName"),
@Result(column = "tel", property = "tel"),
@Result(column = "phone", property = "phone"),
@Result(column = "qq_num", property = "qqNum"),
@Result(column = "address", property = "address"),
@Result(column = "remark", property = "remark"),
@Result(column = "sell_arrange", property = "sellArrange"),
@Result(column = "id",property = "stuffsList",
//many指示我们,查询出来的结果有很多个
many = @Many(
//select = sql语句
select = "com.qingyunqifu.dao.StuffDao.findAllStuffsBySellerId",
//懒加载
fetchType = FetchType.LAZY))
})
Sellers findAllStuffsBySellerId(Integer id);
何时用@one何时用@many,取决于持久化对象中的属性是单一的还是List< T >。如果是单一的就用@one,是list就用@many。
多对多关系的查询,会用到子查询。
select * form tb_article where id IN (select article_id form tb_item where order_id = #{id})
其余
@Results @Result 用来配置列名和属性
@Param 给参数取名字
@ Options附加配置