❝本文已收录至https://github.com/likekk/studyBlog欢迎大家star,共同学习,共同进步。如果文章有错误的地方,欢迎大家指出。后期将在将GitHub上规划前端学习的路线和资源分享。
❞
每一篇文章都希望您有所收获,每一篇文章都希望您能静下心来浏览、阅读。每一篇文章都是作者精心打磨的作品。
如果您觉得杨戬这个小白还有点东西的话,杨戬希望正在看文章的您可以帮忙点亮那个点赞的按钮,对于杨戬这个暖男来说,「真的真的非常重要」,这将是我持续写作的动力。
学习完MyBatis,Spring,SpringMVC之后,我们需要做的就是将这三者联系起来,Spring实现业务对象管理,Spring MVC负责请求的转发和视图管理, MyBatis作为数据对象持久化引擎。这样搭配的优点是:轻量、自由度高、Spring与Spring MVC契合度更好。通过一个图书管理示例完成SSM框架的集成。
1.我们打开idea选择create new project,然后选择Maven项目,我们暂时先不要选择maven骨架,等我们的视图层(WEB-UI层)的时候再选择maven骨架
在Maven世界中,每个工程都有它唯一的 组织名、模块名、版本 ,这三个就是maven项目的坐标。一个maven工程可以打包成jar、war、pom等形式,但是它们都是拥有上述三个坐标的。我们在项目过程中导入的jar包就是通过上述坐标值唯一确定的。因此,我们在创建一个maven工程时会要求指定具体的 组织名、模块名、版本、打包方式。
由于之前的步骤都是一致,这里就不重复上述步骤了,直接完成最后一步
实体层和mmon层一样都是同样的操作步骤,这里我们就不进行截图了,操作步骤都一样
1.子模块web-ui层需要讲一下,因为我们这一层是视图层,所以我们需要选择maven骨架。
2.然后点击next,输入模块名之后finish就可以了,在这里我们需要选择自己本地的中央仓库。自此我们的多模块项目已经搭建好了
4.在这里我们发现视图层多了webapp这个文件夹,以后的开发过程中我们的视图都是放在WEB_INF目录下的
1.接下来我们去父模块的pom.xml文件进行统一的模块管理,为了方便管理版本和后续添加依赖不那么繁琐,我们在父模块pom.xml文件中只对依赖进行统一管理起来,而不进行依赖,当我们需要哪一个依赖的时候,我们只需要引入即可。
父模块pom.xml文件
"1.0" encoding="UTF-8"?>
"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">
4.0.0
com.ssm.booksystem BookSystem pom 1.0-SNAPSHOT Book-Common Book-Dao Book-Service Book-WEBUI Book-Entity 1.0-SNAPSHOT 1.0-SNAPSHOT 1.0-SNAPSHOT 1.0-SNAPSHOT 1.0-SNAPSHOT 2.7.4 3.4 2.6.1 4.12 4.3.18.RELEASE 4.3.0.RELEASE 1.8.9 3.2.4 1.3.0 5.1.38 3.4.1 0.9.1.2 1.2 4.0.1 2.1 5.2.2.Final 2.4 1.3.1 2.3.23 com.ssm.booksystem Book-Common ${Book-Common.version} com.ssm.booksystem Book-Dao ${Book-Dao.version} com.ssm.booksystem Book-Service ${Book-Service.version} com.ssm.booksystem Book-Entity ${Book-Entity.version} com.ssm.booksystem Book-WEBUI ${Book-WEBUI.version} com.fasterxml.jackson.core jackson-databind ${jackson.version} com.fasterxml.jackson.core jackson-core ${jackson.version} com.fasterxml.jackson.core jackson-annotations ${jackson.version} org.apache.commons commons-lang3 ${commons-lang3.version} org.apache.logging.log4j log4j-core ${log4j.version} junit junit ${junit.version} org.springframework spring-test ${spring-test.version} org.springframework spring-context ${spring.version} org.aspectj aspectjweaver ${aspectjweaver.version} cglib cglib ${cglib.version} org.mybatis mybatis-spring ${mybatis-spring.version} org.springframework spring-jdbc ${spring.version} mysql mysql-connector-java ${mysql-connector-java.version} org.mybatis mybatis ${mybatis.version} c3p0 c3p0 ${c3p0.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-context-support ${spring.version} javax.servlet jstl ${jstl.version} javax.servlet javax.servlet-api ${servlet-api.version} javax.servlet.jsp jsp-api ${jsp-api.version} org.hibernate hibernate-validator ${hibernate.version} commons-io commons-io ${commons-io.version} commons-fileupload commons-fileupload ${commons-fileupload.version} org.freemarker freemarker ${freemarker.version}
common模块中的pom.xml文件
"1.0" encoding="UTF-8"?>
"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">
BookSystem com.ssm.booksystem 1.0-SNAPSHOT 4.0.0 Book-Common com.fasterxml.jackson.core jackson-databind com.fasterxml.jackson.core jackson-core com.fasterxml.jackson.core jackson-annotations org.apache.commons commons-lang3
Dao模块中的pom.xml文件,这一层中我们需要依赖common层和实体层,因为在这一层中我们涉及到和数据库打交道,一张表就对应一个POJO
"1.0" encoding="UTF-8"?>
"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">
BookSystem com.ssm.booksystem 1.0-SNAPSHOT 4.0.0 Book-Dao com.ssm.booksystem Book-Common com.ssm.booksystem Book-Entity org.apache.logging.log4j log4j-core junit junit org.springframework spring-test org.springframework spring-context org.aspectj aspectjweaver cglib cglib org.mybatis mybatis-spring org.springframework spring-jdbc mysql mysql-connector-java org.mybatis mybatis c3p0 c3p0
Service模块中的pom.xml文件,这一层中我们需要依赖dao层
"1.0" encoding="UTF-8"?>
"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">
BookSystem com.ssm.booksystem 1.0-SNAPSHOT 4.0.0 Book-Service com.ssm.booksystem Book-Dao
实体层暂时不需要依赖任何的模块,Entity模块中的pom.xml文件
"1.0" encoding="UTF-8"?>
"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">
BookSystem com.ssm.booksystem 1.0-SNAPSHOT 4.0.0 Book-Entity
Service模块中的pom.xml文件,这一层中我们需要依赖service层
"1.0" encoding="UTF-8"?>
"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">
BookSystem com.ssm.booksystem 1.0-SNAPSHOT 4.0.0 Book-WEBUI war Book-WEBUI Maven Webapp http://www.example.com UTF-8 1.7 1.7 junit junit 4.11 test com.ssm.booksystem Book-Service org.springframework spring-webmvc org.springframework spring-context-support javax.servlet javax.servlet-api commons-io commons-io commons-fileupload commons-fileupload javax.servlet.jsp jsp-api javax.servlet jstl Book-WEBUI maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-war-plugin 3.2.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2
1.这是navicat的操作界面,前面的一些连接配置我在这里就不进行过多的描述了。
2.右键localhost_3306选择新建数据库,输入数据的名称,字符集我们选择倒数第二个,也就是utf-8的然后确定就可了。
3.数据库脚本
create table book(
bid int auto_increment primary key not null COMMENT'图书编号',
bname varchar(50) not null COMMENT'图书名称',
bauthor VARCHAR(50) COMMENT'图书作者'
)
INSERT into book(bname,bauthor)VALUES ('斗罗大陆','唐家三少'), ('假如给我三天光明','海伦凯勒'), ('斗破苍穹','天蚕土豆'), ('雪鹰领主','我吃西红柿') SELECT * from book
1.首先我们先移除父模块的src目录,其实移不移除都无所谓,在这里我主要是为了影响干扰,移除之后目录如下:
2.我们需要在每一个子模块添加对应的包名和配置文件 子模块common层
「R.java」
package com.booksystem.common;
import java.util.HashMap;
import java.util.Map;
/** * 返回数据封装 */ public class R extends HashMap { private static final long serialVersionUID = 1L; public R() { put("code", 1); put("msg", "success"); } //错误时 public static R error() { return error(500, "未知异常,请联系管理员"); } public static R error(String msg) { return error(500, msg); } public static R error(int code, String msg) { R r = new R(); r.put("code", code); r.put("msg", msg); return r; } //成功时 public static R ok(String msg) { R r = new R(); r.put("msg", msg); return r; } public static R ok(Map map) { R r = new R(); r.putAll(map); return r; } public static R ok() { return new R(); } public static R ok(Object data) { return new R().put("data",data); } @Override public R put(String key, Object value) { super.put(key, value); return this; } }
BookMapper.xml
"1.0" encoding="UTF-8"?>
"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
"com.booksystem.dao.BookDao" >
resultMap="bookMap"> select bid,bname,bauthor from book "bookMap" type="book"> "bid" property="bid"/> "bname" column="bname"/> "bauthor" property="bauthor"/>
applicationContext.xml
"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" 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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> "classpath*:db.properties" /> "datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> "driverClass" value="${mysql.driver}" /> "jdbcUrl" value="${mysql.url}" /> "user" value="${mysql.uid}" /> "password" value="${mysql.password}" /> "acquireIncrement" value="${mysql.acquireIncrement}"> "initialPoolSize" value="${mysql.initialPoolSize}"> "minPoolSize" value="${mysql.minPoolSize}"> "maxPoolSize" value="${mysql.maxPoolSize}"> "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> "configLocation" value="classpath:mybatis.xml"> "dataSource" ref="datasource"/> "mapperLocations" value="classpath*:mapper/*Mapper.xml"> "org.mybatis.spring.mapper.MapperScannerConfigurer" > "sqlSessionFactoryBeanName" value="sqlSessionFactory"/> "basePackage" value="com.booksystem.dao"/> "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> "dataSource" ref="datasource"> "transactionManager" /> "com.booksystem.dao" />
db.properties
##mysql连接字符串
#驱动
mysql.driver=com.mysql.jdbc.Driver
#连接字符串
mysql.url=jdbc:mysql://localhost:3306/booksystem?useUnicode=true&characterEncoding=UTF-8
#用户名 mysql.uid=root #密码 mysql.password=123456 mysql.acquireIncrement=5 mysql.initialPoolSize=10 mysql.minPoolSize=5 mysql.maxPoolSize=20
mybatis.xml
"1.0" encoding="UTF-8"?>
"-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
"db.properties" >
"logImpl" value="STDOUT_LOGGING"/> "com.booksystem.entity" />
BookDao,java
package com.booksystem.dao;
import com.booksystem.entity.Book;
import java.util.List;
public interface BookDao { //查询全部图书信息 public List getAllBooks(); }
子模块entity层 Book.java
package com.booksystem.entity;
/*
*图书实体类
* */
public class Book {
private long bid; //图书编号 private String bname; // 图书名称 private String bauthor; // 图书作者 public long getBid() { return bid; } public void setBid(long bid) { this.bid = bid; } public String getBname() { return bname; } public void setBname(String bname) { this.bname = bname; } public String getBauthor() { return bauthor; } public void setBauthor(String bauthor) { this.bauthor = bauthor; } //无参构造方法 public Book(){} //带参构造方法 public Book(long bid, String bname, String bauthor) { this.bid = bid; this.bname = bname; this.bauthor = bauthor; } @Override public String toString() { return "Book{" + "bid=" + bid + ", bname='" + bname + '\'' + ", bauthor='" + bauthor + '\'' + '}'; } }
写了怎么多的东西,我们需要测试一下,减少错误,现在我们只是在做练习,代码量比较少但是到了真正项目的时候代码量肯定比现在多许多,为了养成这种习惯,我一般写到Dao层的时候就进行测试一下。
BookTest.java
package com.booksystem.dao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; import static org.junit.Assert.*; //指定bean注入的配置文件 @ContextConfiguration("/applicationContext.xml") //使用标准的junit @RunWith(SpringJUnit4ClassRunner.class) @Transactional //事务管理 @Rollback(true) //是否回滚 public class BookDaoTest { @Autowired private BookDao bookDao; @Test public void getAllBooks() { System.out.println(bookDao.getAllBooks()); } }
BookService
package com.booksystem.service;
import com.booksystem.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List; public interface BookService { //查询全部图书信息 public List getAllBooks(); }
BookImple
package com.booksystem.imple;
import com.booksystem.dao.BookDao;
import com.booksystem.entity.Book;
import com.booksystem.service.BookService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class BookImple implements BookService { @Autowired public BookDao bookDao; public List getAllBooks() { return bookDao.getAllBooks(); } }
子模块webui层 我们发现在这一层中没有存放源代码的文件加,所以我们需要自己添加,选择file,project structure
BookController.java
package com.book.controller;
import com.booksystem.common.R;
import com.booksystem.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/book") public class BookController { @Autowired public BookService bookService; @GetMapping("/getAllBook") @ResponseBody public R getAllBook(){ return R.ok(bookService.getAllBooks()); } }
springmvc-servlet.xml
"1.0" encoding="UTF-8"?>
"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/beans http://www.springframework.org/schema/beans/spring-beans.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"> "com.booksystem" /> enable -matrix-variables="true" /> class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> "removeSemicolonContent" value="false" /> class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> "viewClass" value="org.springframework.web.servlet.view.JstlView" /> "prefix" value="/WEB-INF/views/" /> "suffix" value=".html" /> "contentType" value="text/html;charset=UTF-8" /> "order" value="1" /> "multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"> "/**" allowed-origins="*" allowed-methods="POST,GET, OPTIONS,DELETE,PUT" allowed-headers="Content-Type,ContentType,Access-Control-Allow-Headers, Authorization, X-Requested-With" allow-credentials="true"/>
web.xml
"1.0" encoding="UTF-8"?>
"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">
index.html Spring容器加载监听器 org.springframework.web.context.ContextLoaderListener 设置Spring加载时的配置文件位置,默认位置在WEB-INF/lib目录下 contextConfigLocation classpath*:applicationContext.xml springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath*:springmvc-servlet.xml 1 5242880 20971520 0 springmvc / characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true characterEncodingFilter /*
index.html
"en">
"UTF-8">
图书管理系统
图书管理系统
"myTab" cellpadding="0" cellspacing="0" border="1"> 编号 名称 作者
如果觉得本篇文章对您有用的话,可以麻烦您帮忙点亮那个点赞按钮吗?。
对于杨戬这个暖男来说:「真的真的非常有用」,您的支持将是我继续写文章前进的动力,我们下篇文章见。
「【原创】|二郎神杨戬」
❝二郎神杨戬,一个在互联网前端苟且偷生的划水程序员,专注于前端开发,善于技术分享。 如需转载,请联系作者或者保留原文链接,微信公众号搜索二郎神杨戬或者扫描下方的二维码更加方便。
一起来见证二郎神杨戬的成长吧!更多好文、技术分享尽在下方这个公众号。欢迎关注。
❞