pojo中的实体类属性和表字段不一致时,可以通过添加@Column("表字段")来注释解决。前提是添加如下依赖。@Table("表名") @Id("主键") @Column("表字段")....的使用方式:
@Table(name = "user") public class User { @Id //盖注释加到那个属性上面,意味着这个属性就是表主键 private Integer id; private String name; private String password; private Integer did; //注释该属性不是其user表字段,不属于user表 @Column(insertable = false) private Dept dept; }@Data public class Dept { private Integer did; @Column(name = "dept_name")//该注释作用是说depName属性对应的表字段为dept_name private String depName; }
${}和#{}的区别javax.persistence persistence-api1.0.2 provided
#{}在mapper.xml里面为占位符,而${}为字符串拼接。能使用#{}就不要使用${}。
模糊查询实现方式注意:使用${}必须加单引号
方式一 select * from user where name like '% ${username} %' 方式二 select * from user where name like concat('%', #{username} ,'%') 方式三 select * from user where name like "%" #{username} "%"批量删除
delete from user where id in ${ids} ids为前端传递过来的字符串,如:1,2,3主键回填
resultType和resultMap的区别和使用方式insert into t_user values(null,#{name},#{pwd}) useGeneratedkeys 主键回填设置为true keyProperty 回填之后的主键赋值给id
如果返回值是实体类,则使用resultType即可,如果实体类里面包含了不属于该实体类所对应的表属性,使用resultMap自定义映射。如下所示:要返回员工信息同时带部门信息,就要使用resultMap封装pojo实体类属性和表字段的映射关系。
-
部门表
-
员工表
多对一实例(三种实现方式)
//pojo层 @Data public class User { @Id private Integer id; private String name; private String password; private Integer did; //注释该属性不是其user表字段,不属于user表 @Column(insertable = false) private Dept dept; } @Data public class Dept { private Integer did; private String depName; } //controller层 public class UserController{ @Authoreid private Userservice userservice; @ApiOperation(value = "查询员工部门信息") @GetMapping("/dept/{id}") public User getUserWithDept(@PathVariable Integer id){ return userServie.getUserWithDept(id); } } //service接口层 public interface UserServie { User getUserWithDept(Integer id); } //service实现层 public class UserserviceImpl implements Userservice{ @Override public User getUserWithDept(Integer id) { User userWithDept = userMapper.getUserWithDept(id); return userWithDept; } } //mapper接口层 public class UserMapper{ User getUserWithDept(@Param("id") Integer id); }
方式一:级联映射方式处理多对一映射方式
select * from user left join dept on user.did=dept.did where user.id=#{id}
运行结果:
方式二:association标签 处理多对一映射方式
保持方式一不变,只需要替换resultMap中的dept属性即可。
方式三:分步查询
1、查询部门信息
部门mapper层 @Repository public interface DeptMapper { Dept getDeptById(@Param("did") Integer did); }
部门mapper.xml层
2、查询用户信息,并将1、中查询到的部门信息封装到dept属性中
注:
property="dept" //指用户实体类中的部门属性dept
column="did" //分步查询的条件,就是1.中查询后属性did被赋的值,作为条件查询2
select="com.example.mybatis.mapper.DeptMapper.getDeptById">//分布查询的方式,指向部门mapper接口层中的具体查询方式的路径
由上图运行过程可知:确实执行了两次sql。第一次查询user;第二次通过第一次获取的did查询dept。
一对多实例方式一:collection标签处理一对多映射关系(部门——>多个员工)
pojo层 @Data public class Dept { private Integer did; private String depName; @Column(insertable = false) private Listusers; } @Data @Table(name = "user") public class User { @Id //盖注释加到那个属性上面,意味着这个属性就是表主键 private Integer id; private String name; private String password; private Integer did; //注释该属性不是其user表字段,不属于user表 @Column(insertable = false) private Dept dept; } Mapper接口层 @Repository public interface DeptMapper { Dept getDeptAndUsers(@Param("did") Integer did); }
Mapper.xml持久层
controller层 @RestController @RequestMapping("dept") @Api(value = "提供部门相关管理", tags = "部门管理") public class DeptController { @Autowired private DeptService deptService; @ApiOperation(value = "查找部门下的用户") @GetMapping("/{did}") public Dept getDeptAndUsers(@PathVariable Integer did){ return deptService.getDeptAndUsers(did); } } Service业务接口层 public interface UserServie { User getUserWithDept(Integer id); } Servie业务实现层 @Service public class DeptServiceImpl implements DeptService { @Autowired private DeptMapper deptMapper; @Override public Dept getDeptAndUsers(Integer did) { return deptMapper.getDeptAndUsers(did); } }
注:上图执行过程可知:确实采用左连接一步查询 ,并将数据进行了分装。
方式二:分步查询
注:处了mapper.xml层和方式一不太一样,其他层都差不多,只需要在员工中加一个根据did查找员工的接口和xml即可。
UserMapper接口层 ListgetDeptWithEmployee(@Param("did") Integer did); userMapper.xml持久层 select * from user where did=#{did}
DeptMapper.xml持久层select * from dept where did=#{did}
注:由上图可知:确实分两部查找。