1. 程式人生 > >微信企業號會議助手---一個簡單的SSM例項

微信企業號會議助手---一個簡單的SSM例項

前面基本的配置和工具類都差不多搞定了,如果有缺少在程式碼看到也不難理解了,下面來講一個小例子,迅速讓你理解ssm開發。
這裡寫圖片描述
controller就是spring的控制器,所有前端的ajax的入口就在這,ajax怎麼進來呢?
我就講講一個跳轉到增加會議介面,和建立會議的方法:

@RequestMapping(value = "/addMeeting")
    public ModelAndView addMeeting(String userid){
        Map<String,Object> data = new HashMap<String,Object>
(); data.put("userid",userid); ModelAndView mav = new ModelAndView("/meeting/meeting_add",data); return mav; }

這裡應該很簡單易懂,注意下”/meeting/meeting_add”這個路徑,就是跳轉到WEB-INF/jsp/meeting/meeting_add.jsp。這個我前面SSM架構有講過,在spring配置中限制了跳轉資料夾。
在jsp中接受引數就用:

<%
    String code = request
.getParameter("code"); String userid = request.getParameter("userid"); %>
<script> var wechat_code = '<%=code%>'; var wechat_userid = '<%=userid%>'; </script>

注意一點,這裡帶了單引號,不然要報錯,後面判斷它是否null的時候,要和帶單引號的null字串比較。如wechat_code!=’null’不然你會發現怎麼都不是null。

