当前位置:   article > 正文

idea下使用 Maven构建一个Java Web项目(Maven Webapp)_maven webapp和site

maven webapp和site

开篇之前先讲讲什么是Maven:

Maven 是Apache下的一个开源项目,它是一个创新的项目管理工具,它用于对Java项目进行项目构建、依赖管理及项目信息管理。

总结一下:Maven是个项目管理工具。这个管理工具到底如何使用,有什么用呢?这个才是要说的。在没有maven之前,我们创建一个项目工程或开发中需要依赖某个Jar,都是去下载Jar然后粘贴到当前项目 lib中来,然后每个项目的 lib文件夹下都保存着一堆Jar,然后复制给同事某个项目一看都要几十MB;而使用Maven构建的项目会在项目根目录下生成一个pom.xml, 这个xml文件通过<dependency>...</dependency>引入该项目所依赖的所有Jar。当需要引入某个Jar时,只需要在pom.xml中配置引入即可(Maven仓中存在即用,不存在就会去下载),而这些依赖的Jar都是保存到本地Maven仓中,项目工程都使用这一个Maven仓,项目运行只需要构建build之后便可以运行,是不是感觉轻便了许多,新建项目不需要再去复制一堆Jar了,只要在pom.xml引入即可,项目拷贝到不同机器只要有pom.xml就可以了。

回到主题,使用 idea创建一个Maven Webapp:

一、创建项目

我不想一个项目换个文件夹(workspace),所以我都是创建Module,关于这一点(idea的Project、Module 和eclipse的workspace 、Project的对应关系)的解释可以参考我上一篇博客:https://blog.csdn.net/shenju2011/article/details/103250717

选择Maven ----> Create form archetype ----> 选择 Maven-archetype-webapp ,如下图是红色选项(不是浅蓝色蓝色),不要选错。

之后填写项目名称,选择项目地址即可:

finish之后会在工程里面生成一个空的项目:

这会有一个build的过程。右下角有进度。构建完成后,目录:

可以看到此时的pom.xml,只引入了一个junit包。如下图:

  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/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>mimi</groupId>
  6. <artifactId>mimi</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <name>mimi Maven Webapp</name>
  10. <!-- FIXME change it to the project's website -->
  11. <url>http://www.example.com</url>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <maven.compiler.source>1.7</maven.compiler.source>
  15. <maven.compiler.target>1.7</maven.compiler.target>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>junit</groupId>
  20. <artifactId>junit</artifactId>
  21. <version>4.11</version>
  22. <scope>test</scope>
  23. </dependency>
  24. </dependencies>
  25. <build>
  26. <finalName>mimi</finalName>
  27. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  28. <plugins>
  29. <plugin>
  30. <artifactId>maven-clean-plugin</artifactId>
  31. <version>3.1.0</version>
  32. </plugin>
  33. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  34. <plugin>
  35. <artifactId>maven-resources-plugin</artifactId>
  36. <version>3.0.2</version>
  37. </plugin>
  38. <plugin>
  39. <artifactId>maven-compiler-plugin</artifactId>
  40. <version>3.8.0</version>
  41. </plugin>
  42. <plugin>
  43. <artifactId>maven-surefire-plugin</artifactId>
  44. <version>2.22.1</version>
  45. </plugin>
  46. <plugin>
  47. <artifactId>maven-war-plugin</artifactId>
  48. <version>3.2.2</version>
  49. </plugin>
  50. <plugin>
  51. <artifactId>maven-install-plugin</artifactId>
  52. <version>2.5.2</version>
  53. </plugin>
  54. <plugin>
  55. <artifactId>maven-deploy-plugin</artifactId>
  56. <version>2.8.2</version>
  57. </plugin>
  58. </plugins>
  59. </pluginManagement>
  60. </build>
  61. </project>

二、创建配置文件

    先创建文件,下一步再做配置。

    创建:app-config.xml(applicationContext.xml), mvc-config.xml,创建Java Package:com.mimi.controller、 com.mimi.domain、com.mimi.service ,UserController , User,  UserService ,util等java类文件

