SSM框架整合一(springmvc+spring+mybatis+maven+tomcat)

一,环境说明

jdk1.7.0_07(cmd命令行输入java -version查看),点击下载
eclipse Kepler Service Release 2
apache-maven-3.3.9(cmd命令行输入mvn -v查看),点击下载
apache-tomcat-7.0.52,点击下载
mysql-5.5.28-winx64,点击下载

二,设置Eclipse开发环境

2.1,设置jre运行环境

 

2.2,配置settings.xml文件和仓库路径

settings.xml

<?xml version="1.0"?>  
<settings>  
<localRepository>E:\e3\repository</localRepository><!--需要改成自己的maven的本地仓库地址-->  <mirrors>  <mirror>  <id>alimaven</id>  <name>aliyun maven</name>  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  <mirrorOf>central</mirrorOf>  </mirror>  </mirrors>  <profiles>  <profile>  <id>nexus</id>   <repositories>  <repository>  <id>nexus</id>  <name>local private nexus</name>  <url>http://maven.oschina.net/content/groups/public/</url>  <releases>  <enabled>true</enabled>  </releases>  <snapshots>  <enabled>false</enabled>  </snapshots>  </repository>  </repositories>  <pluginRepositories>  <pluginRepository>  <id>nexus</id>  <name>local private nexus</name>  <url>http://maven.oschina.net/content/groups/public/</url>  <releases>  <enabled>true</enabled>  </releases>  <snapshots>  <enabled>false</enabled>  </snapshots>  </pluginRepository>  </pluginRepositories>  </profile></profiles>  
</settings>  

2.3,设置eclipse默认字符集

2.4,设置JSP页面编码

2.5,配置tomcat运行环境

2.5.1,添加tomcat运行环境

 

2.5.2,添加Tomcat Server

 

2.5.3,显示Tomcat Server

2.5.4,配置tomcat

 

3,工程搭建

3.1,e3-parent(父工程,管理依赖的版本)

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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.e3mall</groupId><artifactId>e3-parent</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><!-- 集中定义依赖版本号 --><properties><junit.version>4.12</junit.version><spring.version>4.2.4.RELEASE</spring.version><mybatis.version>3.2.8</mybatis.version><mybatis.spring.version>1.2.2</mybatis.spring.version><mybatis.paginator.version>1.2.15</mybatis.paginator.version><mysql.version>5.1.32</mysql.version><slf4j.version>1.6.4</slf4j.version><jackson.version>2.4.2</jackson.version><druid.version>1.0.9</druid.version><httpclient.version>4.3.5</httpclient.version><jstl.version>1.2</jstl.version><servlet-api.version>2.5</servlet-api.version><jsp-api.version>2.0</jsp-api.version><joda-time.version>2.5</joda-time.version><commons-lang3.version>3.3.2</commons-lang3.version><commons-io.version>1.3.2</commons-io.version><commons-net.version>3.3</commons-net.version><pagehelper.version>3.4.2-fix</pagehelper.version><jsqlparser.version>0.9.1</jsqlparser.version><commons-fileupload.version>1.3.1</commons-fileupload.version><jedis.version>2.7.2</jedis.version><solrj.version>4.10.3</solrj.version><dubbo.version>2.5.3</dubbo.version><zookeeper.version>3.4.7</zookeeper.version><zkclient.version>0.1</zkclient.version><activemq.version>5.11.2</activemq.version><freemarker.version>2.3.23</freemarker.version><quartz.version>2.2.2</quartz.version></properties><dependencyManagement><dependencies><!-- 时间操作组件 --><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId><version>${joda-time.version}</version></dependency><!-- Apache工具组件 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>${commons-lang3.version}</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-io</artifactId><version>${commons-io.version}</version></dependency><dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId><version>${commons-net.version}</version></dependency><!-- Jackson Json处理工具包 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson.version}</version></dependency><!-- httpclient --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>${httpclient.version}</version></dependency><!-- quartz任务调度框架 --><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId><version>${quartz.version}</version></dependency><!-- 单元测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><!-- 日志处理 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency><!-- Mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>${mybatis.spring.version}</version></dependency><dependency><groupId>com.github.miemiedev</groupId><artifactId>mybatis-paginator</artifactId><version>${mybatis.paginator.version}</version></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>${pagehelper.version}</version></dependency><!-- MySql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><!-- 连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>${druid.version}</version></dependency><!-- Spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><!-- JSP相关 --><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>${jstl.version}</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>${servlet-api.version}</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api</artifactId><version>${jsp-api.version}</version><scope>provided</scope></dependency><!-- 文件上传组件 --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>${commons-fileupload.version}</version></dependency><!-- Redis客户端 --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>${jedis.version}</version></dependency><!-- solr客户端 --><dependency><groupId>org.apache.solr</groupId><artifactId>solr-solrj</artifactId><version>${solrj.version}</version></dependency><!-- dubbo相关 --><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>${dubbo.version}</version></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>${zookeeper.version}</version></dependency><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>${zkclient.version}</version></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>${activemq.version}</version></dependency><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>${freemarker.version}</version></dependency></dependencies></dependencyManagement><build><finalName>${project.artifactId}</finalName><plugins><!-- 资源文件拷贝插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>2.7</version><configuration><encoding>UTF-8</encoding></configuration></plugin><!-- java编译插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.2</version><configuration><source>1.7</source><target>1.7</target><encoding>UTF-8</encoding></configuration></plugin></plugins><pluginManagement><plugins><!-- 配置Tomcat插件 --><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version></plugin></plugins></pluginManagement></build>
</project>

 

3.2,e3-common(工具类)

打包方式选择jar包

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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.e3mall</groupId><artifactId>e3-parent</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>cn.e3mall</groupId><artifactId>e3-common</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><!-- 时间操作组件 --><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId></dependency><!-- Apache工具组件 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-io</artifactId></dependency><dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId></dependency><!-- Jackson Json处理工具包 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency><!-- httpclient --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId></dependency><!-- quartz任务调度框架 --><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId></dependency><!-- 单元测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><!-- 日志处理 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></dependency></dependencies>
</project>

 

3.3,e3-manager(聚合工程,下面所有工程的父工程)

打包方式选择pom

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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.e3mall</groupId><artifactId>e3-parent</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>cn.e3mall</groupId><artifactId>e3-manager</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><dependencies><dependency><groupId>cn.e3mall</groupId><artifactId>e3-common</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies>
</project>

3.4,e3-manager-pojo(实体层)

选择新建maven module工程

 

3.5,e3-manager-dao(持久层)

选择新建maven module工程

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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.e3mall</groupId><artifactId>e3-manager</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>e3-manager-dao</artifactId><dependencies><dependency><groupId>cn.e3mall</groupId><artifactId>e3-manager-pojo</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- 添加对mybatis的依赖 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId></dependency><dependency><groupId>com.github.miemiedev</groupId><artifactId>mybatis-paginator</artifactId></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId></dependency><!-- MySql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- 连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId></dependency></dependencies>
</project>

