微信企业号会议助手---一个简单的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);

好了。大概都是这么多,如有什么问题可在下边留言,后面将会讲更深层次的东西,前面简单化的必须要看懂。不然后边会不明所以!

猜你喜欢

转载自blog.csdn.net/hjucook1988/article/details/70224572