目 录
第一章 HTML.. 1
1 标签... 1
2 css样式属性... 1
第二章 JavaScript 3
1 属性... 3
2 设计步骤... 4
第三章 数据库SQLServer 5
1 建立数据库和建表... 5
2 删除和清空表... 6
3 增加数据... 6
4修改... 6
5 查看、查询、分组等... 6
6 多表链接... 7
7 if和循环... 8
8 事务... 8
9 索引... 9
10 游标... 9
11 触发器... 10
12 存储过程(分页)... 12
第四章 JSP. 14
1 三层... 14
2 jstl 15
第五章 Java开源框架... 16
1 hibernate框架... 16
1.1 使用方法... 16
1.2 hibernate中的关系... 17
1.2.1 多对一关系... 17
1.2.2 一对一关系... 18
1.2.3多对多关系(中间表)... 18
2 mybatis框架... 19
2.1使用方法... 19
3 spring框架... 20
3.1 使用方法... 20
4 struts2框架... 22
第一章 HTML
1 标签
图片:<img src="" />
表格: <table></table>
超链接:<a href="" display=“block”> block占满父窗口
下拉列表:
<select> <options>1</options><options>2</options> </select>
有序列表:<ol></ol>
无序列表:ul> <li><li/> <li><li/> </ul>
表单:<form><input type="" name="a" value="" /></form>
2 css样式属性
<style type="text/css">
display:block:标签的排列是从上到下,每一个标签占一行,可以设宽高。
display:inline:标签从左到右排除,不可以设宽高
字体大小:font-size:
倾斜:font-style:italic/normal
加粗:font-weight:bold/normal
下划线:text-decoration
背景色:background
对齐方式:text-align
首行缩进:text-indent
行高:line-height
字间的距离:letter-spacing
字体:font-family
外边局:margin
内边距:padding
边框:border:1px #000 double/dashed/solid
表格边框合并:border-collapse:collapse
一格占用多行:rowspan=“ ”
一格占用多列:colspan=“ ”
悬浮:float
溢出隐藏:overflow:hidden
溢出显示滚动条:overflow:scroll
鼠标悬挂:id:hover{}
第二章 JavaScript
1 属性
<script type="text/javascript">
窗体加载事件:window.onload=function(){}
点击事件:onclick
鼠标在上:onmouseover
鼠标离开:onmouseout
获取节点:document.getElementById(“ID”);
父节点:parentNode
跳转链接:
1、location.href=“http://www.baidu.com”
2、open(“url”,“_self”)
历史访问的地址
history.back()返回上一个页面
history.forward()去到之前的页
history.go(-1)上一页面
修改节点内容:Id.InnerHTML=“ ”
获取节点高/宽:ID.offsetHeight / ID.offsetHeight -----整型,不可修改
节点样属性
改属性的值:setAttribute()
拿属性的值:getAttribute()
获取节点样式的属性:style
ID.style.height=“45px” 可以修改属性值 --字符串
定时事件
timeObject=setTimeout("xxxxx()",1000);一秒钟后调用xxxxx()一次
cleatTimeout (timeObjeck); 停止时间
setInterval("xxxx()",1000);每隔一秒调用xxxxx()一次
window对象---表示整个窗体
window.alert();
window.setTimeout();
window.open();
window.setInterval();
window.parseInt()
window.parseFloat()
在网页上显示时间:
var t=document.getElementById("show_time");
var time=new Date();
var year=time.getFullYear();
var month=time.getMonth()+1;
var day=time.getDate();
var hours=time.getHours();
var minutes=time.getMinutes();
var seconds=time.getSeconds();
var str=year+"年"+month+"月"+day+"日"+hours+"时"+minutes+"分"+seconds+"秒";
t.innerHTML=str;
setTimeout("showtime()",1000);
2 设计步骤
1 window.onload=function(){}
2 在窗体加载的时候,关联其它的事件
1.拿到标签对象2.关联事件
3 写事件
1.获取节点2.调用节点的属性和方法
事例:
window.onload=function(){
h=document.getElementById(“content”);
h.onclick=honclick;
}
Function honclick(){
};
第三章 数据库SQLServer
1 建立数据库和建表
/*建数据库*/
create database lianxiz
on(
name=‘lianxiz‘,
filename=‘F:\lianxi\sql\lianxiz.mdf‘,
size=3,
filegrowth=1
)
log on
(
name=‘lianxiz_log‘,
filename=‘F:\lianxi\sql\lianxiz_log.ldf‘,
size=3,
filegrowth=1
)
/*打开数据库*/
use lianxiz
/*建表*/
create table lianxiz
(
id int primary key ,
name varchar(20),
course varchar(20) ,
score int
)
2 删除和清空表
/*清空:delete 删除:drop*/
delete *from lianxi where id=?
drop table lianxiz
3 增加数据
/*单行添加*/
insert lianxiz(id, name, course, score) values(1,‘小一‘,‘语文‘,78)
/*多行添加*/
insert lianxiz(id, name, course, score)
select 2,‘小二‘,‘语文‘,88 union
select 3,‘小三‘,‘语文‘,79 union
select 4,‘小四‘,‘数学‘,58 union
select 5,‘小五‘,‘语文‘,87 union
select 6,‘小六‘,‘数学‘,48 union
select 7,‘小七‘,‘语文‘,98
/*添加来自--*/
insert lianxiz(id,name,course,score) select id,name,course,score from lianxibak
/*备份*/
select * into lianxi3 from lianxiz
4修改
/*修改单独一个属性 alter column:修改列*/
alter table lianxiz
alter column score varchar(10)
/* 修改*/
update lianxiz set id=8,score=69 where id=1
5 查看、查询、分组等
/*查看*/
select * from lianxiz
/* 模糊查询 */
select * from lianxiz where LoginId like ‘%x%‘
/*分组(各科平均分)*/
select course,AVG (score) as ‘平均分‘,max(score) as ‘最高分‘,min(score) as ‘最低分‘
from lianxiz
group by course
/*分组(各同学的最高分) union :联合,结果集合并*/
select name,MAX(score) max from
(
select name,yuwen score from SCORE
union all
select name,shuxue score from SCORE
union all
select name,tiyu score from SCORE
) score group by name
/*查询 对象 出现次数2次以上的 */
select cardid, COUNT(cardid) as num from cardtb group by cardid
having COUNT(cardid) > 1
go
where 子句的作用是在对查询结果进行分组前
having 子句的作用在分组之后过滤数据
/*排序(科目and成绩)*/
select * from lianxiz order by course ,score desc
/*显示前3列 */
select top 3 *from lianxiz
/*显示不在前3列 */
select * from lianxiz where id not in (select top 3 id from lianxi)
/*显示第3列后的3列 */
select top 3 *from lianxiz where id not in (select top 2 id from lianxi )
6 多表链接
/*左链接:以左边为目标*/
select * from lianxiz
left join lianxi3 on lianxiz.id=lianxi3.id
/*右链接:以右边为目标*/
select *from lianxiz
right join lianxi3 on lianxiz.id=lianxi3.id
/*内链接:显示共有的*/
select *from lianxiz
inner join lianxi3 on lianxiz.id=lianxi3.id
/*外链接:显示双方全部*/
select * from lianxiz
full join lianxi3 on lianxiz.id=lianxi3.id
7 if和循环
/*if 语句*/
declare @count int =0
select @count =COUNT (id) from lianxiz
if(@count>3)
begin
select * from lianxiz where id not in (select top 3 id from lianxiz)
print @count
end
else
begin
select top 3 * from lianxiz
end
/*循环语句*/
declare @i int
select @i =3
while(@i >1)
begin
select * from lianxiz
select @[email protected]
end
8 事务
四个特性:一致性、原子性、隔离性、持久性
1、一般事务:
begin tran
一条或多条sql语句
If(@@error<>0) --------若有多个sql语句,需要写多个判断
Rollback tran
else
commit tran
2、自动回滚类型事务:
set xact_abort on/off on/off(自动回滚开关)
begin tran
多个sql语句
commit tran
9 索引
1、建立索引
create unique index 索引名
on 表名(列明)
go
2、删除索引
drop index 索引名 on 表名
go
10 游标
/*游标*/
/*创建游标*/
declare youbiao cursor
for select * from lianxiz
go
/*打开游标*/
open youbiao
go
/*抓取游标*/
declare @id int
declare @name varchar(20)
declare @course varchar(20)
declare @score int
while (1=1)
begin
fetch next from youbiao into @id ,@name,@course, @score
if(@@FETCH_STATUS !=0)
begin
break
end
select @id,@name,@course,@score
print @id
print @name
print @course
print @score
end
go
/*关闭游标*/
close youbiao
/*释放游标*/
deallocate youbiao
go
11 触发器
/*触发器*/
select * from lianxiz
go
/*删除数据时时,把删除的数据插入lianxibak表*/
create trigger trigger1
on lianxiz
after delete
as
/*内容*/
insert into lianxi3
select * from deleted
go
delete from lianxiz where id=4
go
/*插入数据,数据有相同,输出提示*/
alter trigger trigger2
on lianxiz
after insert
as
declare @name varchar(33)
declare @count int
select @name=name from inserted
select @count =COUNT(*) from lianxiz where [email protected]
if(@count>1)
begin
print ‘名字有相同‘
rollback
end
go
insert lianxiz(id,name,course,score)
values (10,‘小yi‘,‘语文‘,45)
go
/*删除时,代替、不许删除*/
create trigger trigger3
on lianxiz
instead of delete
as
print ‘没有权限‘
go
delete from lianxiz where id =4
go
/*禁用触发器*/
alter table lianxiz disable trigger trigger3
/*恢复触发器*/
alter table lianxiz enable trigger trigger3
12 存储过程(分页)
/*分页、方法一*/
create proc ffye
(
@sizs int ,
@page int,
@zys int output
)
as
select @zys=COUNT(id) from lianxiz
select top (@sizs) * from lianxiz
where id not in (select top ((@page-1)*@sizs) id from lianxiz order by id)
order by id
go
/* 运用 */
declare @zs int
exec ffye 2 ,3 ,@zs output
go
/* 分页第二方法 */
alter proc ffye2
(
@size int,
@page int
)
as
declare @tou int
declare @wei int
set @wei = (@size * @page)
set @tou = (@size *(@page-1)+1)
declare @tb table (id int identity(1,1) , lianxiid int)
insert @tb select id from lianxiz
select lianxiz.* from @tb as tb
inner join lianxiz on tb.id = lianxiz.id
where tb.id between @tou and @wei
go
exec ffye2 2,3
/* 删除分页 */
drop proc ffye
go
第四章 JSP
1 三层
public class SqlBase {
public Connection conn=null;
public Statement statm=null;
public ResultSet rs=null;
public SqlBase() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");} catch (ClassNotFoundException e) {e.printStackTrace();}
}
public Connection GetConn(){
try {
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=CloneBookShop", "sa", "123456");}
catch (SQLException e) {e.printStackTrace();}
return conn;
}
public ResultSet Select(String sql){
GetConn();
try {statm=conn.createStatement();
rs=statm.executeQuery(sql); }
catch (SQLException e) {e.printStackTrace();}
return rs;
}
public int Update(String sql){
GetConn();
try {
statm=conn.createStatement();
int row=statm.executeUpdate(sql);
return row;
} catch (SQLException e) {e.printStackTrace();}
return -1;
}
public ResultSet perpaer(String sql,Object...name){
GetConn();
PreparedStatement pstatm;
try {pstatm = conn.prepareStatement(sql);
for(int i=0;i<name.length;i++)
{ pstatm.setObject((i+1), name[i]); } rs=pstatm.executeQuery();
} catch (SQLException e) {e.printStackTrace();}
return rs;
}
public void Close(){
try {if(rs!=null)rs.close();}
catch (SQLException e) {e.printStackTrace();}
try {if(statm!=null)statm.close();}
catch (SQLException e) {e.printStackTrace();}
try {if(conn!=null)conn.close();}
catch (SQLException e) {e.printStackTrace();}
}
2 jstl
使用jstl语句前,要页面上添加
%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%
1、输入:
<c:set var="pageMax" value="${Books.pager}" ></c:set>
2、输出:
<c:out value="${ pageMax }"></c:out>
3、选择:
<c:choose>
<c:when ></c:when>
<c:otherwise></c:otherwise>
</c:choose>
4、For循环
<c:forEach items=" " var=" " varStatus="s">
</c:forEach>
第五章 Java开源框架
1 hibernate框架
是ORM框架,对jdbc进行了封装,大大简化了dao层的编码工作。
1.1 使用方法
a、配置文件hibernate.cfg.xml
<?xml version=‘1.0‘ encoding=‘UTF-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521/schooldb</property>
<property name="connection.username">accp</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.show_sql">true</property>
<!--<property name="current_session_context_class">jta</property>-->
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="entity/Grade.hbm.xml" />
</session-factory>
</hibernate-configuration>
b、类文件配置Grade.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.hustner.entity.Grade" table="GRADEINFO" schema="dbo" lazy="true">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="assigned"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="20" not-null="true" />
</property>
<bag name="students">
<key column="GRADE_ID" />
<one-to-many class="com.hustner.entity.Student" />
</bag>
<set name="setStudents">
<key column="GRADE_ID" />
<one-to-many class="com.hustner.entity.Student" />
</set>
<map name="mapStudents">
<key column="GRADE_ID" />
<map-key type="java.lang.Integer" column="ID" />
<one-to-many class="com.hustner.entity.Student"/>
</map>
</class>
</hibernate-mapping>
c、工作测试
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
//sessionFactory.getCurrentSession()
Session session=sessionFactory.openSession();
Transaction tran = null;
try{
tran = session.beginTransaction();
tran.commit();
}catch(Exception e){
if(tran!=null)tran.rollback();
e.printStackTrace();
}finally{
try{session.close();}catch(Exception e){e.printStackTrace();}
}
1.2 hibernate中的关系
1.2.1 多对一关系
多方(Student)
属性:普通对象(Grade)
<many-to-one name="grade" class="entity.Grade" cascade="save-update">
<column name="GID" />
</many-to-one>
一方(Grade)
属性:集合对象(List<T>,Set<T>,Map<T,T>)
<bag name="students" cascade="all" inverse="true">
<key column="GID" />
<one-to-many class="entity.Student" />
</bag>
<set name="students" cascade="all" lazy="false">
<key column="GID" />
<one-to-many class="entity.Student" />
</set>
<map>
<key column="GID" />
<map-key type="java.lang.String" column="SNAME" />
<one-to-many class="entity.Student"/>
</map>
1.2.2 一对一关系
主健方
属性:普通属性
<one-to-one name="paper" property-ref="student" class="entity.Paper" cascade="all"></one-to-one>
外健方
属性:普通属性
<many-to-one name="student" class="entity.Student" unique="true" cascade="all">
<column name="SID"></column>
</many-to-one>
1.2.3多对多关系(中间表)
属性:集合
配置:
<set name="students" table="中间表" cascade="save-update">
<key column="cid" />
<many-to-many class="entity.Student" column="sid" />
</set>
2 mybatis框架
把SQL语句和java代码分离,sql语句在xml中配置。
2.1使用方法
a、导包并配置总配置mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://127.0.0.1:1433;DataBaseName=testdb" />
<property name="username" value="sa"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
</mappers>
</configuration>
b、建表
c、实体类
d、配置实体类
mybatis-config.xml 中配置加入一条语句:
<typeAliases>
<typeAlias alias="Grade" type="entity.Grade"/>
</typeAliases>
f、配置sql语句(任意命名,最好以类名命名。便于管理。例:Grade.xml)
<?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="entity">
<resultMap type="Grade(等于alias="Grade")" id="gradeMap">
<id column="id" property="gradeId"/>
<result column ="name" property="gradeName" />
</resultMap>
<select id="selectAllGrades(命名)" resultMap="gradeMap(映射到)">
select * from gradeinfo
</select>
</mapper>
g、把sql语句配置添加到总配置
<mappers>
<mapper resource="entity/Grade.xml"/>
</mappers>
h、工作或测试
Reader reader = null;
SqlSessionFactory sqlSessionFactory = null;
SqlSession sqlSession=null;
try {
reader = Resources.getResourceAsReader("mybaties-config.xml");
//创建一个连接工厂,通过连接工厂建立连接
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
sqlSession=sqlSessionFactory.openSession();
List<Grade> grades=sqlSession.selectList("selectAllGrades2", null);
for(Grade grade:grades)
{ System.out.println(grade.getGradeName());}
}
catch (IOException e) { e.printStackTrace();}
finally
{try {reader.close();} catch (IOException e){e.printStackTrace(); }
sqlSession.close();
}
3 spring框架
对象:全部对象
3.1 使用方法
配置文件 ( applicationContext.xml )
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="*"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:sqlserver://localhost:1433; DatabaseName=shop</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="configLocation">
<value>classpath:mybaties-config.xml</value>
</property>
</bean>
<bean id="sqlSessionTemplate"
class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory">
</constructor-arg>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="vendorDao" class="com.hustner.dao.VendorDaoImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate" ></property>
</bean>
<bean id="vendorService" class="com.hustner.service.VendorServiceImpl">
<property name="vendorDao" ref="vendorDao"></property>
</bean>
<bean id="vendorAction" class="com.hustner.controller.VendorAction" scope="prototype">
<property name="vendorService" ref="vendorService"></property>
</bean>
</beans>