ecplice搭建ssm
一、环境配置
1. window 7 64位
2. jdk 1.8.0_91 64位
3. ecplice 4.5 64位
4. mysql 5.7.27 64位
5. tomcat 7.0
二、创建一个maven的webapp工程
1. 创建一个maven项目
2. 使用默认的项目空间存储
3. 选择创建类型为webapp
4. 填写Group id和Artifact id以及版本号,点击Finish即可
5. 创建工程成功
三、给项目配置Tomcat
1. 点开菜单栏中的window选择Preferences
2. 打开后选择Server中的Runtime Environment,点击add,添加运行环境
3. 选择你使用的Tomcat版本
4. 选择你的Tomcat安装目录
5. 选择你的Tomcat在电脑上的安装目录,点击确定
6. 选择好目录点击Finish完成即可
7. 选择成功后,效果图如下:
8. 如果选择之后出现如下错误,则需要给该项目再添加一下Build Path
1)右键项目选择Build Path,选择Configure Build Path。
2)选择添加 library
3)选择Server Runtime
4)选择Tomcat 7.0,点击Finish
5)点击ok即可
9. 如果出现以下警告,可以如下处理
1)移除Build Path中的jre
2) 点击add Library
3)选择工程默认的jre
4) 添加成功后,点击ok即可,看到警告消失了
四、配置项目中的配置文件
1. 配置pom.xml文件,来添加SSM框架所需要的jre包(我们在pom.xml中添加好所需要的jar的版本,Maven项目则会自动导入该jar包,第一次导入的时候因为需要下载所以可能等待的时间有点长)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test.java.xiao</groupId> <artifactId>SSMDemo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SSMDemo Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- 测试包相关 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- spring和springMVC 的基本依赖 开始 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.18.RELEASE</version> </dependency> <!-- spring 的基本依赖 结束 --> <!-- 添加mybatis依赖 --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!--spring连接mybats--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!--servlet和jsp的依赖--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!--gson处理json数据--> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> </dependency> <!-- mysql 驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--c3p0连接池--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- 需要的log4j2日志包依赖 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-web</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-1.2-api</artifactId> <version>2.5</version> </dependency> <!--文件上传--> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> </dependencies> <build> <finalName>SSMDemo</finalName> </build> </project>
2. jar加载成功后可在以下的目录下看到:
3. 在src/main/resources下创建以下四个文件(如果main下面没有resources文件夹,可以自己手动创建)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd "> <!-- mybatis:scan会将org.fkit.mapper包里的所有接口当作mapper配置,之后可以自动引入mapper类--> <mybatis:scan base-package="org.fkit.mapper"/> <!-- 扫描org.fkit包下面的java文件,有Spring的相关注解的类,则把这些类注册为Spring的bean --> <context:component-scan base-package="org.fkit"/> <!-- 使用PropertyOverrideConfigurer后处理器加载数据源参数 --> <context:property-override location="classpath:db.properties"/> <!-- 配置c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"/> <!-- 配置SqlSessionFactory,org.mybatis.spring.SqlSessionFactoryBean是Mybatis社区开发用于整合Spring的bean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource"/> <!-- JDBC事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <!-- 启用支持annotation注解方式事务管理 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 自动扫描该包,SpringMVC会将包下用了@controller注解的类注册为Spring的controller --> <context:component-scan base-package="org.fkit.controller"/> <!-- 设置默认配置方案 --> <mvc:annotation-driven/> <!-- 使用默认的Servlet来响应静态文件 --> <mvc:default-servlet-handler/> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix"> <value>/WEB-INF/content/</value> </property> <!-- 后缀 --> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://127.0.0.1:3306/xiaoxiao
dataSource.user=xiaoxiao
dataSource.password=xiaoxiao
dataSource.maxPoolSize=20
dataSource.maxIdleTime=1000
dataSource.minPoolSize=6
dataSource.initialPoolSize=5
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.org.fkit.mapper.TestMapper=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
注意,配置文件中的版本号要改成你下载的jar包对应的版本号,否则会报错的:
注意db.properties中配置的是你的连接的数据库信息,所以需要修改成你的数据库配置:
4. 配置web.xml文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Archetype Created Web Application</display-name> <!-- 配置spring核心监听器,默认会以 /WEB-INF/applicationContext.xml作为配置文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- contextConfigLocation参数用来指定Spring的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 定义Spring MVC的前端控制器 --> <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:springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 让Spring MVC的前端控制器拦截所有请求 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 编码过滤器 --> <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> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
5. 配置好后run as server,如果运行未报错,则证明环境搭建成功。
6. 在src/main/java的目录下创建好对应的文件夹方便我们管理我们的文件
在java的目录下创建org.fkit文件夹,在org.fkit下创建controller domain mapper service 四个文件夹,在service下创建impl文件夹。
在webapp目录下差un感觉static文件夹,在static下创建css image js 三个文件夹。
其中: controller 是用来存放我们的controller文件,
domain 是用来存放我们的表的实体类文件,
mapper 是用来存放我们的sql语句的接口类,
service 是用来存放我们的使用接口,
Impl 是用来存放我们使用接口的实现类,调用我们的sql语句接口类返回的数据,
css 是用来存放我们的css文件,
image 是用来存放我们的图片文件,
js 是用来存放我们的js文件。
五、创建表,并在项目中对该表进行查询操作
1. 在mysql中创建一张表,并添加一些数据
mysql -uxiaoxiao -pxiaoxiao #登录mysql use xiaoxiao #使用xiaoxiao数据库 create table food ( id int primary key, f_name varchar(20), f_price float, f_come float ); # 创建表 insert into food(id,f_name,f_price,f_come) values(1,'苹果',11.8,3.2); insert into food(id,f_name,f_price,f_come) values(2,'橘子',12.7,5.2); insert into food(id,f_name,f_price,f_come) values(3,'香蕉',18.2,8.2); #插入数据
2. 创建该表的实体类(右键domain文件夹选择new选择class)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package org.fkit.domain; public class Food { private int id; //物品编号 private String f_name; //物品名称 private float f_price; //物品价格 private float f_come; //物品进价 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getF_name() { return f_name; } public void setF_name(String f_name) { this.f_name = f_name; } public float getF_price() { return f_price; } public void setF_price(float f_price) { this.f_price = f_price; } public float getF_come() { return f_come; } public void setF_come(float f_come) { this.f_come = f_come; } @Override public String toString() { return "Food [id=" + id + ", f_name=" + f_name + ", f_price=" + f_price + ", f_come=" + f_come + "]"; } }
3. 创建sql语句接口(在mapper文件夹上右键选择new,选择interface,创建一个叫FoodMapper的接口)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package org.fkit.mapper; import java.util.List; import org.apache.ibatis.annotations.Select; import org.fkit.domain.Food; public interface FoodMapper { /*查询食物表中的所有记录*/ @Select("select * from food") List<Food> selectAllFood(); }
4. 创建使用接口
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package org.fkit.service; import java.util.List; import org.fkit.domain.Food; public interface FoodService { //查询食物全部信息 List<Food> selectAllFood(); }
5. 创建使用接口实现类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package org.fkit.service.impl; import java.util.List; import org.fkit.domain.Food; import org.fkit.mapper.FoodMapper; import org.fkit.service.FoodService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service("foodService") public class FoodServiceImpl implements FoodService { @Autowired private FoodMapper foodMapper; public List<Food> selectAllFood() { // TODO Auto-generated method stub return foodMapper.selectAllFood(); } }
6. 创建jsp页面并查询表中数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ 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>
<table>
<tr>
<td>食物编号</td>
<td>食物名称</td>
<td>食物价格</td>
<td>食物进价</td>
</tr>
</table>
</body>
</html>
7. 创建controller
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package org.fkit.controller; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.fkit.domain.Food; import org.fkit.service.FoodService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class FoodController { @Autowired @Qualifier("foodService") private FoodService foodService; @RequestMapping(value="login") public String login(HttpServletRequest request) { HttpSession session=request.getSession(); List<Food> f=foodService.selectAllFood(); session.setAttribute("food", f); System.out.println(f); return "login"; } }
8. 测试结果
可以看到查询成功;