IDEA集成SSM框架(SpringMVC+Spring+MyBatis)

本文已收录至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骨架

2.这一步输入组织名,模块名称

  • Groupld: 「组织名」
  • artifactId: 「模块名称」
  • version: 「版本号」

在Maven世界中,每个工程都有它唯一的 组织名、模块名、版本 ,这三个就是maven项目的坐标。一个maven工程可以打包成jar、war、pom等形式,但是它们都是拥有上述三个坐标的。我们在项目过程中导入的jar包就是通过上述坐标值唯一确定的。因此,我们在创建一个maven工程时会要求指定具体的 组织名、模块名、版本、打包方式。

子模块(common层)

1.创建子模块common层,然后点击next

子模块(Dao层)

由于之前的步骤都是一致,这里就不重复上述步骤了,直接完成最后一步

子模块(Service层)
子模块(Entity层)

实体层和mmon层一样都是同样的操作步骤,这里我们就不进行截图了,操作步骤都一样

子模块(WEB-UI层)

1.子模块web-ui层需要讲一下,因为我们这一层是视图层,所以我们需要选择maven骨架。

2.然后点击next,输入模块名之后finish就可以了,在这里我们需要选择自己本地的中央仓库。自此我们的多模块项目已经搭建好了

3.完整的项目结构如下:

4.在这里我们发现视图层多了webapp这个文件夹,以后的开发过程中我们的视图都是放在WEB_INF目录下的

  • common模块:通用模块,不依赖其它任何模块,主要有utils、可以在多个不同项目中使用
  • entitie模块:POJO、VO、DTO
  • dao模块:数据持久化,访问数据库,这里使用Mybatis
  • service模块:业务模块,处理业务逻辑
  • webui模块:B/S结构的表示层,主要用于在浏览器中显示数据,提供用户接口

模块依赖

父模块依赖

1.接下来我们去父模块的pom.xml文件进行统一的模块管理,为了方便管理版本和后续添加依赖不那么繁琐,我们在父模块pom.xml文件中只对依赖进行统一管理起来,而不进行依赖,当我们需要哪一个依赖的时候,我们只需要引入即可。

