day01(RESTful Web Service、SVN)

时间:2023-01-17 11:10:20
  1. 今日大纲

  1. 搭建SSM环境
  2. 基于SSM环境实现用户管理系统
  3. 学习RESTful Web Service
  4. 学习SVN
  1. 统一开发环境

  1. JDK
    1.7 32? 64? -- 64
  2. Eclipse

    使用4.4.1 luna 课前资料中提供
    day01(RESTful Web Service、SVN)

  3. 数据库,mysql 《淘宝技术这十年》
    1. 统一使用5.6,但是,项目开发阶段可以使用5.5或5.6
    2. Mysql的读写分离,统一成5.6
  4. 数据库的客户端工具
    1. day01(RESTful Web Service、SVN)
    2. Mysql最好用的客户端。
    3. day01(RESTful Web Service、SVN)
  5. Maven
    1. 统一使用3.2.3
    2. 统一使用Maven的私服
      http://192.168.50.22:8081/nexus/#welcome
      day01(RESTful Web Service、SVN)
  6. 实施:《Eclipse相关配置.docx》
    1. 实施

      1. Eclipse

day01(RESTful Web Service、SVN)

  1. 搭建SSM环境

    1. 数据库

      1. 使用navicat创建数据库连接

day01(RESTful Web Service、SVN)

执行SQL脚本:

day01(RESTful Web Service、SVN)

day01(RESTful Web Service、SVN)

  1. Tb_user

查看表结构:

day01(RESTful Web Service、SVN)

表结构:

day01(RESTful Web Service、SVN)

  1. 统一管理依赖的版本

需要:将多个项目的依赖的版本号统一管理起来。

如何实现? --- 使用Maven的继承实现。

  1. 导入itcast-parent

day01(RESTful Web Service、SVN)

day01(RESTful Web Service、SVN)

  1. 完整pom

<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>cn.itcast.parent</groupId>

<artifactId>itcast-parent</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>pom</packaging>

<!-- 集中定义依赖版本号 -->

<properties>

<junit.version>4.10</junit.version>

<spring.version>4.1.3.RELEASE</spring.version>

<mybatis.version>3.2.8</mybatis.version>

<mybatis.spring.version>1.2.2</mybatis.spring.version>

<mybatis.paginator.version>1.2.15</mybatis.paginator.version>

<mysql.version>5.1.32</mysql.version>

<slf4j.version>1.6.4</slf4j.version>

<jackson.version>2.4.2</jackson.version>

<druid.version>1.0.9</druid.version>

<httpclient.version>4.3.5</httpclient.version>

<jstl.version>1.2</jstl.version>

<servlet-api.version>2.5</servlet-api.version>

<jsp-api.version>2.0</jsp-api.version>

<joda-time.version>2.5</joda-time.version>

<commons-lang3.version>3.3.2</commons-lang3.version>

<commons-io.version>1.3.2</commons-io.version>

</properties>

<dependencyManagement>

<dependencies>

<!-- 单元测试 -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>${junit.version}</version>

<scope>test</scope>

</dependency>

<!-- Spring -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aspects</artifactId>

<version>${spring.version}</version>

</dependency>

<!-- Mybatis -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>${mybatis.version}</version>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>${mybatis.spring.version}</version>

</dependency>

<!-- MySql -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>${mysql.version}</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>${slf4j.version}</version>

</dependency>

<!-- Jackson
Json处理工具包 -->

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>${jackson.version}</version>

</dependency>

<!-- 连接池 -->

<dependency>

<groupId>com.jolbox</groupId>

<artifactId>bonecp-spring</artifactId>

<version>0.8.0.RELEASE</version>

</dependency>

<!-- httpclient -->

<dependency>

<groupId>org.apache.httpcomponents</groupId>

<artifactId>httpclient</artifactId>

<version>${httpclient.version}</version>

</dependency>

<!-- JSP相关 -->

<dependency>

<groupId>jstl</groupId>

<artifactId>jstl</artifactId>

<version>${jstl.version}</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>${servlet-api.version}</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jsp-api</artifactId>

<version>${jsp-api.version}</version>

<scope>provided</scope>

</dependency>

<!-- 时间操作组件 -->

<dependency>

<groupId>joda-time</groupId>

<artifactId>joda-time</artifactId>

<version>${joda-time.version}</version>

</dependency>

<!-- Apache工具组件 -->

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-lang3</artifactId>

