以获取员工资料信息为例学习
查到的链接 http://localhost:8081/index.html#/emp/basic
对应的控制器EmpBasicController,method为getEmployeeByPage
@GetMapping("/")
public RespPageBean getEmployeeByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, Employee employee, Date[] beginDateScope) {
return employeeService.getEmployeeByPage(page, size, employee,beginDateScope);
}
单独执行
http://localhost:8081/employee/basic/?page=1&size=10&name=
返回结果中包含了所有结果数,以及当前页的结果值。
查看EmployeeService
的中getEmployeeByPage
方法
public RespPageBean getEmployeeByPage(Integer page, Integer size, Employee employee, Date[] beginDateScope) {
if (page != null && size != null) {
page = (page - 1) * size;
}
List<Employee> data = employeeMapper.getEmployeeByPage(page, size, employee, beginDateScope);
Long total = employeeMapper.getTotal(employee, beginDateScope);
RespPageBean bean = new RespPageBean();
bean.setData(data);
bean.setTotal(total);
return bean;
}
返回值RespPageBean
public class RespPageBean {
//返回所有的结果数
private Long total;
//当前页的结果值
private List> data;
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List> getData() {
return data;
}
public void setData(List> data) {
this.data = data;
}
}
注意这里的data使用的是泛型类,等同于List
getEmployeeByPage方法对应的mapper类位于
对应的sql信息。这里的id使用转义是为了使用oracle.
<select id="getEmployeeByPage" resultMap="AllEmployeeInfo">
select e.*,p.`id` as pid,p.`name` as pname,n.`id` as nid,n.`name` as nname,d.`id` as did,d.`name` as
dname,j.`id` as jid,j.`name` as jname,pos.`id` as posid,pos.`name` as posname from employee e,nation
n,politicsstatus p,department d,joblevel j,position pos where e.`nationId`=n.`id` and e.`politicId`=p.`id` and
e.`departmentId`=d.`id` and e.`jobLevelId`=j.`id` and e.`posId`=pos.`id`
<if test="emp.name !=null and emp.name!=''">
and e.name like concat('%',#{emp.name},'%')
if>
<if test="emp.politicId !=null">
and e.politicId =#{emp.politicId}
if>
<if test="emp.nationId !=null">
and e.nationId =#{emp.nationId}
if>
<if test="emp.departmentId !=null">
and e.departmentId =#{emp.departmentId}
if>
<if test="emp.jobLevelId !=null">
and e.jobLevelId =#{emp.jobLevelId}
if>
<if test="emp.engageForm !=null and emp.engageForm!=''">
and e.engageForm =#{emp.engageForm}
if>
<if test="emp.posId !=null">
and e.posId =#{emp.posId}
if>
<if test="beginDateScope !=null">
and e.beginDate between #{beginDateScope[0]} and #{beginDateScope[1]}
if>
<if test="page !=null and size!=null">
limit #{page},#{size}
if>
select>
sql中使用了test判断,组成动态sql。
要使mapper起作用,要么需要配置xml配置项,要么使用注解方式进行包扫描
这里通过@MapperScan(basePackages = “org.javaboy.vhr.mapper”)来扫描包来实现的。
@SpringBootApplication
@EnableCaching
@MapperScan(basePackages = "org.javaboy.vhr.mapper")
@EnableScheduling
public class VhrApplication {
public static void main(String[] args) {
SpringApplication.run(VhrApplication.class, args);
}
}
返回结果列resultMap
BaseResultMap定义了基础的返回结果,返回的类型type是employee
<resultMap id="BaseResultMap" type="org.javaboy.vhr.model.Employee">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="gender" property="gender" jdbcType="CHAR"/>
<result column="birthday" property="birthday" jdbcType="DATE"/>
<result column="idCard" property="idCard" jdbcType="CHAR"/>
<result column="wedlock" property="wedlock" jdbcType="CHAR"/>
<result column="nationId" property="nationId" jdbcType="INTEGER"/>
<result column="nativePlace" property="nativePlace" jdbcType="VARCHAR"/>
<result column="politicId" property="politicId" jdbcType="INTEGER"/>
<result column="email" property="email" jdbcType="VARCHAR"/>
<result column="phone" property="phone" jdbcType="VARCHAR"/>
<result column="address" property="address" jdbcType="VARCHAR"/>
<result column="departmentId" property="departmentId" jdbcType="INTEGER"/>
<result column="jobLevelId" property="jobLevelId" jdbcType="INTEGER"/>
<result column="posId" property="posId" jdbcType="INTEGER"/>
<result column="engageForm" property="engageForm" jdbcType="VARCHAR"/>
<result column="tiptopDegree" property="tiptopDegree" jdbcType="CHAR"/>
<result column="specialty" property="specialty" jdbcType="VARCHAR"/>
<result column="school" property="school" jdbcType="VARCHAR"/>
<result column="beginDate" property="beginDate" jdbcType="DATE"/>
<result column="workState" property="workState" jdbcType="CHAR"/>
<result column="workID" property="workID" jdbcType="CHAR"/>
<result column="contractTerm" property="contractTerm" jdbcType="DOUBLE"/>
<result column="conversionTime" property="conversionTime" jdbcType="DATE"/>
<result column="notWorkDate" property="notWorkDate" jdbcType="DATE"/>
<result column="beginContract" property="beginContract" jdbcType="DATE"/>
<result column="endContract" property="endContract" jdbcType="DATE"/>
<result column="workAge" property="workAge" jdbcType="INTEGER"/>
resultMap>
<resultMap id="AllEmployeeInfo" type="org.javaboy.vhr.model.Employee" extends="BaseResultMap">
<association property="nation" javaType="org.javaboy.vhr.model.Nation">
<id column="nid" property="id"/>
<result column="nname" property="name"/>
association>
<association property="politicsstatus" javaType="org.javaboy.vhr.model.Politicsstatus">
<id column="pid" property="id"/>
<result column="pname" property="name"/>
association>
<association property="department" javaType="org.javaboy.vhr.model.Department">
<id column="did" property="id"/>
<result column="dname" property="name"/>
association>
<association property="jobLevel" javaType="org.javaboy.vhr.model.JobLevel">
<id column="jid" property="id"/>
<result column="jname" property="name"/>
association>
<association property="position" javaType="org.javaboy.vhr.model.Position">
<id column="posid" property="id"/>
<result column="posname" property="name"/>
association>
resultMap>
然后通过extends高级映射关联,通过association关联其他模型
这里的column='nid’指的是返回sql结果的列nid
property指的是Nation中定义的id属性
其他字段以此类推
<association property="nation" javaType="org.javaboy.vhr.model.Nation">
<id column="nid" property="id"/>
<result column="nname" property="name"/>
association>
这样执行查询后返回整个数据结果信息就是
id: 1,
name: "江南一点雨",
gender: "男",
birthday: "1990-01-01",
idCard: "610122199001011256",
wedlock: "已婚",
nationId: 1,
nativePlace: "陕西",
politicId: 13,
email: "[email protected]",
phone: "18565558897",
address: "深圳市南山区",
departmentId: 5,
jobLevelId: 9,
posId: 31,
engageForm: "劳务合同",
tiptopDegree: "本科",
specialty: "信息管理与信息系统",
school: "深圳大学",
beginDate: "2018-01-01",
workState: "在职",
workID: "00000001",
contractTerm: 2,
conversionTime: "2018-04-01",
notWorkDate: null,
beginContract: "2018-01-01",
endContract: "2020-01-01",
workAge: null,
nation: {
id: 1,
name: "汉族"
},
politicsstatus: {
id: 13,
name: "普通公民"
},
department: {
id: 5,
name: "总办",
parentId: null,
depPath: null,
enabled: null,
children: [ ],
result: null,
parent: null
},
jobLevel: {
id: 9,
name: "教授",
titleLevel: null,
createDate: null,
enabled: null
},
position: {
id: 31,
name: "市场总监",
createDate: null,
enabled: null
},
salary: null
},
另外,getEmployeeByPage这个方法中还设置了默认参数,用于初始时可以设置显示页数默认为每页10条
@RequestParam(defaultValue = “1”)
@RequestParam(defaultValue = “10”)
public RespPageBean getEmployeeByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, Employee employee, Date[] beginDateScope) {
@PostMapping("/")
public RespBean addEmp(@RequestBody Employee employee) {
if (employeeService.addEmp(employee) == 1) {
return RespBean.ok("添加成功!");
}
return RespBean.error("添加失败!");
}
在服务service中的具体保存方法中使用了rabbitmq,邮件服务
public Integer addEmp(Employee employee) {
Date beginContract = employee.getBeginContract();
Date endContract = employee.getEndContract();
double month = (Double.parseDouble(yearFormat.format(endContract)) - Double.parseDouble(yearFormat.format(beginContract))) * 12 + (Double.parseDouble(monthFormat.format(endContract)) - Double.parseDouble(monthFormat.format(beginContract)));
employee.setContractTerm(Double.parseDouble(decimalFormat.format(month / 12)));
int result = employeeMapper.insertSelective(employee);
if (result == 1) {
Employee emp = employeeMapper.getEmployeeById(employee.getId());
//生成消息的唯一id
String msgId = UUID.randomUUID().toString();
MailSendLog mailSendLog = new MailSendLog();
mailSendLog.setMsgId(msgId);
mailSendLog.setCreateTime(new Date());
mailSendLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
mailSendLog.setRouteKey(MailConstants.MAIL_ROUTING_KEY_NAME);
mailSendLog.setEmpId(emp.getId());
mailSendLog.setTryTime(new Date(System.currentTimeMillis() + 1000 * 60 * MailConstants.MSG_TIMEOUT));
mailSendLogService.insert(mailSendLog);
rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME, MailConstants.MAIL_ROUTING_KEY_NAME, emp, new CorrelationData(msgId));
}
return result;
}
先看对应的Insert sql语句
<insert id="insertSelective" parameterType="org.javaboy.vhr.model.Employee" useGeneratedKeys="true"
keyProperty="id">
insert into employee
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
if>
<if test="name != null">
name,
if>
<if test="gender != null">
gender,
if>
<if test="birthday != null">
birthday,
if>
<if test="idCard != null">
idCard,
if>
<if test="wedlock != null">
wedlock,
if>
<if test="nationId != null">
nationId,
if>
<if test="nativePlace != null">
nativePlace,
if>
<if test="politicId != null">
politicId,
if>
<if test="email != null">
email,
if>
<if test="phone != null">
phone,
if>
<if test="address != null">
address,
if>
<if test="departmentId != null">
departmentId,
if>
<if test="jobLevelId != null">
jobLevelId,
if>
<if test="posId != null">
posId,
if>
<if test="engageForm != null">
engageForm,
if>
<if test="tiptopDegree != null">
tiptopDegree,
if>
<if test="specialty != null">
specialty,
if>
<if test="school != null">
school,
if>
<if test="beginDate != null">
beginDate,
if>
<if test="workState != null">
workState,
if>
<if test="workID != null">
workID,
if>
<if test="contractTerm != null">
contractTerm,
if>
<if test="conversionTime != null">
conversionTime,
if>
<if test="notWorkDate != null">
notWorkDate,
if>
<if test="beginContract != null">
beginContract,
if>
<if test="endContract != null">
endContract,
if>
<if test="workAge != null">
workAge,
if>
trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
if>
<if test="gender != null">
#{gender,jdbcType=CHAR},
if>
<if test="birthday != null">
#{birthday,jdbcType=DATE},
if>
<if test="idCard != null">
#{idCard,jdbcType=CHAR},
if>
<if test="wedlock != null">
#{wedlock,jdbcType=CHAR},
if>
<if test="nationId != null">
#{nationId,jdbcType=INTEGER},
if>
<if test="nativePlace != null">
#{nativePlace,jdbcType=VARCHAR},
if>
<if test="politicId != null">
#{politicId,jdbcType=INTEGER},
if>
<if test="email != null">
#{email,jdbcType=VARCHAR},
if>
<if test="phone != null">
#{phone,jdbcType=VARCHAR},
if>
<if test="address != null">
#{address,jdbcType=VARCHAR},
if>
<if test="departmentId != null">
#{departmentId,jdbcType=INTEGER},
if>
<if test="jobLevelId != null">
#{jobLevelId,jdbcType=INTEGER},
if>
<if test="posId != null">
#{posId,jdbcType=INTEGER},
if>
<if test="engageForm != null">
#{engageForm,jdbcType=VARCHAR},
if>
<if test="tiptopDegree != null">
#{tiptopDegree,jdbcType=CHAR},
if>
<if test="specialty != null">
#{specialty,jdbcType=VARCHAR},
if>
<if test="school != null">
#{school,jdbcType=VARCHAR},
if>
<if test="beginDate != null">
#{beginDate,jdbcType=DATE},
if>
<if test="workState != null">
#{workState,jdbcType=CHAR},
if>
<if test="workID != null">
#{workID,jdbcType=CHAR},
if>
<if test="contractTerm != null">
#{contractTerm,jdbcType=DOUBLE},
if>
<if test="conversionTime != null">
#{conversionTime,jdbcType=DATE},
if>
<if test="notWorkDate != null">
#{notWorkDate,jdbcType=DATE},
if>
<if test="beginContract != null">
#{beginContract,jdbcType=DATE},
if>
<if test="endContract != null">
#{endContract,jdbcType=DATE},
if>
<if test="workAge != null">
#{workAge,jdbcType=INTEGER},
if>
trim>
insert>
这里使用了mybatis
的trim标签
mybatis
的trim
标签一般用于去除sql语句中多余的and关键字,逗号,或者给sql语句前拼接 “where“、“set“以及“values(“ 等前缀,或者添加“)“等后缀,可用于选择性插入、更新、删除或者条件查询等操作
prefix前缀
suffix加的后缀
类似拼接成insert into employee (id …)
如果某个test不符合,不会单独出现逗号错误
这里类似value(值1,值2,值3)
类似trim的功能实现还有where 操作
保存成功后发送一个消息
rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME, MailConstants.MAIL_ROUTING_KEY_NAME, emp, new CorrelationData(msgId));
exchange交换器
routingKey 路由
object 数据值
@Override
public void convertAndSend(String exchange, String routingKey, final Object object,
@Nullable CorrelationData correlationData) throws AmqpException {
send(exchange, routingKey, convertMessageIfNecessary(object), correlationData);
}
通过192.168.78.130:15672访问rabbitmq管理控制台
通过默认的guest/guest登陆
对应的交换器 这里与上面代码对应的名称是一样的
对应的路由
点击路由进去可以看到最近接收到的信息
但是这里显示的都是序列化后的信息,不能直观显示我传递的employee信息。
通过将结果列进行指定为json类型的string类型,可以在rabbitmq中直接查看消息
上面的方法没有指定传输数据的类型。
通过MessageProperties 来指定数据类型application/json
ObjectMapper mapper=new ObjectMapper();
String json=mapper.writeValueAsString(emp);
MessageProperties messageProperties= new MessageProperties();
messageProperties.setContentType("application/json");
Message message=new Message(json.getBytes(),messageProperties);
rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME, MailConstants.MAIL_ROUTING_KEY_NAME, message, new CorrelationData(msgId));