一、Maven项目搭建
1)在父工程中进行依赖管理,并没有导入jar包
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>cn.e3mall</groupId> 5 <artifactId>e3-parent</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 <modules> 8 <module>e3-common</module> 9 <module>e3-manager</module> 10 </modules> 11 <packaging>pom</packaging> 12 <!-- 集中定义依赖版本号 --> 13 <properties> 14 <junit.version>4.12</junit.version> 15 <spring.version>4.2.4.RELEASE</spring.version> 16 <mybatis.version>3.2.8</mybatis.version> 17 <mybatis.spring.version>1.2.2</mybatis.spring.version> 18 <mybatis.paginator.version>1.2.15</mybatis.paginator.version> 19 <mysql.version>5.1.32</mysql.version> 20 <slf4j.version>1.6.4</slf4j.version> 21 <jackson.version>2.4.2</jackson.version> 22 <druid.version>1.0.9</druid.version> 23 <httpclient.version>4.3.5</httpclient.version> 24 <jstl.version>1.2</jstl.version> 25 <servlet-api.version>2.5</servlet-api.version> 26 <jsp-api.version>2.0</jsp-api.version> 27 <joda-time.version>2.5</joda-time.version> 28 <commons-lang3.version>3.3.2</commons-lang3.version> 29 <commons-io.version>1.3.2</commons-io.version> 30 <commons-net.version>3.3</commons-net.version> 31 <pagehelper.version>3.2.3</pagehelper.version> 32 <!--<pagehelper.version>3.4.2-fix</pagehelper.version>--> 33 <jsqlparser.version>0.9.1</jsqlparser.version> 34 <commons-fileupload.version>1.3.1</commons-fileupload.version> 35 <jedis.version>2.7.2</jedis.version> 36 <solrj.version>4.10.3</solrj.version> 37 <dubbo.version>2.5.3</dubbo.version> 38 <zookeeper.version>3.4.7</zookeeper.version> 39 <zkclient.version>0.1</zkclient.version> 40 <activemq.version>5.11.2</activemq.version> 41 <freemarker.version>2.3.23</freemarker.version> 42 <quartz.version>2.2.2</quartz.version> 43 </properties> 44 45 <!--这里只是对依赖进行管理,并没有导入jar包--> 46 <dependencyManagement> 47 <dependencies> 48 <!-- 时间操作组件 --> 49 <dependency> 50 <groupId>joda-time</groupId> 51 <artifactId>joda-time</artifactId> 52 <version>${joda-time.version}</version> 53 </dependency> 54 <!-- Apache工具组件 --> 55 <dependency> 56 <groupId>org.apache.commons</groupId> 57 <artifactId>commons-lang3</artifactId> 58 <version>${commons-lang3.version}</version> 59 </dependency> 60 <dependency> 61 <groupId>org.apache.commons</groupId> 62 <artifactId>commons-io</artifactId> 63 <version>${commons-io.version}</version> 64 </dependency> 65 <dependency> 66 <groupId>commons-net</groupId> 67 <artifactId>commons-net</artifactId> 68 <version>${commons-net.version}</version> 69 </dependency> 70 <!-- Jackson Json处理工具包 --> 71 <dependency> 72 <groupId>com.fasterxml.jackson.core</groupId> 73 <artifactId>jackson-databind</artifactId> 74 <version>${jackson.version}</version> 75 </dependency> 76 <!-- httpclient --> 77 <dependency> 78 <groupId>org.apache.httpcomponents</groupId> 79 <artifactId>httpclient</artifactId> 80 <version>${httpclient.version}</version> 81 </dependency> 82 <!-- quartz任务调度框架 --> 83 <dependency> 84 <groupId>org.quartz-scheduler</groupId> 85 <artifactId>quartz</artifactId> 86 <version>${quartz.version}</version> 87 </dependency> 88 <!-- 单元测试 --> 89 <dependency> 90 <groupId>junit</groupId> 91 <artifactId>junit</artifactId> 92 <version>${junit.version}</version> 93 <scope>test</scope> 94 </dependency> 95 <!-- 日志处理 --> 96 <dependency> 97 <groupId>org.slf4j</groupId> 98 <artifactId>slf4j-log4j12</artifactId> 99 <version>${slf4j.version}</version> 100 </dependency> 101 <!-- Mybatis --> 102 <dependency> 103 <groupId>org.mybatis</groupId> 104 <artifactId>mybatis</artifactId> 105 <version>${mybatis.version}</version> 106 </dependency> 107 <dependency> 108 <groupId>org.mybatis</groupId> 109 <artifactId>mybatis-spring</artifactId> 110 <version>${mybatis.spring.version}</version> 111 </dependency> 112 <dependency> 113 <groupId>com.github.miemiedev</groupId> 114 <artifactId>mybatis-paginator</artifactId> 115 <version>${mybatis.paginator.version}</version> 116 </dependency> 117 <dependency> 118 <groupId>com.github.pagehelper</groupId> 119 <artifactId>pagehelper</artifactId> 120 <version>${pagehelper.version}</version> 121 </dependency> 122 <!-- MySql --> 123 <dependency> 124 <groupId>mysql</groupId> 125 <artifactId>mysql-connector-java</artifactId> 126 <version>${mysql.version}</version> 127 </dependency> 128 <!-- 连接池 --> 129 <dependency> 130 <groupId>com.alibaba</groupId> 131 <artifactId>druid</artifactId> 132 <version>${druid.version}</version> 133 </dependency> 134 <!-- Spring --> 135 <dependency> 136 <groupId>org.springframework</groupId> 137 <artifactId>spring-context</artifactId> 138 <version>${spring.version}</version> 139 </dependency> 140 <dependency> 141 <groupId>org.springframework</groupId> 142 <artifactId>spring-beans</artifactId> 143 <version>${spring.version}</version> 144 </dependency> 145 <dependency> 146 <groupId>org.springframework</groupId> 147 <artifactId>spring-webmvc</artifactId> 148 <version>${spring.version}</version> 149 </dependency> 150 <dependency> 151 <groupId>org.springframework</groupId> 152 <artifactId>spring-jdbc</artifactId> 153 <version>${spring.version}</version> 154 </dependency> 155 <dependency> 156 <groupId>org.springframework</groupId> 157 <artifactId>spring-aspects</artifactId> 158 <version>${spring.version}</version> 159 </dependency> 160 <dependency> 161 <groupId>org.springframework</groupId> 162 <artifactId>spring-jms</artifactId> 163 <version>${spring.version}</version> 164 </dependency> 165 <dependency> 166 <groupId>org.springframework</groupId> 167 <artifactId>spring-context-support</artifactId> 168 <version>${spring.version}</version> 169 </dependency> 170 <!-- JSP相关 --> 171 <dependency> 172 <groupId>jstl</groupId> 173 <artifactId>jstl</artifactId> 174 <version>${jstl.version}</version> 175 </dependency> 176 <dependency> 177 <groupId>javax.servlet</groupId> 178 <artifactId>servlet-api</artifactId> 179 <version>${servlet-api.version}</version> 180 <scope>provided</scope> 181 </dependency> 182 <dependency> 183 <groupId>javax.servlet</groupId> 184 <artifactId>jsp-api</artifactId> 185 <version>${jsp-api.version}</version> 186 <scope>provided</scope> 187 </dependency> 188 <!-- 文件上传组件 --> 189 <dependency> 190 <groupId>commons-fileupload</groupId> 191 <artifactId>commons-fileupload</artifactId> 192 <version>${commons-fileupload.version}</version> 193 </dependency> 194 <!-- Redis客户端 --> 195 <dependency> 196 <groupId>redis.clients</groupId> 197 <artifactId>jedis</artifactId> 198 <version>${jedis.version}</version> 199 </dependency> 200 <!-- solr客户端 --> 201 <dependency> 202 <groupId>org.apache.solr</groupId> 203 <artifactId>solr-solrj</artifactId> 204 <version>${solrj.version}</version> 205 </dependency> 206 <!-- dubbo相关 --> 207 <dependency> 208 <groupId>com.alibaba</groupId> 209 <artifactId>dubbo</artifactId> 210 <version>${dubbo.version}</version> 211 </dependency> 212 <dependency> 213 <groupId>org.apache.zookeeper</groupId> 214 <artifactId>zookeeper</artifactId> 215 <version>${zookeeper.version}</version> 216 </dependency> 217 <dependency> 218 <groupId>com.github.sgroschupf</groupId> 219 <artifactId>zkclient</artifactId> 220 <version>${zkclient.version}</version> 221 </dependency> 222 <dependency> 223 <groupId>org.apache.activemq</groupId> 224 <artifactId>activemq-all</artifactId> 225 <version>${activemq.version}</version> 226 </dependency> 227 <dependency> 228 <groupId>org.freemarker</groupId> 229 <artifactId>freemarker</artifactId> 230 <version>${freemarker.version}</version> 231 </dependency> 232 233 </dependencies> 234 </dependencyManagement> 235 236 <build> 237 <finalName>${project.artifactId}</finalName> 238 <plugins> 239 <!-- 资源文件拷贝插件 --> 240 <plugin> 241 <groupId>org.apache.maven.plugins</groupId> 242 <artifactId>maven-resources-plugin</artifactId> 243 <version>2.7</version> 244 <configuration> 245 <encoding>UTF-8</encoding> 246 </configuration> 247 </plugin> 248 <!-- java编译插件 --> 249 <plugin> 250 <groupId>org.apache.maven.plugins</groupId> 251 <artifactId>maven-compiler-plugin</artifactId> 252 <version>3.2</version> 253 <configuration> 254 <source>1.7</source> 255 <target>1.7</target> 256 <encoding>UTF-8</encoding> 257 </configuration> 258 </plugin> 259 </plugins> 260 <pluginManagement> 261 <plugins> 262 <!-- 配置Tomcat插件 --> 263 <plugin> 264 <groupId>org.apache.tomcat.maven</groupId> 265 <artifactId>tomcat7-maven-plugin</artifactId> 266 <version>2.2</version> 267 </plugin> 268 </plugins> 269 </pluginManagement> 270 </build> 271 </project> 272 273 274 <!-- 275 <scope>provided</scope> 276 编译测试都用它,运行是不用(tomcat会提供,可能引起冲突) 277 278 设置资源路径 279 -->
2)pojo层(什么都不引入)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>e3-manager</artifactId> 7 <groupId>cn.e3mall</groupId> 8 <version>0.0.1-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>e3-manager-pojo</artifactId> 13 14 15 16 </project>
3)dao层(引入Mybatis,MySQL,连接池,pojo层)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>e3-manager</artifactId> 7 <groupId>cn.e3mall</groupId> 8 <version>0.0.1-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>e3-manager-dao</artifactId> 13 <dependencies> 14 <dependency> 15 <groupId>cn.e3mall</groupId> 16 <artifactId>e3-manager-pojo</artifactId> 17 <version>0.0.1-SNAPSHOT</version> 18 </dependency> 19 20 <!-- Mybatis --> 21 <dependency> 22 <groupId>org.mybatis</groupId> 23 <artifactId>mybatis</artifactId> 24 </dependency> 25 <dependency> 26 <groupId>org.mybatis</groupId> 27 <artifactId>mybatis-spring</artifactId> 28 </dependency> 29 <dependency> 30 <groupId>com.github.miemiedev</groupId> 31 <artifactId>mybatis-paginator</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>com.github.pagehelper</groupId> 35 <artifactId>pagehelper</artifactId> 36 </dependency> 37 <!-- MySql --> 38 <dependency> 39 <groupId>mysql</groupId> 40 <artifactId>mysql-connector-java</artifactId> 41 </dependency> 42 <!-- 连接池 --> 43 <dependency> 44 <groupId>com.alibaba</groupId> 45 <artifactId>druid</artifactId> 46 </dependency> 47 </dependencies> 48 49 </project>
4)interface层(引入pojo层)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>e3-manager</artifactId> 7 <groupId>cn.e3mall</groupId> 8 <version>0.0.1-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>e3-manager-interface</artifactId> 13 <dependencies> 14 <dependency> 15 <groupId>cn.e3mall</groupId> 16 <artifactId>e3-manager-pojo</artifactId> 17 <version>0.0.1-SNAPSHOT</version> 18 </dependency> 19 </dependencies> 20 21 22 </project>
5)Service层(引入interface层,dao层,Spring)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>e3-manager</artifactId> 7 <groupId>cn.e3mall</groupId> 8 <version>0.0.1-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>e3-manager-service</artifactId> 13 <dependencies> 14 <dependency> 15 <groupId>cn.e3mall</groupId> 16 <artifactId>e3-manager-dao</artifactId> 17 <version>0.0.1-SNAPSHOT</version> 18 </dependency> 19 <dependency> 20 <groupId>cn.e3mall</groupId> 21 <artifactId>e3-manager-interface</artifactId> 22 <version>0.0.1-SNAPSHOT</version> 23 </dependency> 24 <!-- Spring --> 25 <dependency> 26 <groupId>org.springframework</groupId> 27 <artifactId>spring-context</artifactId> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>spring-beans</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework</groupId> 35 <artifactId>spring-webmvc</artifactId> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework</groupId> 39 <artifactId>spring-jdbc</artifactId> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework</groupId> 43 <artifactId>spring-aspects</artifactId> 44 </dependency> 45 <dependency> 46 <groupId>org.springframework</groupId> 47 <artifactId>spring-jms</artifactId> 48 </dependency> 49 <dependency> 50 <groupId>org.springframework</groupId> 51 <artifactId>spring-context-support</artifactId> 52 </dependency> 53 </dependencies> 54 55 56 </project>
6)引入JSP相关
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4 <parent> 5 <artifactId>e3-manager</artifactId> 6 <groupId>cn.e3mall</groupId> 7 <version>0.0.1-SNAPSHOT</version> 8 </parent> 9 10 <modelVersion>4.0.0</modelVersion> 11 <packaging>war</packaging> 12 13 <name>e3-manager-web</name> 14 <artifactId>e3-manager-web</artifactId> 15 16 <dependencies> 17 <dependency> 18 <groupId>cn.e3mall</groupId> 19 <artifactId>e3-manager-service</artifactId> 20 <version>0.0.1-SNAPSHOT</version> 21 </dependency> 22 23 <!-- JSP相关 --> 24 <dependency> 25 <groupId>jstl</groupId> 26 <artifactId>jstl</artifactId> 27 </dependency> 28 <dependency> 29 <groupId>javax.servlet</groupId> 30 <artifactId>servlet-api</artifactId> 31 <scope>provided</scope> 32 </dependency> 33 <dependency> 34 <groupId>javax.servlet</groupId> 35 <artifactId>jsp-api</artifactId> 36 <scope>provided</scope> 37 </dependency> 38 </dependencies> 39 40 </project>
二、SSM项目搭建
1)Springmvc
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--*****************配置各种 注解******************--> <context:component-scan base-package="cn.e3mall.controller" /> <!--*******--> <!-- 配置注解驱动(由于默认的已经过时),用于扫描RequestMapping等注解 --> <mvc:annotation-driven /> <!-- 视图解释器 modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp"); ==> modelAndView.setViewName("itemList"); --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
2)applicationContext-dao.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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- ****************整合Mybatis**************** --> <!-- 读取配置文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 设置数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClass}" /> <property name="url" value="${jdbc.jdbcUrl}" /> <property name="username" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <!-- 建立Mybatis的SQLSessionFactory工厂 --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 核心配置文件的位置 --> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> </bean> <!-- Mapper动态代理开发 扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 基本包,会自动扫描子包 --> <property name="basePackage" value="cn.e3mall.mapper"/> </bean> </beans>
3)applicationContext-service.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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 扫描@Controler,@Service... --> <context:component-scan base-package="cn.e3mall.service"/> </beans>
4)applicationContext-transaction.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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--******************配置 注解 事务********************--> <!-- 配置事务核心管理器,封装了所有事务操作. 依赖于连接池 --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name="dataSource" ref="dataSource" /> </bean> <!-- 开启使用注解管理aop事务 --> <tx:annotation-driven/> </beans>
5)web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 处理POST提交乱码问题 --> <filter> <filter-name>encoding</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>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置寻找的配置文件+监听器 --> <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> <!-- Spring MVC的前端控制器 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <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>dispatcher</servlet-name> <!-- 1. /* 拦截所有 jsp js png .css 真的全拦截 建议不使用 2. *.action *.do 拦截以do action 结尾的请求 肯定能使用 ERP 3. / 拦截所有 (不包括jsp) (包含.js .png.css) 强烈建议使用 前台 面向消费者 www.jd.com/search /对静态资源放行 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
6)sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 设置别名 --> <typeAliases> <!-- 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 --> <package name="cn.x5456.domain" /> </typeAliases> <!--<mappers>--> <!--<package name="cn.x5456.dao"/> <!–配置扫描mapper接口(一定要配)–>--> <!--</mappers>--> </configuration>
三、小Demo
由于使用的是逆向工程的方法,dao,pojo不用我们整
1)Service层(逆向工程条件查询)
@Service @Transactional public class ItemServiceImpl implements ItemService { @Autowired private TbItemMapper itemMapper; @Override public TbItem getItemById(Long id) { // 逆向工程两种通过主键查询的方法 // 方法一: // itemMapper.selectByPrimaryKey(id); // 方法二: TbItemExample example = new TbItemExample(); // 1.设置查询条件 Criteria criteria = example.createCriteria(); criteria.andIdEqualTo(id); // 2.执行查询 List<TbItem> itemList = itemMapper.selectByExample(example); // 3.返回 if (itemList!=null&&itemList.size()>0){ return itemList.get(0); } return null; } }
2)Controller层(引入路径接收参数的方法)
@Controller public class ItemController { @Autowired private ItemService itemService; // 又一种接收参数的方法,路径接受 @RequestMapping("/item/{itemId}") @ResponseBody // 返回一个json private TbItem getItemById(@PathVariable Long itemId){ return itemService.getItemById(itemId); } }
原因
此异常的原因是由于mapper接口编译后在同一个目录下没有找到mapper映射文件而出现的。由于maven工程在默认情况下src/main/java目录下的mapper文件是不发布到target目录下的。
解决方法1
在e3-manager-dao工程的pom文件中添加如下内容:(原理:将配置文件路径,改到java目录下)
<!-- 如果不添加此节点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>
解决方法2:
将映射文件放到resources目录下