父模块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>com.ssm.booksystem</groupId>  <artifactId>BookSystem</artifactId>  <packaging>pom</packaging>  <version>1.0-SNAPSHOT</version>  <modules>  <module>Book-Common</module>  <module>Book-Dao</module>  <module>Book-Service</module>  <module>Book-WEBUI</module>  <module>Book-Entity</module>  </modules>  <properties>  <!--模块内部依赖-->  <Book-Common.version>1.0-SNAPSHOT</Book-Common.version>  <Book-Dao.version>1.0-SNAPSHOT</Book-Dao.version>  <Book-Service.version>1.0-SNAPSHOT</Book-Service.version>  <Book-Entity.version>1.0-SNAPSHOT</Book-Entity.version>  <Book-WEBUI.version>1.0-SNAPSHOT</Book-WEBUI.version>  <!--Common模块管理-->  <jackson.version>2.7.4</jackson.version>  <commons-lang3.version>3.4</commons-lang3.version>  <!--通用部分-->  <log4j.version>2.6.1</log4j.version>  <junit.version>4.12</junit.version>  <spring-test.version>4.3.18.RELEASE</spring-test.version>  <!--Spring-->  <spring.version>4.3.0.RELEASE</spring.version>  <aspectjweaver.version>1.8.9</aspectjweaver.version>  <cglib.version>3.2.4</cglib.version>  <!--mybatis-->  <mybatis-spring.version>1.3.0</mybatis-spring.version>  <mysql-connector-java.version>5.1.38</mysql-connector-java.version>  <mybatis.version>3.4.1</mybatis.version>  <c3p0.version>0.9.1.2</c3p0.version>  <!--spring mvc-->  <jstl.version>1.2</jstl.version>  <servlet-api.version>4.0.1</servlet-api.version>  <jsp-api.version>2.1</jsp-api.version>  <hibernate.version>5.2.2.Final</hibernate.version>  <commons-io.version>2.4</commons-io.version>  <commons-fileupload.version>1.3.1</commons-fileupload.version>  <freemarker.version>2.3.23</freemarker.version>   </properties>  <!--模块依赖进行统一管理-->  <dependencyManagement>  <dependencies>  <dependency>  <groupId>com.ssm.booksystem</groupId>  <artifactId>Book-Common</artifactId>  <version>${Book-Common.version}</version>  </dependency>  <dependency>  <groupId>com.ssm.booksystem</groupId>  <artifactId>Book-Dao</artifactId>  <version>${Book-Dao.version}</version>  </dependency>  <dependency>  <groupId>com.ssm.booksystem</groupId>  <artifactId>Book-Service</artifactId>  <version>${Book-Service.version}</version>  </dependency>  <dependency>  <groupId>com.ssm.booksystem</groupId>  <artifactId>Book-Entity</artifactId>  <version>${Book-Entity.version}</version>  </dependency>  <dependency>  <groupId>com.ssm.booksystem</groupId>  <artifactId>Book-WEBUI</artifactId>  <version>${Book-WEBUI.version}</version>  </dependency>  <!--common模块-->  <!--jackson-->  <dependency>  <groupId>com.fasterxml.jackson.core</groupId>  <artifactId>jackson-databind</artifactId>  <version>${jackson.version}</version>  </dependency>   <dependency>  <groupId>com.fasterxml.jackson.core</groupId>  <artifactId>jackson-core</artifactId>  <version>${jackson.version}</version>  </dependency>   <dependency>  <groupId>com.fasterxml.jackson.core</groupId>  <artifactId>jackson-annotations</artifactId>  <version>${jackson.version}</version>  </dependency>  <!--apache-commons-->  <dependency>  <groupId>org.apache.commons</groupId>  <artifactId>commons-lang3</artifactId>  <version>${commons-lang3.version}</version>  </dependency>   <!--log4j日志包 -->  <dependency>  <groupId>org.apache.logging.log4j</groupId>  <artifactId>log4j-core</artifactId>  <version>${log4j.version}</version>  </dependency>  <!-- JUnit单元测试工具 -->  <dependency>  <groupId>junit</groupId>  <artifactId>junit</artifactId>  <version>${junit.version}</version>  </dependency>  <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-test</artifactId>  <version>${spring-test.version}</version>  </dependency>  <!--Spring框架核心库 -->  <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-context</artifactId>  <version>${spring.version}</version>  </dependency>  <!-- aspectJ AOP 织入器 -->  <dependency>  <groupId>org.aspectj</groupId>  <artifactId>aspectjweaver</artifactId>  <version>${aspectjweaver.version}</version>  </dependency>  <dependency>  <groupId>cglib</groupId>  <artifactId>cglib</artifactId>  <version>${cglib.version}</version>  </dependency>  <!--mybatis-spring适配器 -->  <dependency>  <groupId>org.mybatis</groupId>  <artifactId>mybatis-spring</artifactId>  <version>${mybatis-spring.version}</version>  </dependency>  <!--Spring java数据库访问包,在本例中主要用于提供数据源 -->  <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-jdbc</artifactId>  <version>${spring.version}</version>  </dependency>  <!--mysql数据库驱动 -->  <dependency>  <groupId>mysql</groupId>  <artifactId>mysql-connector-java</artifactId>  <version>${mysql-connector-java.version}</version>  </dependency>  <!-- mybatis ORM框架 -->  <dependency>  <groupId>org.mybatis</groupId>  <artifactId>mybatis</artifactId>  <version>${mybatis.version}</version>  </dependency>  <!--c3p0 连接池 -->  <dependency>  <groupId>c3p0</groupId>  <artifactId>c3p0</artifactId>  <version>${c3p0.version}</version>  </dependency>   <!-- Spring MVC -->  <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-webmvc</artifactId>  <version>${spring.version}</version>  </dependency>  <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-context-support</artifactId>  <version>${spring.version}</version>  </dependency>  <!-- JSTL -->  <dependency>  <groupId>javax.servlet</groupId>  <artifactId>jstl</artifactId>  <version>${jstl.version}</version>  </dependency>  <!-- Servlet核心包 -->  <dependency>  <groupId>javax.servlet</groupId>  <artifactId>javax.servlet-api</artifactId>  <version>${servlet-api.version}</version>  </dependency>  <!--JSP -->  <dependency>  <groupId>javax.servlet.jsp</groupId>  <artifactId>jsp-api</artifactId>  <version>${jsp-api.version}</version>  </dependency>  <!--JSR303 -->  <dependency>  <groupId>org.hibernate</groupId>  <artifactId>hibernate-validator</artifactId>  <version>${hibernate.version}</version>  </dependency>  <!--文件上传 -->  <dependency>  <groupId>commons-io</groupId>  <artifactId>commons-io</artifactId>  <version>${commons-io.version}</version>  </dependency>  <dependency>  <groupId>commons-fileupload</groupId>  <artifactId>commons-fileupload</artifactId>  <version>${commons-fileupload.version}</version>  </dependency>  <!-- FreeMarker -->  <dependency>  <groupId>org.freemarker</groupId>  <artifactId>freemarker</artifactId>  <version>${freemarker.version}</version>  </dependency>  </dependencies>   </dependencyManagement>  </project> 
子模块Comon层模块依赖

