搭建测试项目(学习)【笔记】

一、新建一个项目

1.1、搭建项目环境

1.1.1、基本环境搭建

1、在File中选择new,后再选择Project,进入这个页面后,选择maven,Project SDK选择1.8;

image-20200727091329186

2、点击next,进入这个页面,输入信息;

image-20200727091357865

3、点击next,确认起的名字与之前相似,就可以点击Finsh;

4、当你想要再一个项目中运行多个项目时,可以把主项目中的src给删除;

image-20200727091951828

5、然后再点击File,选择new中的module

image-20200727092124553

6、重复2~3步骤;

image-20200727092224383

7、在模型中选择Add Framework support...

image-20200727092346572

8、勾选Web Application(4.0),然后点击ok;

image-20200727092437337

1.1.2、搭建数据库

1、开启数据库,点击Database里的“+”号,选择mysql

image-20200727102213502

image-20200727102312344

2、进入页面,将Driver:MySQL改为Driver:MySQL for 5.1

image-20200727102411697

image-20200727102450425

3、选择想要的数据库,点击ok

image-20200727102718037

4、这样数据库就配置好了

image-20200727102810880

1.1.3、搭建服务器

1、点击Add Configuration...

image-20200727102902090

2、进入页面后,点击“+”,选择Tomcat Server中的Local

image-20200727102957102

3、该别名和点击“+”,

image-20200727103152113

image-20200727103228066

4、当有多个moduls时,可以选择任意一个加入到服务器,也可以全都加入

5、点击第二行第一个

image-20200727112020319

6、进入页面后,选择Artifacts,在你的项目展开WEB-INF文件查看是否有lib文件,没有则需要新建一个lib文件夹

image-20200727112219945

image-20200727112441594

image-20200727112516307

7、在lib中导入项目的所有jar包

image-20200727112600199

image-20200727112617583

image-20200727112653046

8、点击ok

9、服务器搭建完成

10、这样,项目的环境就已经搭建好了,如果出现问题,请跳到大标题二中查看;

 

1.2、正式开始

1.2.1、配置pom.xml文件

1、环境搭建好后,第一件事就是导入该项目所需要的jar包;

 <?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.gzk</groupId>
     <artifactId>WebProject</artifactId>
     <packaging>pom</packaging>
     <version>1.0-SNAPSHOT</version>
     <modules>
         <module>Web01</module>
     </modules>
 
     <dependencies>
         <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-webmvc</artifactId>
             <version>5.2.8.RELEASE</version>
         </dependency>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>4.12</version>
         </dependency>
         <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
         <dependency>
             <groupId>org.mybatis</groupId>
             <artifactId>mybatis</artifactId>
             <version>3.5.5</version>
         </dependency>
         <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <version>8.0.20</version>
         </dependency>
 
         <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
         <dependency>
             <groupId>org.mybatis</groupId>
             <artifactId>mybatis-spring</artifactId>
             <version>2.0.5</version>
         </dependency>
         <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-jdbc</artifactId>
             <version>5.2.7.RELEASE</version>
         </dependency>
         <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
         <dependency>
             <groupId>org.aspectj</groupId>
             <artifactId>aspectjweaver</artifactId>
             <version>1.9.5</version>
         </dependency>
 
         <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
         <dependency>
             <groupId>com.mchange</groupId>
             <artifactId>c3p0</artifactId>
             <version>0.9.5.2</version>
         </dependency>
 
         <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
         <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>javax.servlet-api</artifactId>
             <version>4.0.1</version>
             <scope>provided</scope>
         </dependency>
 
         <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
         <dependency>
             <groupId>javax.servlet.jsp</groupId>
             <artifactId>jsp-api</artifactId>
             <version>2.2</version>
             <scope>provided</scope>
         </dependency>
 
         <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
         <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>jstl</artifactId>
             <version>1.2</version>
         </dependency>
 
     </dependencies>
 
     <!-- 解决静态资源导出问题 -->
     <build>
         <resources>
             <resource>
                 <directory>src/main/resources</directory>
                 <includes>
                     <include>**/*.xml</include>
                     <include>**/*.properties</include>
                 </includes>
             </resource>
             <resource>
                 <directory>src/main/java</directory>
                 <includes>
                     <include>**/*.xml</include
                   <include>**/*.properties</include
               </includes
           </resource
       </resources
   </build
