Day3:SSM

本文使用IDEA,利用Maven管理项目,整合SSM框架搭建论文管理系统,实现简单的增、删、改、查功能。步骤详细,讲解丰富,适合新手入门。

开发工具

  • IntelliJ IDEA Ultimate 2017.2.2
  • Apache-tomcat-9.0.5
  • JDK 1.8.0_121
  • MySQL 5.7
  • Maven 3.3.9

工程结构

最终完成后的工程结构: 
Day3:SSM

下面开始详细的讲解项目的开发过程。


IDEA+Maven搭建项目骨架

1. 新建Maven项目: 
点击File -> New -> Project -> Maven -> 勾选 Create from archetype -> 选择 maven-archetype-webapp 
Day3:SSM
在弹出的new project 选项卡中填写GroupId和Artifactid,其中GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构,ArtifactID是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。对于入门练习,这两项可以随意填写。

Day3:SSM
之后点击next 选择Maven版本(其中IDEA 专业版自带Maven,也可以选择自己下载的maven)。之后填写项目名称和项目地址,完成后点击Finish,完成项目骨架的创建。 
Day3:SSM

.

2. 在新建的项目中添加所需要的文件/文件夹

创建之后的项目如图所示,我们需要在这之上新建一些目录。 
Day3:SSM

在项目的根目录下新建target文件夹,系统自动将其设置为“Excluded” 
Day3:SSM 
在src/main目录下新建Directory:“java”,并将其设置为“Source Root”(即:此项目默认的代码文件源目录) 
Day3:SSM 
在刚才新建的java文件下新建“com”包,再在com包下新建四个包,分别命名为:pojo,service,dao,controller。(如果出现下图中所示的包名重叠的情况,可以点击图中所示的图标,将“Hide empty middle package取消掉”) 
Day3:SSM 
上面新建的四个包:pojo,service,dao,controller,其所存放的分别是:

  • pojo: 存放自定义的java类。如:paper类,user类,book类等,每个类的属性设为private,并提供public属性的getter/setter方法让外界访问
  • service:定义接口,包含系统所提供的功能。(之后还会在service包下再新建impl包)。
  • dao:定义接口,包含与数据库进行交互的功能。
  • controller:控制器,负责接收页面请求,转发和处理。

在resource包下新建Directory:“mapper”(用于存放xxxMapper.xml文件)和“spring”(用于存放spring-xxx.xml配置文件),新建文件:“jdbc.properties”(mysql数据库配置文件),”log4j.properties”(日志输出配置文件),”mybatis-config.xml”(mybatis框架配置文件)。 
Day3:SSM 
在web-inf目录下新建“jsp”包,存放xxx.jsp显示界面。 
补充之后的项目目录如下图所示,至此项目骨架搭建完毕,开始写代码实现增删改查功能。 
Day3:SSM

3. 自定义Paper类

在这之前要对项目的pom.xml文件进行配置,在此文件中配置项目所需要的jar包。

