二、SSM即Spring、SpringMVC、Mybatis整合


开发环境:jdk1.8、Eclipse、maven3.5
简单来说一下就是,在整合之前我们需要思考一下整合顺序,最合理的整合顺序是从SpringMVC->Spring->Mybatis,因为SpringMVC配置好的话那么Spring的相关jar包我们就不需要导入了,二者有很多重叠的地方,现在开始!

一、Eclipse创建maven项目

1.首先,我们打开Eclipse

File->new->other->Maven->Maven project ,然后点击Next

这里把Create a simple project勾选上,点击Next

Group Id: com.sunhao,对应的是我们对应Java的包的结构
Artifact Id: ssm_demo,其实就是我们的项目名称
Packaging: 我们选择war,因为ssm毕竟是一个web工程,后续会用tomcat启动
点击Finish
至此,我们的Maven项目创建完毕,我们可以看看此时的项目结构

这里,pom文件有个错误,那是因为项目的Project Facets还没设置好,我们设置一下,这里比较关键
右击我们的ssm_demo->properties->Project Facets

可以看到Dynamic Web Module默认勾选2.5
Java默认勾选1.5
JavaScript默认勾选1.0
现在,我们把Java设置成1.8,取消Dynamic Web Module的勾选状态,点击一次Apply;
然后,选中Dynamic Web Module4.0,JavaScript不用任何变化

出现了Further configuration available,我们点击它
在这里插入图片描述
Content directory设置成src/main/webapp
勾选Generate web.xm deployment descriptor,点击OK,再点击Apply and Close
在这里插入图片描述
此时,项目回复正常,一个Maven项目真正的创建成功了!
下面就是要引入ssm项目所需要的全部依赖,我们打开pom.xml文件
在这里插入图片描述
在这里插入图片描述
然后把以下这段代码复制到</project>之前即可。

<dependencies>
		<!-- Servlet API -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>3.0-alpha-1</version>
			<scope>provided</scope>
		</dependency>

		<!--  Spring Web -->
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>5.0.3.RELEASE</version>
		</dependency>

		<!-- Spring SpringMVC -->
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>5.0.3.RELEASE</version>
		</dependency>

		<!-- Spring JDBC -->
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>5.0.3.RELEASE</version>
		</dependency>

		<!-- Spring Aspects -->
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>5.0.3.RELEASE</version>
		</dependency>

		<!-- MyBatis -->
		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.5</version>
		</dependency>

		<!-- MyBatis Spring -->
		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.1</version>
		</dependency>

		<!-- C3P0 -->
		<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
		<dependency>
			<groupId>c3p0</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.1.2</version>
		</dependency>

		<!-- MySQL驱动包 -->
		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.45</version>
		</dependency>

		<!-- JSTL -->
		<!-- https://mvnrepository.com/artifact/jstl/jstl -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
		<dependency>
			<groupId>com.googlecode.json-simple</groupId>
			<artifactId>json-simple</artifactId>
			<version>1.1</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.3</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.8.2</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.7</version>
		</dependency>
	</dependencies>

对了,小伙伴可以自己看以下eclipse如何配置maven,这里不太会的可以问我,等待程序加载完毕
此时的项目多了一个Maven Dependencies
在这里插入图片描述
也就是说,到目前位置SSM项目所依赖的jar已经全部引入,下面要编写配置文件

二、编写配置文件

1.首先,编写web.xml文件,这里需要编写的是和web相关的配置文件,比如Spring容器、前端控制器、字符编码过滤器等
注意:web.xml项目中已经存在,只需要修改即可

打开web.xml,将以下代码复制到web.xml,可以全选复制进去

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="4.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<!--1、启动Spring的容器 -->
	<!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!--2、springmvc的前端控制器,拦截所有请求 -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<!-- 3、字符编码过滤器,一定要放在所有过滤器之前 -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceRequestEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>forceResponseEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 4、使用Rest风格的URI,将页面普通的post请求转为指定的delete或者put请求 -->
	<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>
	<filter>
		<filter-name>HttpPutFormContentFilter</filter-name>
		<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HttpPutFormContentFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

