重温MyBatis(六、注解之与五:基于注解的动态SQL)

UserInfoMapper.java文件的代码:

public interface UserInfoMapper {
     
/**
     * 基于动态SQL的注解配置===开始
     */

    @SelectProvider(type = UserInfoDynaSqlProvider.class, method = "selectWithParam")
    List<UserInfo> findUserInfoByCondWithDynaSQL(Map<String, Object> param);

    @InsertProvider(type = UserInfoDynaSqlProvider.class, method = "insertUserInfo")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertUserInfoWithDynaSQL(UserInfo userInfo);

    @UpdateProvider(type = UserInfoDynaSqlProvider.class, method = "updateUserInfo")
    int updateUserInfoWithDynaSQL(UserInfo userInfo);

    @DeleteProvider(type = UserInfoDynaSqlProvider.class, method = "deleteUserInfo")
    int deleteUserInfoWithDynaSQL(Map<String, Object> param);

    /**
     *  基于动态SQL的注解配置===结束
     */
}

UserInfoDynaSqlProvider.java文件的代码:

package com.springmvc.mapper;


import com.springmvc.pojo.UserInfo;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;


/**
 * @author YuusukeUchiha
 * @date 2020/8/19 19:57
 * @ide IntelliJ IDEA
 */
public class UserInfoDynaSqlProvider {
     
    public String selectWithParam(Map<String, Object> param) {
     
        return new SQL() {
     
            {
     
                SELECT("*");
                FROM("user");
                if (param.get("id") != null) {
     
                    WHERE("id = #{id}");
                }
                if (param.get("userName") != null) {
     
//                    WHERE("userName = #{userName}");
                    WHERE("userName like concat(concat('%', #{userName}), '%')");

                }
                if (param.get("password") != null) {
     
                    WHERE("password = #{password}");
                }
            }
        }.toString();
    }

    public String insertUserInfo(UserInfo userInfo) {
     
        return new SQL() {
     
            {
     
                INSERT_INTO("user");
                if (userInfo.getUserName() != null) {
     
                    VALUES("userName", "#{userName}");
                }
                if (userInfo.getPassword() != null) {
     
                    VALUES("password", "#{password}");
                }
            }
        }.toString();
    }

    public String updateUserInfo(UserInfo userInfo) {
     
        return new SQL() {
     
            {
     
                UPDATE("user");
                if (userInfo.getUserName() != null) {
     
                    SET("userName = #{userName}");
                }
                if (userInfo.getPassword() != null) {
     
                    SET("password = #{password}");
                }
                WHERE("id = #{id}");
            }
        }.toString();
    }

    public String deleteUserInfo(Map<String, Object> param) {
     
        return new SQL() {
     
            {
     
                DELETE_FROM("user");
                if (param.get("id") != null) {
     
                    WHERE("id = #{id}");
                }
                if (param.get("userName") != null) {
     
                    WHERE("userName = #{userName}");
                }
                if (param.get("password") != null) {
     
                    WHERE("password = #{password}");
                }
            }
        }.toString();
    }
}

 /**
     * 基于动态SQL的注解配置===开始
     */
//    @Test
    public void testFindUserInfoByCondWithDynaSQL() {
     
        UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);

        Map<String, Object> param = new HashMap();

        param.put("userName", "张三");

        List<UserInfo> userInfoList = userInfoMapper.findUserInfoByCondWithDynaSQL(param);

        for (UserInfo userInfo : userInfoList) {
     
            System.out.println(userInfo.toString());
        }
    }

    //    @Test
    public void testInsertUserInfoWithDynaSQL() {
     
        UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);

        UserInfo userInfo = new UserInfo();

        userInfo.setUserName("尼古拉赵四");
        userInfo.setPassword("123456");

        int result = userInfoMapper.insertUserInfoWithDynaSQL(userInfo);

        if (result > 0) {
     
            System.out.println("数据新增成功,新增的用户id为:" + userInfo.getId());
        } else {
     
            System.out.println("数据新增失败");
        }
    }

    //    @Test
    public void testUpdateUserInfoWithDynaSQL() {
     
        UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);

        // 使用Map封装查询条件
        Map<String, Object> map = new HashMap<>();
        map.put("id", "14");

        // 直接调用前面的“testFindUserInfoByCond”方法,查询id为14的用户
        System.out.println();
//        List userInfoList = userInfoMapper.findUserInfoByCondWithDynaSQL(map);
        UserInfo userInfo = userInfoMapper.findUserInfoByCondWithDynaSQL(map).get(0);
//        userInfoList.get(0).setPassword("654654");
        userInfo.setPassword("654987");

//        int result = userInfoMapper.updateUserInfoWithDynaSQL(userInfoList.get(0));
        int result = userInfoMapper.updateUserInfoWithDynaSQL(userInfo);

        if (result > 0) {
     
//            System.out.println("数据修改成功,修改的用户的密码为:" + userInfoList.get(0).getPassword());
            System.out.println("数据修改成功,修改的用户的密码为:" + userInfo.getPassword());
        } else {
     
            System.out.println("数据修改失败");
        }
    }

    @Test
    public void testDeleteUserInfoWithDynaSQL() {
     
        UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);

        //  使用Map封装查询条件
        Map<String, Object> map = new HashMap<>();
        map.put("id", "88");
//        map.put("id", "14");

        int result = userInfoMapper.deleteUserInfoWithDynaSQL(map);

        if (result > 0) {
     
            System.out.println("数据删除成功");
        } else {
     
            System.out.println("数据删除失败");
        }
    }


    /**
     *  基于动态SQL的注解配置===结束

你可能感兴趣的:(Java,springmvc,mybatis,java,mysql,mybatis)