一个ssm综合小案例-商品订单管理-第一天
项目需求分析:
功能需求:登录,商品列表查询,修改
项目环境及技术栈:
项目构成及环境: 本项目采用 maven 构建 环境要求: IDEA Version: 2017.2.5 Tomcat Version: 8.5.x > 用到的技术 IOC 容器 spring, 前端web控制层 springmvc, 持久层: mybatis --> mysql
技术栈: J2EE 一系列规范
正式开发:
1、使用 IDEA + maven 搭建整合的 ssm 框架
File --> New Project (注意勾选 Create from archetype 使用模板方便自动生成 webapp 等目录)
特别需要注意的是应该选 maven-archetype-webapp
这个而不应该选另外一个cocoon-22-archetype-webapp
一路 next 点击创建后 稍微等待一下,等待 mvn 后台构建项目,如果中间被打断很可能会出现未知的异常
接下来,就是配置 pom.xml 项目管理文件了,这部分如果不知道如何操作可以单独学习一下 maven 项目管理
主要是 依赖包(dependency)的配置以及 properties (项目构建编码,jar 包版本号等属性)
<?xml version="1.0" encoding="UTF-8"?> <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>com.ghc</groupId> <artifactId>gom</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>gom Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <!-- 设置项目编码编码 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- spring版本号 --> <spring.version>4.3.5.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.4.1</mybatis.version> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <!-- java ee --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--taglib 包--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- 实现slf4j接口并整合 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.2</version> </dependency> <!-- JSON --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.7</version> </dependency> <!-- 数据库驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> <scope>runtime</scope> </dependency> <!-- 数据库c3p0连接池 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- MyBatis jar包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring整合包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- Spring 所有依赖包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</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-context</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-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</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-test</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <finalName>gom</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.0.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.0</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
修改 java web 项目的 web.xml 添加 前端中央控制器 DispatchServlet 以及 configContext 文件监听器
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--添加 contextConfigLocation 配置文件监听器 mybatis 配置文件等--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/mybatis/spring-mybatis.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置springmvc最重要的前端中央控制器 DispatcherServlet--> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class >org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!--添加控制器的 url 映射--> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!--为了编写 RESTful 风格的 url 不用 *.action *.do--> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
mapper.xml 文件编写
<sql id="where_name_password">
<where>
<if test="name!=null and name!='' and password!=null and password!=''">
name=#{name} and password=#{password}
</if>
<!--可惜没有 else 所以这里换成 choose when otherwise 进行判断-->
</where>
mysql 脚本
drop schema if exists gom; CREATE SCHEMA gom; use gom; drop table if exists t_user; -- 用户表 create table t_user( id int auto_increment primary key, email varchar(25), name varchar(20), password varchar(20), -- md5 存储, reg_ip varchar(30), reg_date datetime ) ; -- 登录日志信息表 -- 如果已经有 user_id 存在于 loginfo表则更新之,否则插入 drop table if exists t_loginfo; create table t_loginfo( user_id int, login_ip varchar(30), login_date datetime, FOREIGN KEY(user_id) references t_user(id) ); -- 在未开发注册页面之前插入一条测试数据alter insert into t_user(id,email, name , password , reg_ip , reg_date ) values(0,'frank@gmail.com','frank','123','127.0.0.1',sysdate()); insert into t_loginfo(user_id,login_ip,login_date) select id,reg_ip,reg_date from t_user
如果有来生,一个人去远行,看不同的风景,感受生命的活力。。。