經常使用依賴,而且管理起來css
Q1:父模塊html
<?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模塊java
<?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模塊mysql
<?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模塊jquery
<?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模塊web
<?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.xmlajax
<?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.xmlspring
<?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>
Q8:dbconfig.propertiessql
#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=20
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>
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>
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>
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 { // 返回字符串類型的值 return String.valueOf(xssfCell.getStringCellValue()); } } }
Q13:index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="js/layui/css/layui.css"> </head> <style> table,tr,td{border-collapse: collapse;border:1px solid red;} table{width: 100%;text-align: center;} #test10{background-size: 100% 100%;background-repeat: no-repeat} </style> <body><p> <a href="GoodServlet/joinxml">導出xml</a> <form action="GoodServlet/outxml" method="post" enctype="multipart/form-data"> <input type="file" name="files"/> <button>導入xml</button> </form> </p> <!--分頁--> <div id="demo11"></div> <input type="text" placeholder="請輸入名稱" id="nameMoreIf"/> <input type="text" placeholder="請輸入價格" id="priceMoreIf"/> <input type="button" value="查詢" id="btnMoreIf"/> <table> <thead> <tr> <td><input type="checkbox" class="cheBoxMain"/></td> <td>編號</td> <td>名稱</td> <td>價格</td> <td>圖片</td> <td>操做</td> </tr> </thead> <tbody id="tbodyShow"> </tbody> </table> <input type="button" value="多項刪除" id="btnMoreDelete"/> <fieldset> <legend>數據操做</legend> <p> <label>名稱:</label> <input type="text" id="gname"/> </p> <p> <label>價格:</label> <input type="text" id="gprice"/> </p> <p> <label>圖片:</label> <div class="layui-upload-drag" id="test10"> <i class="layui-icon"></i> <p>點擊上傳,或將文件拖拽到此處</p> </div> </p> <p> <input type="button" value="添加" id="btnAdd"/> <input type="button" value="修改" id="btnUpdate"/> </p> </fieldset> <script src="js/jquery-1.11.3.js"></script> <script src="js/layui/layui.all.js"></script> <script> var gpicture="";var updateid=""; /*文件上傳*/ layui.use('upload', function() { var $ = layui.jquery , upload = layui.upload; upload.render({ elem: '#test10' ,url: 'GoodServlet/fileSave' ,method:'post' ,done: function(res){ $("#test10").css("backgroundImage","url(image/"+res.data+")"); gpicture=res.data; } }); }); /*分頁*/ var pages="";var limits=""; layui.use(['laypage', 'layer'], function() { var laypage = layui.laypage , layer = layui.layer; //分頁方法 function selectPage() { /*查詢全部*/ var number = ""; var names= $("#nameMoreIf").val(); var prices=$("#priceMoreIf").val(); var objectList=[]; objectList.push(names); objectList.push(prices); /*var objectList=new Array(); objectList.push({"gname":names}, {"gprice":prices});*/ $.ajax({ url:'GoodServlet/selectGoodCountMoreIf', type:'post', data:JSON.stringify(objectList), contentType:'application/json;charset=UTF-8', success:function (result) { number=result; /*分頁查詢*/ laypage.render({ elem: 'demo11' , count: number //數據總數 , limit: 4 , curr: pages , jump: function (objs) { pages = objs.curr; limits = objs.limit; var integerList=[]; integerList.push(pages); integerList.push(limits); integerList.push(names); integerList.push(prices); console.log(integerList); /*獲得返回結果*/ /*先清除數據*/ $("#tbodyShow tr").remove(); $.ajax({ type: 'post', url: 'GoodServlet/selectGoodMoreIfPage', data: JSON.stringify(integerList), contentType:'application/json;charset=UTF-8', beforeSend:function(){ console.log(integerList); }, success: function (result) { $.each(result,function (i,obj) { $("<tr/>").append($("<input type='checkbox' class='cheBoxSon' value='"+obj.gid+"'/>").data("obj",obj.gid)) .append($("<td/>").html(obj.gid)) .append($("<td/>").html(obj.gname)) .append($("<td/>").html(obj.gprice)) .append($("<td/>").html($("<img src='"+"image/"+obj.gpicture+"' width='26px' height='20px'/>")).data("obj",obj.gpicture)) .append($("<input type='button' value='刪除' class='btnDel'/>").data("obj",obj.gid)) .append($("<input type='button' value='編輯' class='btnEdit'/>").data("obj",obj)) .appendTo($("#tbodyShow")); }); } }); } }); } }); }; /*刪改查*/ var app={ /*select:function(){ $("#tbodyShow tr").remove(); $.ajax({ url:'GoodServlet/selectAllGood', type:'post', contentType:'application/json;charset=UTF-8', dataType:'json', success:function (result) { $.each(result,function (i,obj) { $("<tr/>").append($("<input type='checkbox' class='cheBoxSon' value='"+obj.gid+"'/>").data("obj",obj.gid)) .append($("<td/>").html(obj.gid)) .append($("<td/>").html(obj.gname)) .append($("<td/>").html(obj.gprice)) .append($("<td/>").html($("<img src='"+"image/"+obj.gpicture+"' width='26px' height='20px'/>")).data("obj",obj.gpicture)) .append($("<input type='button' value='刪除' class='btnDel'/>").data("obj",obj.gid)) .append($("<input type='button' value='編輯' class='btnEdit'/>").data("obj",obj)) .appendTo($("#tbodyShow")); }); } }); },*/ /*添加*/ insert:function(){ var goodList=new Array(); if($("#gname").val()=="" || $("#gprice").val()==""){ alert("名稱或價格不能爲空!"); return false; } goodList.push({ gname: $("#gname").val(), gprice:$("#gprice").val(), gpicture:gpicture }); $.ajax({ url:'GoodServlet/insertGood', type:'post', data: JSON.stringify(goodList), contentType:'application/json;charset=UTF-8', success:function (result) { alert(result);selectPage(); $("#gname").val("");$("#gprice").val("");gpicture=""; $("#test10").css("backgroundImage",""); } }); }, /*刪除*/ del:function(obj){ if(confirm("是否要刪除?")) { var gid = obj; $.ajax({ url: 'GoodServlet/deleteGood', type: 'post', //data: JSON.stringify(gid), data: {"gId": gid}, //contentType:'application/json;charset=UTF-8', success: function (result) { alert(result.msg); selectPage(); } }); } }, /*修改*/ update:function(){ var goodList=new Array(); if($("#gname").val()=="" || $("#gprice").val()==""){ alert("名稱或價格不能爲空!"); return false; } goodList.push({ gname: $("#gname").val(), gprice:$("#gprice").val(), gpicture:gpicture, gid:updateid }); $.ajax({ url:'GoodServlet/updateGood', type:'post', data: JSON.stringify(goodList), contentType:'application/json;charset=UTF-8', success:function (result) { alert(result);selectPage(); $("#gname").val("");$("#gprice").val("");gpicture=""; $("#test10").css("backgroundImage","");updateid=""; } }); }, /*多項刪除*/ deleteMore:function(){ if(confirm("是否要刪除?")) { var integerList = []; $(".cheBoxSon:checked").each(function () { integerList.push($(this).val()); }); $.ajax({ url: 'GoodServlet/deleteGoodMore', type: 'post', data: JSON.stringify(integerList), contentType: 'application/json;charset=UTF-8', success: function (result) { alert(result); selectPage(); } }); } }, /*點擊事件*/ clicks:function(){ $("#tbodyShow").on("click",".btnDel",function () { var gid=$(this).data("obj"); app.del(gid); }); $("#btnAdd").click(function () { app.insert(); }); $("#tbodyShow").on("click",".btnEdit",function () { var obj=$(this).data("obj"); updateid=obj.gid; $("#gname").val(obj.gname);$("#gprice").val(obj.gprice); $("#test10").css("backgroundImage","url(image/"+obj.gpicture+")"); gpicture=obj.gpicture; }); $("#btnUpdate").click(function () { app.update(); }); $(".cheBoxMain").click(function () { if($(".cheBoxMain").prop("checked")==true){ $(".cheBoxSon").prop("checked",true); }else if($(".cheBoxMain").prop("checked")==false){ $(".cheBoxSon").prop("checked",false); } }); $("#tbodyShow").on("click",".cheBoxSon",function () { if($(this).prop("checked")==false){ $(this).prop("checked",false); $(".cheBoxMain").prop("checked",false); }if($(this).prop("checked")==true){ $(this).prop("checked",true); }if(($(".cheBoxSon:checked").length)==($(".cheBoxSon").length)){ $(".cheBoxMain").prop("checked",true); } }); $("#btnMoreDelete").click(function () { app.deleteMore(); }); /*多條件查詢*/ $("#btnMoreIf").click(function () { /*var goodList=new Array(); goodList.push({ gname: $("#nameMoreIf").val(), gprice:$("#priceMoreIf").val() }); $.ajax({ url:'GoodServlet/selectGoodMoreIf', type:'post', data: JSON.stringify(goodList), contentType:'application/json;charset=UTF-8', success:function (result) { /!*先清除數據*!/ $("#tbodyShow tr").remove(); $.each(result,function (i,obj) { $("<tr/>").append($("<input type='checkbox' class='cheBoxSon' value='"+obj.gid+"'/>").data("obj",obj.gid)) .append($("<td/>").html(obj.gid)) .append($("<td/>").html(obj.gname)) .append($("<td/>").html(obj.gprice)) .append($("<td/>").html($("<img src='"+"image/"+obj.gpicture+"' width='26px' height='20px'/>")).data("obj",obj.gpicture)) .append($("<input type='button' value='刪除' class='btnDel'/>").data("obj",obj.gid)) .append($("<input type='button' value='編輯' class='btnEdit'/>").data("obj",obj)) .appendTo($("#tbodyShow")); }); $("#nameMoreIf").val("");$("#priceMoreIf").val(""); } });*/ selectPage(); }); }, /*初始化*/ init:function () { selectPage(); app.clicks(); } }; app.init(); }); </script> </body> </html>
Q14:不污染數據庫test
package com.zhangyuwei.depend.dao; import com.zhangyuwei.depend.entities.Good; import com.zhangyuwei.depend.entities.GoodPage; import com.zhangyuwei.depend.mapper.IGoodDao; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @ContextConfiguration(locations = { "classpath:applicationContext.xml"}) @RunWith(SpringJUnit4ClassRunner.class) @Transactional @Rollback public class goodDaoTestPro { @Autowired IGoodDao dao; @Test public void selectAllGood(){ System.out.println(dao.selectAllGood()); } @Test public void deleteGood(){ int gid=1; int row=dao.deleteGood(gid); Assert.assertEquals(1,row); } @Test public void selectGoodMoreIfPage(){ int page=1; int limit=10; Map<String,Object> map=new HashMap<String, Object>(); map.put("gname","益達尊享護齒裝草本40粒+冰檸40粒+西"); map.put("gprice",25.9); map.put("page",page); map.put("limit",limit); List<Good> entity=dao.selectGoodMoreIfPage(map); System.out.println(entity); } @Test public void selectGoodMoreIfPage2(){ List<GoodPage> pageList=new ArrayList<GoodPage>(); GoodPage gp=new GoodPage(); gp.setPage(1); gp.setLimit(10); pageList.add(gp); List<Good> entity=dao.selectGoodMoreIfPage2(pageList); System.out.println(entity); } @Test public void selectGoodCountMoreIf(){ Map<String,Object> map=new HashMap<String, Object>(); map.put("gname",""); map.put("gprice",""); int count=dao.selectGoodCountMoreIf(map); System.out.println(count); } }
Q15:接口IGoodDao
package com.zhangyuwei.depend.mapper; import com.zhangyuwei.depend.entities.Good; import com.zhangyuwei.depend.entities.GoodPage; import org.apache.ibatis.annotations.Param; import org.junit.Test; import java.util.List; import java.util.Map; public interface IGoodDao { /*查詢全部商品*/ List<Good> selectAllGood(); /*添加商品*/ int insertGood(Good entity); /*修改商品*/ int updateGood(Good entity); /*刪除商品*/ int deleteGood(int gid); /*查詢商品個數*/ int selectGoodCount(); /*查詢商品個數帶有條件*/ int selectGoodCountMoreIf(Map<String,Object> map); /*根據編號進行查詢*/ List<Good> selectGoodById(int gid); /*分頁與多查詢之間的組合*/ List<Good> selectGoodMoreIfPage(Map<String,Object> map); /*分頁與多查詢之間的組合*/ List<Good> selectGoodMoreIfPage2(List<GoodPage> listpage); /*分頁*/ List<Good> selectGoodPage(int param1,int param2); /*多條件查詢*/ List<Good> selectGoodMoreIf(Map<String,Object> map); /*批量刪除*/ int deleteGoodMore(List<Integer> entity); }