SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

使用idea创建一个maven项目( 这里演示 的是 web项目)

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

点击 Finish

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

然后开始配置 pom.xml文件(添加各种依赖jar包)

先去找 spring 所需的 jar包

jar包中心仓库地址:http://mvnrepository.com/

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

找到 Spring Web ( 因为 我这里演示的是一个web项目)

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

把这段 复制到 pom.xml文件中

下面是我配置完整的pom.xml文件 ( 这里 我只在dependencies的下面 添加依赖 其他的 我都没有动  )

 <?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.oukele.demo</groupId>
<artifactId>SSM_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging> <name>SSM_demo 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>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties> <!-- 依赖 -->
<dependencies> <!-- spring 依赖包 -->
<!-- spring web jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<!-- spring aop jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<!-- spring 驱动jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<!-- spring mvc 依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<!-- mybatis 依赖包 -->
<!--mybatis jar包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- mybatis spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency> <!-- 连接数据库驱动的jar包 我这里使用的是 mariadb数据库的驱动包 -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.3.0</version>
</dependency> <!-- 连接接池 c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency> <!-- jstl jar包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> </dependencies> <build>
<finalName>SSM_demo</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>

配置完成后

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

等待 jar包 下载完成。

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

这些都是下载的jar包(这样pom.xml 文件 算是配置完成了。)

接下来开始 其他 xml文件的配置

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

main下面 如果 少一个 java 文件 (我们手动新建)

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

java 文件变成蓝色的就可以了。

建立项目结构

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

开始配置 spring xml 和 spring mvc xml

在 main 目录里 创建 一个 resources 文件,然后将它标志成 资源文件夹

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

在resources文件夹中 新建一个 spring_root.xml 文件

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

spring_root文件的完整配置信息

 <?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:contxt="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 扫描 service 包下面使用@Component/@Service/@Repository/@Controller 注解的类 将其实例化,放入容器中 -->
<contxt:component-scan base-package="com.oukele.bookshop_ssm.service" /> <!--创建数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库驱动 -->
<property name="driverClass" value="org.mariadb.jdbc.Driver" />
<!-- 数据库连接 -->
<property name="jdbcUrl" value="jdbc:mariadb://localhost:3306/data_test" />
<!-- 数据库用户名 -->
<property name="user" value="oukele"/>
<!-- 数据库密码 -->
<property name="password" value="oukele"/>
</bean> <!-- 配置mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源的实例化 -->
<property name="dataSource" ref="dataSource"/>
<!-- 映射 DML 语法 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 映射 数据访问层 ( dao ) 中的接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.oukele.bookshop_ssm.dao"/>
</bean> </beans>

spring_web文件的完整配置信息

 <?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:contxt="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 扫描 控制层 (web) 下面 @Controller 注解的类 将其实例化,放入容器中 -->
<contxt:component-scan base-package="com.oukele.bookshop_ssm.web"/>
<!-- 启动 mvc 的常用注解 -->
<mvc:annotation-driven/>
<!--将所有的静态资源,交给 Servlet 处理-->
<mvc:default-servlet-handler/> <!--配置 view 解析器-->
<!-- 页面接受数据 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean> </beans>

在resources 文件夹 新建一个包 ( 包名随意趣吧,有意义一些的)

然后再那个包里新建一个 xml文件 用来映射 DML语法

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

BookMapper.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"> <!-- 映射 接口 IBookDao -->
<mapper namespace="com.oukele.bookshop_ssm.dao.IBookDao"> <!-- 这里的id 是 接口中的方法名 -->
<select id="listAll" resultType="com.oukele.bookshop_ssm.entity.Book">
select * from ssm_book
</select> </mapper>

这里xml文件差不多配置完成了。

开始写代码啦。

(entity)实体类。

 package com.nf147.bookstore_ssm.entity;

 public class Book {
private int bookid;
private String name;
private int cnt; public Book() {
} public Book(String name, int cnt) {
this.name = name;
this.cnt = cnt;
} public int getBookid() {
return bookid;
} public void setBookid(int bookid) {
this.bookid = bookid;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getCnt() {
return cnt;
} public void setCnt(int cnt) {
this.cnt = cnt;
} @Override
public String toString() {
return "Book{" +
"bookid=" + bookid +
", name='" + name + '\'' +
", cnt=" + cnt +
'}';
}
}

(dao)数据访问层

 package com.oukele.bookshop_ssm.dao;

 import com.oukele.bookshop_ssm.entity.Book;
import org.springframework.stereotype.Repository;
import java.util.List; @Repository
public interface IBookDao {
List<Book> listAll();
}

(service)服务层

 package com.oukele.bookshop_ssm.service;

 import com.oukele.bookshop_ssm.entity.Book;

 import java.util.List;

 public interface BookService {
List<Book> listAll();
}
 package com.oukele.bookshop_ssm.service;

 import com.oukele.bookshop_ssm.dao.IBookDao;
import com.oukele.bookshop_ssm.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class BookServiceTemp implements BookService { // 使用 dao 中的接口
@Autowired
private IBookDao bookDao; @Override
public List<Book> listAll() {
return bookDao.listAll();
}
 package com.oukele.bookshop_ssm.web;

 import com.oukele.bookshop_ssm.entity.Book;
import com.oukele.bookshop_ssm.service.BookServiceTemp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller
public class BookController { @Autowired
private BookServiceTemp bookServiceTemp; // 请求url
@RequestMapping(value = "/books")
public ModelAndView getList(){
// 页面的名字
ModelAndView mv = new ModelAndView("list");
// 获取数据
List<Book> list = bookServiceTemp.listAll();
mv.addObject("book",list);
return mv;
} }

(web)控制层

 package com.oukele.bookshop_ssm.web;

 import com.oukele.bookshop_ssm.entity.Book;
import com.oukele.bookshop_ssm.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller
public class BookController { @Autowired
private BookService bookService; // 请求url
@RequestMapping(value = "/books")
public ModelAndView getList(){
// 页面的名字
ModelAndView mv = new ModelAndView("list");
// 获取数据
List<Book> list = bookService.listAll();
mv.addObject("book",list);
return mv;
} }

jsp页面

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
书籍列表:
<br> <table>
<tr>
<th>编号</th>
<th>书名</th>
<th>数量</th>
</tr>
<tbody>
<c:forEach items="${book}" var="books" >
<tr>
<td>${books.bookid}</td>
<td>${books.name}</td>
<td>${books.cnt}</td>
</tr>
</c:forEach>
</tbody>
</table> </body>
</html>

项目结构:

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

配置 web.xml文件

完整的web.xml文件信息

 <!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> <!--配置 Spring 容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring_root.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!--配置 Spring mvc 容器-->
<!--将所有请求,都交给Spring MVC 处理-->
<servlet>
<servlet-name>webs</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring_web.xml</param-value>
</init-param>
</servlet>
<!--将 请求 截取 转交给 webs -->
<servlet-mapping>
<servlet-name>webs</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

配置tomcat服务器,并启动。

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

介绍可能有点不全且有点乱。(所以查看源码是最快的。)

项目地址:https://github.com/oukele/ssm_demo

E-R流程图:https://www.processon.com/mindmap/5bac29eae4b015327af18db5

上一篇:GT sport赛道详解 - Dragon Trail | 龙之径


下一篇:python读取excel的行数