maven中常用的依賴
阿新 • • 發佈:2018-11-13
常用依賴,並且管理起來
Q1:父模組
<?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.zhangyuwei.dependfather</groupId> <artifactId>DependFather</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>Depend-dao</module> <module>Depend-entities</module> <module>Depend-ui</module> <module>Depend-common</module> <module>Depend-service</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--這裡的名稱隨便定義,下面匯入依賴時進行引入即可--> <!--<junits.version>4.11</junits.version>--> <Depend-dao.version>1.0-SNAPSHOT</Depend-dao.version> <Depend-entities.version>1.0-SNAPSHOT</Depend-entities.version> <Depend-common.version>1.0-SNAPSHOT</Depend-common.version> <Depend-service.version>1.0-SNAPSHOT</Depend-service.version> <jackson.version>2.7.4</jackson.version> <commons-lang3.version>3.4</commons-lang3.version> <log4j.version>2.6.1</log4j.version> <junit.version>4.12</junit.version> <spring.version>4.3.0.RELEASE</spring.version> <aspectjweaver.version>1.8.9</aspectjweaver.version> <cglib.version>3.2.4</cglib.version> <spring-mybatis.version>1.3.0</spring-mybatis.version> <mysql-connector-java.version>8.0.11</mysql-connector-java.version> <mybatis.version>3.4.6</mybatis.version> <c3p0.version>0.9.5.2</c3p0.version> <poi.version>3.11</poi.version> <javax.servlet-api.version>3.0.1</javax.servlet-api.version> <jstl.version>1.2</jstl.version> <jsp-api.version>2.1</jsp-api.version> <commons-io.version>2.4</commons-io.version> <commons-fileupload.version>1.3.1</commons-fileupload.version> <json-simple.version>1.1.1</json-simple.version> <standard.version>1.1.2</standard.version> <spring-test.version>4.3.18.RELEASE</spring-test.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>com.zhangyuwei.dependfather</groupId> <artifactId>Depend-common</artifactId> <version>${Depend-common.version}</version> </dependency> <dependency> <groupId>com.zhangyuwei.dependfather</groupId> <artifactId>Depend-entities</artifactId> <version>${Depend-entities.version}</version> </dependency> <dependency> <groupId>com.zhangyuwei.dependfather</groupId> <artifactId>Depend-dao</artifactId> <version>${Depend-dao.version}</version> </dependency> <dependency> <groupId>com.zhangyuwei.dependfather</groupId> <artifactId>Depend-service</artifactId> <version>${Depend-service.version}</version> </dependency> <!--common模組--> <!--jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <!--apache-commons--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <!--log4j日誌包 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j.version}</version> </dependency> <!-- JUnit單元測試工具 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-test.version}</version> </dependency> <!--Spring框架核心庫 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- aspectJ AOP 織入器 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectjweaver.version}</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>${cglib.version}</version> </dependency> <!--mybatis-spring介面卡 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${spring-mybatis.version}</version> </dependency> <!--Spring java資料庫訪問包,在本例中主要用於提供資料來源 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!--mysql資料庫驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector-java.version}</version> </dependency> <!-- mybatis ORM框架 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!--c3p0 連線池 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>${c3p0.version}</version> </dependency> <!--Excel匯出--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${poi.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${poi.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>${poi.version}</version> </dependency> <!-- Servlet核心包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${javax.servlet-api.version}</version> <scope>provided</scope> </dependency> <!-- JSTL --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <!--JSP --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <!--富文字--> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>${json-simple.version}</version> </dependency> <!--standard--> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>${standard.version}</version> </dependency> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
Q2:common模組
<?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"> <parent> <artifactId>DependFather</artifactId> <groupId>com.zhangyuwei.dependfather</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Depend-common</artifactId> <dependencies> <!--jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </dependency> <!--apache-commons--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> </dependencies> </project>
Q3:dao模組
<?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"> <parent> <artifactId>DependFather</artifactId> <groupId>com.zhangyuwei.dependfather</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Depend-dao</artifactId> <dependencies> <dependency> <groupId>com.zhangyuwei.dependfather</groupId> <artifactId>Depend-common</artifactId> </dependency> <dependency> <groupId>com.zhangyuwei.dependfather</groupId> <artifactId>Depend-entities</artifactId> </dependency> <!--log4j日誌包 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> </dependency> <!-- JUnit單元測試工具 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <!--Spring框架核心庫 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <!-- aspectJ AOP 織入器 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> </dependency> <!--mybatis-spring介面卡 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency> <!--Spring java資料庫訪問包,在本例中主要用於提供資料來源 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <!--mysql資料庫驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- mybatis ORM框架 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <!--c3p0 連線池 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> </dependency> <!--此依賴可以使得伺服器出現RequestMapping--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> </dependencies> </project>
Q4:services模組
<?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"> <parent> <artifactId>DependFather</artifactId> <groupId>com.zhangyuwei.dependfather</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Depend-service</artifactId> <dependencies> <dependency> <groupId>com.zhangyuwei.dependfather</groupId> <artifactId>Depend-dao</artifactId> </dependency> </dependencies> </project>
Q5:ui模組
<?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"> <parent> <artifactId>DependFather</artifactId> <groupId>com.zhangyuwei.dependfather</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Depend-ui</artifactId> <packaging>war</packaging> <name>Depend-ui 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>com.zhangyuwei.dependfather</groupId> <artifactId>Depend-service</artifactId> </dependency> <!-- Servlet核心包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <!-- JSTL --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--JSP --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <scope>provided</scope> </dependency> <!--excel匯出--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> </dependency> </dependencies> <build> <finalName>Depend-ui</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.0.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.7.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.0</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>
Q6:GoodMapper.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.zhangyuwei.depend.mapper.IGoodDao"><!--介面名--> <!--查詢所有的商品--> <!--id應該是介面中的方法,結果型別如沒有配置別名則應該使用全名稱 --> <select id="selectAllGood" resultType="Good"> select *from good </select> <!--新增商品--> <insert id="insertGood" parameterType="Good"> insert into good(gName,gPrice,gPicture) values(#{gname},#{gprice},#{gpicture}) </insert> <!--修改商品--> <update id="updateGood" parameterType="Good"> update good set gName=#{gname},gPrice=#{gprice},gPicture=#{gpicture} where gId=#{gid} </update> <!--刪除商品--> <delete id="deleteGood" parameterType="int"> delete from good where gId=#{gid} </delete> <!--查詢商品數量--> <select id="selectGoodCount" resultType="int"> select count(*)from good </select> <!--查詢商品數量並帶有查詢--> <select id="selectGoodCountMoreIf" resultType="int"> select count(*)from good <where> <if test="gname !=null and gname !=''"> gName like '%${gname}%' </if> <if test="gprice !=null and gprice !=''"> and gprice=#{gprice} </if> </where> </select> <!--根據編號進行查詢--> <select id="selectGoodById" parameterType="int" resultType="Good"> select *from good where gId=#{gid} </select> <!--分頁--><!--這裡可以根據錯誤提示更改引數名--> <select id="selectGoodPage" resultType="Good"> select *from good limit ${param1},${param2} </select> <!--分頁與多條件查詢的組合--> <select id="selectGoodMoreIfPage" resultType="Good"> select *from good <where> <if test="gname !=null and gname !=''"> gName like '%${gname}%' </if> <if test="gprice !=null and gprice !=''"> and gprice=#{gprice} </if> </where> limit ${page},${limit} </select> <!--分頁與多條件查詢的組合--> <select id="selectGoodMoreIfPage2" resultType="Good"> select *from good limit ${page},${limit} </select> <!--多條件查詢--> <select id="selectGoodMoreIf" resultType="Good"> select *from good <where> <if test="gname !=null and gname !=''"> gName like '%${gname}%' </if> <if test="gid !=null and gid !=''"> and gId=#{gid} </if> <if test="gprice !=null and gprice !=''"> and gprice=#{gprice} </if> <if test="gpicture !=null and gpicture !=''"> and gpicture=#{gpicture} </if> </where> </select> <!--批量刪除--> <delete id="deleteGoodMore" parameterType="int"> delete from good <foreach collection="list" open=" where gId in(" separator="," close=")" item="gid"> #{gid} </foreach> </delete> </mapper>
Q7:applicationiContext.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!--1 引入屬性檔案,在配置中佔位使用 --> <context:property-placeholder location="classpath*:dbconfig.properties" /> <!--2 配置C3P0資料來源 --> <!--連線池,提高資料庫的效能,產生更多的併發連線數--> <!--這個來自c3p0包--> <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!--驅動類名 --> <property name="driverClass" value="${mysql.driver}" /> <!-- url --> <property name="jdbcUrl" value="${mysql.url}" /> <!-- 使用者名稱 --> <property name="user" value="${mysql.uid}" /> <!-- 密碼 --> <property name="password" value="${mysql.pwd}" /> <!-- 當連線池中的連線耗盡的時候c3p0一次同時獲取的連線數 --> <property name="acquireIncrement" value="${c3p0.acquireIncrement}"></property> <!-- 初始連線池大小 --> <property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property> <!-- 連線池中連線最小個數 --> <property name="minPoolSize" value="${c3p0.minPoolSize}"></property> <!-- 連線池中連線最大個數 --> <property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property> </bean> <!--3 會話工廠bean sqlSessionFactoryBean --> <!--以前的話,我們需要自己去構建會話工廠,現在通過這個,會自動構建--> <!--這裡面的名稱也就是它所帶方法裡面的屬性--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:configLocation="mybatisConf.xml" > <!-- 資料來源 --> <property name="dataSource" ref="datasource"></property> <!-- 別名 --> <property name="typeAliasesPackage" value="com.zhangyuwei.depend.entities"></property> <!-- sql對映檔案路徑 --> <property name="mapperLocations" value="classpath*:Mapper/*Mapper.xml"></property> <!--<property name="configLocation" value="mybatisConf.xml"></property>--> </bean> <!--4 自動掃描物件關係對映 --> <!--拿到會話工廠,實現與介面之間的對映--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--指定會話工廠,如果當前上下文中只定義了一個則該屬性可省去 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <!-- 指定要自動掃描介面的基礎包,實現介面 --> <property name="basePackage" value="com.zhangyuwei.depend.mapper"></property> </bean> <!--5 宣告式事務管理 --> <!--定義事物管理器,由spring管理事務 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="datasource"></property> </bean> <!--支援註解驅動的事務管理,指定事務管理器 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!--6 容器自動掃描IOC元件 --> <!--這個地方不太瞭解到底知道哪裡?--> <!--這裡放你要自動裝配的物件--> <context:component-scan base-package="com.zhangyuwei.depend.dao"></context:component-scan> <!--7 aspectj支援自動代理實現AOP功能 --> <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> </beans>View Code
Q8:dbconfig.properties
#mysql jdbc mysql.driver=com.mysql.jdbc.Driver mysql.url=jdbc:mysql://localhost:3306/GoodDB?serverTimezone=GMT%2B8 mysql.uid=root mysql.pwd=caoyu3520563253 c3p0.acquireIncrement=5 c3p0.initialPoolSize=10 c3p0.minPoolSize=5 c3p0.maxPoolSize=20View Code
Q9:mybatisConf.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> <!--<!–匯入dbconfig.properties檔案中的所有key-value資料–> <properties resource="dbconfig.properties"></properties>--> <!--設定--> <settings> <!--設定日誌檔案--> <setting name="cacheEnabled" value="true"></setting> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <!--<!–別名–> <typeAliases> <package name="com.zhangyuwei.depend.entities" ></package> </typeAliases> <!–環境配置,default為預設選擇的環境–> <environments default="development"> <!–開發–> <environment id="development"> <!–事務管理–> <transactionManager type="JDBC"/> <!–連線池–> <dataSource type="POOLED"> <!–引用屬性${mysql.driver}–> <property name="driver" value="${mysql.DBDRIVER}"/> <property name="url" value="${mysql.DBURL}"/> <property name="username" value="${mysql.DBUSER}"/> <property name="password" value="${mysql.DBPWD}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="Mapper/GoodMapper.xml"/> </mappers>--> </configuration>View Code
Q10: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"> <!--<listener> <description>Spring容器載入監聽器</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>--> <context-param> <description>設定Spring載入時的配置檔案位置,預設位置在WEB-INF/lib目錄下</description> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> <!--Spring MVC 前置Servlet,中心控制器 --> <servlet> <!--名稱 --> <servlet-name>springmvc</servlet-name> <!-- Servlet類 --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!--SpringMVC配置引數檔案的位置 --> <param-name>contextConfigLocation</param-name> <!--預設名稱為ServletName-servlet.xml ,預設存放在resources目錄下--> <!--spring-Mvc配置--> <param-value>classpath*:springmvc-servlet.xml</param-value> </init-param> <!-- 啟動順序,數字越小,啟動越早 --> <load-on-startup>1</load-on-startup> <!--Servlet3.0以上檔案上傳配置 --> <multipart-config> <max-file-size>5242880</max-file-size><!--上傳單個檔案的最大限制5MB --> <max-request-size>20971520</max-request-size><!--請求的最大限制20MB,一次上傳多個檔案時一共的大小 --> <file-size-threshold>0</file-size-threshold><!--當檔案的大小超過臨界值時將寫入磁碟 --> </multipart-config> </servlet> <!--所有請求都會被springmvc攔截 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <!-- 配置編碼方式過濾器,注意一點:要配置在所有過濾器的前面 --> <filter> <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>View Code
Q11: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" 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 http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- 自動掃描包,實現支援註解的IOC --> <context:component-scan base-package="com.zhangyuwei.depend.controller" /> <!-- Spring MVC不處理靜態資源 --> <mvc:default-servlet-handler /> <!-- 支援mvc註解驅動 --><!--設定為true--> <mvc:annotation-driven enable-matrix-variables="true"/> <!-- 檢視解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 字首 --> <!--<property name="prefix" value="/WEB-INF/view/" />--> <property name="prefix" value="/WEB-INF/view/" /> <!-- 字尾 --> <!--<property name="suffix" value=".jsp" />--> <property name="suffix" value=".jsp" /> </bean> <!-- 配置對映媒體型別的策略 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> <property name="removeSemicolonContent" value="false" /> </bean> <!--檔案上傳--> <!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize" value="10485760000" /> <property name="maxInMemorySize" value="40960" /> </bean>--> <!--檔案上傳解析器3.0以上 --> <bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"> </bean> </beans>View Code
Q12:servlet
package com.zhangyuwei.depend.controller; import com.zhangyuwei.depend.common.util.R; import com.zhangyuwei.depend.entities.Good; import com.zhangyuwei.depend.entities.UpLoad; import com.zhangyuwei.depend.mapper.IGoodDao; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.math.BigDecimal; import java.util.HashMap; import java.util.List; import java.util.Map; @Controller @RequestMapping("/GoodServlet") public class goodServlet { /*IGoodDao ia=new goodDao();*/ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); IGoodDao dao=ctx.getBean(IGoodDao.class); @RequestMapping("/test") public String test(Model model){ model.addAttribute("message","我是一個測試類"); return "index"; } /*查詢所有商品*/ @RequestMapping("/selectAllGood") @ResponseBody public List<Good> selectAllGood(){ List<Good> entity=dao.selectAllGood(); return entity; } /*新增商品*/ @RequestMapping(value = "/insertGood",method = RequestMethod.POST) @ResponseBody public void insertGood(@RequestBody List<Good> goodList, HttpServletResponse response) throws IOException { /*解決服務端的亂碼問題*/ response.setCharacterEncoding("utf-8"); Good good=new Good(); good.setGname(goodList.get(0).getGname()); good.setGprice(goodList.get(0).getGprice()); good.setGpicture(goodList.get(0).getGpicture()); int row=dao.insertGood(good); if(row>0){ response.getWriter().print("新增成功!"); }else{ response.getWriter().print("新增失敗!"); } } /*修改商品*/ @RequestMapping(value = "/updateGood",method = RequestMethod.POST) @ResponseBody public void updateGood(@RequestBody List<Good> goodList,HttpServletResponse response) throws IOException { /*解決服務端的亂碼問題*/ response.setCharacterEncoding("utf-8"); Good good=new Good(); good.setGname(goodList.get(0).getGname()); good.setGprice(goodList.get(0).getGprice()); good.setGpicture(goodList.get(0).getGpicture()); good.setGid(goodList.get(0).getGid()); int row=dao.updateGood(good); if(row>0){ response.getWriter().print("修改成功!"); }else{ response.getWriter().print("修改失敗!"); } } /*刪除商品*/ @RequestMapping(value = "/deleteGood",method = RequestMethod.POST) @ResponseBody public R deleteGood(/*@RequestBody Integer gid,*/HttpServletResponse response,HttpServletRequest request){ /*解決服務端的亂碼問題*/ response.setCharacterEncoding("utf-8"); int gid=Integer.parseInt(request.getParameter("gId")); int row=dao.deleteGood(gid); if(row>0) { /*try { response.getWriter().print(R.ok("刪除成功")); } catch (IOException e) { e.printStackTrace(); } //response.getWriter().print("刪除成功!"); }else{ try { response.getWriter().print("刪除失敗!"); } catch (IOException e) { e.printStackTrace(); } }*/ return R.ok("刪除成功!"); } return R.error("刪除失敗!"); } /*多項刪除*/ @RequestMapping("/deleteGoodMore") @ResponseBody public void deleteGoodMore(@RequestBody List<Integer> integerList,HttpServletResponse response) throws IOException { /*解決服務端的亂碼問題*/ response.setCharacterEncoding("utf-8"); int row=dao.deleteGoodMore(integerList); if(row==integerList.size()){ response.getWriter().print("刪除成功!"); }else{ response.getWriter().print("刪除失敗!"); } } /*查詢商品數量*/ @RequestMapping("/selectGoodCount") @ResponseBody public int selectGoodCount(){ int count=dao.selectGoodCount(); return count; } /*查詢商品數量根據查詢條件*/ @RequestMapping("/selectGoodCountMoreIf") @ResponseBody public int selectGoodCountMoreIf(@RequestBody List<Object> objectList){ Map<String,Object> map=new HashMap<>(); map.put("gname",objectList.get(0)); map.put("gprice",objectList.get(1)); int count=dao.selectGoodCountMoreIf(map); return count; } /*分頁與查詢之間的組合*/ @RequestMapping(value = "/selectGoodMoreIfPage",method = RequestMethod.POST) @ResponseBody public List<Good> selectGoodMoreIfPage(@RequestBody List<Object> integerList){ Map<String,Object> map=new HashMap<>(); Object page=integerList.get(0);Object limit=integerList.get(1); int pages=(int)page;int limits=(int)limit; map.put("page",(pages-1)*limits); map.put("limit",limits); map.put("gname",integerList.get(2)); map.put("gprice",integerList.get(3)); List<Good> entity=dao.selectGoodMoreIfPage(map); return entity; } /*分頁*/ @RequestMapping("/selectGoodPage") @ResponseBody public List<Good> selectGoodPage(@RequestBody List<Integer> integerList){ List<Good> entity=dao.selectGoodPage((integerList.get(0)-1)*(integerList.get(1)),integerList.get(1)); return entity; } /*多條件查詢*/ @RequestMapping(value = "/selectGoodMoreIf",method = RequestMethod.POST) @ResponseBody public List<Good> selectGoodMoreIf(@RequestBody List<Good> goodList){ Map<String,Object> map=new HashMap<String, Object>(); map.put("gname",goodList.get(0).getGname()); map.put("gprice",goodList.get(0).getGprice()); List<Good> entity=dao.selectGoodMoreIf(map); return entity; } /*檔案上傳*/ @RequestMapping(value = "fileSave",method = RequestMethod.POST) @ResponseBody public UpLoad fileSave(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception{ //檔案存放的位置 String path=request.getServletContext().getRealPath("/image"); File fi=new File(path); if(!fi.exists()){ fi.mkdir(); } File tempFile=new File(path, file.getOriginalFilename()); file.transferTo(tempFile); System.out.println(tempFile.getName()); UpLoad upLoad=new UpLoad(); upLoad.setCode(0); upLoad.setMsg("上傳成功!"); upLoad.setData(tempFile.getName()); //response.getWriter().print(upLoad); return upLoad; } /*匯出資料到Excel表格*/ @RequestMapping(value = "/joinxml",method = RequestMethod.GET) @ResponseBody public void joinXml(HttpServletResponse response) throws IOException { //資料的來源 List<Good> entity=dao.selectAllGood(); //設定標題 String head = "商品資訊詳細展示"; //設定表頭行 String[] headrow = {"編號", "名稱", "價格","圖片路徑"}; if (null != entity && entity.size() > 0) { String fileName = "商品資訊.xls";//定義匯出頭 response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1")); //設定檔案頭編碼格式 response.setContentType("APPLICATION/OCTET-STREAM;charset=UTF-8");//設定型別 response.setHeader("Cache-Control", "no-cache");//設定頭 response.setDateHeader("Expires", 0);//設定日期頭 //建立工作簿HSSFWorkbook 物件 HSSFWorkbook book = new HSSFWorkbook(); // 第二步,在webbook中新增一個sheet,對應Excel檔案中的sheet HSSFSheet sheet = book.createSheet(); // 第三步,在sheet中新增表頭第0行,注意老版本poi對Excel的行數列數有限制short HSSFRow row = sheet.createRow(0); //由工作簿建立表HSSFSheet物件 CellStyle cellStyle = book.createCellStyle(); cellStyle.setDataFormat(book.createDataFormat().getFormat("yyyy-MM-dd")); //設定表頭 HSSFCell cell = row.createCell((short) 0); cell.setCellValue(head); row = sheet.createRow(1); for (int i = 0; i < headrow.length; i++) { cell = row.createCell((short) i); cell.setCellValue(headrow[i]); } for (int i = 0; i < entity.size(); i++) { //實體類物件 row = sheet.createRow((i + 2)); BigDecimal money=entity.get(i).getGprice(); row.createCell((short) 0).setCellValue(entity.get(i).getGid()); row.createCell((short) 1).setCellValue(entity.get(i).getGname()); row.createCell((short) 2).setCellValue(money+""); row.createCell((short) 3).setCellValue(entity.get(i).getGpicture()); } //寫出流 重新整理緩衝流 關閉流物件 book.write(response.getOutputStream()); response.getOutputStream().flush(); response.getOutputStream().close(); } } /** * 匯入資料到Excel表格 *excel表格的形式為xlsx * @throws IOException */ @RequestMapping(value = "/outxml",method = RequestMethod.POST) @SuppressWarnings("resource") public String excels(MultipartFile files, HttpServletResponse response, HttpServletRequest request) throws IOException, ServletException { //檔案存放的位置 String path=request.getSession().getServletContext().getRealPath("/files2"); File f=new File(path); if(!f.exists()){ f.mkdir(); } //儲存檔案3 File tempFile=new File(path, files.getOriginalFilename()); files.transferTo(tempFile);//把檔案從記憶體存到磁碟中 System.out.println(path+"\\"+files.getOriginalFilename()); //Excel匯入資料 InputStream is = new FileInputStream(path+"\\"+files.getOriginalFilename()); XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is); Good emp = new Good(); // 迴圈工作表Sheet int row=0;int len=0; for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) { XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet); if (xssfSheet == null) { continue; } // 迴圈行Row for (int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++) { XSSFRow xssfRow = xssfSheet.getRow(rowNum); if (xssfRow != null) { len++; XSSFCell gname = xssfRow.getCell(0); XSSFCell gprice = xssfRow.getCell(1); XSSFCell gpicture = xssfRow.getCell(2); /*String vaId=getValue(eid); String[] arr=vaId.split(".");*/ //double vaId=Double.parseDouble(eid.toString()); //int vaintId=(int)vaId; Double valMoney=Double.parseDouble(getValue(gprice)); BigDecimal bMoney=new BigDecimal(valMoney); emp.setGname(getValue(gname)); emp.setGprice(bMoney); emp.setGpicture(getValue(gpicture)); if(dao.insertGood(emp)>0){ row++; } } } } if(row==len){ try { response.getWriter().print("新增成功!"); } catch (IOException es) { es.printStackTrace(); } }else { try { response.getWriter().print("新增失敗!"); } catch (IOException es) { es.printStackTrace(); } } //request.getRequestDispatcher("index.html").forward(request, response); return "redirect:http://localhost:8080/"; } @SuppressWarnings("static-access") private String getValue(XSSFCell xssfCell) { if (xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN) { // 返回布林型別的值 return String.valueOf(xssfCell.getBooleanCellValue()); } else if (xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC) { // 返回數值型別的值 return String.valueOf(xssfCell.getNumericCellValue()); } else { //