@Controller
@RequestMapping
("/meeting") public class MeetingController { @RequestMapping(value="/roomCreate",method = RequestMethod.POST) public @ResponseBody String roomCreate(HttpServletRequest request,@RequestBody Room room) throws Exception{ room.setRoom_uuid(Uuid.getUUID()); meetingService.roomCreate(room); return "11111"; }`

先講講ajax怎麼跳轉到這裡的:

$.ajax({
            type:"POST",
            contentType:"application/json",
            url:getRootPath()+"meeting/roomCreate.do",
            data:JSON.stringify(sub_results),
            success:function(data){
                $toast.find('p').html("儲存成功");
                $toast.fadeIn(100);
                setTimeout(function () {
                    $toast.fadeOut(100);
                    window.location.href = getRootPath() + "meeting/init.do";
                }, 2000);
            },
            error:function(){
                $toast.find('p').html("儲存失敗");
                $toast.fadeIn(100);
                setTimeout(function () {
                    $toast.fadeOut(100);
                    window.location.href = getRootPath() + "meeting/init.do";
                }, 2000);
            }
        });

引數型別先,contentType:”application/json”,這是申明傳出引數是json,dataType:”application/json”是申明傳入引數是json,不然預設是application/x-www-form-urlencoded; charset=utf-8”,後臺講無法用,@RequestBody Room room講json直接放入Room物件中。具體區別用法,可以查閱相關資料,我這裡就提點一下。前面我講過要事先這種json解析進入物件需要的一些配置。

url:getRootPath()+"meeting/roomCreate.do",。getRootPath()是我封裝的拿到專案的根目錄。如下:
//js獲取專案根路徑,如: http://localhost:8083/uimcardprj
function getRootPath(){
    //獲取當前網址,如: http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath=window.document.location.href;
    //獲取主機地址之後的目錄,如: uimcardprj/share/meun.jsp
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    //獲取主機地址,如: http://localhost:8083
    var localhostPaht=curWwwPath.substring(0,pos);
    //獲取帶"/"的專案名,如:/uimcardprj
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    return(localhostPaht+projectName+"/");
}

然後再回到spring的入口controller。注意點再放到上面那個專案結構的圖片。
controller調了meetingService.roomCreate(room);就是服務層,類似於以前傳統寫法的DAO層,imp裡面放的是service層的實現類。meetingService採用Spring注入的方式。如下:

    @Autowired
    private MeetingService meetingService;

sevice層結構如下:
這裡寫圖片描述
一個抽象類,一個實現類,實現類中邏輯如下:

@Service
public class MeetingServiceImp implements MeetingService{
    @Autowired
    private MeetingMapper meetingMapper;

    public void roomCreate(Room room) {
        meetingMapper.roomCreate(room);
    }
}

如果要補充一些邏輯就在roomCreate方法中加。meetingMapper是mybatis層了,來處理資料物件的儲存。結構如下:
這裡寫圖片描述
MeetingMapper.java是介面類,定義給mybatis查詢的方法如下:

public interface MeetingMapper {
    /**
     * 建立會議室
     * @param room
     */
    public void roomCreate(Room room);
}

MeetingMapper.xml是實現mybatis的具體資料庫儲存語言的。直接上程式碼:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.supcon.mo.common.meeting.data.MeetingMapper">
<insert id="roomCreate" parameterType="Room">
        INSERT INTO `meeting_room`
        <trim prefix="(" suffix=")" suffixOverrides=",">
            `room_uuid`,
            `room_name`,
            `room_city`,
            `office_building`,
            `person_hold`,
            `equipment_tools`,
            `optional_stime`,
            `optional_etime`,
            <if test="is_examine != null">
                `is_examine`,
            </if>
            `user_ids`,
            <if test="is_freeze != null">
                `Is_freeze`,
            </if>
            `freeze_cause`,
            <if test="is_delete != null">
                `Is_delete`,
            </if>
            `Create_time`,
            `Modify_time`,
            `Create_person`,
            `Modify_person`
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            #{room_uuid},
            #{room_name},
            #{room_city},
            #{office_building},
            #{person_hold},
            #{equipment_tools},
            #{optional_stime},
            #{optional_etime},
            <if test="is_examine != null">
                #{is_examine},
            </if>
            #{user_ids},
            <if test="is_freeze != null">
                #{is_freeze},
            </if>
            #{freeze_cause},
            <if test="is_delete != null">
                #{is_delete},
            </if>
            now(),
            #{modify_time},
            #{create_person},
            #{modify_person}
        </trim>
    </insert>

mybatis的具體語法我就不介紹了。網上一大堆,我就告訴你們一個整體的SSM的流程。在附上我建立的Room資料庫:

create table meeting_room(
    room_uuid Varchar(32) primary key,
    room_name Varchar(50) not null,
    room_city Varchar(4) not null,
    office_building Varchar(4) not null,
    person_hold Int(4) not null,
    equipment_tools Varchar(100),
    optional_stime  varchar(10) not null,
    optional_etime  varchar(10) not null,
    is_examine  Varchar(1) not null default '0',
    user_ids Text,
    Is_freeze Varchar(1) not null default '0',
    freeze_cause Varchar(300),
    Is_delete Varchar(1) not null default '0',
    Create_time DATETIME not null,
    Modify_time DATETIME,
    Create_person Varchar(32) not null,
    Modify_person Varchar(32)
);

怎麼測試呢?
直接在前端頁面做一個按鈕,然後自己拼一個json物件,呼叫上邊的ajax傳到後臺去儲存,然後再資料庫看是否儲存成功。

<button onclick=save()>
<script>
function save(){
        var sub_results = {};
        sub_results.room_name = ""
        sub_results.room_city = "";
        sub_results.office_building = "";
        sub_results.person_hold = "";
        sub_results.equipment_tools = "";   
        sub_results.optional_stime = "08:00";
        sub_results.optional_etime = "17:00";
        sub_results.create_person = user_id;
        _is_examine?sub_results.is_examine = "0"
}
</script>

在上述的ajax的data中放入sub_results;不過要將json轉化為字串:JSON.stringify(sub_results);

好了。大概都是這麼多,如有什麼問題可在下邊留言,後面將會講更深層次的東西,前面簡單化的必須要看懂。不然後邊會不明所以!