pom.xml


 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  3. <modelVersion>4.0.0</modelVersion>

  4. <groupId>com</groupId>

  5. <artifactId>first</artifactId>

  6. <packaging>war</packaging>

  7. <version>1.0-SNAPSHOT</version>

  8. <name>first Maven Webapp</name>

  9. <url>http://maven.apache.org</url>

  10. <properties>

  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  12. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  13. <spring.version>5.0.3.RELEASE</spring.version>

  14. <mybatis.version>3.4.4</mybatis.version>

  15. </properties>

  16. <dependencies>

  17. <!-- 单元测试 -->

  18. <dependency>

  19. <groupId>junit</groupId>

  20. <artifactId>junit</artifactId>

  21. <version>3.8.1</version>

  22. <scope>test</scope>

  23. </dependency>

  24. <!-- 第一部分:Spring 配置-->

  25. <!-- Spring core -->

  26. <dependency>

  27. <groupId>org.springframework</groupId>

  28. <artifactId>spring-core</artifactId>

  29. <version>${spring.version}</version>

  30. </dependency>

  31. <dependency>

  32. <groupId>org.springframework</groupId>

  33. <artifactId>spring-beans</artifactId>

  34. <version>${spring.version}</version>

  35. </dependency>

  36. <dependency>

  37. <groupId>org.springframework</groupId>

  38. <artifactId>spring-context</artifactId>

  39. <version>${spring.version}</version>

  40. </dependency>

  41. <dependency>

  42. <groupId>org.springframework</groupId>

  43. <artifactId>spring-context-support</artifactId>

  44. <version>${spring.version}</version>

  45. </dependency>

  46. <!-- Spring DAO -->

  47. <dependency>

  48. <groupId>org.springframework</groupId>

  49. <artifactId>spring-jdbc</artifactId>

  50. <version>${spring.version}</version>

  51. </dependency>

  52. <dependency>

  53. <groupId>org.springframework</groupId>

  54. <artifactId>spring-tx</artifactId>

  55. <version>${spring.version}</version>

  56. </dependency>

  57. <!-- Spring mvc -->

  58. <dependency>

  59. <groupId>org.springframework</groupId>

  60. <artifactId>spring-web</artifactId>

  61. <version>${spring.version}</version>

  62. </dependency>

  63. <dependency>

  64. <groupId>org.springframework</groupId>

  65. <artifactId>spring-webmvc</artifactId>

  66. <version>${spring.version}</version>

  67. </dependency>

  68. <dependency>

  69. <groupId>org.springframework</groupId>

  70. <artifactId>spring-test</artifactId>

  71. <version>${spring.version}</version>

  72. </dependency>

  73. <!-- 第二部分:Servlet web -->

  74. <dependency>

  75. <groupId>javax.servlet</groupId>

  76. <artifactId>javax.servlet-api</artifactId>

  77. <version>3.0.1</version>

  78. <scope>provided</scope>

  79. </dependency>

  80. <dependency>

  81. <groupId>javax.servlet.jsp</groupId>

  82. <artifactId>jsp-api</artifactId>

  83. <version>2.2</version>

  84. <scope>provided</scope>

  85. </dependency>

  86. <dependency>

  87. <groupId>javax.servlet</groupId>

  88. <artifactId>jstl</artifactId>

  89. <version>1.2</version>

  90. </dependency>

  91. <dependency>

  92. <groupId>taglibs</groupId>

  93. <artifactId>standard</artifactId>

  94. <version>1.1.2</version>

  95. </dependency>

  96. <dependency>

  97. <groupId>com.fasterxml.jackson.core</groupId>

  98. <artifactId>jackson-databind</artifactId>

  99. <version>2.9.4</version>

  100. </dependency>

  101. <!-- 第三部分:数据库和mybatis -->

  102. <!-- 数据库 -->

  103. <dependency>

  104. <groupId>mysql</groupId>

  105. <artifactId>mysql-connector-java</artifactId>

  106. <version>5.1.38</version>

  107. </dependency>

  108. <!-- 数据库连接池 -->

  109. <dependency>

  110. <groupId>com.mchange</groupId>

  111. <artifactId>c3p0</artifactId>

  112. <version>0.9.5.2</version>

  113. </dependency>

  114. <!-- MyBatis -->

  115. <dependency>

  116. <groupId>org.mybatis</groupId>

  117. <artifactId>mybatis</artifactId>

  118. <version>${mybatis.version}</version>

  119. </dependency>

  120. <!-- mybatis-spring整合包 -->

  121. <dependency>

  122. <groupId>org.mybatis</groupId>

  123. <artifactId>mybatis-spring</artifactId>

  124. <version>1.3.1</version>

  125. </dependency>

  126. <!-- 第四部分:日志 -->

  127. <!-- 实现slf4j接口并整合 -->

  128. <dependency>

  129. <groupId>ch.qos.logback</groupId>

  130. <artifactId>logback-classic</artifactId>

  131. <version>1.1.1</version>

  132. </dependency>

  133. </dependencies>

  134. <build>

  135. <finalName>first</finalName>

  136. <plugins>

  137. <plugin>

  138. <groupId>org.apache.maven.plugins</groupId>

  139. <artifactId>maven-compiler-plugin</artifactId>

  140. <configuration>

  141. <source>1.8</source>

  142. <target>1.8</target>

  143. </configuration>

  144. </plugin>

  145. </plugins>

  146. </build>

  147. </project>

