vhr微人事系統學習-(2)springboot

springboot

顯示用戶

以獲取員工資料信息爲例學習
在這裏插入圖片描述
查到的鏈接 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<T> data;

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) {
     

增加用戶

在這裏插入圖片描述
彈出窗口是前端處理,這裏看下後端的保存方法addEmp

 @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標籤
mybatistrim標籤一般用於去除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));

新增用戶後發現也是json類型顯示。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章