1. 程式人生 > 實用技巧 >用idae建立一個springMVC專案

用idae建立一個springMVC專案

      用idae建立一個springMVC專案

1.開啟idae建立一個專案

2.下一步

3.下一步

4.下一步

5.下一步

6.下一步

進入你的pom檔案

7.pom程式碼

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.
--> <!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ --> <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<packaging>war</packaging> <name>JavaTest</name> <groupId>com.accp</groupId> <artifactId>JavaTest</artifactId> <version>1.0-SNAPSHOT</version> <build> <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> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.7</version> <configuration> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>8888</port> <maxIdleTime>30000</maxIdleTime> </connector> </connectors> <webAppSourceDirectory>${project.build.directory}/${pom.artifactId}-${pom.version}</webAppSourceDirectory> <contextPath>/</contextPath> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <port>8080</port> <uriEncoding>UTF-8</uriEncoding> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.5</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.9.RELEASE</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> </project>
pom檔案

8.進行對java的建立

8.下一步

9.下一步

10.下一步

11.實體類

我的實體類

package com.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 一方:手機品牌
 * @author Administrator
 *
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MobileBrand {
    private Integer brandid;
    private String brand;
    /*public Integer getBrandid() {
        return brandid;
    }
    public void setBrandid(Integer brandid) {
        this.brandid = brandid;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    @Override
    public String toString() {
        return "MobileBrand [brand=" + brand + ", brandid=" + brandid + "]";
    }
    */
    
    
    

}
MobileBrand.java
package com.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 多方:手機詳細資訊:
 * @author Administrator
 *
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MobileInfo {   
    private Integer id     ;
    private Integer brandid;
    private Double price  ;
    private Integer weight ;
    private String type   ;
    private String size   ;
    private String style  ;

    /*public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getBrandid() {
        return brandid;
    }

    public void setBrandid(Integer brandid) {
        this.brandid = brandid;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public MobileInfo() {
    }

    public MobileInfo(Integer id, Integer brandid, Double price, Integer weight, String type, String size, String style) {
        this.id = id;
        this.brandid = brandid;
        this.price = price;
        this.weight = weight;
        this.type = type;
        this.size = size;
        this.style = style;
    }

    @Override
    public String toString() {
        return "MobileInfo [brandid=" + brandid + ", id=" + id + ", price="
                + price + ", size=" + size + ", style=" + style + ", type="
                + type + ", weight=" + weight + "]";
    }
    
    
    
    */

}
MobileInfo

12.lombok介紹

13.mapper方法

mapper程式碼

package com.mapper;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.entity.MobileBrand;
import com.entity.MobileInfo;

public interface ObjtotabMapper {

    @Select("select * from mobilebrand")
    List<Map<String,Object>> selectOne();
    

    @Select("<script>                                                                        " +
            "   select i.*,b.brand from mobileinfo i,mobilebrand b where i.brandid=b.brandid " +
            "   <if test='id!=null'>                                                         " +
            "        and id=#{id}                                                            " +
            "   </if>                                                                        " +
            "   <if test='brandid!=null'>                                                    " +
            "        and i.brandid=#{brandid}                                                  " +
            "   </if>                                                                        " +
            "   <if test='type!=null'>                                                       " +
            "        and type=#{type}                                                        " +
            "   </if>                                                                        " +
            "   <if test='price!=null'>                                                      " +
            "        and price=#{price}                                                      " +
            "   </if>                                                                        " +
            "   <if test='size!=null'>                                                       " +
            "        and size=#{size}                                                        " +
            "   </if>                                                                        " +
            "   <if test='weight!=null'>                                                     " +
            "        and weight=#{weight}                                                    " +
            "   </if>                                                                        " +
            "   <if test='style!=null'>                                                      " +
            "        and style=#{style}                                                      " +
            "   </if>                                                                        " +
            "</script>                                                                       " )
    List<Map<String,Object>> selectMany(Map<String, Object> map);
    
    

    @Insert("insert into mobileinfo(brandid,type,price,size,weight,style) values(#{brandid},#{type},#{price},#{size},#{weight},#{style})")
    Integer insertMany(MobileInfo mobileInfo);

