1.創(chuàng)建Java Web項(xiàng)目




配置文件
1.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"
? ? ? 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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
? ? <!--1.掃描包-->
? ? <context:component-scan base-package="com.swjd.*"/>
? ? <!--2.加載外部數(shù)據(jù)庫配置文件-->
? ? <context:property-placeholder location="classpath:jdbc.properties"/>
? ? <!--3.配置德魯伊連接池-->
? ? <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
? ? ? ? <property name="driverClassName" value="${jdbc.driver}"/>
? ? ? ? <property name="username" value="${jdbc.user}"/>
? ? ? ? <property name="password" value="${jdbc.pwd}"/>
? ? ? ? <property name="url" value="${jdbc.url}"/>
? ? </bean>
? ? <!--4.整合mybatis框架(獲取mybatis的工廠)-->
? ? <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
? ? ? ? <!--(1)配置數(shù)據(jù)源-->
? ? ? ? <property name="dataSource" ref="ds"/>
? ? ? ? <!--(2)指定mybatis映射文件的位置(mapper.xml)-->
? ? ? ? <property name="mapperLocations" value="classpath:com/swjd/mapper/*.xml"/>
? ? ? ? <!--(3)配置實(shí)體類別名-->
? ? ? ? <property name="typeAliasesPackage" value="com.swjd.bean"/>
? ? </bean>
? ? <!--5.配置mybatis接口層,自動(dòng)生成實(shí)體類-->
? ? <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
? ? ? ? <property name="basePackage" value="com.swjd.mapper"/>
? ? </bean>
? ? <!--6.配置聲明式事務(wù)-->
? ? <!--(1)配置平臺(tái)-->
? ? <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
? ? ? ? <property name="dataSource" ref="ds"/>
? ? </bean>
? ? <!--(2)開啟注解-->
? ? <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
2.jdbc.properties
jdbc.user=root
jdbc.pwd=123456
jdbc.url=jdbc:mysql://localhost:3306/bookdb?CharacterEncoding=utf-8&useUnicode=true
jdbc.driver=com.mysql.jdbc.Driver
3.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: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
? ? ? https://www.springframework.org/schema/context/spring-context.xsd
? ? ? http://www.alibaba.com/schema/stat
? ? ? http://www.alibaba.com/schema/stat.xsd
? ? ? http://www.springframework.org/schema/mvc
? ? ? https://www.springframework.org/schema/mvc/spring-mvc.xsd">
? ? <!--1.掃描,只負(fù)責(zé)controller-->
? ? <context:component-scan base-package="com.swjd.contorller"/>
? ? <!--2.配置視圖解析器-->
? ? <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
? ? ? ? <!--前綴-->
? ? ? ? <property name="prefix" value="/WEB-INF/jsp/"/>
? ? ? ? <!--后綴-->
? ? ? ? <property name="suffix" value=".jsp"/>
? ? </bean>
? ? <!--3.開啟用注解注冊(cè)springmvc組件-->
? ? <mvc:annotation-driven/>
? ? <!--4.放行靜態(tài)資源-->
? ? <mvc:default-servlet-handler/>
? ? <!--5.配置攔截器(根據(jù)實(shí)際情況來寫)-->
<!--? ? <mvc:interceptors>-->
<!--? ? ? ? <mvc:interceptor>-->
<!--? ? ? ? ? ? <!–攔截哪些請(qǐng)求(/**表示所有請(qǐng)求)–>-->
<!--? ? ? ? ? ? <mvc:mapping path="/**"/>-->
<!--? ? ? ? ? ? <!–攔截器有可能會(huì)攔截掉靜態(tài)資源,所以需要放行–>-->
<!--? ? ? ? ? ? <mvc:exclude-mapping path="/**/*.js"/>-->
<!--? ? ? ? ? ? <!–把自定義攔截器創(chuàng)建出來–>-->
<!--? ? ? ? ? ? <bean id="LoginInterceptor" class="com.swjd.Interceptor.LoginInterceptor"/>-->
<!--? ? ? ? </mvc:interceptor>-->
<!--? ? </mvc:interceptors>-->
</beans>
4.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ? xmlns="http://java.sun.com/xml/ns/javaee"
? ? ? ? xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
? ? ? ? http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!--? ? 歡迎頁面-->
? ? <welcome-file-list>
? ? ? ? <welcome-file>/Login.jsp</welcome-file>
? ? </welcome-file-list>
? ? <display-name>Archetype Created Web Application</display-name>
? ? <!--1.指定出spring容器位置:applicationContext.xml-->
? ? <context-param>
? ? ? ? <param-name>contextConfigLocation</param-name>
? ? ? ? <param-value>classpath:applicationContext.xml</param-value>
? ? </context-param>
? ? <!--2.過濾器:解決post亂碼-->
? ? <filter>
? ? ? ? <filter-name>CharacterEncodingFilter</filter-name>
? ? ? ? <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
? ? ? ? <!-- 設(shè)置過濾器中的屬性值 -->
? ? ? ? <init-param>
? ? ? ? ? ? <param-name>encoding</param-name>
? ? ? ? ? ? <param-value>UTF-8</param-value>
? ? ? ? </init-param>
? ? ? ? <!-- 啟動(dòng)過濾器 -->
? ? ? ? <init-param>
? ? ? ? ? ? <param-name>forceEncoding</param-name>
? ? ? ? ? ? <param-value>true</param-value>
? ? ? ? </init-param>
? ? </filter>
? ? <!-- 過濾所有請(qǐng)求 -->
? ? <filter-mapping>
? ? ? ? <filter-name>CharacterEncodingFilter</filter-name>
? ? ? ? <url-pattern>/*</url-pattern>
? ? </filter-mapping>
? ? <!--3.配置監(jiān)聽器:啟動(dòng)出spring容器,一旦web項(xiàng)目啟動(dòng),spring容器就自動(dòng)啟動(dòng)-->
? ? <listener>
? ? ? ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
? ? </listener>
? ? <!--4.配置核心控制器(前端控制器)-->
? ? <servlet>
? ? ? ? <servlet-name>DispatcherServlet</servlet-name>
? ? ? ? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
? ? ? ? <!--加載springmvc配置文件-->
? ? ? ? <init-param>
? ? ? ? ? ? <param-name>contextConfigLocation</param-name>
? ? ? ? ? ? <param-value>classpath:spring-mvc.xml</param-value>
? ? ? ? </init-param>
? ? ? ? <!--指定核心控制器的創(chuàng)建時(shí)間-->
? ? ? ? <load-on-startup>1</load-on-startup>
? ? </servlet>
? ? <servlet-mapping>
? ? ? ? <servlet-name>DispatcherServlet</servlet-name>
? ? ? ? <url-pattern>/</url-pattern>
? ? </servlet-mapping>
</web-app>
5.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.swjd.SSM001</groupId>
? ? <artifactId>BookSSM</artifactId>
? ? <version>1.0-SNAPSHOT</version>
? ? <packaging>war</packaging>
? ? <name>SSM001 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>
? ? ? ? <!--集中定義版本號(hào)-->
? ? </properties>
? ? <dependencies>
? ? ? ? <!-- myBatis-plus -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.baomidou</groupId>
? ? ? ? ? ? <artifactId>mybatis-plus</artifactId>
? ? ? ? ? ? <version>3.1.1</version>
? ? ? ? </dependency>
? ? ? ? <!--Junit-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>junit</groupId>
? ? ? ? ? ? <artifactId>junit</artifactId>
? ? ? ? ? ? <version>4.12</version>
? ? ? ? </dependency>
? ? ? ? <!--數(shù)據(jù)庫驅(qū)動(dòng)-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>mysql</groupId>
? ? ? ? ? ? <artifactId>mysql-connector-java</artifactId>
? ? ? ? ? ? <version>5.1.47</version>
? ? ? ? </dependency>
? ? ? ? <!-- 數(shù)據(jù)庫連接池 -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.alibaba</groupId>
? ? ? ? ? ? <artifactId>druid</artifactId>
? ? ? ? ? ? <version>1.1.10</version>
? ? ? ? </dependency>
? ? ? ? <!--Servlet - JSP -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>javax.servlet</groupId>
? ? ? ? ? ? <artifactId>servlet-api</artifactId>
? ? ? ? ? ? <version>2.5</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>javax.servlet.jsp</groupId>
? ? ? ? ? ? <artifactId>jsp-api</artifactId>
? ? ? ? ? ? <version>2.2</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>javax.servlet</groupId>
? ? ? ? ? ? <artifactId>jstl</artifactId>
? ? ? ? ? ? <version>1.2</version>
? ? ? ? </dependency>
? ? ? ? <!--Mybatis-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.mybatis</groupId>
? ? ? ? ? ? <artifactId>mybatis</artifactId>
? ? ? ? ? ? <version>3.5.2</version>
? ? ? ? </dependency>
? ? ? ? <!--mybatis和spring整合包-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.mybatis</groupId>
? ? ? ? ? ? <artifactId>mybatis-spring</artifactId>
? ? ? ? ? ? <version>2.0.2</version>
? ? ? ? </dependency>
? ? ? ? <!--Spring-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework</groupId>
? ? ? ? ? ? <artifactId>spring-webmvc</artifactId>
? ? ? ? ? ? <version>5.1.9.RELEASE</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework</groupId>
? ? ? ? ? ? <artifactId>spring-jdbc</artifactId>
? ? ? ? ? ? <version>5.1.9.RELEASE</version>
? ? ? ? </dependency>
? ? ? ? <!-- 為了方便進(jìn)行單元測(cè)試,添加spring-test包 -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework</groupId>
? ? ? ? ? ? <artifactId>spring-test</artifactId>
? ? ? ? ? ? <version>4.3.7.RELEASE</version>
? ? ? ? </dependency>
? ? </dependencies>
? ? <build>
? ? ? ? <finalName>SSM001</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>
? ? ? ? <!--編譯的時(shí)候同時(shí)也把包下面的xml文件同時(shí)編譯進(jìn)去-->
? ? ? ? <resources>
? ? ? ? ? ? <resource>
? ? ? ? ? ? ? ? <directory>src/main/java</directory>
? ? ? ? ? ? ? ? <includes>
? ? ? ? ? ? ? ? ? ? <include>**/*.xml</include>
? ? ? ? ? ? ? ? </includes>
? ? ? ? ? ? </resource>
? ? ? ? </resources>
? ? </build>
</project>
編寫java代碼