三、配置设置

1.pom.xml 引入Jar

  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/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>mimi</groupId>
  6. <artifactId>mimi</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <name>mimi Maven Webapp</name>
  10. <!-- FIXME change it to the project's website -->
  11. <url>http://www.example.com</url>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <maven.compiler.source>1.7</maven.compiler.source>
  15. <maven.compiler.target>1.7</maven.compiler.target>
  16. <log4j.version>1.2.17</log4j.version>
  17. <slf4j.version>1.7.7</slf4j.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>junit</groupId>
  22. <artifactId>junit</artifactId>
  23. <version>4.11</version>
  24. <scope>test</scope>
  25. </dependency>
  26. <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
  27. <dependency>
  28. <groupId>org.springframework</groupId>
  29. <artifactId>spring-web</artifactId>
  30. <version>4.2.3.RELEASE</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework</groupId>
  34. <artifactId>spring-webmvc</artifactId>
  35. <version>4.2.3.RELEASE</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework</groupId>
  39. <artifactId>spring-beans</artifactId>
  40. <version>4.2.3.RELEASE</version>
  41. </dependency>
  42. <!-- Spring-Jdbc -->
  43. <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
  44. <dependency>
  45. <groupId>org.springframework</groupId>
  46. <artifactId>spring-jdbc</artifactId>
  47. <version>4.2.3.RELEASE</version>
  48. </dependency>
  49. <!-- Spring aspect 面向切面编程 -->
  50. <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
  51. <dependency>
  52. <groupId>org.springframework</groupId>
  53. <artifactId>spring-aspects</artifactId>
  54. <version>4.2.3.RELEASE</version>
  55. </dependency>
  56. <dependency>
  57. <groupId>com.alibaba</groupId>
  58. <artifactId>druid</artifactId>
  59. <version>1.0.29</version>
  60. </dependency>
  61. <!-- mysql数据库驱动 -->
  62. <dependency>
  63. <groupId>mysql</groupId>
  64. <artifactId>mysql-connector-java</artifactId>
  65. <version>5.1.15</version>
  66. </dependency>
  67. <!-- json start (fastjson & jackjson) -->
  68. <dependency>
  69. <groupId>org.codehaus.jackson</groupId>
  70. <artifactId>jackson-mapper-asl</artifactId>
  71. <version>1.9.13</version>
  72. </dependency>
  73. <dependency>
  74. <groupId>com.alibaba</groupId>
  75. <artifactId>fastjson</artifactId>
  76. <version>1.2.31</version>
  77. </dependency>
  78. <dependency>
  79. <groupId>com.fasterxml.jackson.core</groupId>
  80. <artifactId>jackson-databind</artifactId>
  81. <version>2.9.8</version>
  82. </dependency>
  83. <!-- servlet start -->
  84. <dependency>
  85. <groupId>javax.servlet</groupId>
  86. <artifactId>servlet-api</artifactId>
  87. <version>3.0-alpha-1</version>
  88. </dependency>
  89. <dependency>
  90. <groupId>javax.servlet</groupId>
  91. <artifactId>jstl</artifactId>
  92. <version>1.2</version>
  93. </dependency>
  94. <!-- servlet end -->
  95. <!-- log4j start -->
  96. <dependency>
  97. <groupId>log4j</groupId>
  98. <artifactId>log4j</artifactId>
  99. <version>${log4j.version}</version>
  100. </dependency>
  101. <dependency>
  102. <groupId>org.slf4j</groupId>
  103. <artifactId>slf4j-api</artifactId>
  104. <version>${slf4j.version}</version>
  105. </dependency>
  106. <dependency>
  107. <groupId>org.slf4j</groupId>
  108. <artifactId>slf4j-log4j12</artifactId>
  109. <version>${slf4j.version}</version>
  110. </dependency>
  111. <dependency>
  112. <groupId>junit</groupId>
  113. <artifactId>junit</artifactId>
  114. <version>4.11</version>
  115. <scope>test</scope>
  116. </dependency>
  117. </dependencies>
  118. <build>
  119. <finalName>mimi</finalName>
  120. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  121. <plugins>
  122. <plugin>
  123. <artifactId>maven-clean-plugin</artifactId>
  124. <version>3.1.0</version>
  125. </plugin>
  126. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  127. <plugin>
  128. <artifactId>maven-resources-plugin</artifactId>
  129. <version>3.0.2</version>
  130. </plugin>
  131. <plugin>
  132. <artifactId>maven-compiler-plugin</artifactId>
  133. <version>3.8.0</version>
  134. </plugin>
  135. <plugin>
  136. <artifactId>maven-surefire-plugin</artifactId>
  137. <version>2.22.1</version>
  138. </plugin>
  139. <plugin>
  140. <artifactId>maven-war-plugin</artifactId>
  141. <version>3.2.2</version>
  142. </plugin>
  143. <plugin>
  144. <artifactId>maven-install-plugin</artifactId>
  145. <version>2.5.2</version>
  146. </plugin>
  147. <plugin>
  148. <artifactId>maven-deploy-plugin</artifactId>
  149. <version>2.8.2</version>
  150. </plugin>
  151. </plugins>
  152. </pluginManagement>
  153. </build>
  154. </project>

