SQL映射文件
单条件查询
1. 在ISmbmsUserDao接口添加抽象方法
//根据用户名模糊查询 List<SmbmsUser> getUserListByName();
2. 在UserMapper.xml里添加SQL
<!--根据用户名模糊查询--> <select id="getUserListByName" resultType="SmbmsUser" parameterType="String"> select * from smbms_user where userName like concat(‘%‘,#{userName},‘$‘) </select>
3. 测试类添加方法
//模糊查询 @Test public void testUserListByName(){ List<SmbmsUser> users = mapper.getUserListByName("李"); for (SmbmsUser user : users){ System.out.println(user.getUserName()); } }
多条件查询
1. 在ISmbmsUserDao接口添加抽象方法
//对象入参查询 List<SmbmsUser> getUserListByUser(SmbmsUser user);
2. 在UserMapper.xml里添加SQL
<!--以用户类型入参查询--> <select id="getUserListByUser" parameterType="SmbmsUser" resultType="Smbmsuser"> select * from smbms_user where userName like concat(‘%‘,#{userName},‘%‘) and userRole = #{userRole} </select>
3. 测试类添加方法
//以封装用户入参 @Test public void testUserListByUser(){ List<SmbmsUser> userList = new ArrayList<>(); SmbmsUser user = new SmbmsUser(); user.setUserName("赵"); user.setUserRole(3); userList = mapper.getUserListByUser(user); for (SmbmsUser user1: userList){ System.out.println(user1.getUserName()); } }
原文地址:https://www.cnblogs.com/loveyoul9/p/11473941.html
时间: 2024-11-08 12:52:10