    @Update("update mobileinfo set brandid=#{brandid},type=#{type},price=#{price}," +
            "size=#{size},weight=#{weight},style=#{style} where id=#{id}")
    Integer updateMany(MobileInfo mobileInfo);
    

    @Delete("delete from mobileinfo where id=#{id}")
    Integer delMany(MobileInfo mobileInfo);

}
ObjtotabMapper

14.下一步

package com.service;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.entity.MobileBrand;
import com.entity.MobileInfo;

/**
 * mybatis的業務層介面
 * 該介面中包含一方和多方的所有操作
 * @author Administrator
 *
 */
public interface ObjtotabService {
    //1.一方查詢:MobileBrand
    List<Map<String,Object>> selectOne();
    
    //2.多方查詢:MobileInfo
    List<Map<String,Object>> selectMany(Map<String, Object> map);
    
    
    //3.多方新增:MobileInfo
    Integer insertMany(MobileInfo mobileInfo);
    
    //4.多方修改:MobileInfo
    Integer updateMany(MobileInfo mobileInfo);
    
    //5.多方刪除:MobileInfo
    Integer delMany(MobileInfo mobileInfo);

}
ObjtotabService
package com.service;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.MobileBrand;
import com.entity.MobileInfo;
import com.mapper.ObjtotabMapper;
@Service
public class ObjtotabServiceImpl implements ObjtotabService {
    @Autowired
    private ObjtotabMapper objtotabMapper;

    public Integer delMany(MobileInfo mobileInfo) {
        return objtotabMapper.delMany(mobileInfo);
    }

    public Integer insertMany(MobileInfo mobileInfo) {
        return objtotabMapper.insertMany(mobileInfo);
    }

    public List<Map<String, Object>> selectMany(Map<String, Object> map) {
        return objtotabMapper.selectMany(map);
    }

    public List<Map<String, Object>> selectOne() {
        return objtotabMapper.selectOne();
    }

    public Integer updateMany(MobileInfo mobileInfo) {
        return objtotabMapper.updateMany(mobileInfo);
    }

}
ObjtotabServiceImpl

15.下一步

<?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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <context:component-scan base-package="com"/>

    <mvc:annotation-driven/>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/user?characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>

    </bean>
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.entity"/>
        <!--<property name="mapperLocations" value="classpath:com/mapper/*.xml"/>-->
    </bean>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven/>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <property name="basePackage" value="com.mapper"/>
    </bean>
</beans>
springMVC-servlet.xml

16.關於頁面可以用我的也可以自己寫,畢竟我挺菜的

關於web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>character</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>character</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>
web.xml

17.頁面

<%--
  Created by IntelliJ IDEA.
  User: caijiale
  Date: 2020/10/16
  Time: 10:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<div align="center"><a href="manyadd.jsp">新增</a>
<form action="many.do" method="post">
    手機品牌:
    <select name="brandid">
        <option value="-1">--請選擇--</option>
        <c:forEach var="i" items="${onelist}">
            <option value="${i.brandid}">${i.brand}</option>
        </c:forEach>
    </select>
    <input type="submit" value="查詢"/></form>
</form>
</div>
<table align="center">
    <tr>
        <td>編號</td>
        <td>品牌</td>
        <td>型號</td>
        <td>價格</td>
        <td>尺寸</td>
        <td>重量</td>
        <td>樣式</td>
        <td>操作</td>
    </tr>
    <c:forEach var="i" items="${manylist}">
        <tr>
            <td>${i.id}</td>
            <td>${i.brand}</td>
            <td>${i.type}</td>
            <td>${i.price}</td>
            <td>${i.size}</td>
            <td>${i.weight}</td>
            <td>${i.style}</td>
            <td>
                <a href="manyinfo.do?id=${i.id}">修改</a>
                |
                <a href="manydel.do?id=${i.id}">刪除</a>
            </td>
        </tr>
    </c:forEach>
