IDEA+SSM+MAVEN构建可运行项目

对于java+idea+ssm新手来讲,跑一个demo出来简直是灾难呀。。坑!

 

1、新建maven项目:

 


一路next直到创建完成项目,maven自动创建项目会输出如下信息:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.249 s
[INFO] Finished at: 2018-09-25T19:35:04+08:00
[INFO] Final Memory: 14M/198M
[INFO] ------------------------------------------------------------------------
[INFO] Maven execution finished

2、此时idea该网页项目已经创建完成,配置tomcat运行试试:点击右上角edit configurations


但是运行的结果是报错:404。这里确实还不确定原因,为什么模板构建出来无法运行。因此做以下修改:

选择删除该Web,新建Web,指定Deployment Descriptors与Web Resource Directory为:

D:\workspace\maventest5\src\main\webapp\WEB-INF\web.xml

D:\workspace\maventest5\src\main\webapp

重新启动tomcat运行得到index.jsp的Hello World!网页。在此基础上进行SSM框架的构建。

3、修改maven配置文件pom.xml自动引入工程所需的jar包,对于oracle的jar包需要特殊处理:

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6-Dversion=11.2.0.1.0 -Dpackaging=jar-Dfile=D:\app\hanbin\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar 

由于我在maven的conf文件夹下修改了setting.xml文件:

D:\software\maven\apache-maven-3.3.9\repo
导致安装的包默认到对应目录下,而项目对应的reposity文件夹为D:/M2/REPOSITY文件夹内,因此需要将oracle的jar拷到对应的m2文件夹内。

pom.xml如下:




  4.0.0

  cn.hb
  ssmdemo5
  1.0-SNAPSHOT
  war

  ssmdemo5 Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
    5.0.3.RELEASE
    3.4.4
  

  
      
      
          junit
          junit
          4.12
          test
      
      
      
      
          org.springframework
          spring-core
          ${spring.version}
      
      
          org.springframework
          spring-beans
          ${spring.version}
      
      
          org.springframework
          spring-context
          ${spring.version}
      
      
          org.springframework
          spring-context-support
          ${spring.version}
      
      
      
          org.springframework
          spring-jdbc
          ${spring.version}
      
      
          org.springframework
          spring-tx
          ${spring.version}
      
      
      
          org.springframework
          spring-web
          ${spring.version}
      
      
          org.springframework
          spring-webmvc
          ${spring.version}
      
      
          org.springframework
          spring-test
          ${spring.version}
      
      
      
          javax.servlet
          javax.servlet-api
          3.0.1
          provided
      
      
          javax.servlet.jsp
          jsp-api
          2.2
          provided
      
      
          javax.servlet
          jstl
          1.2
      
      
          taglibs
          standard
          1.1.2
      
      
          com.fasterxml.jackson.core
          jackson-databind
          2.9.4
      
      
      
      
          mysql
          mysql-connector-java
          5.1.38
      
      
          com.oracle
          ojdbc6
          11.2.0.1.0
      
      
      
          com.mchange
          c3p0
          0.9.5.2
      
      
      
          org.mybatis
          mybatis
          ${mybatis.version}
      
      
      
          org.mybatis
          mybatis-spring
          1.3.1
      
      
      
      
          ch.qos.logback
          logback-classic
          1.1.1
      



  

  
    ssmdemo5
    
      
        
          maven-clean-plugin
          3.0.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.7.0
        
        
          maven-surefire-plugin
          2.20.1
        
        
          maven-war-plugin
          3.2.0
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  

注意自己的项目的名称不要错误。引入结束后,应该有如下现象:

4、在main文件夹下新建java、resources文件夹,并依次设定为Sources Root、Resources Root。

在java文件夹下新建package为com及com下controller、dao、pojo、service,依次代表控制器即网页跳转相关、数据库映射、数据库对应的java实体类、所需的服务。在resource下新建mapper、spring文件夹对应放置mybatis映射文件与spring配置相关文件。并且在resource文件夹下新建jdbc.properties、log4j.properties、mybatis-congig.xml对应jdbc的一些配置包括数据库登录相关的信息、日志的设置以及mybatis的设置如驼峰转换等。在WEB-INF下新建jsp文件夹存放网页jsp文件。

jdbc.properties内容如下:

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=test
jdbc.password=123456

注意用户名密码不要错误。

log4j.properties如下:

log4j.rootLogger=ERROR, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

