1、业务模块与数据模块分离
在实际开发中,我们项目的架构业务模块和数据模块是分离的,举个例子,假设我们的项目有"人员管理模块"和"酒店管理模块"两个模块,按照上一章的介绍,我们会建立下图所示的项目结构:
其中,人员管理模块的controller、service、dao、mapper都在一个项目中,而在实际使用中,我们会将数据模块分离出来,即将以上两个子模块的service、dao、mapper拿出来,放在一个子项目中,形成如下的项目结构:
注意以下几点:
- 包的命名最好是com.xxx.mapper.user和不是com.xxx.user.mapper,前者在spring.xml中配置mybatis时更方便,具体见spring.xml的中的注释
- 在controller那一层的项目是需要部署的,即是war,而下边的数据模块是作为war的一个jar,所以在war层的pom.xml需要将下边的数据模块作为一个jar来引入到项目中
- service层到底是放在业务模块处还是放在数据模块处,这个根据需求而定,一般而言,都放在数据模块处,方便彼此service的调用,如userService调用hotelService,如果这个时候把两个service分别放在各自的业务模块层中,相互的调用就要通过RPC了,当然,有的时候可能有些与其他模块都不调用的service放在war层可能会好一些。
- 将来编写的缓存模块类、通用模块类、RPC工具类等都会作为jar被war层调用。
2、实现
我将上一章的项目做了修改,将ssmm项目改成了userManagement项目,并将userManagement项目实现了业务模块和数据模块的分离,具体的操作参照第一章和第二章的相关内容,这里直接给出项目结构和各个文件。
2.1、项目结构
2.2、代码实现
2.2.1、ssmm0
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 <groupId>com.xxx</groupId> 8 <artifactId>ssmm0</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <name>ssmm0</name> 12 <packaging>pom</packaging><!-- 父模块 --> 13 14 <!-- 管理子模块 --> 15 <modules> 16 <module>userManagement</module><!-- 具体业务1-人员管理系统 --> 17 <module>data</module><!-- 封装数据操作 --> 18 </modules> 19 20 <properties> 21 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 22 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 23 </properties> 24 25 <!-- dependencyManagement不会引入实际的依赖,只是作为一个依赖池,供其和其子类使用 --> 26 <dependencyManagement> 27 <dependencies> 28 <!-- json --> 29 <dependency> 30 <groupId>com.alibaba</groupId> 31 <artifactId>fastjson</artifactId> 32 <version>1.1.39</version> 33 </dependency> 34 <!-- servlet --> 35 <dependency> 36 <groupId>javax.servlet</groupId> 37 <artifactId>javax.servlet-api</artifactId> 38 <version>3.0.1</version> 39 <scope>provided</scope> 40 </dependency> 41 <!-- spring --> 42 <dependency> 43 <groupId>org.springframework</groupId> 44 <artifactId>spring-core</artifactId> 45 <version>3.2.6.RELEASE</version> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework</groupId> 49 <artifactId>spring-beans</artifactId> 50 <version>3.2.6.RELEASE</version> 51 </dependency> 52 <dependency> 53 <groupId>org.springframework</groupId> 54 <artifactId>spring-context</artifactId> 55 <version>3.2.6.RELEASE</version> 56 </dependency> 57 <dependency> 58 <groupId>org.springframework</groupId> 59 <artifactId>spring-web</artifactId> 60 <version>3.2.6.RELEASE</version> 61 </dependency> 62 <dependency> 63 <groupId>org.springframework</groupId> 64 <artifactId>spring-webmvc</artifactId> 65 <version>3.2.6.RELEASE</version> 66 </dependency> 67 <!-- 这个是使用velocity的必备包 --> 68 <dependency> 69 <groupId>org.springframework</groupId> 70 <artifactId>spring-context-support</artifactId> 71 <version>3.2.6.RELEASE</version> 72 </dependency> 73 <!-- mysql --> 74 <dependency> 75 <groupId>mysql</groupId> 76 <artifactId>mysql-connector-java</artifactId> 77 <version>5.1.27</version> 78 <scope>runtime</scope> 79 </dependency> 80 <!-- 数据源 --> 81 <dependency> 82 <groupId>org.apache.tomcat</groupId> 83 <artifactId>tomcat-jdbc</artifactId> 84 <version>7.0.47</version> 85 </dependency> 86 <!-- mybatis --> 87 <dependency> 88 <groupId>org.mybatis</groupId> 89 <artifactId>mybatis</artifactId> 90 <version>3.1.1</version> 91 </dependency> 92 <dependency> 93 <groupId>org.mybatis</groupId> 94 <artifactId>mybatis-spring</artifactId> 95 <version>1.1.1</version> 96 </dependency> 97 <!-- velocity --> 98 <dependency> 99 <groupId>org.apache.velocity</groupId> 100 <artifactId>velocity</artifactId> 101 <version>1.5</version> 102 </dependency> 103 <dependency> 104 <groupId>velocity-tools</groupId> 105 <artifactId>velocity-tools-generic</artifactId> 106 <version>1.2</version> 107 </dependency> 108 <!-- 用于加解密 --> 109 <dependency> 110 <groupId>commons-codec</groupId> 111 <artifactId>commons-codec</artifactId> 112 <version>1.7</version> 113 </dependency> 114 <dependency> 115 <groupId>org.bouncycastle</groupId> 116 <artifactId>bcprov-jdk15on</artifactId> 117 <version>1.47</version> 118 </dependency> 119 <!-- 集合工具类 --> 120 <dependency> 121 <groupId>org.apache.commons</groupId> 122 <artifactId>commons-collections4</artifactId> 123 <version>4.0</version> 124 </dependency> 125 <!-- http --> 126 <dependency> 127 <groupId>org.apache.httpcomponents</groupId> 128 <artifactId>httpclient</artifactId> 129 <version>4.2.6</version> 130 </dependency> 131 </dependencies> 132 </dependencyManagement> 133 134 <!-- 引入实际依赖 --> 135 <dependencies> 136 <!-- json --> 137 <dependency> 138 <groupId>com.alibaba</groupId> 139 <artifactId>fastjson</artifactId> 140 </dependency> 141 <!-- spring --> 142 <dependency> 143 <groupId>org.springframework</groupId> 144 <artifactId>spring-core</artifactId> 145 </dependency> 146 <dependency> 147 <groupId>org.springframework</groupId> 148 <artifactId>spring-beans</artifactId> 149 </dependency> 150 <dependency> 151 <groupId>org.springframework</groupId> 152 <artifactId>spring-context</artifactId> 153 </dependency> 154 <!-- 集合工具类 --> 155 <dependency> 156 <groupId>org.apache.commons</groupId> 157 <artifactId>commons-collections4</artifactId> 158 </dependency> 159 </dependencies> 160 161 <build> 162 <resources> 163 <!-- 164 这里配置了这一块儿true,才可以让指定文件(这里是src/main/resources/*.xml)读到pom.xml中的配置信息 , 165 值得注意的是,如果src/main/resources下还有其他文件,而你不想让其读pom.xml, 166 你还必须得把src/main/resources下的其余文件再配置一遍,配置为false(不可读pom.xml), 167 如下边的注释那样,否则,会报这些文件(在这里,就是*.properties)找不到的错误 168 --> 169 <resource> 170 <directory>src/main/resources</directory> 171 <filtering>true</filtering> 172 <includes> 173 <include>*.xml</include> 174 </includes> 175 </resource> 176 <!-- <resource> 177 <directory>src/main/resources</directory> 178 <filtering>false</filtering>可以读 ,若改为false就是不可读 179 <includes> 180 <include>*.properties</include> 181 </includes> 182 </resource> --> 183 </resources> 184 </build> 185 186 <!-- 187 profiles可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果 188 注意两点: 189 1)<activeByDefault>true</activeByDefault>这种情况表示服务器启动的时候就采用这一套env(在这里,就是prod) 190 2)当我们启动服务器后,想采用开发模式,需切换maven的env为dev,如果env的配置本身就是dev,需要将env换成rc或prod,点击apply,然后再将env切换成dev,点击apply才行 191 --> 192 <profiles> 193 <!-- 开发env --> 194 <profile> 195 <id>dev</id> 196 <activation> 197 <activeByDefault>false</activeByDefault> 198 <property> 199 <name>env</name> 200 <value>dev</value> 201 </property> 202 </activation> 203 <properties> 204 <env>dev</env> 205 206 <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName> 207 <!-- 208 对于jdbc.url中内容的配置,如果需要配置 &时,有两种方法: 209 1)如下边这样,使用<![CDATA[XXX]]>包起来 210 2)使用jdbc.properties文件来读取此pom.xml,然后spring.xml再读取jdbc.properties文件 211 显然,前者更方便,而且还省了一个jdbc.properties的文件,但是,有的时候,还是会用后者的; 212 在使用后者的时候,注意三点: 213 1)需要修改上边的build中的内容 214 2)需要在spring.xml中配置<context:property-placeholder location="classpath:jdbc.properties"/> 215 3)jdbc.properties与spring.xml必须在一个项目中,不能将jdbc.properties放在ssmm0-data项目中,否则切换env就不能用了 216 --> 217 <jdbc.url><![CDATA[jdbc:mysql://127.0.0.1:3306/blog?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8]]></jdbc.url> 218 <jdbc.username>root</jdbc.username> 219 <jdbc.password>123456</jdbc.password> 220 </properties> 221 </profile> 222 <!-- 预上线env --> 223 <profile> 224 <id>rc</id> 225 <activation> 226 <activeByDefault>false</activeByDefault> 227 <property> 228 <name>env</name> 229 <value>rc</value> 230 </property> 231 </activation> 232 <properties> 233 <env>rc</env> 234 235 <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName> 236 <!-- 假设的一个地址 --> 237 <jdbc.url><![CDATA[jdbc:mysql://10.10.10.100:3306/blog?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8]]></jdbc.url> 238 <jdbc.username>root2</jdbc.username> 239 <jdbc.password>1234562</jdbc.password> 240 </properties> 241 </profile> 242 <!-- 线上env --> 243 <profile> 244 <id>prod</id> 245 <activation> 246 <activeByDefault>true</activeByDefault> 247 <property> 248 <name>env</name> 249 <value>prod</value> 250 </property> 251 </activation> 252 <properties> 253 <env>prod</env> 254 255 <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName> 256 <!-- 假设的一个地址 --> 257 <jdbc.url><![CDATA[jdbc:mysql://99.99.99.999:3307/blog?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8]]></jdbc.url> 258 <jdbc.username>sadhijhqwui</jdbc.username> 259 <jdbc.password>zxczkchwihcznk=</jdbc.password> 260 </properties> 261 </profile> 262 </profiles> 263 </project>
注意:
- 所有的注意点:都在注释中
- 上述<build>中的resource的配置是为了是spring.xml可以读取pom.xml文件的内容,具体的注意点,查看注释
- profiles的配置是为了配置多套环境(在这里配置了三套env,开发,预上线和线上环境),具体的注意点,查看注释
2.2.2、ssmm0-data
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 指定父模块 --> <parent> <groupId>com.xxx</groupId> <artifactId>ssmm0</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.xxx.ssmm0</groupId> <artifactId>ssmm0-data</artifactId> <name>ssmm0-data</name> <packaging>jar</packaging><!-- 只是作为其他模块使用的工具 --> <!-- 引入实际依赖 --> <dependencies> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 数据源 --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency> </dependencies> </project>
注意:<package>为jar
com.xxx.model.userManagement.Admin
package com.xxx.model.userManagement; /** * 管理员 */ public class Admin { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
com.xxx.mapper.userManagement.AdminMapper
package com.xxx.mapper.userManagement; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import com.xxx.model.userManagement.Admin; /** * 管理员Mapper */ public interface AdminMapper { @Insert("INSERT INTO userinfo(username, password) VALUES(#{username},#{password})") public int insertAdmin(Admin admin); @Select("SELECT * FROM userinfo WHERE username = #{username} AND password = #{password}") @Results(value = { @Result(id = true, column = "id", property = "id"), @Result(column = "username", property = "username"), @Result(column = "password", property = "password") }) public Admin selectAdmin(@Param("username") String username, @Param("password") String password); }
com.xxx.dao.userManagement.AdminDao
package com.xxx.dao.userManagement; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.xxx.mapper.userManagement.AdminMapper; import com.xxx.model.userManagement.Admin; /** * 管理员DAO */ @Repository public class AdminDao { @Autowired private AdminMapper adminMapper; public boolean register(Admin admin){ return adminMapper.insertAdmin(admin)==1?true:false; } public Admin login(String username ,String password){ return adminMapper.selectAdmin(username, password); } }
com.xxx.service.userManagement.AdminService
package com.xxx.service.userManagement; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.xxx.dao.userManagement.AdminDao; import com.xxx.model.userManagement.Admin; /** * 管理员service */ @Service public class AdminService { @Autowired private AdminDao adminDao; public boolean register(Admin admin){ return adminDao.register(admin); } public Admin login(String username, String password) { return adminDao.login(username, password); } }
代码很简单,与之前的基本一样,只是名字换了而已。
值得注意的是包名:com.xxx.mapper.userManagement而非com.xxx.userManagement.mapper。
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 lang="zh-cn"> 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 lang="zh-cn"> 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文件中我写的那块注释的方法。
这样,一个基本上就是企业中开发常用的结构的项目就完成了,这个项目非常重要,一定自己试着去写一个,一定要仔细看其中的每一条注释。