zoukankan      html  css  js  c++  java
  • ssm框架搭建

    一:环境准备

    jdk:java version "1.8.0_251"

    Tomcat:tomacat8

    Maven:Apache Maven 3.6.3 

    二,新建maven工程

     

     

    工程建立完成,

    2,搭建项目结构

     

    项目结构如下

     3,Customer.java

    package com.liam.pojo;
    
    public class Customer {
    
        /**
         * 客户持久层
         */
        private Integer id;
        private String userName;
        private String jobs;
        private String phone;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getJobs() {
            return jobs;
        }
    
        public void setJobs(String jobs) {
            this.jobs = jobs;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
    }

    4,CustomerDao

    package com.liam.dao;
    
    import com.liam.pojo.Customer;
    
    public interface CustomerDao {
        /**
         * 根据id 查询客户信息
         * @param id
         * @return
         */
        public Customer findCustomerById(Integer id);
    }

    5,CustomerService

    package com.liam.service;
    
    import com.liam.pojo.Customer;
    
    public interface CustomerService {
        public Customer findCustomerById(Integer id);
    }

    6,CustomerServiceImpl 

    package com.liam.service.impl;
    
    import com.liam.dao.CustomerDao;
    import com.liam.pojo.Customer;
    import com.liam.service.CustomerService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    @Service
    @Transactional
    public class CustomerServiceImpl implements CustomerService {
    
        @Autowired
        private CustomerDao customerDao;
        @Override
        public Customer findCustomerById(Integer id) {
            return this.customerDao.findCustomerById(id);
        }
    }

    7,CustomerController

    package com.liam.controller;
    
    import com.liam.pojo.Customer;
    import com.liam.service.CustomerService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class CustomerController {
        @Autowired
        private CustomerService customerService;
    
        @RequestMapping("/findCustomerById")
        public String findCustomerById(Integer id, Model model){
            Customer customer = customerService.findCustomerById(id);
            model.addAttribute("customer",customer);
            return "customer";
        }
    }

     9,

    ------------恢复内容开始------------

    一:环境准备

    jdk:java version "1.8.0_251"

    Tomcat:tomacat8

    Maven:Apache Maven 3.6.3 

    二,新建maven工程

     

     

    工程建立完成,

    2,搭建项目结构

     

    项目结构如下

     3,Customer.java

    package com.liam.pojo;
    
    public class Customer {
    
        /**
         * 客户持久层
         */
        private Integer id;
        private String userName;
        private String jobs;
        private String phone;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getJobs() {
            return jobs;
        }
    
        public void setJobs(String jobs) {
            this.jobs = jobs;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
    }

    4,CustomerDao

    package com.liam.dao;
    
    import com.liam.pojo.Customer;
    
    public interface CustomerDao {
        /**
         * 根据id 查询客户信息
         * @param id
         * @return
         */
        public Customer findCustomerById(Integer id);
    }

    5,CustomerService

    package com.liam.service;
    
    import com.liam.pojo.Customer;
    
    public interface CustomerService {
        public Customer findCustomerById(Integer id);
    }

    6,CustomerServiceImpl 

    package com.liam.service.impl;
    
    import com.liam.dao.CustomerDao;
    import com.liam.pojo.Customer;
    import com.liam.service.CustomerService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    @Service
    @Transactional
    public class CustomerServiceImpl implements CustomerService {
    
        @Autowired
        private CustomerDao customerDao;
        @Override
        public Customer findCustomerById(Integer id) {
            return this.customerDao.findCustomerById(id);
        }
    }

    7,CustomerController

    package com.liam.controller;
    
    import com.liam.pojo.Customer;
    import com.liam.service.CustomerService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class CustomerController {
        @Autowired
        private CustomerService customerService;
    
        @RequestMapping("/findCustomerById")
        public String findCustomerById(Integer id, Model model){
            Customer customer = customerService.findCustomerById(id);
            model.addAttribute("customer",customer);
            return "customer";
        }
    }

     9,CustomerDao.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.liam.dao.CustomerDao">
        <!--根据id 查询客户信息-->
        <resultMap id="BaseResultMap" type="com.liam.pojo.Customer">
            <!--<result column="id" property="id" jdbcType="id" />-->
            <result column="userName" property="userName" jdbcType="VARCHAR" />
            <result column="jobs" property="jobs" jdbcType="VARCHAR" />
            <result column="phone" property="phone" jdbcType="VARCHAR" />
        </resultMap>
    
        <select id="findCustomerById" resultMap="BaseResultMap" parameterType="java.lang.Integer">
            select * from customer where id = #{id}
        </select>
        
    </mapper>

    10,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>
        <!-- 别名定义 -->
        <typeAliases>
            <package name="com.liam.pojo"/>
        </typeAliases>
    </configuration>

    11,spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- 使用注解驱动-->
        <mvc:annotation-driven/>
        <!-- 配置视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/view/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <!-- 扫描所有handler(控制器)-->
        <context:component-scan base-package="com.liam.controller"/>
    
    </beans>

    12,spring-mybatis.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"
           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">
    
        <context:component-scan base-package="com.liam.service"/>
        <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="classpath:jdbc.properties"></property>
        </bean>
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <!--数据库连接池-->
            <property name="driverClassName" value="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${user}"/>
            <property name="password" value="${password}"/>
            <!-- 初始化连接大小 -->
            <property name="initialSize" value="${initialSize}"></property>
            <!-- 连接池最大数量 -->
            <property name="maxActive" value="${maxActive}"></property>
            <!-- 连接池最大空闲 -->
            <property name="maxIdle" value="${maxIdle}"></property>
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="${minIdle}"></property>
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="${maxWait}"></property>
        </bean>
    
        <!--配置sqlsession工厂-->
        <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!-- 自动扫描mapping.xml文件 -->
            <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        </bean>
    
        <!--配置DAO所在spring会自动查找下面的类-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.liam.dao"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        </bean>
    
        <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
    
    </beans>

    13,web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    
      <display-name>Archetype Created Web Application</display-name>
      <!--启动spring容器-->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-mybatis.xml</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    
      <!--请求和应答字符编码过滤器-->
      <filter>
        <filter-name>encoding-filter</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>
      </filter>
      <filter-mapping>
        <filter-name>encoding-filter</filter-name>
        <!--<url-pattern>/*</url-pattern>-->
        <servlet-name>springDispatcherServlet</servlet-name>
      </filter-mapping>
    
      <!-- 用前端控制器初始化springmvc容器 -->
      <!-- The front controller of this Spring Web application, responsible for
          handling all application requests -->
      <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
    
      <!-- Map all requests to the DispatcherServlet for handling -->
      <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
    </web-app>

    14,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>org.example</groupId>
      <artifactId>ssmTest2</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>war</packaging>
    
      <name>ssmTest2 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>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>4.3.18.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>4.3.18.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>4.3.18.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>4.3.18.RELEASE</version>
        </dependency>
        <!-- mybatis -->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.5.5</version>
        </dependency>
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>8.0.21</version>
        </dependency>
        <!-- 数据库事务管理-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>4.3.18.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>4.3.18.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>2.0.5</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
          <version>2.5</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>commons-dbcp</groupId>
          <artifactId>commons-dbcp</artifactId>
          <version>1.4</version>
        </dependency>
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.2.73</version>
        </dependency>
      </dependencies>
    
      <build>
        <finalName>ssmTest2</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>

    15,customer.jsp

    <%--
      Created by IntelliJ IDEA.
      User: 31436
      Date: 2020/9/3
      Time: 22:41
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>客户信息</title>
    </head>
    <body>
    <table border=1>
        <tr>
            <td>编号</td>
            <td>名称</td>
            <td>职业</td>
            <td>电话</td>
        </tr>
        <tr>
            <td>${customer.id}</td>
            <td>${customer.userName}</td>
            <td>${customer.jobs}</td>
            <td>${customer.phone}</td>
        </tr>
    </table>
    </body>
    </html>

    16,sql

    CREATE TABLE customer(
        id int(32) PRIMARY key AUTO_INCREMENT,
        username varchar(50),
        jobs varchar(50),
        phone varchar(16)
        );

    INSERT INTO customer VALUES (1, 'liam', 'worker', 123456789);
    INSERT INTO customer VALUES (2, 'wlin', 'teacher', 987654321);

    17,运行结果

     总结:

    1,网上的参考资料比较多,主要参考了:

    https://www.cnblogs.com/wanson/articles/9998829.html

    2,在我第一次搭建的时候,使用的是tomacat10,结果一直出现

    解决无果,换用tomacat8,未出现上面的问题。

    2,

    java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.type.JdbcType.Intege

    mybatis配置的jdbcType类型要是大写的,检查了mybatis的配置文件,果然是这个问题

    3,

    Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory

    参考此链接:

    https://blog.csdn.net/weixin_41955327/article/details/82286309

     报错之前为 userName -> user,对应的 

     4,

    There is no setter for property named 'dd_valueId' in 'class com.manager.pojo.DataDictionary'的错误解决方法

    mybatis 映射文件和 Bean应该对应一致。

    5,

    使用Idea部署SSM项目后,访问路径为url:8080/项目名_war_exploded的解决方案

    在tomcat配置页的Deployment下,修改Application context为/,即可直接使用url:8080访问项目主页。或者任意你想要的路径,

    6,

    Result Maps collection does not contain value for 错误

     当时未添加resultMap 数据类型,parameterType 类型也选的不正确。按照上面正确的配置即可

    7,

    java.sql.SQLException:  The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

    错误信息翻译 —— java.sql.SQLException: 服务器时区值'?? u±e×??±??未被识别或代表多个时区。如果希望利用时区支持,必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性),以使用更特殊的时区值。

    解决方法:JDBC连接的URL后面加上serverTimezone = GMT,如果需要使用GMT + 8时区,需要写成GMT%2B8。

     追加上后面这些

    8,使用Idea构建springmvc框架,出现no bean named ‘cacheManager’ is defined 错误。

     参考链接 https://blog.csdn.net/qq_23184291/article/details/78086398

    9,整个工程参考链接

    https://www.cnblogs.com/wanson/articles/9998829.html

  • 相关阅读:
    JVM探秘1--JVM内存运行时区域划分
    skywalking学习之路---skywalking环境从零搭建部署
    ES6学习笔记之解构赋值
    ES6学习笔记之Symbol
    ES6学习笔记之块级作用域
    Vue开发警告[Vue warn]: Avoid replacing instance root $data. Use nested data properties instead.
    JavaScript实现归并排序
    Vue-cli3.x在开发环境中(router采用 history模式)出现Failed to resolve async component default: Error: Loading chunk {/d} failed.或者Uncaught SyntaxError: Unexpected token <错误
    vue-cli3.x中使用axios发送请求,配合webpack中的devServer编写本地mock数据接口(get/post/put/delete)
    uni-app开发微信小程序引入UI组件库(Vant-weapp)步骤
  • 原文地址:https://www.cnblogs.com/changlili/p/13621326.html
Copyright © 2011-2022 走看看