1.@Select
同@select功能类似的还有@insert,@delete,@update,对应于数据库语句的CRUD。使用@select很方便,不用写配置文件,一般是写在mapper的interface类中,用法如下:
public interface AdmainMapper{
@Select("SELECT * FROM userinfo WHERE username = #{username} AND password = #{password}") @Results(value = { //@Result这项可以不写 @Result(id = true, column = "id", property = "id"), @Result(column = "username", property = "username"), @Result(column = "password", property = "password") }) public Admin selectAdmin(@Param("username") String username, @Param("password") String password);//参数可以是一个对象,sql语句会自动匹配
}
使用注解的方式很简单但是不是很灵活,对于动态条件查询是无法实现的,这时,我们可以使用xml注解的方式。2.xml方式
public interface AdminMapper { public List<Admin> getAdminByConditions(@Param("username")String username, @Param("password")String password, @Param("start")int start, @Param("limit")int limit);//参数可以直接传入一个类,在xml中可以自动匹配 }
注解的方式和xml的方式可以写在同一个类中,就如上所示,这两个方法可以在同一个类中。这里写上@Param,在xml中就不用使用parameterType属性了。
<?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.xxx. ... .AdminMapper"> <!-- 指定字段映射 -->
<resultMap type="com. ... .Student" id="studentResultMap"> //下面方法返回结果存放类型通过id识别 <id property="id" column="id" /> <result property="name" column="name" /> <result property="age" column="age" /> <result property="classId" column="classId" /> </resultMap>
<sql id="Base_Column_List"> //使用这种方法可以使重复使用的字段通过id直接调用即可,很方便 id, name ,age,classId </sql><!-- 若不需要自动返回主键,将useGeneratedKeys="true" keyProperty="id"去掉即可 -->
<select id="getAdminByConditions" parameterType="com. ... .StudentVo" resultMap="studentResultMap"> <![CDATA[ SELECT ]]> //此id要和mapper中的方法名对应 <include refid="Base_Column_List" /> <![CDATA[ FROM student WHERE 1=1]]> <if test="id != null"><![CDATA[ AND id = #{id} ]]></if> <if test="name != null"><![CDATA[ AND name like CONCAT('%',#{name},'%') ]]></if> <if test="age != null"><![CDATA[ AND age = #{age} ]]></if> <![CDATA[ ORDER BY id DESC limit #{startRow} , #{pageSize} ]]> </select></mapper>xml的文件名最好与mapper的名字对应下来。
此外我们还需要对配置文件进行配置,参考http://www.cnblogs.com/java-zhao/p/5120792.html
3.@SelectProvider方式
我觉得这种方式集成了前两种方法的优点,用法如下:
public interface AdmainMapper{ @SelectProvider(type = SqlBuilder.class, method = "queryList")//指定所用sql语句
@ResultMap("student") List<student> getList(@Param("start") int start,
@Param("pageSize") int pageSize,
@Param("apIds") String apIds,
@Param("model") String model); }
public class SqlBuilder {//与type对应 public String queryList(@Param("start") int start, @Param("pageSize") int pageSize, //于method对应 @Param("apIds") String apIds, @Param("model") String model) { String sql = " SELECT ap.* FROM ap WHERE ap.id NOT in ( SELECT apid FROM ap_activity_mapping WHERE del = 0 "; if (StringUtils.isNotEmpty(apIds)) { sql += " and apid not in (" + apIds + ") "; } sql += " ) "; if (StringUtils.isNotEmpty(model)) { sql += " and model = '" + model + "'"; } sql += " GROUP BY ap.id limit " + start + "," + pageSize; return sql; }//返回类型必须是String }
用这种方法要注意的是不要重载,此外还接受Map<String, Object>作为参数。