common模块中的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">
    <parent>
 <artifactId>BookSystem</artifactId>  <groupId>com.ssm.booksystem</groupId>  <version>1.0-SNAPSHOT</version>  </parent>  <modelVersion>4.0.0</modelVersion>   <artifactId>Book-Common</artifactId>  <dependencies>  <!--jackson-->  <dependency>  <groupId>com.fasterxml.jackson.core</groupId>  <artifactId>jackson-databind</artifactId>  </dependency>   <dependency>  <groupId>com.fasterxml.jackson.core</groupId>  <artifactId>jackson-core</artifactId>  </dependency>   <dependency>  <groupId>com.fasterxml.jackson.core</groupId>  <artifactId>jackson-annotations</artifactId>  </dependency>  <!--apache-commons-->  <dependency>  <groupId>org.apache.commons</groupId>  <artifactId>commons-lang3</artifactId>  </dependency>  </dependencies>  </project> 
子模块Dao层模块依赖

Dao模块中的pom.xml文件,这一层中我们需要依赖common层和实体层,因为在这一层中我们涉及到和数据库打交道,一张表就对应一个POJO

<?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">
    <parent>
 <artifactId>BookSystem</artifactId>  <groupId>com.ssm.booksystem</groupId>  <version>1.0-SNAPSHOT</version>  </parent>  <modelVersion>4.0.0</modelVersion>   <artifactId>Book-Dao</artifactId>  <dependencies>  <dependency>  <groupId>com.ssm.booksystem</groupId>  <artifactId>Book-Common</artifactId>  </dependency>  <dependency>  <groupId>com.ssm.booksystem</groupId>  <artifactId>Book-Entity</artifactId>  </dependency>  <!--log4j日志包 -->  <dependency>  <groupId>org.apache.logging.log4j</groupId>  <artifactId>log4j-core</artifactId>  </dependency>  <!-- JUnit单元测试工具 -->  <dependency>  <groupId>junit</groupId>  <artifactId>junit</artifactId>  </dependency>   <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-test</artifactId>  </dependency>  <!--Spring框架核心库 -->  <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-context</artifactId>  </dependency>  <!-- aspectJ AOP 织入器 -->  <dependency>  <groupId>org.aspectj</groupId>  <artifactId>aspectjweaver</artifactId>  </dependency>  <dependency>  <groupId>cglib</groupId>  <artifactId>cglib</artifactId>  </dependency>   <!--mybatis-spring适配器 -->  <dependency>  <groupId>org.mybatis</groupId>  <artifactId>mybatis-spring</artifactId>  </dependency>  <!--Spring java数据库访问包,在本例中主要用于提供数据源 -->  <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-jdbc</artifactId>  </dependency>  <!--mysql数据库驱动 -->  <dependency>  <groupId>mysql</groupId>  <artifactId>mysql-connector-java</artifactId>  </dependency>  <!-- mybatis ORM框架 -->  <dependency>  <groupId>org.mybatis</groupId>  <artifactId>mybatis</artifactId>  </dependency>  <!--c3p0 连接池 -->  <dependency>  <groupId>c3p0</groupId>  <artifactId>c3p0</artifactId>  </dependency>  </dependencies>  </project> 
