@Entity
@Table(name="t_users")
public class User implements Serializable{
private static final long serialVersionUID = 8941012353272388061L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column
private String name;
@Column
private String password;
@Column(name="role_id")
private Long roleId;
}
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.test.dao")
public class MapperTestApplication {
public static void main(String[] args) {
SpringApplication.run(MapperTestApplication.class, args);
}
}
准备就绪,接下来测试;
1)、(单条件查询)根据role_id查询所有人
业务类:
/*
* 根据role_id查询所有人
* select * from t_users where role_id = ?;
*/
@Override
public List<User> selectByUser1(User user) {
Example example = new Example(User.class,true,true);
Example.Criteria ec = example.createCriteria();
ec.andEqualTo("roleId", user.getRoleId());
return userMapper.selectByExample(example);
}
测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
public class MapperTestApplicationTests {
@Autowired
private IUserServ userServ;
@Test
public void contextLoads() {
User user = new User();
user.setRoleId(1L);
List<User> ulist = userServ.selectByUser1(user);
ulist.forEach(System.err::println);
}
}
/*
(多条件查询)根据role_id查询id大于min小于max,或者name为?的人
* select * from t_users where role_id = ? and id between min and max and name = ?
*/
@Override
public List<User> selectByUser2(Long min,Long max,User user) {
Example example = new Example(User.class,true,true);
Example.Criteria ec = example.createCriteria();
ec.andEqualTo("roleId", user.getRoleId()).andBetween("id", min, max).orEqualTo("name", user.getName());
return userMapper.selectByExample(example);
}
测试类:
@Test
public void contextLoads() {
User user = new User();
user.setRoleId(1L);
user.setName("员工4");
List<User> ulist = userServ.selectByUser2(2L,3L,user);
ulist.forEach(System.err::println);
}
测试结果:通过。控制台打印:
DEBUG 14728 --- [main] com.test.dao.UserMapper.selectByExample : ==> Preparing: SELECT id,name,password,role_id FROM t_users WHERE ( role_id = ? and id between ? and ? or name = ? )
DEBUG 14728 --- [main] com.test.dao.UserMapper.selectByExample : ==> Parameters: 1(Long), 2(Long), 3(Long), 员工4(String)
DEBUG 14728 --- [main] com.test.dao.UserMapper.selectByExample : <== Total: 3
User [id=2, name=员工1, password=111, roleId=1, role=Role [id=null, describe=null]]
User [id=3, name=员工2, password=222, roleId=1, role=Role [id=null, describe=null]]
User [id=5, name=员工4, password=444, roleId=1, role=Role [id=null, describe=null]]
3)排序(我们继续用刚才的多条件查询,结果倒序)
select * from t_users where ( role_id = ? and id between ? and ? or name = ? ) order by id DESC
业务层:(加example.setOrderByClause("id DESC");)
@Override
public List<User> selectByUser2(Long min,Long max,User user) {
Example example = new Example(User.class,true,true);
Example.Criteria ec = example.createCriteria();
ec.andEqualTo("roleId", user.getRoleId()).andBetween("id", min, max).orEqualTo("name", user.getName());
example.setOrderByClause("id DESC");
return userMapper.selectByExample(example);
}
测试类不变:
测试结果:通过:控制台打印
DEBUG 10780 --- [main] com.test.dao.UserMapper.selectByExample : ==> Preparing: SELECT id,name,password,role_id FROM t_users WHERE ( role_id = ? and id between ? and ? or name = ? ) order by id DESC
DEBUG 10780 --- [main] com.test.dao.UserMapper.selectByExample : ==> Parameters: 1(Long), 2(Long), 3(Long), 员工4(String)
DEBUG 10780 --- [main] com.test.dao.UserMapper.selectByExample : <== Total: 3
User [id=5, name=员工4, password=444, roleId=1, role=Role [id=null, describe=null]]
User [id=3, name=员工2, password=222, roleId=1, role=Role [id=null, describe=null]]
User [id=2, name=员工1, password=111, roleId=1, role=Role [id=null, describe=null]]