结构
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.storm</groupId>
<artifactId>storm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>storm</name>
<url>http://maven.apache.org</url>
<properties>
<!-- Spring版本号 -->
<spring.version>4.0.2.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<!-- Spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.6</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 引入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- 导入oracle包 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>14</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<!-- log end -->
<!-- 映入JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
<build>
<finalName>storm</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.8.v20150217</version>
<configuration>
<httpConnector>
<port>8090</port>
</httpConnector>
<stopKey>shutdown</stopKey>
<stopPort>9966</stopPort>
</configuration>
</plugin>
</plugins>
</build>
<packaging>war</packaging>
</project>
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"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!-- Spring和MyBatis的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- Spring MVC servlet -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 下面init-param是自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 此处可以配置成*.do,对应struts的后缀习惯 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
springmvc.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: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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描,使SpringMVC认为包下用了@Controller注解的类是控制器 -->
<context:component-scan base-package="cn.zhou.core.controller" />
<!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
<mvc:annotation-driven />
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<!-- JSON转换器 -->
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000"></property>
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960"></property>
<!-- 启用是为了推迟文件解析,以便捕获文件大小异常 -->
<property name="resolveLazily" value="true"></property>
</bean>
<!-- 配置ViewResolver。可用多个ViewResolver。使用order属性排序。 InternalResourceViewResolver
放在最后 -->
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1"></property>
<property name="mediaTypes">
<map>
<!-- 告诉视图解析器,返回的类型是json格式 -->
<entry key="json" value="application/json"></entry>
<entry key="xml" value="application/xml"></entry>
<entry key="htm" value="text/htm"></entry>
</map>
</property>
<property name="defaultViews">
<list>
<!-- ModelAndView里的数据变成JSON -->
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"></bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true"></property>
</bean>
<!-- 定义跳转的文件的前后缀,视图模式配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="cn.zhou" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<!-- 数据源配置, 使用应用中的DBCP数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="maxActive" value="${dbcp.maxActive}" />
<property name="maxIdle" value="${dbcp.maxIdle}" />
<property name="defaultAutoCommit" value="false" />
<!-- 连接Idle一个小时后超时 -->
<property name="timeBetweenEvictionRunsMillis" value="3600000" />
<property name="minEvictableIdleTimeMillis" value="3600000" />
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:cn/zhou/core/mapping/*/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.zhou.core.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
log4j.properties
#log4j.rootLogger=DEBUG, Console
#
##Console
#log4j.appender.Console=org.apache.log4j.ConsoleAppender
#log4j.appender.Console.layout=org.apache.log4j.PatternLayout
#log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#
#log4j.logger.java.sql.ResultSet=INFO
#log4j.logger.org.apache=INFO
#log4j.logger.java.sql.Connection=DEBUG
#log4j.logger.java.sql.Statement=DEBUG
#log4j.logger.java.sql.PreparedStatement=DEBUG
jdbc.properties
#\u8FDE\u63A5\u6570\u636E\u5E93
#jdbc.driver=net.sf.log4jdbc.DriverSpy
#jdbc.url=jdbc:log4jdbc:oracle:thin:@192.168.0.125:1521:xe
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=zhou
jdbc.password=zhou
#dbcp settings
dbcp.maxIdle=5
dbcp.maxActive=50
ProductMapper.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="cn.zhou.core.dao.product.ProductDao">
<resultMap id="product" type="cn.zhou.core.bean.product.Product">
<result property="id" column="id" />
<result property="no" column="no" />
<result property="name" column="name" />
<result property="weight" column="weight" />
<result property="isNew" column="is_new" />
<result property="isHot" column="is_hot" />
<result property="isCommend" column="is_commend" />
<result property="createTime" column="create_time" />
<result property="createUserId" column="create_user_id" />
<result property="checkTime" column="check_time" />
<result property="checkUserId" column="check_user_id" />
<result property="isShow" column="is_show" />
<result property="isDel" column="is_del" />
<result property="typeId" column="type_id" />
<result property="brandId" column="brand_id" />
<result property="keywords" column="keywords" />
<result property="sales" column="sales" />
<result property="description" column="description" />
<result property="packageList" column="package_list" />
<result property="feature" column="feature" />
<result property="color" column="color" />
<result property="size_num" column="size_num" />
</resultMap>
<!-- 根据主键查询 -->
<select id="getProductByKey" resultMap="product" parameterType="java.lang.Integer">
SELECT
id,no,name,weight,is_new,is_hot,is_commend,create_time,create_user_id,check_time,check_user_id,is_show,is_del,type_id,brand_id,keywords,sales,description,package_list,feature,color,size_num
FROM product
WHERE
id=#{id}
</select>
<!-- 添加 useGeneratedKeys="true" keyProperty="id" 自己生成key,key属性为Product对象的id属性,sql结果值通过produc-id
返回 -->
<insert id="addProduct" parameterType="cn.zhou.core.bean.product.Product">
INSERT INTO product
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="no != null">
no,
</if>
<if test="name != null">
name,
</if>
<if test="weight != null">
weight,
</if>
<if test="isNew != null">
is_new,
</if>
<if test="isHot != null">
is_hot,
</if>
<if test="isCommend != null">
is_commend,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="createUserId != null">
create_user_id,
</if>
<if test="checkTime != null">
check_time,
</if>
<if test="checkUserId != null">
check_user_id,
</if>
<if test="isShow != null">
is_show,
</if>
<if test="isDel != null">
is_del,
</if>
<if test="typeId != null">
type_id,
</if>
<if test="brandId != null">
brand_id,
</if>
<if test="keywords != null">
keywords,
</if>
<if test="sales != null">
sales,
</if>
<if test="description != null">
description,
</if>
<if test="packageList != null">
package_list,
</if>
<if test="feature != null">
feature,
</if>
<if test="color != null">
color,
</if>
<if test="size_num != null">
size_num
</if>
</trim>
VALUES
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id},
</if>
<if test="no != null">
#{no},
</if>
<if test="name != null">
#{name},
</if>
<if test="weight != null">
#{weight},
</if>
<if test="isNew != null">
#{isNew},
</if>
<if test="isHot != null">
#{isHot},
</if>
<if test="isCommend != null">
#{isCommend},
</if>
<if test="createTime != null">
#{createTime},
</if>
<if test="createUserId != null">
#{createUserId},
</if>
<if test="checkTime != null">
#{checkTime},
</if>
<if test="checkUserId != null">
#{checkUserId},
</if>
<if test="isShow != null">
#{isShow},
</if>
<if test="isDel != null">
#{isDel},
</if>
<if test="typeId != null">
#{typeId},
</if>
<if test="brandId != null">
#{brandId},
</if>
<if test="keywords != null">
#{keywords},
</if>
<if test="sales != null">
#{sales},
</if>
<if test="description != null">
#{description},
</if>
<if test="packageList != null">
#{packageList},
</if>
<if test="feature != null">
#{feature},
</if>
<if test="color != null">
#{color},
</if>
<if test="size_num != null">
#{size_num}
</if>
</trim>
</insert>
</mapper>
Product
package cn.zhou.core.bean.product;
import java.util.*;
import java.io.Serializable;
/**
*/
public class Product implements Serializable {
/**
* 序列化ID
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String no;//商品编号
private String name;//商品名称
private Double weight;//重量 单位:克
private Integer isNew ;//是否新品:0:旧品,1:新品
private Integer isHot ;//是否热销:0,否 1:是
private Integer isCommend ;//推荐 1推荐 0 不推荐
private Date createTime;//添加时间
private String createUserId;//添加人ID
private Date checkTime;//审核时间
private String checkUserId;//审核人ID
private Integer isShow = 0;//上下架:0否 1是,默认下架,这样代码是下架的可以不用设置
private Integer isDel = 1;//是否删除:0删除,1,没删除
private Integer typeId;//类型ID
private Integer brandId;//品牌ID
private String keywords;//检索关键词
private Integer sales;//销量
private String description;//商品描述
private String packageList;//包装清单
private String feature;//商品属性集
private String color;//颜色集 9,10,11,12
private String size_num;//尺寸集 S,L,XXL
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
public Integer getIsNew() {
return isNew;
}
public void setIsNew(Integer isNew) {
this.isNew = isNew;
}
public Integer getIsHot() {
return isHot;
}
public void setIsHot(Integer isHot) {
this.isHot = isHot;
}
public Integer getIsCommend() {
return isCommend;
}
public void setIsCommend(Integer isCommend) {
this.isCommend = isCommend;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getCreateUserId() {
return createUserId;
}
public void setCreateUserId(String createUserId) {
this.createUserId = createUserId;
}
public Date getCheckTime() {
return checkTime;
}
public void setCheckTime(Date checkTime) {
this.checkTime = checkTime;
}
public String getCheckUserId() {
return checkUserId;
}
public void setCheckUserId(String checkUserId) {
this.checkUserId = checkUserId;
}
public Integer getIsShow() {
return isShow;
}
public void setIsShow(Integer isShow) {
this.isShow = isShow;
}
public Integer getIsDel() {
return isDel;
}
public void setIsDel(Integer isDel) {
this.isDel = isDel;
}
public Integer getTypeId() {
return typeId;
}
public void setTypeId(Integer typeId) {
this.typeId = typeId;
}
public Integer getBrandId() {
return brandId;
}
public void setBrandId(Integer brandId) {
this.brandId = brandId;
}
public String getKeywords() {
return keywords;
}
public void setKeywords(String keywords) {
this.keywords = keywords;
}
public Integer getSales() {
return sales;
}
public void setSales(Integer sales) {
this.sales = sales;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPackageList() {
return packageList;
}
public void setPackageList(String packageList) {
this.packageList = packageList;
}
public String getFeature() {
return feature;
}
public void setFeature(String feature) {
this.feature = feature;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getSize_num() {
return size_num;
}
public void setSize_num(String size_num) {
this.size_num = size_num;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((brandId == null) ? 0 : brandId.hashCode());
result = prime * result + ((checkTime == null) ? 0 : checkTime.hashCode());
result = prime * result + ((checkUserId == null) ? 0 : checkUserId.hashCode());
result = prime * result + ((color == null) ? 0 : color.hashCode());
result = prime * result + ((createTime == null) ? 0 : createTime.hashCode());
result = prime * result + ((createUserId == null) ? 0 : createUserId.hashCode());
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + ((feature == null) ? 0 : feature.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((isCommend == null) ? 0 : isCommend.hashCode());
result = prime * result + ((isDel == null) ? 0 : isDel.hashCode());
result = prime * result + ((isHot == null) ? 0 : isHot.hashCode());
result = prime * result + ((isNew == null) ? 0 : isNew.hashCode());
result = prime * result + ((isShow == null) ? 0 : isShow.hashCode());
result = prime * result + ((keywords == null) ? 0 : keywords.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((no == null) ? 0 : no.hashCode());
result = prime * result + ((packageList == null) ? 0 : packageList.hashCode());
result = prime * result + ((sales == null) ? 0 : sales.hashCode());
result = prime * result + ((size_num == null) ? 0 : size_num.hashCode());
result = prime * result + ((typeId == null) ? 0 : typeId.hashCode());
result = prime * result + ((weight == null) ? 0 : weight.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Product other = (Product) obj;
if (brandId == null) {
if (other.brandId != null)
return false;
} else if (!brandId.equals(other.brandId))
return false;
if (checkTime == null) {
if (other.checkTime != null)
return false;
} else if (!checkTime.equals(other.checkTime))
return false;
if (checkUserId == null) {
if (other.checkUserId != null)
return false;
} else if (!checkUserId.equals(other.checkUserId))
return false;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
if (createTime == null) {
if (other.createTime != null)
return false;
} else if (!createTime.equals(other.createTime))
return false;
if (createUserId == null) {
if (other.createUserId != null)
return false;
} else if (!createUserId.equals(other.createUserId))
return false;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (feature == null) {
if (other.feature != null)
return false;
} else if (!feature.equals(other.feature))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (isCommend == null) {
if (other.isCommend != null)
return false;
} else if (!isCommend.equals(other.isCommend))
return false;
if (isDel == null) {
if (other.isDel != null)
return false;
} else if (!isDel.equals(other.isDel))
return false;
if (isHot == null) {
if (other.isHot != null)
return false;
} else if (!isHot.equals(other.isHot))
return false;
if (isNew == null) {
if (other.isNew != null)
return false;
} else if (!isNew.equals(other.isNew))
return false;
if (isShow == null) {
if (other.isShow != null)
return false;
} else if (!isShow.equals(other.isShow))
return false;
if (keywords == null) {
if (other.keywords != null)
return false;
} else if (!keywords.equals(other.keywords))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (no == null) {
if (other.no != null)
return false;
} else if (!no.equals(other.no))
return false;
if (packageList == null) {
if (other.packageList != null)
return false;
} else if (!packageList.equals(other.packageList))
return false;
if (sales == null) {
if (other.sales != null)
return false;
} else if (!sales.equals(other.sales))
return false;
if (size_num == null) {
if (other.size_num != null)
return false;
} else if (!size_num.equals(other.size_num))
return false;
if (typeId == null) {
if (other.typeId != null)
return false;
} else if (!typeId.equals(other.typeId))
return false;
if (weight == null) {
if (other.weight != null)
return false;
} else if (!weight.equals(other.weight))
return false;
return true;
}
@Override
public String toString() {
return "Product [id=" + id + ", no=" + no + ", name=" + name + ", weight=" + weight + ", isNew=" + isNew
+ ", isHot=" + isHot + ", isCommend=" + isCommend + ", createTime=" + createTime + ", createUserId="
+ createUserId + ", checkTime=" + checkTime + ", checkUserId=" + checkUserId + ", isShow=" + isShow
+ ", isDel=" + isDel + ", typeId=" + typeId + ", brandId=" + brandId + ", keywords=" + keywords
+ ", sales=" + sales + ", description=" + description + ", packageList=" + packageList + ", feature="
+ feature + ", color=" + color + ", size_num=" + size_num + "]";
}
}
ProductDao.java
package cn.zhou.core.dao.product;
import cn.zhou.core.bean.product.Product;
public interface ProductDao {
/**
* 添加
* @param product
*/
public Integer addProduct(Product product);
/**
*
* @param
*/
public Product getProductByKey(Integer id);
}
ProductService.java
package cn.zhou.core.service.product;
import cn.zhou.core.bean.product.Product;
/**
*
*/
public interface ProductService {
/**
* 基本插入
*
* @return
*/
public Integer addProduct(Product product);
/**
* 根据主键查询
*/
public Product getProductByKey(Integer id);
}
ProductServiceImpl.java
package cn.zhou.core.service.product;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.zhou.core.bean.product.Product;
import cn.zhou.core.dao.product.ProductDao;
/**
* 商品事务层
*
* @author lixu
* @Date [2014-3-27 下午03:31:57]
*/
@Service
@Transactional
public class ProductServiceImpl implements ProductService {
@Resource
ProductDao productDao;
/**
* 插入数据库
*
* @return
*/
public Integer addProduct(Product product) {
Integer i = productDao.addProduct(product);
return i;
}
/**
* 根据主键查找
*/
@Transactional(readOnly = true)
public Product getProductByKey(Integer id) {
Product product = productDao.getProductByKey(id);
return product;
}
}
ProductController.java
package cn.zhou.core.controller;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.zhou.core.bean.product.Product;
import cn.zhou.core.service.product.ProductService;
/**
* 后台商品管理 商品列表 商品添加 商品上架
*
* @author lx
*
*/
@Controller
public class ProductController {
private static Logger log = LoggerFactory
.getLogger(ProductController.class);
// 商品
@Autowired
private ProductService productService;
//商品添加
@RequestMapping(value="add.do")
public String add(){
Random r = new Random();
for (int i = 0; i < 1500000; i++) {
Product product = new Product();
String no = UUID.randomUUID().toString().replaceAll("-", "");// 商品编号
String name = UUID.randomUUID().toString().replaceAll("-", "");// 商品名称
Double weight = (r.nextInt(9999) + 1) * 1.0;// 重量 单位:克
Integer isNew = r.nextInt(8)%2;// 是否新品:0:旧品,1:新品
Integer isHot = r.nextInt(8)%2;// 是否热销:0,否 1:是
Integer isCommend = r.nextInt(8)%2;// 推荐 1推荐 0 不推荐
Date createTime = new Date();// 添加时间
String createUserId = UUID.randomUUID().toString().replaceAll("-", "");// 添加人ID
Date checkTime = new Date();// 审核时间
String checkUserId = UUID.randomUUID().toString().replaceAll("-", "");// 审核人ID
Integer isShow = r.nextInt(8)%2;// 上下架:0否 1是,默认下架,这样代码是下架的可以不用设置
Integer isDel = r.nextInt(8)%2;// 是否删除:0删除,1,没删除
Integer typeId = r.nextInt(99999999) + 1;// 类型ID
Integer brandId = r.nextInt(99999999) + 1;// 品牌ID
String keywords = UUID.randomUUID().toString().replaceAll("-", "");// 检索关键词
Integer sales = r.nextInt(1000) + 1;// 销量
String description = UUID.randomUUID().toString().replaceAll("-", "");// 商品描述
String packageList = UUID.randomUUID().toString().replaceAll("-", "");// 包装清单
String feature = UUID.randomUUID().toString().replaceAll("-", "");// 商品属性集
String color = String.valueOf(r.nextInt(10) + 1);// 颜色集 9,10,11,12
String size_num = String.valueOf(r.nextInt(10) + 1);// 尺寸集 S,L,XXL
product.setBrandId(brandId);
product.setCheckTime(checkTime);
product.setCheckUserId(checkUserId);
product.setColor(color);
product.setCreateTime(createTime);
product.setCreateUserId(createUserId);
product.setDescription(description);
product.setFeature(feature);
product.setIsCommend(isCommend);
product.setIsDel(isDel);
product.setIsHot(isHot);
product.setIsNew(isNew);
product.setIsShow(isShow);
product.setKeywords(keywords);
product.setName(name);
product.setNo(no);
product.setPackageList(packageList);
product.setSales(sales);
product.setTypeId(typeId);
product.setWeight(weight);
product.setSize_num(size_num);
productService.addProduct(product);
System.out.println(i);
}
return "";
}
//显示商品
@RequestMapping(value = "/showProduct.do")
public String showProduct(HttpServletRequest request, Model model) {
Product product = this.productService.getProductByKey(1);
log.debug(product.toString());
model.addAttribute("product", product);
return "showProduct";
}
}
整个项目路径url:
数据库
drop table product;
create table product (
id varchar2(32) primary key,
no varchar2(32),
name varchar2(32) ,
weight float(4),
is_new number(1),
is_hot number(1) ,
is_commend number(1) ,
create_time date,
create_user_id varchar2(32),
check_time date,
check_user_id varchar2(32),
is_show number(1),
is_del number(1) ,
type_id number(8),
brand_id number(8),
keywords varchar2(32),
sales number(11),
description varchar2(2000),
package_list varchar2(2000),
feature varchar2(32),
color varchar2(8),
size_num varchar2(4)
)tablespace MYSPACE;
drop sequence seq_product;
create sequence seq_product
minvalue 1 --最小值
nomaxvalue --不设置最大值
start with 1 --从1开始计数
increment by 1 --每次加1个
nocycle --一直累加,不循环
cache 1000;
create or replace trigger tri_product
BEFORE INSERT ON product
FOR EACH ROW
begin
select seq_product.nextval into:new.id from dual;
end;
/