SSM学习心得

SSM框架学习心得


大致上分为service,controller,dao三层
SSM学习心得_第1张图片
jsp页面层

        $.ajax({
            url: basePath + "/main/getHiveSource",
            type: "post",
            data: {
                sourceType: sourceType
            },
            dataType: "json",
            success: function (data) {
                console.log(data);
                var select = $("#hiveSource");
                select.html("");
                select.append("");
                for (index in data) {
                    select.append("");
                }
            }
        })

页面通过ajax发出请求,发送到URL指定的路径

controller层

@Controller
@RequestMapping("/main")
public class MainController {

 @RequestMapping("/{page}")
    public String pageControl(@PathVariable String page){
        return "main"+File.separator+page;
    }
    
    @ResponseBody
    @RequestMapping(value = "getHiveSource", produces = "application/json;charset=utf-8")
    public String getHiveSource(String sourceType) {
        Integer userId = 1;
        List list = mainService.getDataSourceHive(userId);
        return JSONArray.fromObject(list).toString();
    }
    }

1.在controller层创建一个Controller类,由通过@RequestMapping("/main")控制请求路径
2.通过pageControl方法实现具体的页面跳转.
3.@ResponseBody, @RequestMapping(value = “getHiveSource”)获得请求,并执行下面的方法.参数为jsp页面的各种标签的name,可以通过$("#")获得 ,方法中调用service层的实例化对象的方法

service层
SSM学习心得_第2张图片
接口和实现类

实现类中调用工具类中封装好的方法,如

    @Override
    public Boolean saveSourceHive(Integer userId, Integer group_id, String source_name, String hdfsPath, String[] columnNames, String[] columnTypes) {
        hiveUtil.init();
        // 切换至用户自己的数据库 - user + {userId}
        hiveUtil.changeDatabase("user" + userId);
        // 执行建表语句
        hiveUtil.execute("");
        // 执行导入数据语句
        hiveUtil.execute("");
        return null;
    }

ps:需要在实例化对象和对应工具类前加上注解 如

    @Resource
    private HDFSUtil hdfsUtil;
    @Resource
    private IDataSourceDao dataSourceDao;

@Component("hdfsUtil")
public class HDFSUtil {}

差不多就是这样

dao层 和数据库交互
SSM学习心得_第3张图片
这三个包 po为实体类,mapper为映射关系,扫描dao层方法,如

//dao层的方法名是map中的id 形成映射关系
    

public List selectAllBySourceTypeAndUserId(String source_type, Integer user_id);

当实例化出dao去调用方法时,map会匹配到对应的id,然后执行sql语句,并将结果返回

emmm SSM的配置文件
由于web-info下的文件不能直接跳转所以

//静态文件js/css获取路径

//页面跳转
    
        
        
        
        
    

你可能感兴趣的:(JavaWeb,SSM框架学习心得)