2.编写Spring MVC配置文件
在src/main/webapp/WEB-INF目录中,创建Spring MVC配置文件,文件名为dispatcherServlet-servlet.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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<!-- 启动注解扫描功能  -->
	<context:component-scan base-package="com.sunhao" use-default-filters="false">
		<!--只扫描控制器  -->
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!--配置视图解析器,方便页面返回  -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
	</bean>
	
	<!--两个标准配置  -->
	<!-- 将springmvc不能处理的请求交给tomcat -->
	<mvc:default-servlet-handler/>
	<!-- 能支持springmvc更高级的一些功能,JSR303校验,快捷的ajax...映射动态请求 -->
	<mvc:annotation-driven/>

</beans>

3.编写Spring配置文件
在src/main/resources路径下,创建Spring配置文件,文件名为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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<context:component-scan base-package="com.sunhao">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<!-- Spring的配置文件,这里主要配置和业务逻辑有关的 -->
	<!--=================== 数据源,事务控制,xxx ================-->
	<context:property-placeholder location="classpath:dbconfig.properties" />
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>

	<!-- 配置SqlSessionFactoryBean -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 配置MapperScannerConfigurer,Dao接口所在包名,Spring会自动查找其下的类 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.sunhao.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>

	<!-- 配置DataSourceTransactionManager(事务管理) -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 启用基于注解的声明式事务管理配置 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	
</beans>

4.编写数据库属性文件
在src/main/resources目录下,创建数据库属性文件,文件名为dbconfig.properties,内容如下:

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/eshop
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456

这里数据库为eshop,数据库root/123456,需要该为你本地对应的密码,eshop的数据库文件我会在以下展示:

/*
SQLyog v10.2 
MySQL - 5.5.27 : Database - eshop
*********************************************************************
*/


/*!40101 SET NAMES utf8 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`eshop` /*!40100 DEFAULT CHARACTER SET gbk */;

USE `eshop`;

/*Table structure for table `admin_info` */

DROP TABLE IF EXISTS `admin_info`;

CREATE TABLE `admin_info` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` varchar(16) DEFAULT NULL,
  `pwd` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=gbk;

/*Data for the table `admin_info` */

insert  into `admin_info`(`id`,`name`,`pwd`) values (1,'admin','123456'),(2,'my','123456'),(3,'sj','123456'),(4,'lxf','123456');

/*Table structure for table `functions` */

DROP TABLE IF EXISTS `functions`;

CREATE TABLE `functions` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL COMMENT '功能菜单',
  `parentid` int(4) DEFAULT NULL,
  `url` varchar(50) DEFAULT NULL,
  `isleaf` bit(1) DEFAULT NULL,
  `nodeorder` int(4) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=gbk;

/*Data for the table `functions` */

insert  into `functions`(`id`,`name`,`parentid`,`url`,`isleaf`,`nodeorder`) values (1,'电子商城管理后台',0,NULL,'\0',0),(2,'商品管理',1,NULL,'\0',1),(3,'商品列表',2,NULL,'',1),(4,'商品类型列表',2,NULL,'',2),(5,'订单管理',1,NULL,'\0',2),(6,'查询订单',5,NULL,'',1),(7,'创建订单',5,NULL,'',2),(8,'用户管理',1,NULL,'\0',3),(9,'用户列表',8,NULL,'',1),(11,'退出系统',1,NULL,'',1);

/*Table structure for table `order_detail` */

DROP TABLE IF EXISTS `order_detail`;

CREATE TABLE `order_detail` (
  `id` int(4) NOT NULL AUTO_INCREMENT COMMENT '订单明细id',
  `oid` int(4) DEFAULT NULL COMMENT '订单id',
  `pid` int(4) DEFAULT NULL COMMENT '产品id',
  `num` int(4) DEFAULT NULL COMMENT '购买数量',
  PRIMARY KEY (`id`),
  KEY `pid` (`pid`),
  KEY `oid` (`oid`),
  CONSTRAINT `order_detail_ibfk_1` FOREIGN KEY (`oid`) REFERENCES `order_info` (`id`),
  CONSTRAINT `order_detail_ibfk_2` FOREIGN KEY (`pid`) REFERENCES `product_info` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

/*Data for the table `order_detail` */

insert  into `order_detail`(`id`,`oid`,`pid`,`num`) values (1,1,1,1),(2,1,2,1),(3,2,4,1),(4,2,5,1),(5,2,8,1);

/*Table structure for table `order_info` */

DROP TABLE IF EXISTS `order_info`;

CREATE TABLE `order_info` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `uid` int(4) DEFAULT NULL,
  `status` varchar(16) DEFAULT NULL,
  `ordertime` datetime DEFAULT NULL,
  `orderprice` decimal(8,2) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `uid` (`uid`),
  CONSTRAINT `order_info_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `user_info` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

