MyBatis有两种开发方式:XML开发和注解开发
注解与XML对应表格
注解 | 对应XML | 说明 |
---|---|---|
@Insert | <insert> | 新增SQL |
@Update | <update> | 更新SQL |
@Delete | <delete> | 删除SQL |
@Select | <select> | 查询SQL |
@Param | - - - | 参数映射 |
@Results | <resultMap> | 结果映射 |
@Result | <id> <result> | 字段映射 |
XML开发
@Repository
public interface UserDao {
/**
* 有多个参数时,需要使用@Param注解进行绑定关联
*/
List<User> selectBySexAndAge(@Param("sex")String sex , @Param("age")Integer age , @Param("limit")Integer limit);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cd.blog.dao.UserDao">
<select id="selectBySexAndAge" resultType="User">
select * from t_user where sex = #{sex} and age = #{age} order by age
</select>
</mapper>
注解开发
@Repository
public interface UserDao {
/**
* 有多个参数时,需要使用@Param注解进行绑定关联
*/
@Select("select * from t_user where sex = #{sex} and age = #{age} order by age limit 0 , #{limit}")
List<User> selectBySexAndAge(@Param("sex")String sex , @Param("age")Integer age , @Param("limit")Integer limit);
}