子模块Service层模块依赖

Service模块中的pom.xml文件,这一层中我们需要依赖dao层

<?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">
    <parent>
 <artifactId>BookSystem</artifactId>  <groupId>com.ssm.booksystem</groupId>  <version>1.0-SNAPSHOT</version>  </parent>  <modelVersion>4.0.0</modelVersion>   <artifactId>Book-Service</artifactId>  <dependencies>  <dependency>  <groupId>com.ssm.booksystem</groupId>  <artifactId>Book-Dao</artifactId>  </dependency>  </dependencies>  </project> 
子模块Entity层模块依赖

实体层暂时不需要依赖任何的模块,Entity模块中的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">
    <parent>
 <artifactId>BookSystem</artifactId>  <groupId>com.ssm.booksystem</groupId>  <version>1.0-SNAPSHOT</version>  </parent>  <modelVersion>4.0.0</modelVersion>   <artifactId>Book-Entity</artifactId>   </project> 
子模块WEB-UI层模块依赖

Service模块中的pom.xml文件,这一层中我们需要依赖service层

<?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">
    <parent>
 <artifactId>BookSystem</artifactId>  <groupId>com.ssm.booksystem</groupId>  <version>1.0-SNAPSHOT</version>  </parent>  <modelVersion>4.0.0</modelVersion>   <artifactId>Book-WEBUI</artifactId>  <packaging>war</packaging>   <name>Book-WEBUI 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>  <dependency>  <groupId>junit</groupId>  <artifactId>junit</artifactId>  <version>4.11</version>  <scope>test</scope>  </dependency>  <dependency>  <groupId>com.ssm.booksystem</groupId>  <artifactId>Book-Service</artifactId>  </dependency>  <!-- Spring MVC -->  <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-webmvc</artifactId>  </dependency>   <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-context-support</artifactId>  </dependency>  <!-- Servlet核心包 -->  <dependency>  <groupId>javax.servlet</groupId>  <artifactId>javax.servlet-api</artifactId>  </dependency>  <!--文件上传 -->  <dependency>  <groupId>commons-io</groupId>  <artifactId>commons-io</artifactId>  </dependency>   <dependency>  <groupId>commons-fileupload</groupId>  <artifactId>commons-fileupload</artifactId>  </dependency>  <dependency>  <groupId>javax.servlet.jsp</groupId>  <artifactId>jsp-api</artifactId>  </dependency>  <dependency>  <groupId>javax.servlet</groupId>  <artifactId>jstl</artifactId>  </dependency>  </dependencies>   <build>  <finalName>Book-WEBUI</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.1.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.8.0</version>  </plugin>  <plugin>  <artifactId>maven-surefire-plugin</artifactId>  <version>2.22.1</version>  </plugin>  <plugin>  <artifactId>maven-war-plugin</artifactId>  <version>3.2.2</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> 
最终的依赖效果

数据库连接

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<String, Object> {  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<String, Object> 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;  } } 