mybatis-congig.xml如下:




    
    
        
        

        
        

        
        
    

开启驼峰转化,网上说是避免下划线“_”的错误,目前还没有遇到。

5、本文部分转载一下博客,因此采用的例子与该博客相同,略有改动。

博客链接为:https://blog.csdn.net/khxu666/article/details/79851070

仿照一个论文管理系统的增删改查操作。

6、在oracle数据库建立表

create table PAPER
(
PAPER_ID NUMBER(20) not null,
PAPERNAME VARCHAR2(100) not null,
PAPERNUMBER NUMBER(11) not null,
DETAIL VARCHAR2(200) not null
)

插入几条数据:

INSERT INTO paper VALUES (2, '深度学习', 3, 'dldldl');
INSERT INTO paper VALUES (3, '大数据', 4, 'bdbdbd');
7、在pojo package下新建java实体类Paper,与数据库表内字段一一对应:

package com.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;
    }
}

idea内alt+insert选择自动插入setter和getter,setter和getter是必须的,理由还没看。。。

8、在dao下新建PaperDao接口类:

package com.dao;

import com.pojo.Paper;

import java.util.List;

public interface PaperDao {
    int addPaper(Paper paper);

    int deletePaperById(long id);

    int updatePaper(Paper paper);

    Paper queryById(long id);

    List queryAllPaper();
}

实现对数据库表的增删改查基础操作。

9、此时配置SSM数据库与DAO的映射,在spring下新建spring-dao.xml并在mapper下新建PapperMapper.xml文件,完成数据库的映射。

spring-dao.xml内容为:



    
    
    

    
    
        
        
        
        
        

        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
    

 

PapperMapper.xml内容为:

 

 




    
        
        
        
        
    
    
        INSERT INTO paper(paper_id,papername,papernumber,detail) VALUE (#{paperId},#{paperName}, #{paperNum}, #{paperDetail})
    

    
        DELETE FROM paper WHERE paper_id=#{paperID}
    

    
        UPDATE paper
        SET PAPERNAME = #{paperName},PAPERNUMBER = #{paperNum},detail = #{paperDetail}
        WHERE  paper_id = #{paperId}
    

    
    

 

但是注意,mapper中类型转换的问题。varchar2可以转换为java中的String。但是number要根据位数来判断是integer或long,否则会出现其他正常但是始终查询结果为空的情况。

 

10、在service下新建PaperService接口类:

package com.service;

import com.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 queryAllPaper();

}

并在service下新建package  impl并在impl下新建

PaperServiceImpl服务实现类:
package com.service.impl;

import com.dao.PaperDao;
import com.pojo.Paper;
import com.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 queryAllPaper() {
        return paperDao.queryAllPaper();
    }

}

11、在spring下新建spring-service.xml


    
    

    
    
        
        
    
    
    


12、这样,可以通过调用服务调用数据相关的操作。在controller下新建

PaperController类:
package com.controller;

import com.pojo.Paper;
import com.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 PapperController {
    @Autowired
    private PaperService paperService;

    @RequestMapping("/allPaper")
    public String list(Model model) {
        List 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";
    }
}

 

13、在spring下新建spring-mvc.xml



    
    
    
    

    
    

    
    
        
        
        
    

    
    

 

14、修改web.xml配置:






  Archetype Created Web Application
  
    dispatcher
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:spring/spring-*.xml
    
  
  
    dispatcher
    
    /
  
  
    encodingFilter
    
      org.springframework.web.filter.CharacterEncodingFilter
    
    
      encoding
      utf-8
    
  

  
    encodingFilter
    /*
  


15、至此整个工程构建完毕,构成结构如图所示:

 

16、在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(); %>


    Paper列表
    
    
    


论文编号 论文名字 论文数量 论文详情 操作
${paper.paperId} ${paper.paperName} ${paper.paperNum} ${paper.paperDetail} 更改 | 删除

 

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 + "/";
%>


    新增论文
    
    
    


论文名称:


论文数量:


论文详情:


 

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 + "/";
%>


    修改论文
    
    
    


论文名称: 论文数量: 论文详情:

17、设置运行默认地址为:http://localhost:8080/paper/allPaper点击运行tomcat启动项目,得到如下结果:


 

感谢原作者,但是在自己的IDEA跑了好几个都是404,找了很多博客最终运行成功!希望接下来能顺利完成开发任务!

你可能感兴趣的:(JAVA基础,SSM,MAVEN,IDEA,JAVA)