本项目所用的数据库为MySQL 5.7 建表语句如下:


 
  1. SET FOREIGN_KEY_CHECKS=0;

  2.  
  3. -- ----------------------------

  4. -- Table structure for `paper`

  5. -- ----------------------------

  6. DROP TABLE IF EXISTS `paper`;

  7. CREATE TABLE `paper` (

  8. `paper_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'paperID',

  9. `name` varchar(100) NOT NULL COMMENT 'paper名称',

  10. `number` int(11) NOT NULL COMMENT 'paper数量',

  11. `detail` varchar(200) NOT NULL COMMENT 'paper描述',

  12. PRIMARY KEY (`paper_id`)

  13. ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COMMENT='paper表';

  14.  
  15. -- ----------------------------

  16. -- Records of paper

  17. -- ----------------------------

  18. INSERT INTO `paper` VALUES ('1', '机器学习', '2', 'mlmlmlml');

  19. INSERT INTO `paper` VALUES ('2', '深度学习', '3', 'dldldl');

  20. INSERT INTO `paper` VALUES ('3', '大数据', '4', 'bdbdbd');

下面开始写java代码:

  • 在pojo包中新建Paper.java

 
  1. package com.pojo;

  2.  
  3. public class Paper {

  4. private long paperId;

  5. private String paperName;

  6. private int paperNum;

  7. private String paperDetail;

  8.  
  9. public long getPaperId() {

  10. return paperId;

  11. }

  12.  
  13. public void setPaperId(long paperId) {

  14. this.paperId = paperId;

  15. }

  16.  
  17. public String getPaperName() {

  18. return paperName;

  19. }

  20.  
  21. public void setPaperName(String paperName) {

  22. this.paperName = paperName;

  23. }

  24.  
  25. public int getPaperNum() {

  26. return paperNum;

  27. }

  28.  
  29. public void setPaperNum(int paperNum) {

  30. this.paperNum = paperNum;

  31. }

  32.  
  33. public String getPaperDetail() {

  34. return paperDetail;

  35. }

  36.  
  37. public void setPaperDetail(String paperDetail) {

  38. this.paperDetail = paperDetail;

  39. }

  40.  
  41. }

其中getter/setter方法可以在定义完私有属性之后使用IDEA提供的快捷键:ALT+Insert 来实现快速添加 
Day3:SSM

  • 在service包中新建接口 PaperService:

 
  1. package com.service;

  2.  
  3. import com.pojo.Paper;

  4.  
  5. import java.util.List;

  6.  
  7. public interface PaperService {

  8. int addPaper(Paper paper);

  9.  
  10. int deletePaperById(long id);

  11.  
  12. int updatePaper(Paper paper);

  13.  
  14. Paper queryById(long id);

  15.  
  16. List<Paper> queryAllPaper();

  17. }

  • 在service包下新建impl包,并在此包中新建PaperServiceImpl.java

 
  1. package com.service.impl;

  2.  
  3. import com.pojo.Paper;

  4. import com.dao.PaperDao;

  5. import com.service.PaperService;

  6.  
  7. import org.springframework.beans.factory.annotation.Autowired;

  8. import org.springframework.stereotype.Service;

  9.  
  10. import java.util.List;

  11.  
  12. @Service

  13. public class PaperServiceImpl implements PaperService {

  14. @Autowired

  15. private PaperDao paperDao;

  16.  
  17. @Override

  18. public int addPaper(Paper paper) {

  19. return paperDao.addPaper(paper);

  20. }

  21.  
  22. @Override

  23. public int deletePaperById(long id) {

  24. return paperDao.deletePaperById(id);

  25. }

  26.  
  27. @Override

  28. public int updatePaper(Paper paper) {

  29. return paperDao.updatePaper(paper);

  30. }

  31.  
  32. @Override

  33. public Paper queryById(long id) {

  34. return paperDao.queryById(id);

  35. }

  36.  
  37. @Override

  38. public List<Paper> queryAllPaper() {

  39. return paperDao.queryAllPaper();

  40. }

  41.  
  42. }

  • 在dao包中新建接口 PaperDao.java

 
  1. package com.dao;

  2.  
  3. import com.pojo.Paper;

  4.  
  5. import java.util.List;

  6.  
  7. public interface PaperDao {

  8. int addPaper(Paper paper);

  9.  
  10. int deletePaperById(long id);

  11.  
  12. int updatePaper(Paper paper);

  13.  
  14. Paper queryById(long id);

  15.  
  16. List<Paper> queryAllPaper();

  17.  
  18.  
  19. }

  • 在controller包中新建 PaperController.java

 
  1. package com.controller;

  2.  
  3. import com.pojo.Paper;

  4. import com.service.PaperService;

  5.  
  6. import java.util.List;

  7.  
  8. import org.springframework.beans.factory.annotation.Autowired;

  9. import org.springframework.stereotype.Controller;

  10. import org.springframework.ui.Model;

  11. import org.springframework.web.bind.annotation.PathVariable;

  12. import org.springframework.web.bind.annotation.RequestMapping;

  13.  
  14. @Controller

  15. @RequestMapping("/paper")

  16. public class PaperController {

  17. @Autowired

  18. private PaperService paperService;

  19.  
  20. @RequestMapping("/allPaper")

  21. public String list(Model model) {

  22. List<Paper> list = paperService.queryAllPaper();

  23. model.addAttribute("list", list);

  24. return "allPaper";

  25. }

  26.  
  27. @RequestMapping("toAddPaper")

  28. public String toAddPaper() {

  29. return "addPaper";

  30. }

  31.  
  32. @RequestMapping("/addPaper")

  33. public String addPaper(Paper paper) {

  34. paperService.addPaper(paper);

  35. return "redirect:/paper/allPaper";

  36. }

  37.  
  38. @RequestMapping("/del/{paperId}")

  39. public String deletePaper(@PathVariable("paperId") Long id) {

  40. paperService.deletePaperById(id);

  41. return "redirect:/paper/allPaper";

  42. }

  43.  
  44. @RequestMapping("toUpdatePaper")

  45. public String toUpdatePaper(Model model, Long id) {

  46. model.addAttribute("paper", paperService.queryById(id));

  47. return "updatePaper";

  48. }

  49.  
  50. @RequestMapping("/updatePaper")

  51. public String updatePaper(Model model, Paper paper) {

  52. paperService.updatePaper(paper);

  53. paper = paperService.queryById(paper.getPaperId());

  54. model.addAttribute("paper", paper);

  55. return "redirect:/paper/allPaper";

  56. }

  57. }

  • 在resource/mapper包下新建 PaperMapper.xml(mybatis框架mapper代理开发配置文件)

 
  1. <?xml version="1.0" encoding="UTF-8" ?>

  2. <!DOCTYPE mapper

  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

  5. <mapper namespace="com.dao.PaperDao">

  6. <resultMap type="Paper" id="paperResultMap" >

  7. <id property="paperId" column="paper_id"/>

  8. <result property="paperName" column="name"/>

  9. <result property="paperNum" column="number"/>

  10. <result property="paperDetail" column="detail"/>

  11. </resultMap>

  12. <insert id="addPaper" parameterType="Paper">

  13. INSERT INTO paper(paper_id,name,number,detail) VALUE (#{paperId},#{paperName}, #{paperNum}, #{paperDetail})

  14. </insert>

  15.  
  16. <delete id="deletePaperById" parameterType="long">

  17. DELETE FROM paper WHERE paper_id=#{paperID}

  18. </delete>

  19.  
  20. <update id="updatePaper" parameterType="Paper">

  21. UPDATE paper

  22. SET NAME = #{paperName},NUMBER = #{paperNum},detail = #{paperDetail}

  23. WHERE paper_id = #{paperId}

  24. </update>

  25.  
  26. <select id="queryById" resultType="Paper" parameterType="long">

  27. SELECT paper_id,name,number,detail

  28. FROM paper

  29. WHERE paper_id=#{paperId}

  30. </select>

  31. <select id="queryAllPaper" resultMap="paperResultMap">

  32. SELECT paper_id,name,number,detail

  33. FROM paper

  34. </select>

  35.  
  36. </mapper>

  • 在resource/spring包下分别新建配置文件:spring-dao.xml , spring-mvc.xml , spring-service.xml

Day3:SSM

spring-dao.xml:(spring-mybatis整合配置文件)


 
  1. <?xml version="1.0" encoding="UTF-8" ?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xsi:schemaLocation="http://www.springframework.org/schema/beans

  6. http://www.springframework.org/schema/beans/spring-beans.xsd

  7. http://www.springframework.org/schema/context

  8. http://www.springframework.org/schema/context/spring-context.xsd">

  9. <!-- 配置整合mybatis过程 -->

  10. <!-- 1.配置数据库相关参数properties的属性:${url} -->

  11. <context:property-placeholder location="classpath:jdbc.properties"/>

  12.  
  13. <!-- 2.数据库连接池 -->

  14. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

  15. <!-- 配置连接池属性 -->

  16. <property name="driverClass" value="${jdbc.driver}"/>

  17. <property name="jdbcUrl" value="${jdbc.url}"/>

  18. <property name="user" value="${jdbc.username}"/>

  19. <property name="password" value="${jdbc.password}"/>

  20.  
  21. <!-- c3p0连接池的私有属性 -->

  22. <property name="maxPoolSize" value="30"/>

  23. <property name="minPoolSize" value="10"/>

  24. <!-- 关闭连接后不自动commit -->

  25. <property name="autoCommitOnClose" value="false"/>

  26. <!-- 获取连接超时时间 -->

  27. <property name="checkoutTimeout" value="10000"/>

  28. <!-- 当获取连接失败重试次数 -->

  29. <property name="acquireRetryAttempts" value="2"/>

  30. </bean>

  31.  
  32. <!-- 3.配置SqlSessionFactory对象 -->

  33. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

  34. <!-- 注入数据库连接池 -->

  35. <property name="dataSource" ref="dataSource"/>

  36. <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->

  37. <property name="configLocation" value="classpath:mybatis-config.xml"/>

  38. <!-- 扫描pojo包 使用别名 -->

  39. <property name="typeAliasesPackage" value="com.pojo"/>

  40. <!-- 扫描sql配置文件:mapper需要的xml文件 -->

  41. <property name="mapperLocations" value="classpath:mapper/*.xml"/>

  42. </bean>

  43.  
  44. <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->

  45. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

  46. <!-- 注入sqlSessionFactory -->

  47. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

  48. <!-- 给出需要扫描Dao接口包 -->

  49. <property name="basePackage" value="com.dao"/>

  50. </bean>

  51. </beans>

spring-mvc.xml:(spring mvc配置文件)


 
  1. <?xml version="1.0" encoding="UTF-8" ?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xmlns:mvc="http://www.springframework.org/schema/mvc"

  6. xsi:schemaLocation="http://www.springframework.org/schema/beans

  7. http://www.springframework.org/schema/beans/spring-beans.xsd

  8. http://www.springframework.org/schema/context

  9. http://www.springframework.org/schema/context/spring-context.xsd

  10. http://www.springframework.org/schema/mvc

  11. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

  12. <!-- 配置SpringMVC -->

  13. <!-- 1.开启SpringMVC注解模式 -->

  14. <!-- 简化配置:

  15. (1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter

  16. (2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持

  17. -->

  18. <mvc:annotation-driven />

  19.  
  20. <!-- 2.静态资源默认servlet配置

  21. (1)加入对静态资源的处理:js,gif,png

  22. (2)允许使用"/"做整体映射

  23. -->

  24. <mvc:default-servlet-handler/>

  25.  
  26. <!-- 3.配置jsp 显示ViewResolver -->

  27. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

  28. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />

  29. <property name="prefix" value="/WEB-INF/jsp/" />

  30. <property name="suffix" value=".jsp" />

  31. </bean>

  32.  
  33. <!-- 4.扫描web相关的bean -->

  34. <context:component-scan base-package="com.controller" />

  35. </beans>

spring-service.xml


 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xmlns:tx="http://www.springframework.org/schema/tx"

  6. xsi:schemaLocation="http://www.springframework.org/schema/beans

  7. http://www.springframework.org/schema/beans/spring-beans.xsd

  8. http://www.springframework.org/schema/context

  9. http://www.springframework.org/schema/context/spring-context.xsd

  10. http://www.springframework.org/schema/tx

  11. http://www.springframework.org/schema/tx/spring-tx.xsd">

  12. <!-- 扫描service包下所有使用注解的类型 -->

  13. <context:component-scan base-package="com.service" />

  14.  
  15. <!-- 配置事务管理器 -->

  16. <bean id="transactionManager"

  17. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

  18. <!-- 注入数据库连接池 -->

  19. <property name="dataSource" ref="dataSource" />

  20. </bean>

  21. <!-- 配置基于注解的声明式事务 -->

  22. <tx:annotation-driven transaction-manager="transactionManager" />

  23. </beans>

  • 在jdbc.properties文件中配置mysql数据库(这里的数据库名、表名,用户名,密码一定要记得修改!!!)。
  • 
     
    1. jdbc.driver=com.mysql.jdbc.Driver

    2. jdbc.url=jdbc:mysql://127.0.0.1:3306/papersystem?useUnicode=true&characterEncoding=utf8

    3. jdbc.username=root

    4. jdbc.password=12345678

     

  • 在log4j.properties文件中配置日志设定

 
  1. log4j.rootLogger=ERROR, stdout

  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender

  3. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

  4. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

  • 在mybatis-config.xml文件中配置mybtis框架

 
  1. <?xml version="1.0" encoding="UTF-8" ?>

  2. <!DOCTYPE configuration

  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">

  5. <configuration>

  6. <!-- 配置全局属性 -->

  7. <settings>

  8. <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->

  9. <setting name="useGeneratedKeys" value="true" />

  10.  
  11. <!-- 使用列别名替换列名 默认:true -->

  12. <setting name="useColumnLabel" value="true" />

  13.  
  14. <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->

  15. <setting name="mapUnderscoreToCamelCase" value="true" />

  16. </settings>

  17. </configuration>

  • 配置web.xml文件

 
  1. <!DOCTYPE web-app PUBLIC

  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >

  4.  
  5. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  6. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

  7. http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

  8. version="3.1" metadata-complete="true">

  9. <display-name>Archetype Created Web Application</display-name>

  10. <servlet>

  11. <servlet-name>dispatcher</servlet-name>

  12. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  13. <!-- 配置springMVC需要加载的配置文件

  14. spring-dao.xml,spring-service.xml,spring-mvc.xml

  15. Mybatis - > spring -> springmvc

  16. -->

  17. <init-param>

  18. <param-name>contextConfigLocation</param-name>

  19. <param-value>classpath:spring/spring-*.xml</param-value>

  20. </init-param>

  21. </servlet>

  22. <servlet-mapping>

  23. <servlet-name>dispatcher</servlet-name>

  24. <!-- 默认匹配所有的请求 -->

  25. <url-pattern>/</url-pattern>

  26. </servlet-mapping>

  27. <filter>

  28. <filter-name>encodingFilter</filter-name>

  29. <filter-class>

  30. org.springframework.web.filter.CharacterEncodingFilter

  31. </filter-class>

  32. <init-param>

  33. <param-name>encoding</param-name>

  34. <param-value>utf-8</param-value>

  35. </init-param>

  36. </filter>

  37.  
  38. <filter-mapping>

  39. <filter-name>encodingFilter</filter-name>

  40. <url-pattern>/*</url-pattern>

  41. </filter-mapping>

  42. </web-app>

  • 修改index.jsp文件

index.jsp


 
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"

  2. pageEncoding="UTF-8" %>

  3. <%

  4. pageContext.setAttribute("path", request.getContextPath());

  5. %>

  6. <!DOCTYPE HTML>

  7. <html>

  8. <head>

  9. <title>首页</title>

  10. <style type="text/css">

  11. a {

  12. text-decoration: none;

  13. color: black;

  14. font-size: 18px;

  15. }

  16.  
  17. h3 {

  18. width: 180px;

  19. height: 38px;

  20. margin: 100px auto;

  21. text-align: center;

  22. line-height: 38px;

  23. background: deepskyblue;

  24. border-radius: 4px;

  25. }

  26. </style>

  27. </head>

  28. <body>

  29. <div class="container">

  30. <div class="row clearfix">

  31. <div class="col-md-12 column">

  32. <div class="page-header">

  33. <h1>

  34. 基于SSM框架的管理系统:简单实现增、删、改、查。

  35. </h1>

  36. </div>

  37. </div>

  38. </div>

  39. </div>

  40. <br><br>

  41. <h3>

  42. <a href="${path }/paper/allPaper">点击进入管理页面</a>

  43. </h3>

  44. </body>

  45. </html>

  • 在WEB-INF/jsp包下分别新建addPaper,jsp , appPaper.jsp , updatePaper.jsp

allPaper.jsp:所有论文显示界面


 
  1. <%--

  2. Created by IntelliJ IDEA.

  3. User: lenovo

  4. Date: 2018/4/6

  5. Time: 16:57

  6. To change this template use File | Settings | File Templates.

  7. --%>

  8. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

  9. <%@ page contentType="text/html;charset=UTF-8" language="java" %>

  10. <% String appPath = request.getContextPath(); %>

  11. <html>

  12. <head>

  13. <title>Paper列表</title>

  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">

  15. <!-- 引入 Bootstrap -->

  16. <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

  17. </head>

  18. <body>

  19. <div class="container">

  20. <div class="row clearfix">

  21. <div class="col-md-12 column">

  22. <div class="page-header">

  23. <h1>

  24. 基于SSM框架的管理系统:简单实现增、删、改、查。

  25. </h1>

  26. </div>

  27. </div>

  28. </div>

  29.  
  30. <div class="row clearfix">

  31. <div class="col-md-12 column">

  32. <div class="page-header">

  33. <h1>

  34. <small>论文列表 —— 显示所有论文</small>

  35. </h1>

  36. </div>

  37. </div>

  38. </div>

  39. <div class="row">

  40. <div class="col-md-4 column">

  41. <a class="btn btn-primary" href="${path}/paper/toAddPaper">新增</a>

  42. </div>

  43. </div>

  44. <div class="row clearfix">

  45. <div class="col-md-12 column">

  46. <table class="table table-hover table-striped">

  47. <thead>

  48. <tr>

  49. <th>论文编号</th>

  50. <th>论文名字</th>

  51. <th>论文数量</th>

  52. <th>论文详情</th>

  53. <th>操作</th>

  54. </tr>

  55. </thead>

  56. <tbody>

  57. <c:forEach var="paper" items="${requestScope.get('list')}" varStatus="status">

  58. <tr>

  59. <td>${paper.paperId}</td>

  60. <td>${paper.paperName}</td>

  61. <td>${paper.paperNum}</td>

  62. <td>${paper.paperDetail}</td>

  63. <td>

  64. <a href="${path}/paper/toUpdatePaper?id=${paper.paperId}">更改</a> |

  65. <a href="<%=appPath%>/paper/del/${paper.paperId}">删除</a>

  66. </td>

  67. </tr>

  68. </c:forEach>

  69. </tbody>

  70. </table>

  71. </div>

  72. </div>

  73. </div>

addPaper,jsp:论文添加页面


 
  1. <%--

  2. Created by IntelliJ IDEA.

  3. User: lenovo

  4. Date: 2018/4/7

  5. Time: 16:45

  6. To change this template use File | Settings | File Templates.

  7. --%>

  8. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

  9. <%@ page contentType="text/html;charset=UTF-8" language="java" %>

  10. <%

  11. String path = request.getContextPath();

  12. String basePath = request.getScheme() + "://"

  13. + request.getServerName() + ":" + request.getServerPort()

  14. + path + "/";

  15. %>

  16. <html>

  17. <head>

  18. <title>新增论文</title>

  19. <meta name="viewport" content="width=device-width, initial-scale=1.0">

  20. <!-- 引入 Bootstrap -->

  21. <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

  22. </head>

  23. <body>

  24. <div class="container">

  25. <div class="row clearfix">

  26. <div class="col-md-12 column">

  27. <div class="page-header">

  28. <h1>

  29. 基于SSM框架的管理系统:简单实现增、删、改、查。

  30. </h1>

  31. </div>

  32. </div>

  33. </div>

  34.  
  35. <div class="row clearfix">

  36. <div class="col-md-12 column">

  37. <div class="page-header">

  38. <h1>

  39. <small>新增论文</small>

  40. </h1>

  41. </div>

  42. </div>

  43. </div>

  44. <form action="" name="userForm">

  45. 论文名称:<input type="text" name="paperName"><br><br><br>

  46. 论文数量:<input type="text" name="paperNum"><br><br><br>

  47. 论文详情:<input type="text" name="paperDetail"><br><br><br>

  48. <input type="button" value="添加" οnclick="addPaper()">

  49. </form>

  50.  
  51. <script type="text/javascript">

  52. function addPaper() {

  53. var form = document.forms[0];

  54. form.action = "<%=basePath %>paper/addPaper";

  55. form.method = "post";

  56. form.submit();

  57. }

  58. </script>

  59. </div>

updatePaper.jsp:论文更改界面


 
  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>

  3. <%

  4. String path = request.getContextPath();

  5. String basePath = request.getScheme() + "://"

  6. + request.getServerName() + ":" + request.getServerPort()

  7. + path + "/";

  8. %>

  9. <html>

  10. <head>

  11. <title>修改论文</title>

  12. <meta name="viewport" content="width=device-width, initial-scale=1.0">

  13. <!-- 引入 Bootstrap -->

  14. <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

  15. </head>

  16. <body>

  17. <div class="container">

  18. <div class="row clearfix">

  19. <div class="col-md-12 column">

  20. <div class="page-header">

  21. <h1>

  22. 基于SSM框架的管理系统:简单实现增、删、改、查。

  23. </h1>

  24. </div>

  25. </div>

  26. </div>

  27.  
  28. <div class="row clearfix">

  29. <div class="col-md-12 column">

  30. <div class="page-header">

  31. <h1>

  32. <small>修改论文</small>

  33. </h1>

  34. </div>

  35. </div>

  36. </div>

  37.  
  38. <form action="" name="userForm">

  39. <input type="hidden" name="paperId" value="${paper.paperId}"/>

  40. 论文名称:<input type="text" name="paperName" value="${paper.paperName}"/>

  41. 论文数量:<input type="text" name="paperNum" value="${paper.paperNum}"/>

  42. 论文详情:<input type="text" name="paperDetail" value="${paper.paperDetail }"/>

  43. <input type="button" value="提交" οnclick="updatePaper()"/>

  44. </form>

  45. <script type="text/javascript">

  46. function updatePaper() {

  47. var form = document.forms[0];

  48. form.action = "<%=basePath %>paper/updatePaper";

  49. form.method = "post";

  50. form.submit();

  51. }

  52. </script>

  53. </div>

4. 利用tomcat进行项目部署

  • 下载 安装 tomcat:tomcat 9 下载地址
  • IDEA配置tomcat: 
    点击上方的 Run 选项,选择Edit Configuration

Day3:SSM

选择default -> tomcat -> local 选择下载安装到本地的tomcat服务器的地址

Day3:SSM

切换到Deployment选项页 点击 + 号选择 Arctifact ,

Day3:SSM
添加 项目名:war exploded 打包 
Day3:SSM
最后在Application context 中选择 空白 那一项,点击 Apply 应用。 
Day3:SSM

  • 最后切换到index.jsp界面,右键运行项目

Day3:SSM

5. 运行界面

Day3:SSM

论文列表界面: 
Day3:SSM
新增论文界面: 
Day3:SSM
论文修改界面: 
Day3:SSM

至此,利用IDEA+Maven+SSM框架实现的简单的增、删、改、查功能已经构建完毕!

上一篇:就最近看的paper谈谈预训练语言模型发展


下一篇:vs2010_相关目录