<version>${commons-lang3.version}</version>

</dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-io</artifactId>

<version>${commons-io.version}</version>

</dependency>

</dependencies>

</dependencyManagement>

<build>

<finalName>${project.artifactId}</finalName>

<plugins>

<!-- 资源文件拷贝插件 -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-resources-plugin</artifactId>

<version>2.7</version>

<configuration>

<encoding>UTF-8</encoding>

</configuration>

</plugin>

<!-- java编译插件 -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.2</version>

<configuration>

<source>1.7</source>

<target>1.7</target>

<encoding>UTF-8</encoding>

</configuration>

</plugin>

</plugins>

<pluginManagement>

<plugins>

<!-- 配置Tomcat插件 -->

<plugin>

<groupId>org.apache.tomcat.maven</groupId>

<artifactId>tomcat7-maven-plugin</artifactId>

<version>2.2</version>

</plugin>

</plugins>

</pluginManagement>

</build>

</project>

  1. 继承parent

day01(RESTful Web Service、SVN)

  1. 出现小红叉

day01(RESTful Web Service、SVN)

解决:

day01(RESTful Web Service、SVN)

day01(RESTful Web Service、SVN)

原因:

day01(RESTful Web Service、SVN)

定义的java编译器插件的jdk版本和默认使用的jdk版本不一致,导致。

day01(RESTful Web Service、SVN)

day01(RESTful Web Service、SVN)

  1. 创建工程

    1. 新建工程

day01(RESTful Web Service、SVN)

day01(RESTful Web Service、SVN)

  1. 填写项目的Maven坐标

day01(RESTful Web Service、SVN)

day01(RESTful Web Service、SVN)

Maven会自动从192.168.50.22的私服下载所需要的依赖。

  1. 子工程使用依赖

day01(RESTful Web Service、SVN)

day01(RESTful Web Service、SVN)

  1. Parent中的依赖管理

day01(RESTful Web Service、SVN)

  1. 导入依赖

<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>cn.itcast.parent</groupId>

<artifactId>itcast-parent</artifactId>

<version>0.0.1-SNAPSHOT</version>

</parent>

<groupId>cn.itcast.manage</groupId>

<artifactId>itcast-usermanage</artifactId>

<version>1.0.0-SNAPSHOT</version>

<packaging>war</packaging>

<dependencies>

<!-- 单元测试 -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aspects</artifactId>

</dependency>

<!-- Mybatis -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

</dependency>

<!-- 分页助手 -->

<dependency>

<groupId>com.github.pagehelper</groupId>

<artifactId>pagehelper</artifactId>

</dependency>

<dependency>

<groupId>com.github.jsqlparser</groupId>

<artifactId>jsqlparser</artifactId>

</dependency>

<!-- 通用Mapper -->

<dependency>

<groupId>com.github.abel533</groupId>

<artifactId>mapper</artifactId>

</dependency>

<!-- MySql -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

</dependency>

<!-- Jackson
Json处理工具包 -->

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

</dependency>

<!-- 连接池 -->

<dependency>

<groupId>com.jolbox</groupId>

<artifactId>bonecp-spring</artifactId>

</dependency>

<!-- JSP相关 -->

<dependency>

<groupId>jstl</groupId>

<artifactId>jstl</artifactId>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jsp-api</artifactId>

<scope>provided</scope>

</dependency>

<!-- 时间操作组件 -->

<dependency>

<groupId>joda-time</groupId>

<artifactId>joda-time</artifactId>

</dependency>

<!-- Apache工具组件 -->

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-lang3</artifactId>

</dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-io</artifactId>

</dependency>

</dependencies>

<build>

<plugins>

<!-- 配置Tomcat插件 -->

<plugin>

<groupId>org.apache.tomcat.maven</groupId>

<artifactId>tomcat7-maven-plugin</artifactId>

<configuration>

<port>80</port>

<!--

http://127.0.0.1:{port}/{path}

-->

<path>/</path>

</configuration>

</plugin>

</plugins>

</build>

</project>

  1. 运行tomcat插件

day01(RESTful Web Service、SVN)

运行出错:

day01(RESTful Web Service、SVN)

问题:

day01(RESTful Web Service、SVN)

解决:

需要将itcast-parent安装到本地仓库。

day01(RESTful Web Service、SVN)

day01(RESTful Web Service、SVN)

  1. Web.xml

<?xml
version="1.0"
encoding="UTF-8"?>

