eclipse使用maven搭建ssm项目流程

一、创建项目

1、创建文件,选择Other

步骤1

2、搜索Maven,选择Maven Project

步骤2

3、点击Next

步骤3

4、选择Maven-archetype-webapp,点击Next

步骤4

5、填写项目信息,点击Finish

步骤5

注:可能会出现的问题如下

1、index.jsp提示错误。
2、项目缺少src/main/java和src/test/java文件夹。
3、el表达式无法解析。

二、创建相关结构

1、包结构

在这里插入图片描述

2、web资源结构

在这里插入图片描述

三、通过Maven导入相关jar包

1、定义属性

<properties>
  	<spring.version>4.2.0.RELEASE</spring.version>
  	<mybatis.version>3.4.0</mybatis.version>
</properties>

2、jar坐标

 <!--测试-->
<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>3.8.1</version>
 	<scope>test</scope>
</dependency>

<!--添加servlet相关支持-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
<!--添加jsp相关支持-->
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
</dependency>

<!-- jstl标签库支持 -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</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-context-support</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
</dependency>

<!-- spring test支持 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
</dependency>

<!-- spring mvc支持 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>

<!-- spring 事务管理支持 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
</dependency>
<!-- spring jdbc支持 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
</dependency>

<!-- spring aop编程支持 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</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>1.3.0</version>
</dependency>

<!-- 数据库 -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
    <version>5.1.30</version>
</dependency>
<!-- 连接池 -->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid</artifactId>
    <version>1.0.16</version>
</dependency>

<!-- 注解 -->
<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>

注:更多坐标

点击查看》

四、创建配置文件

1、配置文件结构

在这里插入图片描述

2、db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?userUnicode=UTF-8
jdbc.username=root
jdbc.password=root

3、spring-mvc.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"
    xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
	
	<!-- 扫描包(Controller) -->
	<context:component-scan base-package="com"/>
	
	<!-- 开启注解驱动 -->
	<mvc:annotation-driven />
	
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/jsp/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	<!-- 配置静态资源 -->
	<mvc:resources location="/resources/" mapping="/resources/**" />
</beans>

4、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>
	
</configuration>

5、spring-mybatis.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
    <!-- 加载数据库链接配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
    	<property name="driverClassName" value="${jdbc.driver}"/>
    	<property name="url" value="${jdbc.url}"/>
    	<property name="username" value="${jdbc.username}"/>
    	<property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!-- 配置sqlSession工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
		<property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml" />
		<property name="typeAliasesPackage" value="com.entity" />
	</bean>
	
	<!-- Mapper扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.mapper"/>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
	</bean>
    
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- 配置事务管理 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    	<tx:attributes>
    		<tx:method name="save*" propagation="REQUIRED"/>
    		<tx:method name="delete*" propagation="REQUIRED"/>
    		<tx:method name="update*" propagation="REQUIRED"/>
    		<tx:method name="add*" propagation="REQUIRED"/>
    		<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
    		<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
    		<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
    	</tx:attributes>
    </tx:advice>
    
    <!-- 设置切面 -->
    <aop:config>
    	<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.service.impl.*.*(..))"/>
    </aop:config>
</beans>

6、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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>student</display-name>
  <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>
  
   <!-- 加载spring容器 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:spring/spring-*.xml</param-value>
  </context-param>
  
  <!-- 加载spring容器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 将所有请求交给springmvc框架处理 -->
  <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>
  <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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/</url-pattern>
  </filter-mapping>
 </web-app>

五、测试

1、创建相关文件

在这里插入图片描述

2、Student.java(实体类)

package com.entity;

public class Student {
	private Integer stuId;
	private String stuNumber;
	private String stuName;
	private String stuPassword;
	private String stuSex;
	private String stuClass;
	
	public Student(){}
	public Student(Integer stuId, String stuNumber, String stuName, String stuPassword, String stuSex,
			String stuClass) {
		this.stuId = stuId;
		this.stuNumber = stuNumber;
		this.stuName = stuName;
		this.stuPassword = stuPassword;
		this.stuSex = stuSex;
		this.stuClass = stuClass;
	}
	
	public Integer getStuId() {
		return stuId;
	}
	public void setStuId(Integer stuId) {
		this.stuId = stuId;
	}
	public String getStuNumber() {
		return stuNumber;
	}
	public void setStuNumber(String stuNumber) {
		this.stuNumber = stuNumber;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public String getStuPassword() {
		return stuPassword;
	}
	public void setStuPassword(String stuPassword) {
		this.stuPassword = stuPassword;
	}
	public String getStuSex() {
		return stuSex;
	}
	public void setStuSex(String stuSex) {
		this.stuSex = stuSex;
	}
	public String getStuClass() {
		return stuClass;
	}
	public void setStuClass(String stuClass) {
		this.stuClass = stuClass;
	}
	
	@Override
	public String toString() {
		return "Student [stuId=" + stuId + ", stuNumber=" + stuNumber + ", stuName=" + stuName + ", stuPassword="
				+ stuPassword + ", stuSex=" + stuSex + ", stuClass=" + stuClass + "]";
	}
	
}

3、StudentMapper.java(Mapper接口)

package com.mapper;

import java.util.List;

import com.entity.Student;

public interface StudentMapper {
	List<Student> allStudent();
}

4、StudentMapper.xml(映射文件)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Configuration 3.0//EN" "mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.StudentMapper"> 
  <resultMap type="Student" id="studentMap">
    <id property="stuId" column="s_id" javaType="java.lang.Integer"/> 
    <result property="stuNumber" column="s_number" javaType="java.lang.String"/> 
    <result property="stuName" column="s_name" javaType="java.lang.String"/> 
    <result property="stuPassword" column="s_password" javaType="java.lang.String"/> 
    <result property="stuSex" column="s_sex" javaType="java.lang.String"/> 
    <result property="stuClass" column="s_class" javaType="java.lang.String"/>
  </resultMap>  
   
  <select id="allStudent" resultMap="studentMap">
	select * from t_student
  </select>
</mapper>

5、StudentService(业务层)

package com.service;

import java.util.List;

import com.entity.Student;

public interface StudentService {
	List<Student> allStudent();
}

6、StudentServiceImpl(业务层实现类)

package com.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.entity.Student;
import com.mapper.StudentMapper;
import com.service.StudentServicer;

@Service
public class StudentServiceImpl implements StudentService{
	@Resource
	private StudentMapper studentMapper;
	
	public List<Student> allStudent() {
		return studentMapper.allStudent();
	}

}

7、StudentController(控制层)

package com.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.entity.Student;
import com.service.StudentServicer;

@Controller
public class StudentController {
	@Resource
	private StudentService studentService;
	
	@RequestMapping("/index")
	public String allStudent(Model model) {
		List<Student> list=studentService.allStudent();
		for (Student student : list) {
			System.out.println(student);
		}
		return "index";
	}
}

8、index.jsp(页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String path=request.getContextPath();
	String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<base href="<%=basePath %>">
		<title>Insert title here</title>
	</head>
	<body>
	<table>
		<thead>
			<tr>
              <th>学号</th>
              <th>姓名</th>
              <th>性别</th>
              <th>班级</th>
            </tr>
		</thead>
		<tbody>
			<c:forEach items="${students}" var="student">
				<tr>
					<td>${student.stuNumber }</td>
					<td>${student.stuName }</td>
					<td>${student.stuSex }</td>
					<td>${student.stuClass }</td>
				</tr>
			</c:forEach>
		</tbody>
	</table>
	
	</body>
</html>

9、运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值