增
xml:<br><insert id="save" parameterType="com.smile.entity.User" useGeneratedKeys="true" keyProperty="id"><br> insert into t_user (id, name, username, password)<br> values (#{id}, #{name}, #{username}, #{password});<br></insert><br><br>java:<br>int save(User user);<br><br>说明:<br>parameterType:参数类型<br>useGeneratedKeys:是否自动生成主键<br>keyProperty:自动生成的主键赋值给哪个属性<br>
删
xml:<br><delete id="delete" parameterType="Long"><br> delete<br> from t_user<br> where id = #{id}<br></delete><br><br>java:<br>int delete(Long id);<br>
改
xml:<br><update id="update" parameterType="com.smile.entity.User"><br> update t_user<br> <set><br> <if test="name != null and name != ''"><br> name = #{name},<br> </if><br> <if test="username != null and username != ''"><br> username = #{username},<br> </if><br> </set><br> where id = #{id}<br></update><br><br>java:<br>int update(User user);<br>
查询
动态条件查询
xml:<br><sql id="select"><br> select name, username, password<br> from t_user<br> <where><br> <if test="name != null and name != ''"><br> name like concat('%', #{name}, '%'),<br> </if><br> </where><br></sql><br><select id="select" parameterType="com.smile.rc.UserCondition" resultType="com.smile.entity.User"><br> <include refid="select"/><br></select><br><br>java:<br>List<User> select(UserCondition c);<br>
查询单条
模糊查询
mysql:like concat('%',#{name},'%')
oracle:like '%'||#{name}||'%'
in查询
xml:<br><select id="selectByIds" resultMap="VoResultMap"><br> select<br> ...<br> from t_table_c c<br> left join t_table_b b on c.id = b._id<br> where c.id in<br> <foreach item="id" collection="ids" open="(" separator="," close=")"><br> #{id}<br> </foreach><br></select><br><br>java:<br>List<User> select(UserCondition c);<br>
分页查询
xml:<br><select id="selectPage" parameterType="com.smile.rc.UserCondition" resultType="com.smile.entity.User"><br> <include refid="select"/><br> <if test="pageFrom != null and pageSize != null"><br> limit #{pageFrom} , #{pageSize}<br> </if><br></select><br><select id="selectPageCount" parameterType="com.smile.rc.UserCondition" resultType="int"><br> select count(1) from (<br> <include refid="select"/><br> ) t<br></select><br><br>java:<br>List<User> selectPage(UserCondition c);<br>Integer selectPageCount(UserCondition c);<br>
一对一查询
一对多查询