使用MyBatis注解开发,可以省去类配置文件,简洁方便。但是比较复杂的SQL和动态SQL还是建议书写类配置文件。
注解还是不推荐使用的。只是了解了解! 简单的CRUD可以使用注解。简单写写。
把之前的例子改成使用注解的。
UserMapper.java
package cn.itxm.mybatis.mapper; 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.Options; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.ResultMap; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import cn.itxm.mybatis.beans.UserBean; public interface UserMapper { // 简单的增删改查可以使用注解 // 注解+配置文件 /** * 新增用戶 * @param user * @return * @throws Exception */ @Insert("insert into t_user value (null,user.username,user.password,user.account)") @Options(useGeneratedKeys=true,keyProperty="user.id") public int insertUser(@Param("user")UserBean user) throws Exception; /** * 修改用戶 * @param user * @param id * @return * @throws Exception */ @Update(" update t_user set username=#{u.username},password=#{u.password},account=#{u.account} where id=#{id}") public int updateUser (@Param("u")UserBean user,@Param("id")int id) throws Exception; /** * 刪除用戶 * @param id * @return * @throws Exception */ @Delete(" delete from t_user where id=#{id} ") public int deleteUser(int id) throws Exception; /** * 根据id查询用户信息 * @param id * @return * @throws Exception */ @Select(" select * from t_user where id=#{id}") @Results({ @Result(id=true,property="id",column="id",javaType=Integer.class), @Result(property="username",column="username",javaType=String.class), @Result(property="password",column="password",javaType=String.class), @Result(property="account",column="account",javaType=Double.class) }) public UserBean selectUserById(int id) throws Exception; /** * 查询所有的用户信息 * @return * @throws Exception */ @Select(" select * from t_user") @ResultMap("userMap") public List<UserBean> selectAllUser() throws Exception; /** * 批量增加 * @param user * @return * @throws Exception */ public int batchInsertUser(@Param("users")List<UserBean> user) throws Exception; /** * 批量删除 * @param list * @return * @throws Exception */ public int batchDeleteUser(@Param("list")List<Integer> list) throws Exception; /** * 分页查询数据 * @param parma * @return * @throws Exception */ public List<UserBean> pagerUser(Map<String, Object> parmas) throws Exception; /** * * 分页统计数据 * @param parma * @return * @throws Exception */ public int countUser(Map<String, Object> parmas) throws Exception; }
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.itxm.mybatis.mapper.UserMapper"> <!-- 自定义返回结果集 --> <resultMap id="userMap" type="UserBean"> <id property="id" column="id" javaType="java.lang.Integer"></id> <result property="username" column="username" javaType="java.lang.String"></result> <result property="password" column="password" javaType="java.lang.String"></result> <result property="account" column="account" javaType="java.lang.Double"></result> </resultMap> <!-- 批量操作和foreach标签 --> <insert id="batchInsertUser" parameterType="java.util.List"> insert into t_user values <foreach collection="users" item="users" separator=","> (null,#{users.username},#{users.password},#{users.account}) </foreach> </insert> <delete id="batchDeleteUser"> delete from t_user where id in ( <foreach collection="list" item="list" separator=","> #{id} </foreach> ) </delete> <!--collection 为用于遍历的元素(必选),支持数组、List、Set --> <!-- item 表示集合中每一个元素进行迭代时的别名. --> <!--separator表示在每次进行迭代之间以什么符号作为分隔 符. --> <!--#在生成SQL时,对于字符类型参数,会拼装引号 $在生成SQL时,不会拼装引号,可用于order by之类的参数拼装 --> <select id="pagerUser" parameterType="java.util.Map" resultMap="userMap"> select * from t_user where 1=1 <if test="username!=null"> and username like '%${username}%' </if> limit ${index},${pageSize} </select> <select id="countUser" parameterType="java.util.Map" resultType="int"> select count(*) from t_user where 1=1 <if test="username != null"> and username like '%${username}%' </if> </select> </mapper>
简单的一个一对一的使用注解的。
package com.lovo.mybatis.mapper; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.One; import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.ResultType; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import com.lovo.mybatis.beans.HusbandBean; import com.lovo.mybatis.beans.WifeBean; public interface HusbandMapper { /** * 保存丈夫 * @param husband * @return */ @Insert("insert into t_husband values (null,#{h.name})") @Options(useGeneratedKeys=true,keyProperty="h.id") public int saveHusband(@Param("h")HusbandBean husband); /** * 根据ID查询丈夫资料 * @param id * @return */ @Select("select * from t_husband where id=#{id}") @ResultType(HusbandBean.class) public HusbandBean findHusbandById(int id); /*** 根据ID查询丈夫与妻子资料 * @param id * @return */ @Select("select * from t_husband where id=#{id}") @Results({ @Result(id=true,property="id",column="id",javaType=Integer.class), @Result(property="name",column="name",javaType=String.class), @Result(property="wife",column="id",javaType=WifeBean.class,[email protected](select="com.lovo.mybatis.mapper.WifeMapper.findWifeByHusbandId")) }) public HusbandBean findHusbandAndWife(int id); }
package cn.itxm.mybatis.mapper; import org.apache.ibatis.annotations.ResultType; import org.apache.ibatis.annotations.Select; import cn.itxm.mybatis.beans.WifeBean; public interface WifeMapper { @Select("select * from t_wife where fk_husband_id = #{id}") @ResultType(WifeBean.class) public WifeBean selectWifeByHusbandId(int id)throws Exception; }
注意:使用resultType时,一定要保证,你属性名与字段名相同;如果不相同,就使用resultMap 。
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/14100.html