mybatis通用mapper的Example查詢
mybatis的通用mapper,多用於單表查詢,介面內部為我們提供了單表查詢的基礎查詢語法,可以極大地幫助我們簡化程式設計。
接下來讓我們動手試一試:
我建的是springboot專案:
先導依賴:
<dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.4</version> </dependency>
這裡連線池我用的是阿里的druid,資料庫用的mysql
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
我的資料庫兩張測試用表:
接下來搭建基本框架,先寫實體類:
需要注意的是:使用通用Mapper需要給實體類加註解:
@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; }
別忘了實體類的getter/setter方法,然後dao層實現Mapper介面,注意UserMapper是介面不是類
public interface UserMapper extends Mapper<User> {
}
然後在主類上開啟mapper掃描,
注意是tk.mybatis下的@MapperScan, 不要導成org.mybatis.spring.annotation.MapperScan
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);
}
}
測試結果:通過:控制檯列印
DEBUG 6268 --- [main] com.test.dao.UserMapper.selectByExample : ==> Preparing: SELECT id,name,password,role_id FROM t_users WHERE ( role_id = ? )
DEBUG 6268 --- [main] com.test.dao.UserMapper.selectByExample : ==> Parameters: 1(Long)
DEBUG 6268 --- [main] com.test.dao.UserMapper.selectByExample : <== Total: 4
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=4, name=員工3, password=333, roleId=1, role=Role [id=null, describe=null]]
User [id=5, name=員工4, password=444, roleId=1, role=Role [id=null, describe=null]]
2)測試二:(多條件查詢)
業務類:
/*
(多條件查詢)根據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]]
此外:Example類還為我們封裝了指定列查詢,排除列查詢等許多單表查詢的方法,具體需求具體分析,我們可以去追 Example類的原始碼找尋自己需要的方法。
總結:通用Mapper適合於基於單表的複雜查詢,涉及多張表的查詢建議使用反向對映生成mapper.XML查詢