mybatis通⽤mapper的Example查询
mybatis的通⽤mapper,多⽤于单表查询,接⼝内部为我们提供了单表查询的基础查询语法,可以极⼤地帮助我们简化编程。接下来让我们动⼿试⼀试:
我建的是springboot项⽬:
先导依赖:
<dependency>
梦见很多蛇是什么意思<groupId&batis</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>
我的数据库两张测试⽤表:
t_roles t_urs
接下来搭建基本框架,先写实体类:
需要注意的是:使⽤通⽤Mapper需要给实体类加注解:
@Entity
@Table(name="t_urs")
public class Ur implements Serializable{
private static final long rialVersionUID = 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/tter⽅法,然后dao层实现Mapper接⼝,注意UrMapper是接⼝不是类
public interface UrMapper extends Mapper<Ur> {
}
然后在主类上打开mapper扫描,
注意是tk.mybatis下的@MapperScan, 不要导成batis.spring.annotation.MapperScan毕竟西湖
batis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("st.dao")
public class MapperTestApplication {
public static void main(String[] args) {
SpringApplication.run(MapperTestApplication.class, args);
}
}
准备就绪,接下来测试;
1)、(单条件查询)根据role_id查询所有⼈
业务类:
/*
* 根据role_id查询所有⼈
* lect * from t_urs where role_id = ?;
*/
@Override
public List<Ur> lectByUr1(Ur ur) {
Example example = new Example(Ur.class,true,true);
Example.Criteria ec = ateCriteria();
ec.andEqualTo("roleId", ur.getRoleId());
return urMapper.lectByExample(example);
}
测试类:
元宵怎么做好吃@RunWith(SpringRunner.class)
@SpringBootTest
public class MapperTestApplicationTests {
@Autowired
private IUrServ urServ;
@Test
public void contextLoads() {
Ur ur = new Ur();
ur.tRoleId(1L);
List<Ur> ulist = urServ.lectByUr1(ur);
ulist.::println);
}
}
测试结果:通过:控制台打印
DEBUG 6268 --- [main] st.dao.UrMapper.lectByExample : ==> Preparing: SELECT id,name,password,role_id FROM t_urs WHERE ( role_id DEBUG 6268 --- [main] st.dao.UrMapper.lectByExample : ==> Parameters: 1(Long)
DEBUG 6268 --- [main] st.dao.UrMapper.lectByExample : <== Total: 4
Ur [id=2, name=员⼯1, password=111, roleId=1, role=Role [id=null, describe=null]]
Ur [id=3, name=员⼯2, password=222, roleId=1, role=Role [id=null, describe=null]]
Ur [id=4, name=员⼯3, password=333, roleId=1, role=Role [id=null, describe=null]]
Ur [id=5, name=员⼯4, password=444, roleId=1, role=Role [id=null, describe=null]]
2)测试⼆:(多条件查询)
附红细胞体业务类:
/*
(多条件查询)根据role_id查询id⼤于min⼩于max,或者name为?的⼈
* lect * from t_urs where role_id = ? and id between min and max and name = ?
*/
@Override
public List<Ur> lectByUr2(Long min,Long max,Ur ur) {
Example example = new Example(Ur.class,true,true);
Example.Criteria ec = ateCriteria();
ec.andEqualTo("roleId", ur.getRoleId()).andBetween("id", min, max).orEqualTo("name", ur.getName());
return urMapper.lectByExample(example);
}
测试类:
@Test
public void contextLoads() {
Ur ur = new Ur();
ur.tRoleId(1L);
ur.tName("员⼯4");
List<Ur> ulist = urServ.lectByUr2(2L,3L,ur);廉政行动2004
ulist.::println);
}
测试结果:通过。控制台打印:
DEBUG 14728 --- [main] st.dao.UrMapper.lectByExample : ==> Preparing: SELECT id,
name,password,role_id FROM t_urs WHERE ( role_ DEBUG 14728 --- [main] st.dao.UrMapper.lectByExample : ==> Parameters: 1(Long), 2(Long), 3(Long), 员⼯4(String)
DEBUG 14728 --- [main] st.dao.UrMapper.lectByExample : <== Total: 3
Ur [id=2, name=员⼯1, password=111, roleId=1, role=Role [id=null, describe=null]]
Ur [id=3, name=员⼯2, password=222, roleId=1, role=Role [id=null, describe=null]]
Ur [id=5, name=员⼯4, password=444, roleId=1, role=Role [id=null, describe=null]]
3)排序(我们继续⽤刚才的多条件查询,结果倒序)
lect * from t_urs where ( role_id = ? and id between ? and ? or name = ? ) order by id DESC
业务层:(加example.tOrderByClau("id DESC");)
@Override
public List<Ur> lectByUr2(Long min,Long max,Ur ur) {
Example example = new Example(Ur.class,true,true);
Example.Criteria ec = ateCriteria();
ec.andEqualTo("roleId", ur.getRoleId()).andBetween("id", min, max).orEqualTo("name", ur.getName());
富贵竹水养技巧example.tOrderByClau("id DESC");
return urMapper.lectByExample(example);
}
测试类不变:
测试结果:通过:控制台打印
DEBUG 10780 --- [main] st.dao.UrMapper.lectByExample : ==> Preparing: SELECT id,name,password,role_id FROM t_urs WHERE ( role_ DEBUG 10780 --- [main] st.dao.UrMapper.lectByExample : ==> Parameters: 1(Long), 2(Long), 3(Long), 员⼯4(String)
DEBUG 10780 --- [main] st.dao.UrMapper.lectByExample : <== Total: 3
Ur [id=5, name=员⼯4, password=444, roleId=1, role=Role [id=null, describe=null]]
Ur [id=3, name=员⼯2, password=222, roleId=1, role=Role [id=null, describe=null]]
Ur [id=2, name=员⼯1, password=111, roleId=1, role=Role [id=null, describe=null]]
此外:Example类还为我们封装了指定列查询,排除列查询等许多单表查询的⽅法,具体需求具体分析,我们可以去追 Example类的源码
找寻⾃⼰需要的⽅法。
总结:通⽤Mapper适合于基于单表的复杂查询,涉及多张表的查询建议使⽤反向映射⽣成mapper.XML查询
千古风流人物