​ 
</project>

2、第二步,解决静态资源导出问题

 <!-- 解决静态资源导出问题 -->
 <build>
     <resources>
         <resource>
             <directory>src/main/resources</directory>
             <includes>
                 <include>**/*.xml</include>
                 <include>**/*.properties</include>
             </includes>
         </resource>
         <resource>
             <directory>src/main/java</directory>
             <includes>
                 <include>**/*.xml</include>
                 <include>**/*.properties</include>
             </includes>
         </resource>
     </resources>
 </build>

1.2.2、配置web.xml

3、第三步,配置好web.xml;当<param-value>classpath:springmvc-servlet.xml</param-value>这个爆红没关系,因为可能你还没有在resources中新建一个springmvc-servlet.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">
 
     <!-- DispatcherServlet -->
     <servlet>
         <servlet-name>springmvc</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>classpath:applicationContext.xml</param-value>
         </init-param>
         <!-- 启动级别:1 -->
         <load-on-startup>1</load-on-startup>
     </servlet>
     <!-- 这里要注意
     /   :   匹配所有的请求:(不包括.jsp)
     /* :   匹配所有的请求:(包括.jsp)
     -->
     <servlet-mapping>
         <servlet-name>springmvc</servlet-name>
         <url-pattern>/</url-pattern>
     </servlet-mapping>
 
 
     <!-- 乱码过滤 -->
     <filter>
         <filter-name>encoding</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-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>
 
     <!-- sqlSession -->
     <session-config>
         <session-timeout>15</session-timeout>
     </session-config>
 
 </web-app>

1.2.3、配置resources

1.2.3.1、springmvc-servlet.xml

4、第四步,在resources中新建一个springmvc-servlet.xml文件,与web.xml中的<param-value>classpath:springmvc-servlet.xml</param-value>保持一致;

image-20200727101147372

5、第五步,配置springmvc-servlet.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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
         https://www.springframework.org/schema/beans/spring-beans.xsd">
 
     <!-- 处理器映射器 -->
     <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
     <!-- 处理器适配器 -->
     <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
     <!-- 视图解析器 -->
     <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <!-- 前缀 -->
         <property name="prefix" value="/WEB-INF/jsp/"/>
         <!-- 后缀 -->
         <property name="suffix" value=".jsp"/>
     </bean>
 
     <bean id="/hello" class="com.gzk.HelloController"/>
 
 </beans>

 

第二种:注解版

 <?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
         https://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         https://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/mvc
         https://www.springframework.org/schema/mvc/spring-mvc.xsd">
 
     <!-- 1.扫描包:controller -->
     <context:component-scan base-package="com.gzk.controller"/>
     <!-- 2.静态资源过滤 -->
     <mvc:default-servlet-handler/>
     <!-- 3.注解驱动 -->
     <mvc:annotation-driven/>
     <!--
     支持mvc注解驱动
         在spring中一般采用@RequestMapping注解来完成映射关系
         要想使@RequestMapping注解生效
         必须向上下文中注册DefoultAnnotationHandleMapping
         和一个AnnotationMethodHandlerAdapter实例
         这两个实例分别在类级别和方法级别处理
         而annotationn-driven配置帮助我们自动完成上述两个实例的注入。
     -->
 
     <!-- 4.视图解析器 -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
         <!-- 前缀 -->
         <property name="prefix" value="/WEB-INF/jsp/"/>
         <!-- 后缀 -->
         <property name="suffix" value=".jsp"/>
     </bean>
 
 </beans>

 

1.2.3.2、db.properties

 driverClass=com.mysql.cj.jdbc.Driver
 jdbcUrl=jdbc:mysql://localhost:3306/goods?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
 user=root
 password=root

 

1.2.3.3、mybatis.xml

mybatis-config.xml可要可不要,所有配置都可以在springmvc-dao.xml中实现,但是,可以把<settings><typeAliases>放在mybatiis-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>
         <typeAlias type="com.gzk.pojo.User" alias="User"/>
     </typeAliases>
 </configuration>

 

1.2.3.4、springmvc-dao.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
         https://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         https://www.springframework.org/schema/context/spring-context.xsd">
 
     <!-- 1.关联数据库配置文件 -->
     <context:property-placeholder location="classpath:db.properties"/>
 
     <!-- 2.连接池
             dbcp:   半自动化操作,不能自动连接
             c3p0:   自动化操作(自动化的加载配置文件,并且可以自动设置导对象中
             druid: 强大!
     -->
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
         <property name="driverClass" value="${driverClass}"/>
         <property name="jdbcUrl" value="${jdbcUrl}"/>
         <property name="user" value="${user}"/>
         <property name="password" value="${password}"/>
 
         <!-- c3p0连接池的私有属性 -->
         <property name="maxPoolSize" value="30"/>
         <property name="minPoolSize" value="10"/>
         <!-- 关闭连接后不自动commit -->
         <property name="autoCommitOnClose" value="false"/>
         <!-- 获取连接超时时间 -->
         <property name="checkoutTimeout" value="10000"/>
         <!-- 当获取连接失败重试次数 -->
         <property name="acquireRetryAttempts" value="2"/>
     </bean>
 
     <!-- sqlSessionFactory[Bean] -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         <property name="dataSource" ref="dataSource"/>
         <!-- 绑定mybatis的配置文件 -->
         <property name="configLocation" value="classpath:mybatis-config.xml"/>
         <property name="mapperLocations" value="classpath:com/gzk/dao/UserMapper.xml"/>
     </bean>
 
 <!--     可以不用创建UserMapperImpl.java去实现UserMapper接口,且在配置文件中去<bean>UserMapperImpl -->
 <!--     配置dao接口扫描包,动态地实现了Dao接口可以注入导Spring容器中!【企业用的比较多】 -->
         <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
             <!-- 注入sqlSessionFactory -->
             <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
             <!-- 要扫描的dao包 -->
             <property name="basePackage" value="com.gzk.dao"/>
         </bean>
 
 </beans>

 

1.2.3.5、springmvc-service.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
         https://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         https://www.springframework.org/schema/context/spring-context.xsd">
 
     <!-- 1.扫描service下的包 -->
     <context:component-scan base-package="com.gzk.service"/>
 
     <!-- 2.将我们的所有业务类,注入到Spring,可以通过配置,或者注解实现 -->
     <bean id="UserService" class="com.gzk.service.UserServiceImpl">
         <property name="userMapper" ref="userMapper"/>
     </bean>
 
     <!-- 3.声明式事务配置 -->
     <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <!-- 注入数据源 -->
         <property name="dataSource" ref="dataSource"/>
     </bean>
 
     <!-- 4.aop事务支持! -->
 
 </beans>

 

1.2.3.6、applicationContext.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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
         https://www.springframework.org/schema/beans/spring-beans.xsd">
 
     <import resource="classpath:springmvc-servlet.xml"/>
     <import resource="classpath:springmvc-service.xml"/>
     <import resource="classpath:springmvc-dao.xml"/>
 
     <bean id="userMapper" class="com.gzk.dao.UserMapperImpl"/>
 
 </beans>

 

 

 

posted @ 2020-08-08 14:55  沉默非沉默  阅读(152)  评论(0)    收藏  举报