//ajax的post方式提交表单//$("form").serialize()将表单序列号为key-value的形式的字符串//(这里注意,在form之 中的input里需要增加 name )$.post("/item/save",$("form").serialize(),function(data)){
}
function formToJson(formObj){
var o={};
var a=formObj.serializeArray();
$.each(a, function() {if(this.value){
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name]=[ o[this.name] ];
}
o[this.name].push(this.value || null);
}else {
if($("[name='"+this.name+"']:checkbox",formObj).length){
o[this.name]=[this.value];
}else{
o[this.name]=this.value || null;
}
}
}
});
return JSON.stringify(o);
};
通过逆向工程生成:
generatorConfig.xml(使用方式见标黄处)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><!-- 配置Run As Maven build : Goals 参数 : mybatis-generator:generate -Dmybatis.generator.overwrite=true -->
<!-- 配置 tableName,使用 Run As Maven build 生成 dao model 层 -->
<generatorConfiguration>
<!-- 配置文件路径 -->
<properties url="${mybatis.generator.generatorConfig.properties}"/><!--数据库驱动包路径 -->
<classPathEntry location="${drive.class.path}"/><context id="DB2Tables" targetRuntime="MyBatis3">
<!--关闭注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator><!--数据库连接信息 -->
<jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}"
password="${jdbc.password}">
</jdbcConnection><!--生成的model 包路径 -->
<javaModelGenerator targetPackage="${model.package}" targetProject="${target.project}">
<property name="enableSubPackages" value="ture"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator><!--生成xml mapper文件 路径 -->
<sqlMapGenerator targetPackage="${xml.mapper.package}" targetProject="${target.project}">
<property name="enableSubPackages" value="ture"/>
</sqlMapGenerator><!-- 生成的Dao接口 的包路径 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="${dao.package}" targetProject="${target.project}">
<property name="enableSubPackages" value="ture"/>
</javaClientGenerator><!--对应数据库表名 -->
<!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
是否生成 example类 -->
<!-- <table schema="${jdbc.schema}" tableName="director_task"
domainObjectName="DirectorTask" enableCountByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true">
</table>
<table schema="${jdbc.schema}" tableName="task"
domainObjectName="Task" enableCountByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true">
</table>
<table schema="${jdbc.schema}" tableName="regular"
domainObjectName="Regular" enableCountByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true">
</table>
<table schema="${jdbc.schema}" tableName="issue_member"
domainObjectName="IssueMember" enableCountByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true">
</table>
<table schema="${jdbc.schema}" tableName="consultant"
domainObjectName="Consultant" enableCountByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true">
</table>
<table schema="${jdbc.schema}" tableName="doctor"
domainObjectName="Doctor" enableCountByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true">
</table>
<table schema="${jdbc.schema}" tableName="member"
domainObjectName="Member" enableCountByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true">
</table>
<table schema="${jdbc.schema}" tableName="consultant_doctor_relative"
domainObjectName="ConsultantDoctorRelative" enableCountByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true">
</table>
<table schema="${jdbc.schema}" tableName="contract_information"
domainObjectName="ContractInformation" enableCountByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true">
</table>
<table schema="${jdbc.schema}" tableName="right_info"
domainObjectName="RightInfo" enableCountByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true">
</table>
<table schema="${jdbc.schema}" tableName="role_info"
domainObjectName="RoleInfo" enableCountByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true">
</table>
<table schema="${jdbc.schema}" tableName="role_right"
domainObjectName="RoleRight" enableCountByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true">
</table>
<table schema="${jdbc.schema}" tableName="user_role"
domainObjectName="UserRole" enableCountByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true">
</table>
<table schema="${jdbc.schema}" tableName="issue_member"
domainObjectName="IssueMember" enableCountByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true">
</table>
-->
<table schema="${jdbc.schema}" tableName="user_list"
domainObjectName="UserList" enableCountByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
enableUpdateByExample="true">
</table>
</context>
</generatorConfiguration>
generatorConfig.properties
# 数据库驱动jar 路径
drive.class.path=C:\\Users\\lmk\\.m2\\repository\\mysql\\mysql-connector-java\\5.1.30\\mysql-connector-java-5.1.30.jar# 数据库连接参数
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://?useUnicode=true&characterEncoding=utf-8
jdbc.username=
jdbc.password=
jdbc.schema=HealthControlSystem# 包路径配置
model.package=cn.edu.bupt.springmvc.web.model.zunzun
dao.package=cn.edu.bupt.springmvc.web.dao.zunzun
xml.mapper.package=cn.edu.bupt.springmvc.web.dao.zunzuntarget.project=src/main/java
2,Service层
定义service接口(interface下的方法不加以实现,连{}也没有)
实现类impl(implement)
实现逻辑主要是以下两种:
接受或者new 目标对象,对象属性(get set方式)
调用Mapper的insert方法插入数据。
在service层中关于调用mapper类的方法,有一类方法:XXXByExammpleWithBLOBs(example)
//根据cid查询规格参数模板
TbItemParamExample example = new TbItemParamExample(); //new 一个
Criteria criteria = example.createCriteria();
criteria.andItemCatIdEqualTo(cid);
//判断是否查询到结果
if (list != null&&list.size() > 0) {
TbItemParam itemParam = list.get(0); // get(0)
return TaotaoResult.ok(itemParam);
}
3,Controller层
主要的实现逻辑:
指定method的post或者get方法以及请求的路径
调用service进行数据的CRUD
@PathVariable
@ResponseBody
@RequestMapping
@Controller
@Autowired(用@resource代替)
4,涉及的json与java知识
//json数据A转换成java对象
List<Map>mapList = JsonUtils.jsonToList(A, Map.class);
String json = JsonUtils.objectToJson(result);
//java处理
sb = new StringBUffer()对象.append()
map.get("XXX")方式取得
return sb.toString()
//List的使用
List resultList = new ArrayList<>();
resultList.add();
//判空
StringUtils.isBlank(callback);
三、Js跨域
跨域的定义:
1、如果两个url的域名不同
2、Url相同,端口不同也是跨域
3、Ip不同也是跨域
可以使用jsonp解决跨域的问题。
1、在js中不能跨域请求数据,js可以跨域请求js片段。
2、可以把数据包装成js片段。可以把数据使用js方法来包装,形成一条方法的调用语句。
3、可以使用ajax请求js片段,当js判断到达浏览器会被立即执行。
4、在浏览器端,先创建好回调方法,在回调方法中通过参数可以获得请求的数据。
1,前期准备:
1、需要把js的回调方法先写好。
2、做ajax请求时,需要把回调方法的方法名传递给服务端。
服务端接收回调方法名,把数据包装好,响应给客户端。
跨域,没实现过,不好写。。