企业项目开发--企业中的项目架构以及多环境分配(3)
此文已由作者赵计刚授权网易云社区发布。
欢迎访问网易云社区,了解更多网易技术产品运营经验。
2.2.3、ssmm0-userManagement
pom.xml
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 5 <modelVersion>4.0.0</modelVersion> 6 7 <!-- 指定父模块 --> 8 <parent> 9 <groupId>com.xxx</groupId> 10 <artifactId>ssmm0</artifactId> 11 <version>1.0-SNAPSHOT</version> 12 </parent> 13 14 <groupId>com.xxx.ssmm0</groupId> 15 <artifactId>ssmm0-userManagement</artifactId> 16 <!--<version>1.0-SNAPSHOT</version>--><!-- 父模块已经指定了版本号,这里就不用了--> 17 18 <name>ssmm0-userManagement</name> 19 <packaging>war</packaging><!-- 需要部署的模块 --> 20 21 <!-- 引入实际依赖 --> 22 <dependencies> 23 <!-- 将ssmm0-data项目作为一个jar引入项目中 --> 24 <dependency> 25 <groupId>com.xxx.ssmm0</groupId> 26 <artifactId>ssmm0-data</artifactId> 27 <version>1.0-SNAPSHOT</version> 28 </dependency> 29 <!-- servlet --> 30 <dependency> 31 <groupId>javax.servlet</groupId> 32 <artifactId>javax.servlet-api</artifactId> 33 </dependency> 34 <!-- spring mvc --> 35 <dependency> 36 <groupId>org.springframework</groupId> 37 <artifactId>spring-web</artifactId> 38 </dependency> 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-webmvc</artifactId> 42 </dependency> 43 <!-- 这个是使用velocity的必备包 --> 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-context-support</artifactId> 47 </dependency> 48 <!-- velocity --> 49 <dependency> 50 <groupId>org.apache.velocity</groupId> 51 <artifactId>velocity</artifactId> 52 </dependency> 53 <dependency> 54 <groupId>velocity-tools</groupId> 55 <artifactId>velocity-tools-generic</artifactId> 56 </dependency> 57 </dependencies> 58 </project>
注意:将ssmm0-data作为普通的jar引入即可。
spring.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-3.2.xsd 9 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 10 11 <!-- 注解扫描 --> 12 <context:component-scan base-package="com.xxx" /> 13 14 <!-- 配置fastjson转换器 --> 15 <mvc:annotation-driven> 16 <mvc:message-converters register-defaults="true"> 17 <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"></bean> 18 </mvc:message-converters> 19 </mvc:annotation-driven> 20 21 <!-- 引入数据源,这里变量的读取都是从ssmm0的pom.xml中读取的 --> 22 <bean id="xxxDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"> 23 <property name="driverClassName" value="${jdbc.driverClassName}" /> 24 <property name="url" value="${jdbc.url}" /> 25 <property name="username" value="${jdbc.username}" /> 26 <property name="password" value="${jdbc.password}" /> 27 </bean> 28 29 <!-- 引入mybatis --> 30 <bean id="xxxSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 31 <property name="dataSource" ref="xxxDataSource" /> 32 </bean> 33 <bean id="xxxMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 34 <!-- 35 这里就是包名为什么就做com.xxx.mapper.user而非com.xxx.user.mapper, 36 这样的话,比如说有两个项目com.xxx.mapper.user和com.xxx.mapper.hotel,value只需写作com.xxx.mapper即可 37 否则,value就要写作com.xxx.user.mapper,com.xxx.hotel.mapper 38 --> 39 <property name="basePackage" value="com.xxx.mapper" /> 40 <property name="sqlSessionFactoryBeanName" value="xxxSqlSessionFactory" /> 41 </bean> 42 43 <!-- 配置velocity --> 44 <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> 45 <property name="resourceLoaderPath"> 46 <value>WEB-INF/templates/</value> 47 </property> 48 <property name="velocityProperties"> 49 <props> 50 <prop key="input.encoding">UTF-8</prop> 51 <prop key="output.encoding">UTF-8</prop> 52 </props> 53 </property> 54 </bean> 55 <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> 56 <property name="suffix" value=".vm" /> 57 <property name="contentType" value="text/html;charset=utf-8" /> 58 <property name="dateToolAttribute" value="date"/> 59 <property name="numberToolAttribute" value="number"/> 60 </bean> 61 </beans>
注意:这里对包名的体现,由于直接使用spring.xml去读ssmm0的 pom.xml了,所以jdbc.properties文件就不要了,在spring.xml中,指定文件位置的<context:property-placeholder>标签就删掉了
web.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 5 6 <servlet> 7 <servlet-name>dispatcherServlet</servlet-name> 8 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 9 <init-param> 10 <param-name>contextConfigLocation</param-name> 11 <param-value>classpath:spring.xml</param-value> 12 </init-param> 13 <load-on-startup>1</load-on-startup> 14 </servlet> 15 <servlet-mapping> 16 <servlet-name>dispatcherServlet</servlet-name> 17 <url-pattern>/</url-pattern> 18 </servlet-mapping> 19 20 <filter> 21 <filter-name>encodingFilter</filter-name> 22 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 23 <init-param> 24 <param-name>encoding</param-name> 25 <param-value>UTF-8</param-value> 26 </init-param> 27 <init-param> 28 <param-name>forceEncoding</param-name> 29 <param-value>true</param-value> 30 </init-param> 31 </filter> 32 <filter-mapping> 33 <filter-name>encodingFilter</filter-name> 34 <url-pattern>/*</url-pattern> 35 </filter-mapping> 36 37 <welcome-file-list> 38 <welcome-file>/index.jsp</welcome-file> 39 </welcome-file-list> 40 </web-app>
com.xxx.web.admin.AdminController
1 package com.xxx.web.admin; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestParam; 7 import org.springframework.web.bind.annotation.ResponseBody; 8 import org.springframework.web.servlet.ModelAndView; 9 10 import com.xxx.model.userManagement.Admin; 11 import com.xxx.service.userManagement.AdminService; 12 13 /** 14 * adminController 15 */ 16 @Controller 17 @RequestMapping("/admin") 18 public class AdminController { 19 20 @Autowired 21 private AdminService adminService; 22 23 /** 24 * 管理员注册 25 */ 26 @ResponseBody 27 @RequestMapping("/register") 28 public boolean register(@RequestParam("username") String username, 29 @RequestParam("password") String password){ 30 Admin admin = new Admin(); 31 admin.setUsername(username); 32 admin.setPassword(password); 33 34 boolean isRegisterSuccess = adminService.register(admin); 35 36 return isRegisterSuccess; 37 } 38 39 /** 40 * 管理员登录 41 */ 42 @RequestMapping("/login") 43 public ModelAndView login(@RequestParam("username") String username, 44 @RequestParam("password") String password){ 45 Admin admin = adminService.login(username, password); 46 47 ModelAndView modelAndView = new ModelAndView(); 48 if(admin == null){ 49 modelAndView.addObject("message", "用户不存在或者密码错误!请重新输入"); 50 modelAndView.setViewName("error"); 51 }else{ 52 modelAndView.addObject("admin", admin); 53 modelAndView.setViewName("userinfo"); 54 } 55 56 return modelAndView; 57 } 58 }
error.vm
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>登录失败</title> 6 </head> 7 <body> 8 <div> 9 $message 10 </div> 11 </body> 12 </html>
userinfo.vm
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>登录成功</title> 6 </head> 7 <body> 8 <div> 9 id:$admin.id 10 username:$admin.username 11 password:$admin.password 12 </div> 13 </body> 14 </html>
具体测试过程:
直接运行jetty的话,会发现连不上数据库服务器99.99.99.999,这是因为默认启动服务器之后,我们使用的是prod的一套env,这套env中的数据库服务器是我自己乱写的:99.99.99.999
这时候,在ssmm0-userManagement项目上右击-->"Build Path"-->"Configure Build Path"-->"Maven"-->修改env,如下图所示,这样就切换到了dev的env下,之后再运行jetty,如果依旧不行的话,就采用ssmm0的pom.xml文件中我写的那块注释的方法。
这样,一个基本上就是企业中开发常用的结构的项目就完成了,这个项目非常重要,一定自己试着去写一个,一定要仔细看其中的每一条注释。
免费领取验证码、内容安全、短信发送、直播点播体验包及云服务器等套餐
更多网易技术、产品、运营经验分享请点击。
相关文章:
【推荐】 深情留不住,套路得人心--聊聊套路那些事儿
【推荐】 Bug是一种财富-------研发同学的错题集、测试同学的遗漏用例集
【推荐】 PaaS服务之路漫谈(三)