赞
踩
本文主要记录自己初次使用idea搭建SSM框架项目的步骤,参考了https://blog.csdn.net/khxu666/article/details/79851070,供新手学习,项目结构如下:
新建文件夹时,可通过下图选择文件夹属性
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.chby.ssm</groupId> <artifactId>web-ssm</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>web-ssm 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> <!-- spring版本号 --> <spring.version>5.0.8.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.4.5</mybatis.version> <!-- mysql驱动版本号 --> <mysql-driver.version>8.0.15</mysql-driver.version> <!-- log4j日志包版本号 --> <slf4j.version>1.7.18</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <!--junit 测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 第一部分:Spring 配置--> <!-- Spring core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</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-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring DAO --> <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> <!-- Spring mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- 第二部分:Servlet web --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.4</version> </dependency> <!-- 第三部分:数据库和mybatis --> <!--数据库 mysql 驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-driver.version}</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring整合包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- 添加数据库连接池依赖 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <!-- 日志文件管理包 --> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- 格式化对象,方便输出日志 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> </dependencies> <build> <finalName>web-ssm</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>
新建jdbc.properties文件
driverClasss=com.mysql.cj.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/ssm?useSSL=false&serverTimezone=UTC
username=root
password=1312344326
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
新建log4j.properties
log4j.rootLogger=INFO,Console,File
#控制台日志
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
#普通文件日志
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=logs/ssm.log
log4j.appender.File.MaxFileSize=10MB
#输出日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
新建mybatis-config.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> <!-- 配置全局属性 --> <settings> <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 --> <setting name="useGeneratedKeys" value="true"/> <!-- 使用列别名替换列名 默认:true --> <setting name="useColumnLabel" value="true"/> <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>
新建spring-mvc文件
<?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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 配置SpringMVC --> <!-- 1.开启SpringMVC注解模式 --> <!-- 简化配置: (1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter (2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持 --> <mvc:annotation-driven /> <!-- 2.静态资源默认servlet配置 (1)加入对静态资源的处理:js,gif,png (2)允许使用"/"做整体映射 --> <mvc:default-servlet-handler/> <!-- 3.配置jsp 显示ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 4.扫描web相关的bean --> <context:component-scan base-package="com.chby.controller" /> </beans>
新建spring-mybatis文件
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置数据库相关参数properties的属性:${url} --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/ssm?useSSL=false&serverTimezone=UTC "/> <property name="username" value="root"/> <property name="password" value="1312344326"/> <!-- 初始化连接大小 --> <property name="initialSize" value="0"></property> <!-- 连接池最大数量 --> <property name="maxActive" value="20"></property> <!-- 连接池最大空闲 --> <property name="maxIdle" value="20"></property> <!-- 连接池最小空闲 --> <property name="minIdle" value="1"></property> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="60000"></property> </bean> <!-- 配置SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 配置MyBaties全局配置文件:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- 扫描pojo包 使用别名 --> <property name="typeAliasesPackage" value="com.chby.pojo"/> <!-- 扫描sql配置文件:mapper需要的xml文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.chby.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <property name="annotationClass" value="org.springframework.stereotype.Repository"/> </bean> <!-- 扫描service包下所有使用注解的类型 --> <context:component-scan base-package="com.chby.service"/> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置基于注解的声明式事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
PaperMapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.chby.dao.PaperDao"> <resultMap type="Paper" id="paperResultMap" > <id property="paperId" column="paper_id"/> <result property="paperName" column="name"/> <result property="paperNum" column="number"/> <result property="paperDetail" column="detail"/> </resultMap> <insert id="addPaper" parameterType="Paper"> INSERT INTO paper(paper_id,name,number,detail) VALUES (#{paperId},#{paperName}, #{paperNum}, #{paperDetail}) </insert> <delete id="deletePaperById" parameterType="long"> DELETE FROM paper WHERE paper_id=#{paperID} </delete> <update id="updatePaper" parameterType="Paper"> UPDATE paper SET NAME = #{paperName},NUMBER = #{paperNum},detail = #{paperDetail} WHERE paper_id = #{paperId} </update> <select id="queryById" resultType="Paper" parameterType="long"> SELECT paper_id,name,number,detail FROM paper WHERE paper_id=#{paperId} </select> <select id="queryAllPaper" resultMap="paperResultMap"> SELECT paper_id,name,number,detail FROM paper </select> </mapper>
在controller包中新建PaperController.java
package com.chby.controller; import com.chby.pojo.Paper; import com.chby.service.PaperService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; @Controller @RequestMapping("/paper") public class PaperController { @Autowired private PaperService paperService; @RequestMapping("/allPaper") public String list(Model model) { List<Paper> list = paperService.queryAllPaper(); model.addAttribute("list", list); return "allPaper"; } @RequestMapping("toAddPaper") public String toAddPaper() { return "addPaper"; } @RequestMapping("/addPaper") public String addPaper(Paper paper) { paperService.addPaper(paper); return "redirect:/paper/allPaper"; } @RequestMapping("/del/{paperId}") public String deletePaper(@PathVariable("paperId") Long id) { paperService.deletePaperById(id); return "redirect:/paper/allPaper"; } @RequestMapping("toUpdatePaper") public String toUpdatePaper(Model model, Long id) { model.addAttribute("paper", paperService.queryById(id)); return "updatePaper"; } @RequestMapping("/updatePaper") public String updatePaper(Model model, Paper paper) { paperService.updatePaper(paper); paper = paperService.queryById(paper.getPaperId()); model.addAttribute("paper", paper); return "redirect:/paper/allPaper"; } }
在dao包中新建PaperDao接口
package com.chby.dao; import com.chby.pojo.Paper; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface PaperDao { int addPaper(Paper paper); int deletePaperById(long id); int updatePaper(Paper paper); Paper queryById(long id); List<Paper> queryAllPaper(); }
在pojo包中新建Paper.java
package com.chby.pojo; public class Paper { private long paperId; private String paperName; private int paperNum; private String paperDetail; public long getPaperId() { return paperId; } public void setPaperId(long paperId) { this.paperId = paperId; } public String getPaperName() { return paperName; } public void setPaperName(String paperName) { this.paperName = paperName; } public int getPaperNum() { return paperNum; } public void setPaperNum(int paperNum) { this.paperNum = paperNum; } public String getPaperDetail() { return paperDetail; } public void setPaperDetail(String paperDetail) { this.paperDetail = paperDetail; } }
在service包中新建PaperService接口
package com.chby.service; import com.chby.pojo.Paper; import java.util.List; public interface PaperService { int addPaper(Paper paper); int deletePaperById(long id); int updatePaper(Paper paper); Paper queryById(long id); List<Paper> queryAllPaper(); }
然后在service包中新建包impl,在impl包中新建PaperServiceImpl.java
package com.chby.service.impl; import com.chby.dao.PaperDao; import com.chby.pojo.Paper; import com.chby.service.PaperService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class PaperServiceImpl implements PaperService { @Autowired private PaperDao paperDao; @Override public int addPaper(Paper paper) { return paperDao.addPaper(paper); } @Override public int deletePaperById(long id) { return paperDao.deletePaperById(id); } @Override public int updatePaper(Paper paper) { return paperDao.updatePaper(paper); } @Override public Paper queryById(long id) { return paperDao.queryById(id); } @Override public List<Paper> queryAllPaper() { return paperDao.queryAllPaper(); } }
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <% pageContext.setAttribute("path", request.getContextPath()); %> <!DOCTYPE HTML> <html> <head> <title>首页</title> <style type="text/css"> a { text-decoration: none; color: black; font-size: 18px; } h3 { width: 180px; height: 38px; margin: 100px auto; text-align: center; line-height: 38px; background: deepskyblue; border-radius: 4px; } </style> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> 基于SSM框架的管理系统:简单实现增、删、改、查。 </h1> </div> </div> </div> </div> <br><br> <h3> <a href="${path }/paper/allPaper">点击进入管理页面</a> </h3> </body> </html>
在WEB-INF下新建jsp文件夹,在其中新建allPaper.jsp,addPaper.jsp,updatePaper.jsp
allPaper.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2018/4/6 Time: 16:57 To change this template use File | Settings | File Templates. --%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String appPath = request.getContextPath(); %> <html> <head> <title>Paper列表</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- 引入 Bootstrap --> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> 基于SSM框架的管理系统:简单实现增、删、改、查。 </h1> </div> </div> </div> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>论文列表 —— 显示所有论文</small> </h1> </div> </div> </div> <div class="row"> <div class="col-md-4 column"> <a class="btn btn-primary" href="${path}/paper/toAddPaper">新增</a> </div> </div> <div class="row clearfix"> <div class="col-md-12 column"> <table class="table table-hover table-striped"> <thead> <tr> <th>论文编号</th> <th>论文名字</th> <th>论文数量</th> <th>论文详情</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach var="paper" items="${requestScope.get('list')}" varStatus="status"> <tr> <td>${paper.paperId}</td> <td>${paper.paperName}</td> <td>${paper.paperNum}</td> <td>${paper.paperDetail}</td> <td> <a href="${path}/paper/toUpdatePaper?id=${paper.paperId}">更改</a> | <a href="<%=appPath%>/paper/del/${paper.paperId}">删除</a> </td> </tr> </c:forEach> </tbody> </table> </div> </div> </div>
addPaper.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2018/4/7 Time: 16:45 To change this template use File | Settings | File Templates. --%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <html> <head> <title>新增论文</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- 引入 Bootstrap --> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> 基于SSM框架的管理系统:简单实现增、删、改、查。 </h1> </div> </div> </div> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>新增论文</small> </h1> </div> </div> </div> <form action="" name="userForm"> 论文名称:<input type="text" name="paperName"><br><br><br> 论文数量:<input type="text" name="paperNum"><br><br><br> 论文详情:<input type="text" name="paperDetail"><br><br><br> <input type="button" value="添加" οnclick="addPaper()"> </form> <script type="text/javascript"> function addPaper() { var form = document.forms[0]; form.action = "<%=basePath %>paper/addPaper"; form.method = "post"; form.submit(); } </script> </div>
updatePaper.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <html> <head> <title>修改论文</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- 引入 Bootstrap --> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> 基于SSM框架的管理系统:简单实现增、删、改、查。 </h1> </div> </div> </div> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>修改论文</small> </h1> </div> </div> </div> <form action="" name="userForm"> <input type="hidden" name="paperId" value="${paper.paperId}"/> 论文名称:<input type="text" name="paperName" value="${paper.paperName}"/> 论文数量:<input type="text" name="paperNum" value="${paper.paperNum}"/> 论文详情:<input type="text" name="paperDetail" value="${paper.paperDetail }"/> <input type="button" value="提交" οnclick="updatePaper()"/> </form> <script type="text/javascript"> function updatePaper() { var form = document.forms[0]; form.action = "<%=basePath %>paper/updatePaper"; form.method = "post"; form.submit(); } </script> </div>
如图,点击Edit Configuration
选择“+”号 -> tomcat -> local 选择下载安装到本地的tomcat服务器的地址
切换到Deployment选项页 点击 + 号选择 Arctifact
添加 项目名:war exploded 打包
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。