今天是实训的第7天,7天之痒让我写日志的心情变得沉重,意识到坚持做一件事情真的不是纸上谈兵那么容易。ok,进入今天的总结吧~
一、三级联动:顾名思义~就是一级接着一级的联发进行,今天我做的例子是省城乡的三级联动。
<%@
page
language
=
"java"
contentType
=
"text/html; charset=UTF-8"
pageEncoding
=
"UTF-8"
%>
<!
DOCTYPE
html
PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"
>
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<
title
>
Insert title here
</
title
>
</
head
>
<
body
>
<
form
action
=
""
>
收货地址:
<
select
id
=
"pro"
onchange
=
"loadCity()"
>
</
select
>
<
select
id
=
"city"
onchange
=
"loadCountry()"
></
select
>
<
select
id
=
"country"
></
select
>
</
form
>
<
script
type
=
"text/javascript"
src
=
"js/china_area.js"
></
script
>
<
script
type
=
"text/javascript"
>
var
pro=document.getElementById(
"pro"
);
function
loadProvince(){
for
(
var
i=0;i<provinceJson.length;i++){
//<option></option>
var
opt= document.createElement(
"option"
);
//往 省的值设置给 option //<option value="id">北京市</option>
opt.innerHTML=provinceJson[i].province;
opt.setAttribute(
"value"
,provinceJson[i].id);
//将生产在opt孩子放入到省对应的 select中
pro.appendChild(opt);
}
}
/**
加载对应的城市
*/
var
city = document.getElementById(
"city"
);
function
loadCity(){
city.options.length = 0;
var
proValue = pro.value;
for
(
var
j = 0;j<cityJson.length;j++){
if
(proValue==cityJson[j].parent){
var
opt =
new
Option(cityJson[j].city,cityJson[j].id);
city.appendChild(opt);
}
}
}
/**
加载对应的城镇
*/
var
country = document.getElementById(
"country"
);
function
loadCountry(){
country.options.length = 0;
var
cityValue = city.value;
console.log(cityValue);
for
(
var
j = 0;j<countyJson.length;j++){
if
(cityValue==countyJson[j].parent){
var
opt =
new
Option(countyJson[j].county,countyJson[j].id);
country.appendChild(opt);
}
}
}
window.onload=
function
(){
loadProvince();
loadCity();
loadCountry();
}
</
script
>
</
body
>
</
html
>
其中onchange()就是加载下一个函数,
appendChild() 方法可向节点的子节点列表的末尾添加新的子节点
在项目的webcontent--WEBINF---lib目录下导入一个js,上网很容易找到我这里就不贴了~~
ps:我导入的js里面json的country他写成了county我也很无奈hhhhh
二、一对一与一对多
就是一张表对应一张表和一张表和多张表有联系
数据库实体间有三种对应关系:
一对一,一对多,多对多。
一对一关系示例:
一个学生对应一个学生档案材料,或者每个人都有唯一的身份证编号。
一对多关系示例:
一个学生只属于一个班,但是一个班级有多名学生。
多对多关系示例:
一个学生可以选择多门课,一门课也有多名学生。
(多对多代码明天补充)
一对一:
<
select
id
=
"getInforByClassID"
parameterType
=
"int"
resultMap
=
"ClsMap"
>
select * from
cls
INNER JOIN teacher t on cls.tid=t.id INNER JOIN student s
on s.classID=cls.id and cls.id=#{id}
</
select
>
<
resultMap
type
=
"Cls"
id
=
"ClsMap"
>
<!-- column代表的 cls.setId(rs.getInt("id")) -->
<
id
column
=
"id"
property
=
"id"
/>
<
result
column
=
"cname"
property
=
"cname"
/>
<!-- 整个teacher装配进去 1: 1 association-->
<
association
property
=
"teacher"
javaType
=
"Teacher"
>
<
id
column
=
"id"
property
=
"id"
/>
<
result
column
=
"tname"
property
=
"tname"
/>
</
association
>
<!-- 装配一个集合 collection 申明集合中的元素类型ofType=-->
<
collection
property
=
"list"
ofType
=
"Student"
>
<
id
column
=
"id"
property
=
"id"
/>
<
result
column
=
"sname"
property
=
"sname"
/>
</
collection
>
</
resultMap
>
用<association>标签实现插入teacher表的数据,用<connection>将学生list传进Cls中
一对多:
<!-- 装配:嵌套查询:子查询 -->
<
select
id
=
"getAllById"
parameterType
=
"int"
resultMap
=
"ClassMap2"
>
select * from
cls
where id=#{id}
</
select
>
<
resultMap
type
=
"Cls"
id
=
"ClassMap2"
>
<
id
column
=
"id"
property
=
"id"
/>
<
id
column
=
"cname"
property
=
"cname"
/>
<
association
property
=
"teacher"
column
=
"tid"
javaType
=
"Teacher"
select
=
"getTeacher"
>
</
association
>
<
collection
property
=
"list"
column
=
"id"
ofType
=
"Student"
select
=
"getStudents"
></
collection
>
</
resultMap
>
<
select
id
=
"getTeacher"
parameterType
=
"int"
resultType
=
"Teacher"
>
select * from teacher where id=#{id}
</
select
>
<
select
id
=
"getStudents"
parameterType
=
"int"
resultType
=
"Student"
>
select id id,
sname
from student where classID=#{id}
</
select
>
装配:嵌套查询:子查询,和上一种方法相比,不需要设置参数,通过子查询将数据获取。
今天就这样了,晚安。