/*Data for the table `order_info` */

insert  into `order_info`(`id`,`uid`,`status`,`ordertime`,`orderprice`) values (1,1,'未付款','2018-05-12 00:00:00','10687.00'),(2,2,'已付款','2018-05-09 00:00:00','12997.00');

/*Table structure for table `powers` */

DROP TABLE IF EXISTS `powers`;

CREATE TABLE `powers` (
  `aid` int(4) NOT NULL,
  `fid` int(4) NOT NULL,
  PRIMARY KEY (`aid`,`fid`),
  KEY `fid` (`fid`),
  KEY `aid` (`aid`),
  CONSTRAINT `powers_ibfk_1` FOREIGN KEY (`aid`) REFERENCES `admin_info` (`id`),
  CONSTRAINT `powers_ibfk_2` FOREIGN KEY (`fid`) REFERENCES `functions` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;

/*Data for the table `powers` */

insert  into `powers`(`aid`,`fid`) values (1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8),(1,9),(1,11);

/*Table structure for table `product_info` */

DROP TABLE IF EXISTS `product_info`;

CREATE TABLE `product_info` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `code` varchar(16) DEFAULT NULL COMMENT '商品编号',
  `name` varchar(255) DEFAULT NULL COMMENT '商品名称',
  `tid` int(4) DEFAULT NULL COMMENT '商品类别',
  `brand` varchar(20) DEFAULT NULL COMMENT '商品品牌',
  `pic` varchar(255) DEFAULT NULL COMMENT '商品图片',
  `num` int(4) unsigned zerofill DEFAULT NULL COMMENT '商品库存',
  `price` decimal(10,0) unsigned zerofill DEFAULT NULL COMMENT '商品小图',
  `intro` longtext COMMENT '商品简介',
  `status` int(4) DEFAULT '1' COMMENT '商品状态',
  PRIMARY KEY (`id`),
  KEY `tid` (`tid`),
  CONSTRAINT `product_info_ibfk_1` FOREIGN KEY (`tid`) REFERENCES `type` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

/*Data for the table `product_info` */

insert  into `product_info`(`id`,`code`,`name`,`tid`,`brand`,`pic`,`num`,`price`,`intro`,`status`) values (1,'1378538','AppleMJVE2CH/A',1,'APPLE','1378538.jpg',0100,'0000006488','Apple MacBook Air 13.3英寸笔记本电脑 银色(Core i5 处理器/4GB内存/128GB SSD闪存 MJVE2CH/A)',1),(2,'1309456','ThinkPadE450C(20EH0001CD)',1,'ThinkPad','1309456.jpg',0097,'0000004199','联想(ThinkPad) 轻薄系列E450C(20EH0001CD)14英寸笔记本电脑(i5-4210U 4G 500G 2G独显 Win8.1)',1),(3,'1999938','联想小新300经典版',1,'联想(Lenovo)','1999938.jpg',0099,'0000004399','联想(Lenovo)小新300经典版 14英寸超薄笔记本电脑(i7-6500U 4G 500G 2G独显 全高清屏 Win10)黑色',1),(4,'1466274','华硕FX50JX',1,'华硕(ASUS)','1466274.jpg',0100,'0000004799','华硕(ASUS)飞行堡垒FX50J 15.6英寸游戏笔记本电脑(i5-4200H 4G 7200转500G GTX950M 2G独显 全高清)',1),(5,'1981672','华硕FL5800',1,'华硕(ASUS)','1981672.jpg',0100,'0000004999','华硕(ASUS)FL5800 15.6英寸笔记本电脑 (i7-5500U 4G 128G SSD+1TB 2G独显 蓝牙 Win10 黑色)',1),(6,'1904696','联想G50-70M',1,'联想(Lenovo)','1904696.jpg',0012,'0000003499','联想(Lenovo)G50-70M 15.6英寸笔记本电脑(i5-4210U 4G 500G GT820M 2G独显 DVD刻录 Win8.1)金属黑',1),(7,'751624','美的BCD-206TM(E)',2,' 美的(Midea)','751624.jpg',0100,'0000001298','美的(Midea) BCD-206TM(E) 206升 三门冰箱 节能保鲜 闪白银',1),(8,'977433','美的BCD-516WKM(E)',2,' 美的(Midea)','977433.jpg',0100,'0000003199','美的(Midea) BCD-516WKM(E) 516升 对开门冰箱 风冷无霜 电脑控温 纤薄设计 节能静音 (泰坦银)',1),(9,'1143562','海尔BCD-216SDN',2,' 海尔(Haier)','1143562.jpg',0100,'0000001699','海尔(Haier)BCD-216SDN 216升 三门冰箱 电脑控温 中门 宽幅变温 大冷冻能力低能耗更省钱',1),(10,'1560207','海尔BCD-258WDPM',2,' 海尔(Haier)','1560207.jpg',0100,'0000002699','海尔(Haier)BCD-258WDPM 258升 风冷无霜三门冰箱 除菌 3D立体环绕风不风干 中门5℃~-18℃变温室',1),(11,'1721668','海信(Hisense)BCD-559WT/Q',2,' 海信(Hisense)','1721668.jpg',0100,'0000003499','海信(Hisense)BCD-559WT/Q 559升 金色电脑风冷节能对开门冰箱',1),(12,'823125','海信BCD-211TD/E',2,' 海信(Hisense)','823125.jpg',0100,'0000001699','海信(Hisense) BCD-211TD/E 211升 电脑三门冰箱 (亮金刚)',1);

/*Table structure for table `type` */

DROP TABLE IF EXISTS `type`;

CREATE TABLE `type` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=gbk;

/*Data for the table `type` */

insert  into `type`(`id`,`name`) values (1,'电脑'),(2,'冰箱'),(3,'电视机'),(4,'洗衣机'),(5,'数码相机');

/*Table structure for table `user_info` */

DROP TABLE IF EXISTS `user_info`;

CREATE TABLE `user_info` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `userName` varchar(16) DEFAULT NULL,
  `password` varchar(16) DEFAULT NULL,
  `realName` varchar(8) DEFAULT NULL,
  `sex` varchar(4) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `regDate` date DEFAULT NULL,
  `status` int(4) DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

/*Data for the table `user_info` */

insert  into `user_info`(`id`,`userName`,`password`,`realName`,`sex`,`address`,`email`,`regDate`,`status`) values (1,'tom','123456','汤姆','女','江苏省苏州市吴中区','tom@123.com','2013-07-14',1),(2,'john','123456','约翰','女','江苏省南京市玄武区','wen@135.com','2013-07-14',1),(3,'my','123456','my','男','江苏省南京市玄武区','a@135.com','2015-09-16',1),(4,'sj','123456','sj','男','江苏省南京市玄武区','b@135.com','2015-09-16',1),(5,'lxf','123456','lxf','男','江苏省南京市玄武区','c@135.com','2015-09-16',1),(6,'lj','123456','lj','男','江苏省南京市玄武区','a@135.com','2015-09-20',1);

/* Procedure structure for procedure `sp_sale` */

/*!50003 DROP PROCEDURE IF EXISTS  `sp_sale` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_sale`(c int)
BEGIN	
	declare stmt varchar(2000);
	set @sqlstr=concat("SELECT p.id AS id, p.name AS NAME,SUM(od.num) AS total ,SUM(od.num)*price AS money
	FROM order_detail od, product_info p 
	WHERE p.id=od.p_id 
	GROUP BY p.id,p.name,p.price ORDER BY total DESC LIMIT 1,",c);
     prepare stmt from @sqlstr;
     execute stmt;
    END */$$
DELIMITER ;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

这里说一下这个怎么用,把上述代码复制,然后粘贴到eshop.sql文件中,导入数据库即可,到这个时候我们的整合工作已经完毕,下面简单创建一个登录的小项目尝试即可,后面引入bootstrap、vue等UI框架来完善项目!
总结:
1.web.xml
2.Spring MVC配置文件
3.Spring配置文件
4.数据库属性文件

三、编写源码

1.在src/main/java目录下,新建一个com.my.pojo包,在包中新建一个实体类UserInfo

package com.sunhao.pojo;

public class UserInfo {
	private int id;
	private String userName;
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

2.在src/main/java目录中,新建一个com.my.dao包,用于存放数据访问层接口。在包中新建一个接口UserInfoDao,在接口中声明方法

package com.sunhao.dao;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.sunhao.pojo.UserInfo;

public interface UserInfoDao {
	//根据用户名和密码查询
	@Select("select *from user_info where userName=#{userName} and password=#{password}")
	public UserInfo findUserInfoByCond(@Param("userName") String userName,@Param("password") String password);
}

3.在src/main/java目录中,新建一个com.my.service包,用于存放业务逻辑层接口。在包中新建一个接口UserInfoService,在接口中声明一个login方法,用于登录验证

package com.sunhao.service;

import com.sunhao.pojo.UserInfo;

public interface UserInfoService {
	public UserInfo login(String userName,String password);

}

4.新建UserInfoService接口的实现类serInfoServiceImpl,存放在com.my.service.impl包中,以实现login方法

package com.sunhao.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sunhao.dao.UserInfoDao;
import com.sunhao.pojo.UserInfo;
import com.sunhao.service.UserInfoService;

@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
	
	@Autowired
	private UserInfoDao userInfoDao;
	
	@Override
	public UserInfo login(String userName, String password) {
		
		return userInfoDao.findUserInfoByCond(userName, password);
	}

}

5.在src/main/java目录中,创建包com.my.controller,用于存放控制器类。在包中新建一个类UserInfoController

package com.sunhao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sunhao.pojo.UserInfo;
import com.sunhao.service.UserInfoService;

@Controller
public class UserInfoController {
	@Autowired
	private UserInfoService userInfoService;
	
	@RequestMapping("/login")
	public String login(UserInfo ui) {
		UserInfo tempUi = userInfoService.login(ui.getUserName(), ui.getPassword());
		if(tempUi!=null&&tempUi.getUserName()!=null) {
			return "index";
		}else {
			return "redirect:/login.jsp";
		}
	}
}

6.在src/main/webapp目录下,新建一个登录页login.jsp

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
	<table>
		<tr>
			<td>用户名:</td>
			<td><input type="text" name="userName" /></td>
		</tr>
		<tr>
			<td>密码:</td>
			<td><input type="text" name="password" /></td>
		</tr>
		<tr>
			<td><input type="submit" value="登录"></td>
		</tr>
	</table>
</form>
</body>
</html>

7.在src/main/webapp目录下,新建一个登录页index.jsp

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	欢迎您,登录成功!
</body>
</html>

我们看看此时的项目结构
在这里插入图片描述

此时,所有代码已经写完,就是输入用户名和密码,交给程序,程序去和数据库交互,判断是否匹配,如果匹配则跳转成功页面,否则就不跳转,实现这样一个非常简单的小功能,主要还是搭建环境的知识希望和大家分享。

四、运行

在包资源管理器中右击项目名,然后选择Run As、Run on Server命令,打开Run on Server对话框。选择Tomcat v9.0Server作为Web服务器,点击Finish按钮,在浏览器输入http://localhost:8080/ssm_demo/login.jsp即可访问,我们稍微演示一下
在这里插入图片描述
login.jsp页面
我们先看看数据库里有什么数据
在这里插入图片描述
我们用tom和123456测试一下
首先输入tom 12345
在这里插入图片描述
页面不跳转
在这里插入图片描述
然后输入tom和123456
在这里插入图片描述
结果
在这里插入图片描述
到这里,整个ssm从创建到配置到编码到运行,就结束啦!希望大家批评指正!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
springmvc4.3.3和mybatis3.4.1集成最新全部jar包,还包含了其他一些常用的jar包,很全,已经在项目中验证过。 lib/antlr-2.7.2.jar lib/aopalliance-1.0.jar lib/asm-3.3.1.jar lib/aspectjweaver-1.6.5.jar lib/axis-1.4.jar lib/axis-jaxrpc-1.4.jar lib/axis-saaj-1.4.jar lib/axis-wsdl4j-1.5.1.jar lib/cglib-2.2.2.jar lib/com.springsource.javax.xml.rpc-1.1.0.v20110517.jar lib/commons-chain-1.2.jar lib/commons-dbcp-1.4.jar lib/commons-pool-1.5.4.jar lib/gson-2.1.jar lib/hessian-4.0.7.jar lib/hsqldb-1.8.0.10.jar lib/ibatis-sqlmap-2.3.4.726.jar lib/jackson-all-1.9.9.jar lib/javax.inject.jar lib/javax.wsdl-1.6.1.jar lib/jettison-1.1.jar lib/json-lib-2.4-jdk15.jar lib/jstl-1.2.jar lib/jxl.jar lib/oro-2.0.8.jar lib/servlet-api.jar lib/tiles-api-2.2.0.jar lib/tiles-core-2.2.0.jar lib/tiles-jsp-2.2.0.jar lib/tiles-servlet-2.2.0.jar lib/tiles-template-2.2.0.jar lib/urlrewritefilter-3.1.0.jar lib/xpp3_min-1.1.4c.jar lib/xstream-1.3.jar lib/activation.jar lib/commons-collections-3.2.1.jar lib/dom4j-1.6.1.jar lib/ezmorph-1.0.2.jar lib/mail.jar lib/stax-api-1.0.1.jar lib/xmlbeans-2.3.0.jar lib/bouncycastle.jar lib/commons-codec-1.10.jar lib/httpclient-4.2.3.jar lib/httpclient-cache-4.2.3.jar lib/httpcore-4.2.2.jar lib/standard-1.1.2.jar lib/pinyin4j-2.5.0.jar lib/log4j-1.2.17.jar lib/jsqlparser-0.9.5.jar lib/kaptcha-2.3.2.jar lib/pagehelper-4.1.6.jar lib/log4j-api-2.3.jar lib/log4j-core-2.3.jar lib/mybatis-3.4.1.jar lib/slf4j-api-1.7.21.jar lib/slf4j-log4j12-1.7.21.jar lib/mybatis-ehcache-1.0.3.jar lib/mybatis-spring-1.3.0.jar lib/spring-aop-4.3.3.RELEASE.jar lib/spring-aspects-4.3.3.RELEASE.jar lib/spring-beans-4.3.3.RELEASE.jar lib/spring-context-4.3.3.RELEASE.jar lib/spring-context-support-4.3.3.RELEASE.jar lib/spring-core-4.3.3.RELEASE.jar lib/spring-expression-4.3.3.RELEASE.jar lib/spring-instrument-4.3.3.RELEASE.jar lib/spring-instrument-tomcat-4.3.3.RELEASE.jar lib/spring-jdbc-4.3.3.RELEASE.jar lib/spring-jms-4.3.3.RELEASE.jar lib/spring-messaging-4.3.3.RELEASE.jar lib/spring-orm-4.3.3.RELEASE.jar lib/spring-oxm-4.3.3.RELEASE.jar lib/spring-test-4.3.3.RELEASE.jar lib/spring-tx-4.3.3.RELEASE.jar lib/spring-web-4.3.3.RELEASE.jar lib/spring-webmvc-4.3.3.RELEASE.jar lib/spring-webmvc-portlet-4.3.3.RELEASE.jar lib/spring-websocket-4.3.3.RELEASE.jar lib/commons-beanutils-1.9.2.jar lib/commons-discovery-0.5.jar lib/commons-httpclient-3.1.jar lib/commons-logging-1.2.jar lib/druid-1.0.9.jar lib/ehcache-core-2.6.10.jar lib/freemarker-2.3.8.jar lib/mysql-connector-java-5.1.34.jar lib/struts2-core-2.0.11.jar lib/poi-3.11-20141221.jar lib/poi-excelant-3.11-20141221.jar lib/poi-ooxml-3.11-20141221.jar lib/poi-ooxml-schemas-3.11-20141221.jar lib/poi-scratchpad-3.11-20141221.jar lib/sqljdbc4.jar lib/ueditor.jar lib/commons-fileupload-1.3.2.jar lib/commons-io-2.5.jar lib/commons-lang-2.6.jar lib/commons-validator-1.5.1.jar

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值