<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID"
version="2.5">

<display-name>itcast-usermanage</display-name>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring/applicationContext*.xml</param-value>

</context-param>

<!--Spring的ApplicationContext 载入 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!-- 编码过滤器,以UTF8编码 -->

<filter>

<filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!-- 配置SpringMVC框架入口 -->

<servlet>

<servlet-name>itcast-usermanage</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring/itcast-usermanage-servlet.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>itcast-usermanage</servlet-name>

<!--

可以:

*.xxx

/xxx/*

/

不可以:

/*

-->

<url-pattern>/rest/*</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

  1. Jdbc.properties

day01(RESTful Web Service、SVN)

  1. Spring容器配置文件

<beans
xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"
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:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<!-- 使用spring自带的占位符替换功能 -->

<bean

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<!-- 允许JVM参数覆盖 -->

<property
name="systemPropertiesModeName"
value="SYSTEM_PROPERTIES_MODE_OVERRIDE"
/>

<!-- 忽略没有找到的资源文件 -->

<property
name="ignoreResourceNotFound"
value="true"
/>

<!-- 配置资源文件 -->

<property
name="locations">

<list>

<value>classpath:jdbc.properties</value>

</list>

</property>

</bean>

<!-- 扫描包 -->

<context:component-scan
base-package="cn.itcast"/>

<!-- 定义数据源 -->

<bean
id="dataSource"
class="com.jolbox.bonecp.BoneCPDataSource"

destroy-method="close">

<!-- 数据库驱动 -->

<property
name="driverClass"
value="${jdbc.driverClassName}"
/>

<!-- 相应驱动的jdbcUrl -->

<property
name="jdbcUrl"
value="${jdbc.url}"
/>

<!-- 数据库的用户名 -->

<property
name="username"
value="${jdbc.username}"
/>

<!-- 数据库的密码 -->

<property
name="password"
value="${jdbc.password}"
/>

,如果要取消则设置为0 -->

<property
name="idleConnectionTestPeriod"
value="60"
/>

,如果要永远存活设置为0 -->

<property
name="idleMaxAge"
value="30"
/>

<!-- 每个分区最大的连接数 -->

<!--

判断依据:请求并发数

-->

<property
name="maxConnectionsPerPartition"
value="100"
/>

<!-- 每个分区最小的连接数 -->

<property
name="minConnectionsPerPartition"
value="5"
/>

</bean>

</beans>

  1. Spring事务

<beans
xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"
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:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<!-- 定义事务管理器 -->

<bean
id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property
name="dataSource"
ref="dataSource"
/>

</bean>

<!-- 定义事务策略 -->

<tx:advice
id="txAdvice"
transaction-manager="transactionManager">

<tx:attributes>

<!--所有以query开头的方法都是只读的 -->

<tx:method
name="query*"
read-only="true"
/>

<!--其他方法使用默认事务策略 -->

<tx:method
name="*"
/>

</tx:attributes>

</tx:advice>

<aop:config>

<!--pointcut元素定义一个切入点,execution中的第一个星号用以匹配方法的返回类型,

这里星号表明匹配所有返回类型。 com.abc.dao.*.*(..)表明匹配cn.itcast.mybatis.service包下的所有类的所有

方法 -->

<aop:pointcut
id="myPointcut"
expression="execution(* cn.itcast.usermanage.service.*.*(..))"
/>

<!--将定义好的事务处理策略应用到上述的切入点 -->

<aop:advisor
advice-ref="txAdvice"
pointcut-ref="myPointcut"
/>

</aop:config>

</beans>

  1. SpringMVC配置文件

day01(RESTful Web Service、SVN)

  1. Mybatis和Spring的整合

    1. 整合文件

day01(RESTful Web Service、SVN)

  1. Mybatis的全局配置文件

day01(RESTful Web Service、SVN)

  1. 导入jsp页面

day01(RESTful Web Service、SVN)

day01(RESTful Web Service、SVN)

  1. 启动报错

由于在mappers目录中没有xml配置文件,所以报错。

day01(RESTful Web Service、SVN)

  1. 通用页面跳转

day01(RESTful Web Service、SVN)

测试:

day01(RESTful Web Service、SVN)

  1. 配置通用Mapper

    1. 导入依赖

已完成。

  1. 配置通用Mapper的插件

day01(RESTful Web Service、SVN)

  1. 作业

分页助手和通用Mapper的配置的顺序是否能颠倒?

  1. User实体添加JPA注解

day01(RESTful Web Service、SVN)

  1. 创建UserMapper接口

day01(RESTful Web Service、SVN)

  1. EasyUI的datagrid的数据结构

day01(RESTful Web Service、SVN)

  1. 封装EasyUIResult

day01(RESTful Web Service、SVN)

  1. 实现用户列表的查询

    1. Controller实现

day01(RESTful Web Service、SVN)

  1. 分页(分页助手)

    1. 导入依赖

已完成。

  1. 配置插件

day01(RESTful Web Service、SVN)

  1. 使用分页助手

day01(RESTful Web Service、SVN)

  1. UserService

使用分页助手完成分页功能。

day01(RESTful Web Service、SVN)

  1. UserMapper

day01(RESTful Web Service、SVN)

  1. 效果

day01(RESTful Web Service、SVN)

  1. Datagrid的formatter方法

后台返回的数据是时间戳:

day01(RESTful Web Service、SVN)

day01(RESTful Web Service、SVN)

day01(RESTful Web Service、SVN)

  1. 扩展JS的内置对象的方法

在内置对象的原型上扩展方法:

day01(RESTful Web Service、SVN)

  1. 开源中国

day01(RESTful Web Service、SVN)

day01(RESTful Web Service、SVN)

  1. 大神

day01(RESTful Web Service、SVN)

http://my.oschina.net/flags

  1. RESTful Web Service

    1. 大纲

day01(RESTful Web Service、SVN)

  1. REST是什么?

大神:

day01(RESTful Web Service、SVN)

论文:Roy Thomas Fielding博士论文REST(中文版).pdf

day01(RESTful Web Service、SVN)

  1. REST到底是什么?

day01(RESTful Web Service、SVN)

  1. RESTful是什么?

day01(RESTful Web Service、SVN)

Web service:

JAX-WS

JAX-RS

day01(RESTful Web Service、SVN)

  1. REST 架构的主要原则

day01(RESTful Web Service、SVN)

  1. URI和URL

day01(RESTful Web Service、SVN)

  1. 无状态性

day01(RESTful Web Service、SVN)

  1. 资源操作

day01(RESTful Web Service、SVN)

之前的操作:

http://127.0.0.1/user/query/1 GET 根据用户id查询用户数据

http://127.0.0.1/user/save POST 新增用户

http://127.0.0.1/user/update POST 修改用户信息

http://127.0.0.1/user/delete GET/POST 删除用户信息

RESTful用法:

http://127.0.0.1/user/1 GET 根据用户id查询用户数据

http://127.0.0.1/user POST 新增用户

http://127.0.0.1/user PUT 修改用户信息

http://127.0.0.1/user DELETE 删除用户信息

  1. REST接口定义

day01(RESTful Web Service、SVN)

day01(RESTful Web Service、SVN)

  1. 最佳实践

    1. REST接口设计

day01(RESTful Web Service、SVN)

  1. 响应设计

day01(RESTful Web Service、SVN)

  1. 响应示例

day01(RESTful Web Service、SVN)

  1. 指定响应的属性字段

day01(RESTful Web Service、SVN)

  1. http响应状态码

day01(RESTful Web Service、SVN)

  1. SpringMVC实现RESTful服务

day01(RESTful Web Service、SVN)

  1. 查询资源

day01(RESTful Web Service、SVN)

  1. 新增资源

day01(RESTful Web Service、SVN)

测试:

day01(RESTful Web Service、SVN)

  1. 更新资源

day01(RESTful Web Service、SVN)

Service:

day01(RESTful Web Service、SVN)

测试:

day01(RESTful Web Service、SVN)

默认情况下,PUT请求是无法提交表单数据的,需要在web.xml中添加过滤器解决:

<!-- 解决PUT请求无法提交表单数据的问题 -->

<filter>

<filter-name>HttpMethodFilter</filter-name>

<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>HttpMethodFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

  1. 删除资源

day01(RESTful Web Service、SVN)

测试:

day01(RESTful Web Service、SVN)

需要在web.xml中添加过滤器解决DELETE请求无法提交表单数据的问题:

<!--

将POST请求转化为DELETE或者是PUT

要用_method指定真正的请求参数

-->

<filter>

<filter-name>HiddenHttpMethodFilter</filter-name>

<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>HiddenHttpMethodFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

  1. SVN

参考:

day01(RESTful Web Service、SVN)