3.6,e3-manager-interface(接口)

选择新建maven module工程

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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.e3mall</groupId><artifactId>e3-manager</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>e3-manager-interface</artifactId><dependencies><dependency><groupId>cn.e3mall</groupId><artifactId>e3-manager-pojo</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies>
</project>

 

3.7,e3-manager-service(服务层)

选择新建maven module工程

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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.e3mall</groupId><artifactId>e3-manager</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>e3-manager-service</artifactId><dependencies><dependency><groupId>cn.e3mall</groupId><artifactId>e3-manager-dao</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>cn.e3mall</groupId><artifactId>e3-manager-interface</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- spring的依赖 --><!-- Spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId></dependency></dependencies>
</project>

 

3.8,e3-manager-web(web工程)

选择新建maven module工程

打包方式选择war包

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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.e3mall</groupId><artifactId>e3-manager</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>e3-manager-web</artifactId><packaging>war</packaging><dependencies><dependency><groupId>cn.e3mall</groupId><artifactId>e3-manager-service</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- JSP相关 --><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api</artifactId><scope>provided</scope></dependency></dependencies>
</project>

4,新建数据库导入表结构和数据

tb_item.sql

/*
Navicat MySQL Data TransferSource Server         : 本地
Source Server Version : 50528
Source Host           : localhost:3306
Source Database       : e3mall-32Target Server Type    : MYSQL
Target Server Version : 50528
File Encoding         : 65001Date: 2018-07-18 20:54:10
*/SET FOREIGN_KEY_CHECKS=0;-- ----------------------------
-- Table structure for tb_item
-- ----------------------------
DROP TABLE IF EXISTS `tb_item`;
CREATE TABLE `tb_item` (`id` bigint(20) NOT NULL COMMENT '商品id,同时也是商品编号',`title` varchar(100) NOT NULL COMMENT '商品标题',`sell_point` varchar(500) DEFAULT NULL COMMENT '商品卖点',`price` bigint(20) NOT NULL COMMENT '商品价格,单位为:分',`num` int(10) NOT NULL COMMENT '库存数量',`barcode` varchar(30) DEFAULT NULL COMMENT '商品条形码',`image` varchar(500) DEFAULT NULL COMMENT '商品图片',`cid` bigint(10) NOT NULL COMMENT '所属类目,叶子类目',`status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '商品状态,1-正常,2-下架,3-删除',`created` datetime NOT NULL COMMENT '创建时间',`updated` datetime NOT NULL COMMENT '更新时间',PRIMARY KEY (`id`),KEY `cid` (`cid`),KEY `status` (`status`),KEY `updated` (`updated`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='商品表';-- ----------------------------
-- Records of tb_item
-- ----------------------------
INSERT INTO `tb_item` VALUES ('536563', 'new2 - 阿尔卡特 (OT-927) 炭黑 联通3G手机 双卡双待', '清仓!仅北京,武汉仓有货!', '29900000', '99999', '', 'http://image.e3mall.cn/jd/4ef8861cf6854de9889f3db9b24dc371.jpg', '560', '1', '2015-03-08 21:33:18', '2015-04-11 20:38:38');

5,ssm框架整合

5.1,整合思路

SqlMapConfig.xml:spring整合mybatis需要引入SqlMapConfig.xml,里面可以没有实质性的内容,但是必须保留文件头。

applicationContext-dao.xml:由spring创建数据库连接池,管理SqlSessionFactory、mapper代理对象。

applicationContext-service.xml:所有service对象交由spring容器管理

applicationContext-trans.xml:由spring来管理实务

springmvc.xml:引入springmvc,由spring来管理controller

web.xml:加载spring容器,加载springmvc

5.2,SqlMapConfig.xml

在e3-manager-web/src/main/resources/目录下创建三个文件夹conf、spring、mybatis

在mybatis目录下创建SqlMapConfig.xml文件,文件内容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration></configuration>

 

5.3,applicationContext-dao.xml

在spring目录下创建applicationContext-dao.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><!-- 数据库连接池 --><!-- 加载配置文件 --><context:property-placeholder location="classpath:conf/db.properties" /><!-- 数据库连接池 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"destroy-method="close"><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="driverClassName" value="${jdbc.driver}" /><property name="maxActive" value="10" /><property name="minIdle" value="5" /></bean><!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 数据库连接池 --><property name="dataSource" ref="dataSource" /><!-- 加载mybatis的全局配置文件 --><property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="cn.e3mall.mapper" /></bean>
</beans>

5.4,applicationContext-service.xml

在spring目录下创建applicationContext-service.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><!-- 配置包扫描器 --><context:component-scan base-package="cn.e3mall.service"/></beans>

5.5,applicationContext-trans.xml

在spring目录下创建applicationContext-trans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><!-- 事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 数据源 --><property name="dataSource" ref="dataSource" /></bean><!-- 通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- 传播行为 --><tx:method name="save*" propagation="REQUIRED" /><tx:method name="insert*" propagation="REQUIRED" /><tx:method name="add*" propagation="REQUIRED" /><tx:method name="create*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="find*" propagation="SUPPORTS" read-only="true" /><tx:method name="select*" propagation="SUPPORTS" read-only="true" /><tx:method name="get*" propagation="SUPPORTS" read-only="true" /></tx:attributes></tx:advice><!-- 切面 --><aop:config><aop:advisor advice-ref="txAdvice"pointcut="execution(* cn.e3mall.service..*.*(..))" /></aop:config>
</beans>

5.6,springmvc.xml

在spring目录下创建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-4.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"><context:component-scan base-package="cn.e3mall.controller" /><mvc:annotation-driven /><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean>
</beans>

5.7,web.xml

在webapp目录下创建WEB-INF目录,创建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_2_5.xsd"version="2.5"><display-name>e3-manager-web</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><!-- 加载spring容器 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/applicationContext-*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 解决post乱码 --><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><!-- springmvc的前端控制器 --><servlet><servlet-name>e3-manager</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>e3-manager</servlet-name><url-pattern>/</url-pattern></servlet-mapping><context-param><param-name>log4jConfigLocation</param-name><param-value>classpath:conf/log4j.properties</param-value></context-param><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener>
</web-app>

5.8,db.properties

在conf目录下创建db.properties,注意数据库名、用户名和密码:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/e3mall-demo?characterEncoding=utf-8
jdbc.username=root
jdbc.password=a123

 

5.9,log4j.properties

在conf目录下创建log4j.properties文件:

log4j.rootLogger=INFO,Console,File
#\u63A7\u5236\u53F0\u65E5\u5FD7
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
#\u666E\u901A\u6587\u4EF6\u65E5\u5FD7
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=logs/ssm.log
log4j.appender.File.MaxFileSize=10MB
#\u8F93\u51FA\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n

6,开发一个小的查询功能

6.1,需求描述

浏览器输入http://localhost:8080/e3-manager-web/item/536563能查到对应的商品信息

6.2,创建实体类

创建cn.e3mall.pojo包:

TbItem.java

package cn.e3mall.pojo;import java.util.Date;public class TbItem {private Long id;private String title;private String sellPoint;private Long price;private Integer num;private String barcode;private String image;private Long cid;private Byte status;private Date created;private Date updated;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title == null ? null : title.trim();}public String getSellPoint() {return sellPoint;}public void setSellPoint(String sellPoint) {this.sellPoint = sellPoint == null ? null : sellPoint.trim();}public Long getPrice() {return price;}public void setPrice(Long price) {this.price = price;}public Integer getNum() {return num;}public void setNum(Integer num) {this.num = num;}public String getBarcode() {return barcode;}public void setBarcode(String barcode) {this.barcode = barcode == null ? null : barcode.trim();}public String getImage() {return image;}public void setImage(String image) {this.image = image == null ? null : image.trim();}public Long getCid() {return cid;}public void setCid(Long cid) {this.cid = cid;}public Byte getStatus() {return status;}public void setStatus(Byte status) {this.status = status;}public Date getCreated() {return created;}public void setCreated(Date created) {this.created = created;}public Date getUpdated() {return updated;}public void setUpdated(Date updated) {this.updated = updated;}
}

TbItemExample.java

package cn.e3mall.pojo;import java.util.ArrayList;
import java.util.Date;
import java.util.List;public class TbItemExample {protected String orderByClause;protected boolean distinct;protected List<Criteria> oredCriteria;public TbItemExample() {oredCriteria = new ArrayList<Criteria>();}public void setOrderByClause(String orderByClause) {this.orderByClause = orderByClause;}public String getOrderByClause() {return orderByClause;}public void setDistinct(boolean distinct) {this.distinct = distinct;}public boolean isDistinct() {return distinct;}public List<Criteria> getOredCriteria() {return oredCriteria;}public void or(Criteria criteria) {oredCriteria.add(criteria);}public Criteria or() {Criteria criteria = createCriteriaInternal();oredCriteria.add(criteria);return criteria;}public Criteria createCriteria() {Criteria criteria = createCriteriaInternal();if (oredCriteria.size() == 0) {oredCriteria.add(criteria);}return criteria;}protected Criteria createCriteriaInternal() {Criteria criteria = new Criteria();return criteria;}public void clear() {oredCriteria.clear();orderByClause = null;distinct = false;}protected abstract static class GeneratedCriteria {protected List<Criterion> criteria;protected GeneratedCriteria() {super();criteria = new ArrayList<Criterion>();}public boolean isValid() {return criteria.size() > 0;}public List<Criterion> getAllCriteria() {return criteria;}public List<Criterion> getCriteria() {return criteria;}protected void addCriterion(String condition) {if (condition == null) {throw new RuntimeException("Value for condition cannot be null");}criteria.add(new Criterion(condition));}protected void addCriterion(String condition, Object value, String property) {if (value == null) {throw new RuntimeException("Value for " + property + " cannot be null");}criteria.add(new Criterion(condition, value));}protected void addCriterion(String condition, Object value1, Object value2, String property) {if (value1 == null || value2 == null) {throw new RuntimeException("Between values for " + property + " cannot be null");}criteria.add(new Criterion(condition, value1, value2));}public Criteria andIdIsNull() {addCriterion("id is null");return (Criteria) this;}public Criteria andIdIsNotNull() {addCriterion("id is not null");return (Criteria) this;}public Criteria andIdEqualTo(Long value) {addCriterion("id =", value, "id");return (Criteria) this;}public Criteria andIdNotEqualTo(Long value) {addCriterion("id <>", value, "id");return (Criteria) this;}public Criteria andIdGreaterThan(Long value) {addCriterion("id >", value, "id");return (Criteria) this;}public Criteria andIdGreaterThanOrEqualTo(Long value) {addCriterion("id >=", value, "id");return (Criteria) this;}public Criteria andIdLessThan(Long value) {addCriterion("id <", value, "id");return (Criteria) this;}public Criteria andIdLessThanOrEqualTo(Long value) {addCriterion("id <=", value, "id");return (Criteria) this;}public Criteria andIdIn(List<Long> values) {addCriterion("id in", values, "id");return (Criteria) this;}public Criteria andIdNotIn(List<Long> values) {addCriterion("id not in", values, "id");return (Criteria) this;}public Criteria andIdBetween(Long value1, Long value2) {addCriterion("id between", value1, value2, "id");return (Criteria) this;}public Criteria andIdNotBetween(Long value1, Long value2) {addCriterion("id not between", value1, value2, "id");return (Criteria) this;}public Criteria andTitleIsNull() {addCriterion("title is null");return (Criteria) this;}public Criteria andTitleIsNotNull() {addCriterion("title is not null");return (Criteria) this;}public Criteria andTitleEqualTo(String value) {addCriterion("title =", value, "title");return (Criteria) this;}public Criteria andTitleNotEqualTo(String value) {addCriterion("title <>", value, "title");return (Criteria) this;}public Criteria andTitleGreaterThan(String value) {addCriterion("title >", value, "title");return (Criteria) this;}public Criteria andTitleGreaterThanOrEqualTo(String value) {addCriterion("title >=", value, "title");return (Criteria) this;}public Criteria andTitleLessThan(String value) {addCriterion("title <", value, "title");return (Criteria) this;}public Criteria andTitleLessThanOrEqualTo(String value) {addCriterion("title <=", value, "title");return (Criteria) this;}public Criteria andTitleLike(String value) {addCriterion("title like", value, "title");return (Criteria) this;}public Criteria andTitleNotLike(String value) {addCriterion("title not like", value, "title");return (Criteria) this;}public Criteria andTitleIn(List<String> values) {addCriterion("title in", values, "title");return (Criteria) this;}public Criteria andTitleNotIn(List<String> values) {addCriterion("title not in", values, "title");return (Criteria) this;}public Criteria andTitleBetween(String value1, String value2) {addCriterion("title between", value1, value2, "title");return (Criteria) this;}public Criteria andTitleNotBetween(String value1, String value2) {addCriterion("title not between", value1, value2, "title");return (Criteria) this;}public Criteria andSellPointIsNull() {addCriterion("sell_point is null");return (Criteria) this;}public Criteria andSellPointIsNotNull() {addCriterion("sell_point is not null");return (Criteria) this;}public Criteria andSellPointEqualTo(String value) {addCriterion("sell_point =", value, "sellPoint");return (Criteria) this;}public Criteria andSellPointNotEqualTo(String value) {addCriterion("sell_point <>", value, "sellPoint");return (Criteria) this;}public Criteria andSellPointGreaterThan(String value) {addCriterion("sell_point >", value, "sellPoint");return (Criteria) this;}public Criteria andSellPointGreaterThanOrEqualTo(String value) {addCriterion("sell_point >=", value, "sellPoint");return (Criteria) this;}public Criteria andSellPointLessThan(String value) {addCriterion("sell_point <", value, "sellPoint");return (Criteria) this;}public Criteria andSellPointLessThanOrEqualTo(String value) {addCriterion("sell_point <=", value, "sellPoint");return (Criteria) this;}public Criteria andSellPointLike(String value) {addCriterion("sell_point like", value, "sellPoint");return (Criteria) this;}public Criteria andSellPointNotLike(String value) {addCriterion("sell_point not like", value, "sellPoint");return (Criteria) this;}public Criteria andSellPointIn(List<String> values) {addCriterion("sell_point in", values, "sellPoint");return (Criteria) this;}public Criteria andSellPointNotIn(List<String> values) {addCriterion("sell_point not in", values, "sellPoint");return (Criteria) this;}public Criteria andSellPointBetween(String value1, String value2) {addCriterion("sell_point between", value1, value2, "sellPoint");return (Criteria) this;}public Criteria andSellPointNotBetween(String value1, String value2) {addCriterion("sell_point not between", value1, value2, "sellPoint");return (Criteria) this;}public Criteria andPriceIsNull() {addCriterion("price is null");return (Criteria) this;}public Criteria andPriceIsNotNull() {addCriterion("price is not null");return (Criteria) this;}public Criteria andPriceEqualTo(Long value) {addCriterion("price =", value, "price");return (Criteria) this;}public Criteria andPriceNotEqualTo(Long value) {addCriterion("price <>", value, "price");return (Criteria) this;}public Criteria andPriceGreaterThan(Long value) {addCriterion("price >", value, "price");return (Criteria) this;}public Criteria andPriceGreaterThanOrEqualTo(Long value) {addCriterion("price >=", value, "price");return (Criteria) this;}public Criteria andPriceLessThan(Long value) {addCriterion("price <", value, "price");return (Criteria) this;}public Criteria andPriceLessThanOrEqualTo(Long value) {addCriterion("price <=", value, "price");return (Criteria) this;}public Criteria andPriceIn(List<Long> values) {addCriterion("price in", values, "price");return (Criteria) this;}public Criteria andPriceNotIn(List<Long> values) {addCriterion("price not in", values, "price");return (Criteria) this;}public Criteria andPriceBetween(Long value1, Long value2) {addCriterion("price between", value1, value2, "price");return (Criteria) this;}public Criteria andPriceNotBetween(Long value1, Long value2) {addCriterion("price not between", value1, value2, "price");return (Criteria) this;}public Criteria andNumIsNull() {addCriterion("num is null");return (Criteria) this;}public Criteria andNumIsNotNull() {addCriterion("num is not null");return (Criteria) this;}public Criteria andNumEqualTo(Integer value) {addCriterion("num =", value, "num");return (Criteria) this;}public Criteria andNumNotEqualTo(Integer value) {addCriterion("num <>", value, "num");return (Criteria) this;}public Criteria andNumGreaterThan(Integer value) {addCriterion("num >", value, "num");return (Criteria) this;}public Criteria andNumGreaterThanOrEqualTo(Integer value) {addCriterion("num >=", value, "num");return (Criteria) this;}public Criteria andNumLessThan(Integer value) {addCriterion("num <", value, "num");return (Criteria) this;}public Criteria andNumLessThanOrEqualTo(Integer value) {addCriterion("num <=", value, "num");return (Criteria) this;}public Criteria andNumIn(List<Integer> values) {addCriterion("num in", values, "num");return (Criteria) this;}public Criteria andNumNotIn(List<Integer> values) {addCriterion("num not in", values, "num");return (Criteria) this;}public Criteria andNumBetween(Integer value1, Integer value2) {addCriterion("num between", value1, value2, "num");return (Criteria) this;}public Criteria andNumNotBetween(Integer value1, Integer value2) {addCriterion("num not between", value1, value2, "num");return (Criteria) this;}public Criteria andBarcodeIsNull() {addCriterion("barcode is null");return (Criteria) this;}public Criteria andBarcodeIsNotNull() {addCriterion("barcode is not null");return (Criteria) this;}public Criteria andBarcodeEqualTo(String value) {addCriterion("barcode =", value, "barcode");return (Criteria) this;}public Criteria andBarcodeNotEqualTo(String value) {addCriterion("barcode <>", value, "barcode");return (Criteria) this;}public Criteria andBarcodeGreaterThan(String value) {addCriterion("barcode >", value, "barcode");return (Criteria) this;}public Criteria andBarcodeGreaterThanOrEqualTo(String value) {addCriterion("barcode >=", value, "barcode");return (Criteria) this;}public Criteria andBarcodeLessThan(String value) {addCriterion("barcode <", value, "barcode");return (Criteria) this;}public Criteria andBarcodeLessThanOrEqualTo(String value) {addCriterion("barcode <=", value, "barcode");return (Criteria) this;}public Criteria andBarcodeLike(String value) {addCriterion("barcode like", value, "barcode");return (Criteria) this;}public Criteria andBarcodeNotLike(String value) {addCriterion("barcode not like", value, "barcode");return (Criteria) this;}public Criteria andBarcodeIn(List<String> values) {addCriterion("barcode in", values, "barcode");return (Criteria) this;}public Criteria andBarcodeNotIn(List<String> values) {addCriterion("barcode not in", values, "barcode");return (Criteria) this;}public Criteria andBarcodeBetween(String value1, String value2) {addCriterion("barcode between", value1, value2, "barcode");return (Criteria) this;}public Criteria andBarcodeNotBetween(String value1, String value2) {addCriterion("barcode not between", value1, value2, "barcode");return (Criteria) this;}public Criteria andImageIsNull() {addCriterion("image is null");return (Criteria) this;}public Criteria andImageIsNotNull() {addCriterion("image is not null");return (Criteria) this;}public Criteria andImageEqualTo(String value) {addCriterion("image =", value, "image");return (Criteria) this;}public Criteria andImageNotEqualTo(String value) {addCriterion("image <>", value, "image");return (Criteria) this;}public Criteria andImageGreaterThan(String value) {addCriterion("image >", value, "image");return (Criteria) this;}public Criteria andImageGreaterThanOrEqualTo(String value) {addCriterion("image >=", value, "image");return (Criteria) this;}public Criteria andImageLessThan(String value) {addCriterion("image <", value, "image");return (Criteria) this;}public Criteria andImageLessThanOrEqualTo(String value) {addCriterion("image <=", value, "image");return (Criteria) this;}public Criteria andImageLike(String value) {addCriterion("image like", value, "image");return (Criteria) this;}public Criteria andImageNotLike(String value) {addCriterion("image not like", value, "image");return (Criteria) this;}public Criteria andImageIn(List<String> values) {addCriterion("image in", values, "image");return (Criteria) this;}public Criteria andImageNotIn(List<String> values) {addCriterion("image not in", values, "image");return (Criteria) this;}public Criteria andImageBetween(String value1, String value2) {addCriterion("image between", value1, value2, "image");return (Criteria) this;}public Criteria andImageNotBetween(String value1, String value2) {addCriterion("image not between", value1, value2, "image");return (Criteria) this;}public Criteria andCidIsNull() {addCriterion("cid is null");return (Criteria) this;}public Criteria andCidIsNotNull() {addCriterion("cid is not null");return (Criteria) this;}public Criteria andCidEqualTo(Long value) {addCriterion("cid =", value, "cid");return (Criteria) this;}public Criteria andCidNotEqualTo(Long value) {addCriterion("cid <>", value, "cid");return (Criteria) this;}public Criteria andCidGreaterThan(Long value) {addCriterion("cid >", value, "cid");return (Criteria) this;}public Criteria andCidGreaterThanOrEqualTo(Long value) {addCriterion("cid >=", value, "cid");return (Criteria) this;}public Criteria andCidLessThan(Long value) {addCriterion("cid <", value, "cid");return (Criteria) this;}public Criteria andCidLessThanOrEqualTo(Long value) {addCriterion("cid <=", value, "cid");return (Criteria) this;}public Criteria andCidIn(List<Long> values) {addCriterion("cid in", values, "cid");return (Criteria) this;}public Criteria andCidNotIn(List<Long> values) {addCriterion("cid not in", values, "cid");return (Criteria) this;}public Criteria andCidBetween(Long value1, Long value2) {addCriterion("cid between", value1, value2, "cid");return (Criteria) this;}public Criteria andCidNotBetween(Long value1, Long value2) {addCriterion("cid not between", value1, value2, "cid");return (Criteria) this;}public Criteria andStatusIsNull() {addCriterion("status is null");return (Criteria) this;}public Criteria andStatusIsNotNull() {addCriterion("status is not null");return (Criteria) this;}public Criteria andStatusEqualTo(Byte value) {addCriterion("status =", value, "status");return (Criteria) this;}public Criteria andStatusNotEqualTo(Byte value) {addCriterion("status <>", value, "status");return (Criteria) this;}public Criteria andStatusGreaterThan(Byte value) {addCriterion("status >", value, "status");return (Criteria) this;}public Criteria andStatusGreaterThanOrEqualTo(Byte value) {addCriterion("status >=", value, "status");return (Criteria) this;}public Criteria andStatusLessThan(Byte value) {addCriterion("status <", value, "status");return (Criteria) this;}public Criteria andStatusLessThanOrEqualTo(Byte value) {addCriterion("status <=", value, "status");return (Criteria) this;}public Criteria andStatusIn(List<Byte> values) {addCriterion("status in", values, "status");return (Criteria) this;}public Criteria andStatusNotIn(List<Byte> values) {addCriterion("status not in", values, "status");return (Criteria) this;}public Criteria andStatusBetween(Byte value1, Byte value2) {addCriterion("status between", value1, value2, "status");return (Criteria) this;}public Criteria andStatusNotBetween(Byte value1, Byte value2) {addCriterion("status not between", value1, value2, "status");return (Criteria) this;}public Criteria andCreatedIsNull() {addCriterion("created is null");return (Criteria) this;}public Criteria andCreatedIsNotNull() {addCriterion("created is not null");return (Criteria) this;}public Criteria andCreatedEqualTo(Date value) {addCriterion("created =", value, "created");return (Criteria) this;}public Criteria andCreatedNotEqualTo(Date value) {addCriterion("created <>", value, "created");return (Criteria) this;}public Criteria andCreatedGreaterThan(Date value) {addCriterion("created >", value, "created");return (Criteria) this;}public Criteria andCreatedGreaterThanOrEqualTo(Date value) {addCriterion("created >=", value, "created");return (Criteria) this;}public Criteria andCreatedLessThan(Date value) {addCriterion("created <", value, "created");return (Criteria) this;}public Criteria andCreatedLessThanOrEqualTo(Date value) {addCriterion("created <=", value, "created");return (Criteria) this;}public Criteria andCreatedIn(List<Date> values) {addCriterion("created in", values, "created");return (Criteria) this;}public Criteria andCreatedNotIn(List<Date> values) {addCriterion("created not in", values, "created");return (Criteria) this;}public Criteria andCreatedBetween(Date value1, Date value2) {addCriterion("created between", value1, value2, "created");return (Criteria) this;}public Criteria andCreatedNotBetween(Date value1, Date value2) {addCriterion("created not between", value1, value2, "created");return (Criteria) this;}public Criteria andUpdatedIsNull() {addCriterion("updated is null");return (Criteria) this;}public Criteria andUpdatedIsNotNull() {addCriterion("updated is not null");return (Criteria) this;}public Criteria andUpdatedEqualTo(Date value) {addCriterion("updated =", value, "updated");return (Criteria) this;}public Criteria andUpdatedNotEqualTo(Date value) {addCriterion("updated <>", value, "updated");return (Criteria) this;}public Criteria andUpdatedGreaterThan(Date value) {addCriterion("updated >", value, "updated");return (Criteria) this;}public Criteria andUpdatedGreaterThanOrEqualTo(Date value) {addCriterion("updated >=", value, "updated");return (Criteria) this;}public Criteria andUpdatedLessThan(Date value) {addCriterion("updated <", value, "updated");return (Criteria) this;}public Criteria andUpdatedLessThanOrEqualTo(Date value) {addCriterion("updated <=", value, "updated");return (Criteria) this;}public Criteria andUpdatedIn(List<Date> values) {addCriterion("updated in", values, "updated");return (Criteria) this;}public Criteria andUpdatedNotIn(List<Date> values) {addCriterion("updated not in", values, "updated");return (Criteria) this;}public Criteria andUpdatedBetween(Date value1, Date value2) {addCriterion("updated between", value1, value2, "updated");return (Criteria) this;}public Criteria andUpdatedNotBetween(Date value1, Date value2) {addCriterion("updated not between", value1, value2, "updated");return (Criteria) this;}}public static class Criteria extends GeneratedCriteria {protected Criteria() {super();}}public static class Criterion {private String condition;private Object value;private Object secondValue;private boolean noValue;private boolean singleValue;private boolean betweenValue;private boolean listValue;private String typeHandler;public String getCondition() {return condition;}public Object getValue() {return value;}public Object getSecondValue() {return secondValue;}public boolean isNoValue() {return noValue;}public boolean isSingleValue() {return singleValue;}public boolean isBetweenValue() {return betweenValue;}public boolean isListValue() {return listValue;}public String getTypeHandler() {return typeHandler;}protected Criterion(String condition) {super();this.condition = condition;this.typeHandler = null;this.noValue = true;}protected Criterion(String condition, Object value, String typeHandler) {super();this.condition = condition;this.value = value;this.typeHandler = typeHandler;if (value instanceof List<?>) {this.listValue = true;} else {this.singleValue = true;}}protected Criterion(String condition, Object value) {this(condition, value, null);}protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {super();this.condition = condition;this.value = value;this.secondValue = secondValue;this.typeHandler = typeHandler;this.betweenValue = true;}protected Criterion(String condition, Object value, Object secondValue) {this(condition, value, secondValue, null);}}
}

6.3,dao层

创建cn.e3mall.mapper包:

TbItemMapper.java

package cn.e3mall.mapper;import cn.e3mall.pojo.TbItem;
import cn.e3mall.pojo.TbItemExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;public interface TbItemMapper {int countByExample(TbItemExample example);int deleteByExample(TbItemExample example);int deleteByPrimaryKey(Long id);int insert(TbItem record);int insertSelective(TbItem record);List<TbItem> selectByExample(TbItemExample example);TbItem selectByPrimaryKey(Long id);int updateByExampleSelective(@Param("record") TbItem record, @Param("example") TbItemExample example);int updateByExample(@Param("record") TbItem record, @Param("example") TbItemExample example);int updateByPrimaryKeySelective(TbItem record);int updateByPrimaryKey(TbItem record);
}

TbItemMapper.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.e3mall.mapper.TbItemMapper" ><resultMap id="BaseResultMap" type="cn.e3mall.pojo.TbItem" ><id column="id" property="id" jdbcType="BIGINT" /><result column="title" property="title" jdbcType="VARCHAR" /><result column="sell_point" property="sellPoint" jdbcType="VARCHAR" /><result column="price" property="price" jdbcType="BIGINT" /><result column="num" property="num" jdbcType="INTEGER" /><result column="barcode" property="barcode" jdbcType="VARCHAR" /><result column="image" property="image" jdbcType="VARCHAR" /><result column="cid" property="cid" jdbcType="BIGINT" /><result column="status" property="status" jdbcType="TINYINT" /><result column="created" property="created" jdbcType="TIMESTAMP" /><result column="updated" property="updated" jdbcType="TIMESTAMP" /></resultMap><sql id="Example_Where_Clause" ><where ><foreach collection="oredCriteria" item="criteria" separator="or" ><if test="criteria.valid" ><trim prefix="(" suffix=")" prefixOverrides="and" ><foreach collection="criteria.criteria" item="criterion" ><choose ><when test="criterion.noValue" >and ${criterion.condition}</when><when test="criterion.singleValue" >and ${criterion.condition} #{criterion.value}</when><when test="criterion.betweenValue" >and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}</when><when test="criterion.listValue" >and ${criterion.condition}<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >#{listItem}</foreach></when></choose></foreach></trim></if></foreach></where></sql><sql id="Update_By_Example_Where_Clause" ><where ><foreach collection="example.oredCriteria" item="criteria" separator="or" ><if test="criteria.valid" ><trim prefix="(" suffix=")" prefixOverrides="and" ><foreach collection="criteria.criteria" item="criterion" ><choose ><when test="criterion.noValue" >and ${criterion.condition}</when><when test="criterion.singleValue" >and ${criterion.condition} #{criterion.value}</when><when test="criterion.betweenValue" >and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}</when><when test="criterion.listValue" >and ${criterion.condition}<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >#{listItem}</foreach></when></choose></foreach></trim></if></foreach></where></sql><sql id="Base_Column_List" >id, title, sell_point, price, num, barcode, image, cid, status, created, updated</sql><select id="selectByExample" resultMap="BaseResultMap" parameterType="cn.e3mall.pojo.TbItemExample" >select<if test="distinct" >distinct</if><include refid="Base_Column_List" />from tb_item<if test="_parameter != null" ><include refid="Example_Where_Clause" /></if><if test="orderByClause != null" >order by ${orderByClause}</if></select><select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >select <include refid="Base_Column_List" />from tb_itemwhere id = #{id,jdbcType=BIGINT}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >delete from tb_itemwhere id = #{id,jdbcType=BIGINT}</delete><delete id="deleteByExample" parameterType="cn.e3mall.pojo.TbItemExample" >delete from tb_item<if test="_parameter != null" ><include refid="Example_Where_Clause" /></if></delete><insert id="insert" parameterType="cn.e3mall.pojo.TbItem" >insert into tb_item (id, title, sell_point, price, num, barcode, image, cid, status, created, updated)values (#{id,jdbcType=BIGINT}, #{title,jdbcType=VARCHAR}, #{sellPoint,jdbcType=VARCHAR}, #{price,jdbcType=BIGINT}, #{num,jdbcType=INTEGER}, #{barcode,jdbcType=VARCHAR}, #{image,jdbcType=VARCHAR}, #{cid,jdbcType=BIGINT}, #{status,jdbcType=TINYINT}, #{created,jdbcType=TIMESTAMP}, #{updated,jdbcType=TIMESTAMP})</insert><insert id="insertSelective" parameterType="cn.e3mall.pojo.TbItem" >insert into tb_item<trim prefix="(" suffix=")" suffixOverrides="," ><if test="id != null" >id,</if><if test="title != null" >title,</if><if test="sellPoint != null" >sell_point,</if><if test="price != null" >price,</if><if test="num != null" >num,</if><if test="barcode != null" >barcode,</if><if test="image != null" >image,</if><if test="cid != null" >cid,</if><if test="status != null" >status,</if><if test="created != null" >created,</if><if test="updated != null" >updated,</if></trim><trim prefix="values (" suffix=")" suffixOverrides="," ><if test="id != null" >#{id,jdbcType=BIGINT},</if><if test="title != null" >#{title,jdbcType=VARCHAR},</if><if test="sellPoint != null" >#{sellPoint,jdbcType=VARCHAR},</if><if test="price != null" >#{price,jdbcType=BIGINT},</if><if test="num != null" >#{num,jdbcType=INTEGER},</if><if test="barcode != null" >#{barcode,jdbcType=VARCHAR},</if><if test="image != null" >#{image,jdbcType=VARCHAR},</if><if test="cid != null" >#{cid,jdbcType=BIGINT},</if><if test="status != null" >#{status,jdbcType=TINYINT},</if><if test="created != null" >#{created,jdbcType=TIMESTAMP},</if><if test="updated != null" >#{updated,jdbcType=TIMESTAMP},</if></trim></insert><select id="countByExample" parameterType="cn.e3mall.pojo.TbItemExample" resultType="java.lang.Integer" >select count(*) from tb_item<if test="_parameter != null" ><include refid="Example_Where_Clause" /></if></select><update id="updateByExampleSelective" parameterType="map" >update tb_item<set ><if test="record.id != null" >id = #{record.id,jdbcType=BIGINT},</if><if test="record.title != null" >title = #{record.title,jdbcType=VARCHAR},</if><if test="record.sellPoint != null" >sell_point = #{record.sellPoint,jdbcType=VARCHAR},</if><if test="record.price != null" >price = #{record.price,jdbcType=BIGINT},</if><if test="record.num != null" >num = #{record.num,jdbcType=INTEGER},</if><if test="record.barcode != null" >barcode = #{record.barcode,jdbcType=VARCHAR},</if><if test="record.image != null" >image = #{record.image,jdbcType=VARCHAR},</if><if test="record.cid != null" >cid = #{record.cid,jdbcType=BIGINT},</if><if test="record.status != null" >status = #{record.status,jdbcType=TINYINT},</if><if test="record.created != null" >created = #{record.created,jdbcType=TIMESTAMP},</if><if test="record.updated != null" >updated = #{record.updated,jdbcType=TIMESTAMP},</if></set><if test="_parameter != null" ><include refid="Update_By_Example_Where_Clause" /></if></update><update id="updateByExample" parameterType="map" >update tb_itemset id = #{record.id,jdbcType=BIGINT},title = #{record.title,jdbcType=VARCHAR},sell_point = #{record.sellPoint,jdbcType=VARCHAR},price = #{record.price,jdbcType=BIGINT},num = #{record.num,jdbcType=INTEGER},barcode = #{record.barcode,jdbcType=VARCHAR},image = #{record.image,jdbcType=VARCHAR},cid = #{record.cid,jdbcType=BIGINT},status = #{record.status,jdbcType=TINYINT},created = #{record.created,jdbcType=TIMESTAMP},updated = #{record.updated,jdbcType=TIMESTAMP}<if test="_parameter != null" ><include refid="Update_By_Example_Where_Clause" /></if></update><update id="updateByPrimaryKeySelective" parameterType="cn.e3mall.pojo.TbItem" >update tb_item<set ><if test="title != null" >title = #{title,jdbcType=VARCHAR},</if><if test="sellPoint != null" >sell_point = #{sellPoint,jdbcType=VARCHAR},</if><if test="price != null" >price = #{price,jdbcType=BIGINT},</if><if test="num != null" >num = #{num,jdbcType=INTEGER},</if><if test="barcode != null" >barcode = #{barcode,jdbcType=VARCHAR},</if><if test="image != null" >image = #{image,jdbcType=VARCHAR},</if><if test="cid != null" >cid = #{cid,jdbcType=BIGINT},</if><if test="status != null" >status = #{status,jdbcType=TINYINT},</if><if test="created != null" >created = #{created,jdbcType=TIMESTAMP},</if><if test="updated != null" >updated = #{updated,jdbcType=TIMESTAMP},</if></set>where id = #{id,jdbcType=BIGINT}</update><update id="updateByPrimaryKey" parameterType="cn.e3mall.pojo.TbItem" >update tb_itemset title = #{title,jdbcType=VARCHAR},sell_point = #{sellPoint,jdbcType=VARCHAR},price = #{price,jdbcType=BIGINT},num = #{num,jdbcType=INTEGER},barcode = #{barcode,jdbcType=VARCHAR},image = #{image,jdbcType=VARCHAR},cid = #{cid,jdbcType=BIGINT},status = #{status,jdbcType=TINYINT},created = #{created,jdbcType=TIMESTAMP},updated = #{updated,jdbcType=TIMESTAMP}where id = #{id,jdbcType=BIGINT}</update>
</mapper>

6.4,接口

创建cn.e3mall.service包:

ItemService.java

package cn.e3mall.service;import cn.e3mall.pojo.TbItem;public interface ItemService {TbItem getItemById(long itemId);
}

6.5,service

创建cn.e3mall.service.impl包

ItemServiceImpl.java

package cn.e3mall.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import cn.e3mall.mapper.TbItemMapper;
import cn.e3mall.pojo.TbItem;
import cn.e3mall.pojo.TbItemExample;
import cn.e3mall.pojo.TbItemExample.Criteria;
import cn.e3mall.service.ItemService;/*** 商品管理Service* <p>Title: ItemServiceImpl</p>* <p>Description: </p>* <p>Company: www.itcast.cn</p> * @version 1.0*/
@Service
public class ItemServiceImpl implements ItemService {@Autowiredprivate TbItemMapper itemMapper;@Overridepublic TbItem getItemById(long itemId) {//根据主键查询//TbItem tbItem = itemMapper.selectByPrimaryKey(itemId);TbItemExample example = new TbItemExample();Criteria criteria = example.createCriteria();//设置查询条件criteria.andIdEqualTo(itemId);//执行查询List<TbItem> list = itemMapper.selectByExample(example);if (list != null && list.size() > 0) {return list.get(0);}return null;}}

6.6,controller

创建cn.e3mall.controller目录

ItemController.java

package cn.e3mall.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import cn.e3mall.pojo.TbItem;
import cn.e3mall.service.ItemService;/*** 商品管理Controller* <p>Title: ItemController</p>* <p>Description: </p>* <p>Company: www.itcast.cn</p> * @version 1.0*/
@Controller
public class ItemController {@Autowiredprivate ItemService itemService;@RequestMapping("/item/{itemId}")@ResponseBodypublic TbItem getItemById(@PathVariable Long itemId) {TbItem tbItem = itemService.getItemById(itemId);return tbItem;}
}

6.7,解决mapper绑定异常的问题

在pom.xml文件的project节点内添加

<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 --><build><resources><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource></resources></build>

7,部署并且启动应用

选中e3-manager,右键,Run As -》 Maven Build

输入clean tomcat7:run

打开浏览器,访问http://localhost:8080/e3-manager-web/item/536563

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/530444.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何在windows机器上安装apache ab

为什么要使用apache ab&#xff0c;apache ab的是用来干嘛的&#xff0c;apache ab的命令&#xff1f; 如果您是来找上面这些问题的答案&#xff0c;那么请立刻关掉当前页面&#xff0c;因为这篇文章是纯粹的安装教程&#xff0c;避免浪费时间。 一&#xff0c;安装环境 win…

低版本Eclipse如何快速设置黑色主题

低版本的Eclipse&#xff08;例如Kepler Service Release 2&#xff09;&#xff0c;没有自带的黑色主题&#xff0c;并且在线安装黑色主题会报错 去网上找了一圈结果都是骗人的&#xff0c;这个时候别着急&#xff0c;只需三个简单的步骤即可完成黑色主题配置。 下面是设置完…

eclipse中配置jad反编译插件

1 下载Eclipse支持jad插件的jar包 将下载得到的net.sf.jadclipse_3.3.0.jar放到eclipse》plugins目录下 2 eclipse配置jad插件 下载jad.exe 3 设置文件关联

SpringCloud学习之路(一)-简单Demo

首先,我们先在C:\Windows\System32\drivers\etc路径下的hosts最后添加两个地址用于模拟服务端以及消费端的IP 127.0.0.1 dept-8001.com #微服务端127.0.0.1 client.com #消费端 SpringCloud的一个最基础Demo,本地真实模拟环境(服务的创建者以及消费者),这是我们Demo截图: 分为一…

eclipse工作区打不开的解决方案

今天上班的时候发现eclipse打开总是闪退&#xff0c;刚开始以为是eclipse的问题&#xff0c;后来才发现是工作区的问题。 分享出来&#xff0c;希望能够帮助遇到同样问题的人。 1 设置询问打开的工作区 eclipse安装目录\configuration\.settings\org.eclipse.ui.ide.prefs&a…

解决debug JDK source无法查看局部变量的问题

首先进入传送门&#xff1a;https://blog.csdn.net/majian_1987/article/details/51273609 进行上面的操作之后&#xff0c;这个时候我们来debug一下jdk源码&#xff0c;发现可以查看局部变量。但是这时候又发现一个问题&#xff0c;在我自己的代码里按住ctrl鼠标左键单击无法…

配置JAVA开发环境

1、首先去Oracle官方网站下载所需版本的JDKhttp://java.sun.com/products/archive/&#xff0c;然后安装&#xff0c;其实只需要安装jdk就可以了&#xff0c;不需要安装jre 2、按照后配置环境变量JAVA_HOME 然后在path最前面添加%JAVA_HOME%\bin; 3、cmd中输入java -version,提…

【JAVA基础篇】集合框架

一、集合框架图 Java集合框架主要包含两种类型的容器&#xff0c;一是集合(Collection)&#xff0c;存储元素集合&#xff0c;二是图(Map)&#xff0c;存储键(key)-值(value)对.Collection接口下面有两个重要的子接口List和Set&#xff0c;再下面是一些抽象类&#xff0c;最后是…

【JAVA基础篇】对象初始化过程

我们都知道&#xff0c;创建对象是由 new关键字调用构造方法 返回类实例&#xff08;实际上还可以通过反射来创建实例&#xff09;。 例如 : Person jack new Person(); 这句话到底做了什么事情呢 &#xff1f; 其实就是讲对象的初始化过程。 1、 new 用到了Person.class,所…

eclipse指定JDK版本启动,解决version XXX of the JVM is not suitable for this product.Version:XXX 问题

问题描述&#xff1a;启动eclipse时&#xff0c;提示version 1.7.0 of the JVM is not suitable for this product.Version:1.8 or greater is required. 原因分析&#xff1a;原因是我的笔记本安装了多个JDK版本&#xff0c;但是现在我的JAVA_HOME配置的是jdk1.7的路径&#x…

【JAVA基础篇】IO流

一、流的概念 “对语言设计人员来说&#xff0c;创建好的输入&#xff0f;输出系统是一项特别困难的任务。” ――《Think in Java》 无论是系统、还是语言的设计中IO的设计都是异常复杂的。面临的最大的挑战一般是如何覆盖所有可能的因素&#xff0c;我们不仅仅要考虑文件、…

【JAVA基础篇】运算符

一、表达式 表达式由运算符和操作数组成 例如&#xff1a; 5 num1 num1num2 sumnum1num2 二、运算符分类 算数运算符、赋值运算符、关系运算符、逻辑运算符、条件运算符、位运算符 三、算数运算符 四、赋值运算符 格式&#xff1a;变量表达式 例如&#xff1a;int n3…

a4纸网页打印 table_打印模板不愁人,你还在打印单调的A4纸吗?

软件介绍早在几年前&#xff0c;社会上就已经开始了数字化、无纸化的推广&#xff0c;但是就算再怎么无纸化&#xff0c;纸张还是有它必要的存在&#xff0c;在工作、学习过程中&#xff0c;打印的需求也必不可少的。但是一般的打印都是比较平庸的&#xff0c;要做会议记录&…

上证指数30年k线图_技术预判2020:上证指数要突破3500点才会“井喷”

2019年的行情很快就要收官了&#xff0c;截止目前&#xff0c;上证指数今年的涨幅是20.5%&#xff0c;不过可能有部分投资者今年的收益率还没达到大盘指数的平均水平。不管怎样&#xff0c;今年很快就要翻篇了&#xff0c;关键是看2020年股市能不能迎来更好的行情了。而总结得失…

的优缺点_浅谈桉木家具的优缺点

家具现在的材质是有很多的&#xff0c;木质的&#xff0c;石材的&#xff0c;还有真空充气的&#xff0c;都是很不错的类型。桉木家具是现在很多人都喜欢的一种材质&#xff0c;但是很多人对桉木家具的优缺点不是很清楚&#xff0c;为了能够让大家更加清楚的了解桉木家具&#…

客户说发货慢怎么回复_女生微信说身体不舒服怎么回复关心她?

当你不在女生身边&#xff0c;女生微信给你说身体不舒服&#xff0c;肯定需要说点话来安慰她了。多喝热水肯定是不行了&#xff0c;一点用处都没有&#xff0c;还会让女生觉得你根本不重视她&#xff0c;是在敷衍她&#xff0c;那女生微信说身体不舒服怎么回复关心她呢&#xf…

【算法篇】八种内排序算法

常用的八种内排序算法分别是&#xff1a; 交换排序&#xff1a;冒泡排序、快速排序选择排序&#xff1a;简单选择排序、堆排序插入排序&#xff1a;直接插入排序、希尔排序归并排序基数排序 内排序巧记&#xff1a;选(选择)舰(简单选择)队(堆)的时候脚(交换)毛(冒泡)快(快速)&…

数据分析专题报告范文6篇_小学生看图写话范文:小熊玩跷跷板?(6篇),让孩子参考练习...

​范文01&#xff1a;小熊跷跷板一天&#xff0c;天气晴朗&#xff0c;胖乎乎的小熊和小白兔一起玩跷跷板。小熊一屁股坐在地上&#xff0c;小白兔说&#xff1a;“啊&#xff01;我有恐高症哇&#xff01;”小熊说&#xff1a;“我比你重&#xff0c;所以你没有办法把我翘起来…

win10环境安装使用svn客户端和服务端

一、下载安装包 安装包下载传送门http://subversion.apache.org/packages.html 无法下载的童鞋去百度云下载 链接&#xff1a;https://pan.baidu.com/s/1EuGohoZKIPmRvynp5-Subw 提取码&#xff1a;ohna 链接&#xff1a;https://pan.baidu.com/s/1EJrd5DzGCBE4rRfdhuno6Q …

所选元素非联通_非固化橡胶沥青防水涂料与耐根穿刺防水卷材(沥青基)施工要点...

目前&#xff0c;非固化复合耐根穿刺防水卷材在车库顶板的应用逐渐受到客户及用户的认可&#xff0c;也有不少慕名而来的防水从业者打电话来咨询此系统的应用情况及优势。下面就由小编来给大家系统介绍此应用系统的特点吧。01性能优势1.两种材料高度的相容性非固化橡胶沥青防水…