2、配置web.xml

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  5. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  6. id="WebApp_ID" version="3.1">
  7. <display-name>Archetype Created Web Application</display-name>
  8. <!-- 日志记录 -->
  9. <context-param>
  10. <!-- 日志配置文件路径 -->
  11. <param-name>log4jConfigLocation</param-name>
  12. <param-value>classpath:config/log4j.properties</param-value>
  13. </context-param>
  14. <context-param>
  15. <!-- 日志页面的刷新间隔 -->
  16. <param-name>log4jRefreshInterval</param-name>
  17. <param-value>6000</param-value>
  18. </context-param>
  19. <!-- <context-param>-->
  20. <!-- <param-name>contextConfigLocation</param-name>-->
  21. <!-- <param-value>classpath:config/app-config.xml</param-value>-->
  22. <!-- </context-param>-->
  23. <!-- <listener>-->
  24. <!-- <listener-class>org.springframework.sodc.context.ContextLoaderListener</listener-class>-->
  25. <!-- </listener>-->
  26. <!-- 编码过滤器 -->
  27. <filter>
  28. <filter-name>encodingFilter</filter-name>
  29. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  30. <init-param>
  31. <param-name>encoding</param-name>
  32. <param-value>UTF-8</param-value>
  33. </init-param>
  34. </filter>
  35. <filter-mapping>
  36. <filter-name>encodingFilter</filter-name>
  37. <url-pattern>/*</url-pattern>
  38. </filter-mapping>
  39. <!-- 配置DispatcherServlet -->
  40. <servlet>
  41. <servlet-name>SpringMVC</servlet-name>
  42. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  43. <!-- 配置springMVC需要加载的配置文件-->
  44. <init-param>
  45. <param-name>contextConfigLocation</param-name>
  46. <!-- <param-value>classpath:/config/mvc-config.xml</param-value>-->
  47. <param-value>classpath:/conf/app-config.xml</param-value>
  48. <!-- <param-value>classpath:/config/spring/mvc-config.xml</param-value>-->
  49. </init-param>
  50. <load-on-startup>1</load-on-startup>
  51. <async-supported>true</async-supported>
  52. </servlet>
  53. <servlet-mapping>
  54. <servlet-name>SpringMVC</servlet-name>
  55. <!-- 匹配所有请求,此处也可以配置成 * 形式 -->
  56. <url-pattern>/</url-pattern>
  57. </servlet-mapping>
  58. <welcome-file-list>
  59. <welcome-file>index.html</welcome-file>
  60. <welcome-file>index.jsp</welcome-file>
  61. </welcome-file-list>
  62. <session-config>
  63. <session-timeout>60</session-timeout>
  64. </session-config>
  65. </web-app>

3、app-config.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"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:task="http://www.springframework.org/schema/task"
  7. xmlns:mvc="http://www.springframework.org/schema/mvc"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xmlns:jee="http://www.springframework.org/schema/jee"
  10. xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
  11. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  12. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/context
  14. http://www.springframework.org/schema/context/spring-context.xsd
  15. http://www.springframework.org/schema/tx
  16. http://www.springframework.org/schema/tx/spring-tx.xsd
  17. http://www.springframework.org/schema/task
  18. http://www.springframework.org/schema/task/spring-task.xsd
  19. http://www.springframework.org/schema/jee
  20. http://www.springframework.org/schema/jee/spring-jee.xsd
  21. ">
  22. <!-- 上面 虽然没有标注版本号 ; 但spring可以自动配置最合适的版本号 -->
  23. <!-- 采用注释的方式配置bean -->
  24. <context:annotation-config />
  25. <!-- 自动扫描注解 -->
  26. <context:component-scan base-package="com.mimi"/>
  27. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
  28. <jee:jndi-lookup id="dataSource"
  29. jndi-name="jdbc/AppService"
  30. cache="true"
  31. resource-ref="true"
  32. lookup-on-startup="false"
  33. proxy-interface="javax.sql.DataSource"/>
  34. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  35. <property name="dataSource" ref="dataSource"/>
  36. </bean>
  37. <!-- enable the configuration of transactional behavior based on annotations -->
  38. <tx:annotation-driven transaction-manager="transactionManager"/>
  39. <!-- a PlatformTransactionManager is still required -->
  40. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  41. <property name="dataSource" ref="dataSource"/>
  42. </bean>
  43. <!-- 定时任务 -->
  44. <!-- <bean id="gDate" class="GetEChartsDataService" />-->
  45. <!-- <task:scheduled-tasks>-->
  46. <!-- <task:scheduled ref="gDate" method="refreshData" cron="0/30 * * * * ?" />-->
  47. <!-- </task:scheduled-tasks>-->
  48. <!-- Configures Spring MVC -->
  49. <import resource="mvc-config.xml" />
  50. </beans>

4、mvc-config.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"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  12. <!-- 扫描 -->
  13. <context:annotation-config />
  14. <context:component-scan base-package="com.mimi"/>
  15. <!-- 开启SpringMVC注解模式 -->
  16. <mvc:annotation-driven/>
  17. <!-- 静态资源默认servlet配置 -->
  18. <mvc:default-servlet-handler/>
  19. <!-- 视图解析 -->
  20. <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  21. <property name="viewResolvers">
  22. <list>
  23. <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
  24. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  25. <!-- <property name="prefix" value="/WEB-INF/jsp/"/>
  26. <property name="suffix" value=".jsp"/> -->
  27. </bean>
  28. </list>
  29. </property>
  30. <property name="defaultViews">
  31. <list>
  32. <!--json视图 -->
  33. <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
  34. <!--xml视图-->
  35. <!-- <bean class="org.springframework.sodc.servlet.view.xml.MarshallingView">-->
  36. <!-- <constructor-arg>-->
  37. <!-- <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">-->
  38. <!-- <property name="classesToBeBound">-->
  39. <!-- <list>-->
  40. <!-- <value>com.cn.my.entity.Course</value>-->
  41. <!-- <value>com.cn.my.entity.CourseList</value>-->
  42. <!-- </list>-->
  43. <!-- </property>-->
  44. <!-- </bean>-->
  45. <!-- </constructor-arg>-->
  46. <!-- </bean>-->
  47. </list>
  48. </property>
  49. </bean>
  50. <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
  51. <property name="favorParameter" value="true"/>
  52. <property name="parameterName" value="format"/>
  53. <property name="ignoreAcceptHeader" value="false"/>
  54. <property name="mediaTypes">
  55. <value>
  56. json=application/json
  57. xml=application/xml
  58. html=text/html
  59. </value>
  60. </property>
  61. <property name="defaultContentType" value="text/html"/>
  62. </bean>
  63. <!-- 配置jsp 显示ViewResolver -->
  64. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  65. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
  66. <property name="prefix" value="/WEB-INF/views/"/>
  67. <property name="suffix" value=".jsp"/>
  68. </bean>
  69. <!-- 解决 IE浏览器 不能正常解析 json 问题 -->
  70. <!-- <mvc:annotation-driven>-->
  71. <!-- <mvc:message-converters register-defaults="true">-->
  72. <!-- <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">-->
  73. <!-- <property name="supportedMediaTypes">-->
  74. <!-- <list>-->
  75. <!-- <value>text/plain;charset=utf-8</value>-->
  76. <!-- <value>application/json;charset=utf-8</value>-->
  77. <!-- <value>application/x-www-form-urlencoded</value>-->
  78. <!-- </list>-->
  79. <!-- </property>-->
  80. <!-- </bean>-->
  81. <!-- </mvc:message-converters>-->
  82. <!-- </mvc:annotation-driven>-->
  83. <!--配置静态资源映射,防止静态资源被拦截后导致controller返回的页面的js,css失效 -->
  84. <!-- <mvc:resources location="/static/" mapping="/static/**"></mvc:resources>-->
  85. </beans>

5、Java类

UserController.java

  1. package com.mimi.controller;
  2. import com.mimi.domain.BaseResp;
  3. import com.mimi.domain.User;
  4. import com.mimi.service.UserService;
  5. import com.mimi.util.Util;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.ModelAttribute;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpSession;
  13. @Controller
  14. @RequestMapping("/user")
  15. public class UserController {
  16. @Autowired
  17. UserService userService;
  18. @Autowired(required = true)
  19. private HttpServletRequest request;
  20. @RequestMapping("/login")
  21. @ResponseBody
  22. public BaseResp login(String userId, String pwd, @ModelAttribute("clientIpAddress") String clientIpAddress)
  23. {
  24. try {
  25. System.out.println("userId : "+userId +"pwd : " + pwd);
  26. // 校验用户名,密码,检验码,设置最后登陆时间和IP地址
  27. HttpSession session = request.getSession();
  28. User user = this.userService.checkLogin(userId, pwd);
  29. System.out.println(user);
  30. if (Util.isBlank(user)) {
  31. // 记录日志
  32. return BaseResp.getFailInstance("Account or Password error, please check your account.");
  33. }
  34. // 获取岗位等其他信息
  35. session.setAttribute("user", user);
  36. // session.setAttribute("emp", emp);
  37. // session.setAttribute("orgName", orgName);
  38. // session.setAttribute("rndNum", userId + sDate);
  39. session.setAttribute("clientIpAddress", clientIpAddress);
  40. // 记录日志
  41. return BaseResp.getSuccessInstance(user);
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. return BaseResp.getFailInstance();
  45. }
  46. }
  47. }

User.java 

  1. package com.mimi.domain;
  2. public class User {
  3. private String userId;
  4. private String password;
  5. private String status;
  6. public String getUserId() {
  7. return userId;
  8. }
  9. public void setUserId(String userId) {
  10. this.userId = userId;
  11. }
  12. public String getPassword() {
  13. return password;
  14. }
  15. public void setPassword(String password) {
  16. this.password = password;
  17. }
  18. public String getStatus() {
  19. return status;
  20. }
  21. public void setStatus(String status) {
  22. this.status = status;
  23. }
  24. @Override
  25. public String toString() {
  26. return "User{" +
  27. "userId='" + userId + '\'' +
  28. ", password='" + password + '\'' +
  29. ", status='" + status + '\'' +
  30. '}';
  31. }
  32. }

BaseResp.java 

  1. package com.mimi.domain;
  2. public class BaseResp {
  3. public final static Integer HTTP_OK = 200;
  4. public final static String MESSAGE_OK = "SUCCESS";
  5. public final static Integer HTTP_ERROR_500 = 500;
  6. public final static String MESSAGE_ERROR = "The system error, please wait.";
  7. public final static Integer HTTP_ERROR_501 = 501;
  8. private Integer retCode;
  9. private String message;
  10. private Object data;
  11. public BaseResp() { }
  12. public BaseResp(int retCode, String message, Object data) {
  13. this.retCode = retCode;
  14. this.message = message;
  15. this.data = data;
  16. }
  17. public static BaseResp getSuccessInstance() {
  18. return new BaseResp(HTTP_OK, MESSAGE_OK, null);
  19. }
  20. public static BaseResp getSuccessInstance(Object data) {
  21. return new BaseResp(HTTP_OK, MESSAGE_OK, data);
  22. }
  23. // 系统异常
  24. public static BaseResp getFailInstance() {
  25. return new BaseResp(HTTP_ERROR_500, MESSAGE_ERROR, null);
  26. }
  27. // 业务异常
  28. public static BaseResp getFailInstance(String errorMsg) {
  29. return new BaseResp(HTTP_ERROR_501, errorMsg, null);
  30. }
  31. public Integer getRetCode() {
  32. return retCode;
  33. }
  34. public void setRetCode(Integer retCode) {
  35. this.retCode = retCode;
  36. }
  37. public String getMessage() {
  38. return message;
  39. }
  40. public void setMessage(String message) {
  41. this.message = message;
  42. }
  43. public Object getData() {
  44. return data;
  45. }
  46. public void setData(Object data) {
  47. this.data = data;
  48. }
  49. }

UserService.java 

  1. package com.mimi.service;
  2. import com.mimi.domain.User;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.dao.EmptyResultDataAccessException;
  5. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  6. import org.springframework.jdbc.core.JdbcTemplate;
  7. import org.springframework.stereotype.Service;
  8. import javax.sql.DataSource;
  9. @Service
  10. public class UserService {
  11. @Autowired
  12. private JdbcTemplate jdbcTemplate;
  13. @Autowired
  14. private void setDataSource(DataSource dataSource) {
  15. this.jdbcTemplate = new JdbcTemplate(dataSource);
  16. }
  17. public User checkLogin(String userId, String pwd) {
  18. try {
  19. String sql = "select * from T_USER where userId=? and password=?";
  20. System.out.println("sql="+sql);
  21. final Object[] params = new Object[]{userId, pwd};
  22. return this.jdbcTemplate.queryForObject(sql, params, new BeanPropertyRowMapper<User>(User.class));
  23. } catch (EmptyResultDataAccessException e) {
  24. return null;
  25. }
  26. }
  27. }

6、context.xml

 这个是数据库连接参数的配置,这里就不说怎么生成这个context.xml,不知道的话可以看看我上一篇文章。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Context path="/">
  3. <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
  4. maxActive="20" maxIdle="3"
  5. name="jdbc/AppService" password="root123456"
  6. type="javax.sql.DataSource"
  7. url="jdbc:mysql://localhost:3306/myfb?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true"
  8. username="root"/>
  9. </Context>

 

四、数据库、Tomcat的配置

和上一篇使用的是同一个数据库,访问的是同一张表。

Tomcat的配置,就是不说了,上篇文章已经说过了。

配置完Tomcat启动成功后会在 浏览器看到hello word。这里也不做前端页面了,使用一下 postman验证一下登陆接口:

 

总结:

     其实 使用maven来构建项目,就是将Jar交给 pom.xml来引入,然后项目的目录结构有稍微的区别。还有就是在 Idea中使用Maven来构建,他会自动配置 Project Structure,不用我们再像之前那样手动的设置 Modules 、Facets、Artifacts等配置信息。其他的和之前我写过的 简单的apringMva的 Java Web一样。

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/936300
推荐阅读
相关标签
  

闽ICP备14008679号