目录
目录
准备环境
创建maven web项目
SSM项目搭建:
第一步:准备数据
第二步:开发持久层(mybatis)
第三步: 开发业务层(spring)
第四步: 开发web层 (springmvc)
准备环境
操作系统: windows 10 家庭中文版
开发工具:IntelliJ IDEA 2017.3 x64
jdk :1.8
MySql :5.7
Maven : 3.5.4
Maven仓库 : 自己准备
参考 : maven的安装配置以及本地仓库的配置
注意:项目所有路径都不要出现中文
创建maven web项目
第一步:选择创建maven项目,选择maven项目,jdk选择1.8
第二步:
- 设置GroupId(项目组织唯一标识,实际对应java的包结构,是mian目录里java的目录结构,一般是公司域名倒序)
- 设置ArtifactId(项目的唯一标识符,实际对应项目的名称,就是项目根目录的名称)
第三步:
设置工程名称
第四步:确定创建
第五步: 导入maven仓库
打开IDEA设置 File ---------> Settings
1.根据自己maven的安装位置和maven仓库的位置配置,进行如下配置。
2.配置maven的runner,输入: -DarchetypeCatalog=internal JDK选择1.8
SSM项目搭建
第一步:准备数据
1.1 创建数据库
CREATE DATABASE qisoft1213_ssm;
CREATE TABLE `traveladdress` (
`id` BIGINT (9),
`name` VARCHAR (150),
`price` DOUBLE ,
`pic` VARCHAR (150),
`createtime` TIMESTAMP ,
`detail` VARCHAR (150)
);
INSERT INTO `traveladdress` (`id`, `name`, `price`, `pic`, `createtime`, `detail`) VALUES('1','宁夏大学','99',NULL,'2018-06-04 13:48:14','带你飞');
INSERT INTO `traveladdress` (`id`, `name`, `price`, `pic`, `createtime`, `detail`) VALUES('2','中卫校区','88',NULL,'2018-06-04 13:48:31','人生巅峰');
1.2数据库展示
第二步:开发持久层(mybatis)
2.1 在pom.xml文件中引入SSM依赖
调试技巧:当导入依赖坐标时发生错误,先到库中查看库是否存在,若存在,检查版本是否一致,若一致,则在pom.xml文件中先删除出错依赖的版本信息(或者将依赖删除),删除后重新添加。
<?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>cn.edu.nxu</groupId>
<artifactId>qisoft1213_ssm_01</artifactId>
<version>1.0-SNAPSHOT</version>
<!---打包方式-->
<packaging>war</packaging>
<properties>
<spring.version>5.0.4.RELEASE</spring.version>
<springmvc.version>5.0.4.RELEASE</springmvc.version>
<mybatis.version>3.4.5</mybatis.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- springMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springmvc.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</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-expression</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-aspects</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-test</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>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Mybatis和mybatis与spring的整合 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- MySql驱动 -->
<!-- druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<!-- springMVC核心 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency> <!-- spring相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency> <!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency> <!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
</project>
2.2代码实现 : dao持久层接口 domain 实体类 mybatis框架
配置文件:
ssm整合后被spring的 applicationContext.xml 代替
数据源 dataSource
接口扫描类 MapperScaner
session工厂 sessionFactory
2.3目录结构 :
2.4编写持久层接口ITravelAddressDao
package cn.edu.nxu.dao;
import cn.edu.nxu.domain.TravelAddress;
/**
* @Author qisnd@foxmail.com
* @Date 2018/11/20 20:30
* @Description:
* 接口搭配xml文件开发规则:
* 接口路径和mapper映射文件路径要一致
* namespace 等于接口的全路径
* 接口类名和映射文件的id相同
* 参数和返回值类型要和映射文件类型一致
**/
public interface ITravelAddressDao {
//查询所有
TravelAddress findById(Integer id);
}
2.5编写实体类TravelAddress代码,属性名和字段名保持一致
package cn.edu.nxu.domain;
import java.io.Serializable;
import java.util.Date;
/**
* @Author qisnd@foxmail.com
* @Date 2018/11/20 20:30
* @Description:
**/
public class TravelAddress implements Serializable {
//成员变量名称和数据库表字段名称要相同(否则需要去映射)
private Integer id;
private String name;
private Float price;
private String pic;
private Date createTime;
private String detail;
//get | set 方法
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
//toString
@Override
public String toString() {
return "TravelAddress{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", pic='" + pic + '\'' +
", createTime=" + createTime +
", detail='" + detail + '\'' +
'}';
}
}
2.6配置映射文件(路径和dao接口目录保持一致,名称也要一致)ITravelAddressDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!--namespace是接口的全限定类名-->
<mapper namespace="cn.edu.nxu.dao.ITravelAddressDao">
<!--id要和接口中的方法名称相同 ,返回值或参数类型,此处用别名(实体类名第一个字母小写)-->
<select id="findById" resultType="travelAddress" parameterType="Integer">
SELECT * from traveladdress WHERE id =#{id};
</select>
</mapper>
2.7配置applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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:mvc="http://www.springframework.org/schema/mvc"
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.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!--数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///qisoft1213_ssm"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!--session工厂-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--别名的路径扫描-->
<property name="typeAliasesPackage" value="cn.edu.nxu.domain"></property>
</bean>
<!--扫描接口生成代理实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.edu.nxu.dao"></property>
</bean>
</beans>
2.8编写mybatis测试类代码
package cn.edu.nxu.test;
import cn.edu.nxu.dao.ITravelAddressDao;
import cn.edu.nxu.domain.TravelAddress;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author qisnd@foxmail.com
* @Date 2018/11/22 0:58
* @Description:
* 测试方法一: 通过注解测试
* 测试方法二 : 通过加载文件
**/
public class TestMybatis {
@Test
public void testMybatis(){
//创建spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//获取实现dao接口代理对象
ITravelAddressDao bean = ac.getBean(ITravelAddressDao.class);
TravelAddress address = bean.findById(1);
System.out.println(address);
}
}
2.9结果显示
第三步:开发业务层(spring)
3.1 代码实现 service层接口 service层接口实现类 搭建spring框架并整合mybatis
配置文件:
applicationContext.xml
包扫描 context:component-scan 扫描自定义业务实现类管理
事务管理器 transactionManager
事务的通知 tx:Advice
aop切面配置 aop:config
3.2 目录结构
3.3 创建service接口 ITravelAddressService
package cn.edu.nxu.service;
import cn.edu.nxu.domain.TravelAddress;
/**
* @Author qisnd@foxmail.com
* @Date 2018/11/22 1:51
* @Description:
**/
public interface ITravelAddressService {
TravelAddress findById(Integer id);
}
3.4 创建service接口的实现类TraveladdressServiceImpl
package cn.edu.nxu.service.Impl;
import cn.edu.nxu.dao.ITravelAddressDao;
import cn.edu.nxu.domain.TravelAddress;
import cn.edu.nxu.service.ITravelAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @Author qisnd@foxmail.com
* @Date 2018/11/22 1:53
* @Description:
**/
//该注解表示将该类交给spring来管理
@Service
public class TraveladdressServiceImpl implements ITravelAddressService {
//注入
@Autowired
private ITravelAddressDao travelAddressDao;
@Override
public TravelAddress findById(Integer id) {
return travelAddressDao.findById(id);
}
}
3.5 在applicationContext.xml中添加service层的相关配置
<!--配置Service层-->
<!--包扫描器,扫描自定义业务实现类管理-->
<Context:component-scan base-package="cn.edu.nxu.service"></Context:component-scan>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务的通知-->
<tx:advice id="txAdvice">
<!--设置支持事务的方法-->
<tx:attributes>
<!--REQUIRED : 必须开启事务-->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--配置aop切面 定义事务支持的规则,哪些类,哪些方法需要支持到事务-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="pc" expression="execution(* cn.edu.nxu.service.*.*(..))"></aop:pointcut>
<!--将事务通知和切入点整合到一起-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"></aop:advisor>
</aop:config>
3.6 创建service搭建完成后的测试类 TestSpring
package cn.edu.nxu.test;
import cn.edu.nxu.domain.TravelAddress;
import cn.edu.nxu.service.ITravelAddressService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @Author qisnd@foxmail.com
* @Date 2018/11/22 2:17
* @Description:
**/
//spring的测试类和junit的测试类整合
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestSpring {
@Autowired
private ITravelAddressService travelAddressService;
@Test
public void testSpring(){
TravelAddress address = travelAddressService.findById(1);
System.out.println(address);
}
}
3.7 结果展示
第四步: 开发web层
4.1代码实现 接受请求过来的动作类 Controller
将spring和spring MVC整合 : 即将service层整合到Controller
配置文件:
springMVC.xml
扫描动作类的包路径
视图解析器
MVC注册驱动
静态资源放行 本质是静态资源不需要叫给spring MVC处理
Web.xml
核心控制器
Spring 监听器
乱码过滤器
4.2 目录结构
4.3 编写Controller类 TravelAddressController
package cn.edu.nxu.web;
import cn.edu.nxu.domain.TravelAddress;
import cn.edu.nxu.service.ITravelAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author qisnd@foxmail.com
* @Date 2018/11/22 2:43
* @Description:
**/
@Controller
@RequestMapping("/travelAddress")
public class TravelAddressController {
@Autowired
private ITravelAddressService travelAddressService;
/**
* model 对象的作用域 request
* model对象最终将数据丢到了request中
* 相当于HttpServiceRequest
*/
@RequestMapping("/findById")
public String findById(Integer id, Model model) {
TravelAddress ta = travelAddressService.findById(id);
model.addAttribute(ta);
return "success";
}
}
4.4 配置sprinMvc.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!--配置要扫描的Controller包-->
<context:component-scan base-package="cn.edu.nxu.web"></context:component-scan>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--驱动代理适配器和映射器-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--静态资源放行,不被springmvc处理,交由handler处理-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>
4.5 测试成功页面 success.jsp
<%--
Created by IntelliJ IDEA.
User: qisnd@foxmail.com
Date: 2018/11/22
Time: 2:53
To change this template use File | Settings | File Templates.
--%>
<%@ 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>
<form>
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td>${travelAddress.name }</td>
</tr>
<tr>
<td>商品价格</td>
<td>${travelAddress.price }</td>
</tr>
<tr>
<td>创建时间</td>
<td>${travelAddress.createTime}</td>
</tr>
<tr>
<td>商品描述</td>
<td>${travelAddress.detail}
</td>
</tr>
</table>
</form>
</body>
</html>
4.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">
<!--配置核心控制器-->
<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.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>
<!--spring框架的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--乱码过滤器-->
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4.7 编写index.jsp测试代码
<%--
Created by IntelliJ IDEA.
User: qisnd@foxmail.com
Date: 2018/11/22
Time: 2:53
To change this template use File | Settings | File Templates.
--%>
<%@ 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>
<a href="travelAddress/findById?id=1">测试连接</a>
</body>
</html>
4.8 方法一:idea右侧,启动tomcat插件测试
注意:如果编译时需要下载,请联网。
测试结果展示:
如需直接部署到tomcat服务器上进行测试,请参考