</table>
</body>
</html>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'add.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
       <form action="manyadd.do" method="post">
           <table align="center">
              <tr align="center">
                 <td>手機品牌:</td>
                 <td>
                     <select name="brandid">
                       <option value="-1">--請選擇--</option>
                       <c:forEach var="i" items="${onelist}">
                          <option value="${i.brandid}">${i.brand}</option>
                       </c:forEach>
                      </select>
                 </td>
              </tr>
              <tr align="center">
                <td>型別:</td>
                <td><input type="text" name="type"/></td>
              </tr>
              <tr align="center">
                <td>價格:</td>
                <td><input type="text" name="price"/></td>
              </tr>
              <tr align="center">
                <td>尺寸:</td>
                <td><input type="text" name="size"/></td>
              </tr>
              <tr align="center">
                <td>重量:</td>
                <td><input type="text" name="weight"/></td>
              </tr>
              <tr align="center">
                <td>樣式:</td>
                <td><input type="text" name="style"/></td>
              </tr>
              <tr align="center">
                <td colspan="2">
                  <input type="submit" value="提交"/>
                  <input type="reset" value="重置"/>
                 </td>
              </tr>
           </table>
           </form>
  </body>
</html>
manyadd.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'add.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
       <form action="manyupd.do" method="post">
          <input type="hidden" name="id" align="center" value="${manyinfo.id}"/>
           <table align="center">
              <tr>
                 <td>手機品牌:</td>
                 <td>
                     <select name="brandid">
                       <c:forEach var="i" items="${onelist}">
                          <c:choose>
                            <c:when test="${manyinfo.brandid eq i.brandid}">
                               <option value="-1">--請選擇--</option>
                               <option value="${i.brandid}" selected="selected">${i.brand}</option>
                            </c:when>
                            <c:otherwise>
                               <option value="-1">--請選擇--</option>
                               <option value="${i.brandid}">${i.brand}</option>
                            </c:otherwise>
                          </c:choose>
                       </c:forEach>
                      </select>
                 </td>
              </tr>
              <tr>
                <td>型別:</td>
                <td><input type="text" name="type" value="${manyinfo.type}"/></td>
              </tr>
              <tr>
                <td>價格:</td>
                <td><input type="text" name="price" value="${manyinfo.price}"/></td>
              </tr>
              <tr>
                <td>尺寸:</td>
                <td><input type="text" name="size" value="${manyinfo.size}"/></td>
              </tr>
              <tr>
                <td>重量:</td>
                <td><input type="text" name="weight" value="${manyinfo.weight}"/></td>
              </tr>
              <tr>
                <td>樣式:</td>
                <td><input type="text" name="style" value="${manyinfo.style}"/></td>
              </tr>
              <tr>
                <td colspan="2">
                  <input type="submit" value="提交"/>
                  <input type="reset" value="重置"/>
                 </td>
              </tr>
           </table>
           </form>
  </body>
</html>
manyupd.jsp

18.Controller

package com.Controller;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.entity.MobileBrand;
import com.entity.MobileInfo;
import com.service.ObjtotabService;
@Controller
public class CenterController {
    @Autowired
    private ObjtotabService objtotabService;
    
    //一方查詢
    @RequestMapping("/one.do")
    public String selectOne(HttpSession session) {
        session.setAttribute("onelist", objtotabService.selectOne());
        return "redirect:/many.do";
    }
    //多方查詢
    @RequestMapping("/many.do")
    public String selectMany(@RequestParam Map<String, Object> map,Model model) {
        model.addAttribute("manylist", objtotabService.selectMany(map));
        return "/index.jsp";
    }
    
    //多方新增
    @RequestMapping("/manyadd.do")
    public String insertMany(MobileInfo mobileInfo) {
//        System.out.println(map.get("brandid"));
//        System.out.println(map.get("weight"));
        objtotabService.insertMany(mobileInfo);
        return "redirect:/many.do";
    }

    
    //多方刪除
    @RequestMapping("/manydel.do")
    public String delMany(MobileInfo mobileInfo) {
        objtotabService.delMany(mobileInfo);
        return "redirect:/many.do";
    }

    //多方查單條
    @RequestMapping("/manyinfo.do")
    public String selectManyInfo(@RequestParam Map<String, Object> map,Model model) {
        model.addAttribute("manyinfo", objtotabService.selectMany(map).get(0));
        return "/manyupd.jsp";
    }
    
    //多方修改
    @RequestMapping("/manyupd.do")
    public String updateMany(MobileInfo mobileInfo) {
        objtotabService.updateMany(mobileInfo);
        return "redirect:/many.do";
    }

}
CenterController