子模块dao层

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">
<!--命名空间应该是对应接口的包名+接口名 -->
<mapper namespace="com.booksystem.dao.BookDao">
    <!--查询全部图书信息-->
 <select id="getAllBooks" resultMap="bookMap">  select bid,bname,bauthor from book  </select>  <!--配置数据表和实体的映射关系-->  <resultMap id="bookMap" type="book">  <id column="bid" property="bid"/>  <result property="bname" column="bname"/>  <result column="bauthor" property="bauthor"/>  </resultMap> </mapper> 

applicationContext.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: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">   <!--1 引入属性文件,在配置中占位使用 -->  <context:property-placeholder location="classpath*:db.properties" />   <!--2 配置C3P0数据源 -->  <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  destroy-method="close">  <!--驱动类名 -->  <property name="driverClass" value="${mysql.driver}" />  <!-- url -->  <property name="jdbcUrl" value="${mysql.url}" />  <!-- 用户名 -->  <property name="user" value="${mysql.uid}" />  <!-- 密码 -->  <property name="password" value="${mysql.password}" />  <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数 -->  <property name="acquireIncrement" value="${mysql.acquireIncrement}"></property>  <!-- 初始连接池大小 -->  <property name="initialPoolSize" value="${mysql.initialPoolSize}"></property>  <!-- 连接池中连接最小个数 -->  <property name="minPoolSize" value="${mysql.minPoolSize}"></property>  <!-- 连接池中连接最大个数 -->  <property name="maxPoolSize" value="${mysql.maxPoolSize}"></property>  </bean>   <!--3 会话工厂bean sqlSessionFactoryBean -->  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  <!-- mybatis核心配置文件路径 -->  <property name="configLocation" value="classpath:mybatis.xml"></property>  <!-- 数据源 -->  <property name="dataSource" ref="datasource"/>  <!-- sql映射文件路径[mapper路径] -->  <property name="mapperLocations" value="classpath*:mapper/*Mapper.xml"></property>  </bean>   <!--4 自动扫描对象关系映射 -->  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  <!--指定会话工厂,如果当前上下文中只定义了一个则该属性可省去 -->  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>  <!-- 指定要自动扫描接口的基础包,实现接口 -->  <property name="basePackage" value="com.booksystem.dao"/>  </bean>   <!--5 声明式事务管理 -->  <!--定义事物管理器,由spring管理事务 -->  <bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  <property name="dataSource" ref="datasource"></property>  </bean>  <!--支持注解驱动的事务管理,指定事务管理器 -->  <tx:annotation-driven transaction-manager="transactionManager" />   <!--6 容器自动扫描IOC组件 -->  <context:component-scan base-package="com.booksystem.dao"/>   <!--7 aspectj支持自动代理实现AOP功能 -->  <aop:aspectj-autoproxy/>  </beans> 

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

<?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>
    <properties resource="db.properties"></properties>
    <settings>
 <!--指定mybatis使用日志组件 -->  <setting name="logImpl" value="STDOUT_LOGGING"/>  </settings>  <!--设置别名-->  <typeAliases>  <package name="com.booksystem.entity"/>  </typeAliases> </configuration> 

BookDao,java

package com.booksystem.dao;

import com.booksystem.entity.Book;

import java.util.List;
 public interface BookDao {  //查询全部图书信息  public List<Book> 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());  } } 

子模块service层

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<Book> 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<Book> 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

<?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"
       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">   <!-- 自动扫描包,实现支持注解的IOC -->  <context:component-scan base-package="com.booksystem" />   <!-- Spring MVC不处理静态资源 -->  <mvc:default-servlet-handler />  <!-- 支持mvc注解驱动 -->  <mvc:annotation-driven enable-matrix-variables="true" />  <!-- 配置映射媒体类型的策略 -->  <bean  class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">  <property name="removeSemicolonContent" value="false" />  </bean>   <!-- 内部视图解析器,JSP与JSTL模板 -->  <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver"  id="internalResourceViewResolver">  <!--指定视图渲染类 -->  <property name="viewClass"  value="org.springframework.web.servlet.view.JstlView" />  <!--自动添加到路径中的前缀 -->  <property name="prefix" value="/WEB-INF/views/" />  <!--自动添加到路径中的后缀 -->  <property name="suffix" value=".html" />  <!--设置所有视图的内容类型,如果视图本身设置内容类型视图类可以忽略 -->  <property name="contentType" value="text/html;charset=UTF-8" />  <!-- 优先级,越小越前 -->  <property name="order" value="1" />  </bean>   <!--文件上传解析器 -->  <!--Spring MVC默认不能识别multipart格式的文件内容 -->  <bean id="multipartResolver"  class="org.springframework.web.multipart.support.StandardServletMultipartResolver">  </bean>  <mvc:cors>  <mvc:mapping path="/**"  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"/>  </mvc:cors> </beans> 

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">
  <welcome-file-list>  <!--欢迎页-->  <welcome-file>index.html</welcome-file>  </welcome-file-list>   <listener>  <description>Spring容器加载监听器</description>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>  <description>设置Spring加载时的配置文件位置,默认位置在WEB-INF/lib目录下</description>  <param-name>contextConfigLocation</param-name>  <param-value>classpath*:applicationContext.xml</param-value>  </context-param>  <!--Spring MVC 前置Servlet,中心控制器 -->  <servlet>  <servlet-name>springmvc</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <init-param>  <param-name>contextConfigLocation</param-name>  <!--Spring MVC配置文件路径 -->  <param-value>classpath*:springmvc-servlet.xml</param-value>  </init-param>  <!-- 启动动优先级,越小越早加载 -->  <load-on-startup>1</load-on-startup>  <!--Servlet3.0以上文件上传配置 -->  <multipart-config>  <!--上传文件的最大限制5MB -->  <max-file-size>5242880</max-file-size>  <!--请求的最大限制20MB -->  <max-request-size>20971520</max-request-size>  <!--当文件的大小超过临界值时将写入磁盘 -->  <file-size-threshold>0</file-size-threshold>  </multipart-config>  </servlet>  <!-- 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> 

3.配置tomcat

4.项目部署

5.启动项目

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图书管理系统</title>
 <style>  h1{  text-align: center;  }  #myTab{  width: 800px;  margin: 0 auto;  }  </style> </head> <body> <h1>图书管理系统</h1> <table id="myTab" cellpadding="0" cellspacing="0" border="1">  <tr>  <td>编号</td>  <td>名称</td>  <td>作者</td>  </tr> </table> <script src="js/jquery-1.11.3.min.js"></script> <script>  $.ajax({  url:'/book/getAllBook',  type:'get',  dataType:'json',  }).done(function (data) {  if(data.code===1){  $("#myTab tr:not(:eq(0))").remove();  for (var i=0;i<data.data.length;i++){  var tr="<tr>";  tr+="<td>"+(i+1)+"</td>";  tr+="<td>"+data.data[i].bname+"</td>";  tr+="<td>"+data.data[i].bauthor+"</td>";  tr+="</tr>"  $("#myTab").append(tr);  }  }  }) </script> </body> </html> 

结尾

如果觉得本篇文章对您有用的话,可以麻烦您帮忙点亮那个点赞按钮吗?。

对于杨戬这个暖男来说:「真的真的非常有用」,您的支持将是我继续写文章前进的动力,我们下篇文章见。

「【原创】|二郎神杨戬」

二郎神杨戬,一个在互联网前端苟且偷生的划水程序员,专注于前端开发,善于技术分享。 如需转载,请联系作者或者保留原文链接,微信公众号搜索二郎神杨戬或者扫描下方的二维码更加方便。

一起来见证二郎神杨戬的成长吧!更多好文、技术分享尽在下方这个公众号。欢迎关注。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值