赞
踩
开篇之前先讲讲什么是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包。如下图:
- <?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/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>mimi</groupId>
- <artifactId>mimi</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>war</packaging>
-
- <name>mimi Maven Webapp</name>
- <!-- FIXME change it to the project's website -->
- <url>http://www.example.com</url>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.7</maven.compiler.source>
- <maven.compiler.target>1.7</maven.compiler.target>
- </properties>
-
- <dependencies>
-
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
-
- </dependencies>
-
- <build>
- <finalName>mimi</finalName>
- <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
- <plugins>
- <plugin>
- <artifactId>maven-clean-plugin</artifactId>
- <version>3.1.0</version>
- </plugin>
- <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
- <plugin>
- <artifactId>maven-resources-plugin</artifactId>
- <version>3.0.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.8.0</version>
- </plugin>
- <plugin>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>2.22.1</version>
- </plugin>
- <plugin>
- <artifactId>maven-war-plugin</artifactId>
- <version>3.2.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-install-plugin</artifactId>
- <version>2.5.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-deploy-plugin</artifactId>
- <version>2.8.2</version>
- </plugin>
- </plugins>
- </pluginManagement>
- </build>
- </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类文件
- <?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/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>mimi</groupId>
- <artifactId>mimi</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>war</packaging>
-
- <name>mimi Maven Webapp</name>
- <!-- FIXME change it to the project's website -->
- <url>http://www.example.com</url>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.7</maven.compiler.source>
- <maven.compiler.target>1.7</maven.compiler.target>
- <log4j.version>1.2.17</log4j.version>
- <slf4j.version>1.7.7</slf4j.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
-
- <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>4.2.3.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>4.2.3.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>4.2.3.RELEASE</version>
- </dependency>
-
- <!-- Spring-Jdbc -->
- <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>4.2.3.RELEASE</version>
- </dependency>
- <!-- Spring aspect 面向切面编程 -->
- <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- <version>4.2.3.RELEASE</version>
- </dependency>
-
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.29</version>
- </dependency>
-
- <!-- mysql数据库驱动 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.15</version>
- </dependency>
-
- <!-- json start (fastjson & jackjson) -->
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-mapper-asl</artifactId>
- <version>1.9.13</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.31</version>
- </dependency>
-
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.9.8</version>
- </dependency>
-
- <!-- servlet start -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>3.0-alpha-1</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- </dependency>
- <!-- servlet end -->
-
- <!-- log4j start -->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>${log4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
-
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
-
- </dependencies>
-
- <build>
- <finalName>mimi</finalName>
- <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
- <plugins>
- <plugin>
- <artifactId>maven-clean-plugin</artifactId>
- <version>3.1.0</version>
- </plugin>
- <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
- <plugin>
- <artifactId>maven-resources-plugin</artifactId>
- <version>3.0.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.8.0</version>
- </plugin>
- <plugin>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>2.22.1</version>
- </plugin>
- <plugin>
- <artifactId>maven-war-plugin</artifactId>
- <version>3.2.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-install-plugin</artifactId>
- <version>2.5.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-deploy-plugin</artifactId>
- <version>2.8.2</version>
- </plugin>
- </plugins>
- </pluginManagement>
- </build>
- </project>
- <!DOCTYPE web-app PUBLIC
- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd" >
-
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
- id="WebApp_ID" version="3.1">
- <display-name>Archetype Created Web Application</display-name>
-
- <!-- 日志记录 -->
- <context-param>
- <!-- 日志配置文件路径 -->
- <param-name>log4jConfigLocation</param-name>
- <param-value>classpath:config/log4j.properties</param-value>
- </context-param>
- <context-param>
- <!-- 日志页面的刷新间隔 -->
- <param-name>log4jRefreshInterval</param-name>
- <param-value>6000</param-value>
- </context-param>
-
- <!-- <context-param>-->
- <!-- <param-name>contextConfigLocation</param-name>-->
- <!-- <param-value>classpath:config/app-config.xml</param-value>-->
- <!-- </context-param>-->
- <!-- <listener>-->
- <!-- <listener-class>org.springframework.sodc.context.ContextLoaderListener</listener-class>-->
- <!-- </listener>-->
-
- <!-- 编码过滤器 -->
- <filter>
- <filter-name>encodingFilter</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>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <!-- 配置DispatcherServlet -->
- <servlet>
- <servlet-name>SpringMVC</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <!-- 配置springMVC需要加载的配置文件-->
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <!-- <param-value>classpath:/config/mvc-config.xml</param-value>-->
- <param-value>classpath:/conf/app-config.xml</param-value>
- <!-- <param-value>classpath:/config/spring/mvc-config.xml</param-value>-->
- </init-param>
- <load-on-startup>1</load-on-startup>
- <async-supported>true</async-supported>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>SpringMVC</servlet-name>
- <!-- 匹配所有请求,此处也可以配置成 * 形式 -->
- <url-pattern>/</url-pattern>
- </servlet-mapping>
-
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
-
- <session-config>
- <session-timeout>60</session-timeout>
- </session-config>
-
- </web-app>
-
-
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:task="http://www.springframework.org/schema/task"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:jee="http://www.springframework.org/schema/jee"
- xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task.xsd
- http://www.springframework.org/schema/jee
- http://www.springframework.org/schema/jee/spring-jee.xsd
- ">
- <!-- 上面 虽然没有标注版本号 ; 但spring可以自动配置最合适的版本号 -->
- <!-- 采用注释的方式配置bean -->
- <context:annotation-config />
- <!-- 自动扫描注解 -->
- <context:component-scan base-package="com.mimi"/>
-
- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
-
- <jee:jndi-lookup id="dataSource"
- jndi-name="jdbc/AppService"
- cache="true"
- resource-ref="true"
- lookup-on-startup="false"
- proxy-interface="javax.sql.DataSource"/>
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <!-- enable the configuration of transactional behavior based on annotations -->
- <tx:annotation-driven transaction-manager="transactionManager"/>
- <!-- a PlatformTransactionManager is still required -->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- </bean>
-
- <!-- 定时任务 -->
- <!-- <bean id="gDate" class="GetEChartsDataService" />-->
- <!-- <task:scheduled-tasks>-->
- <!-- <task:scheduled ref="gDate" method="refreshData" cron="0/30 * * * * ?" />-->
- <!-- </task:scheduled-tasks>-->
-
-
- <!-- Configures Spring MVC -->
- <import resource="mvc-config.xml" />
-
- </beans>
- <?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: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.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
- <!-- 扫描 -->
- <context:annotation-config />
- <context:component-scan base-package="com.mimi"/>
- <!-- 开启SpringMVC注解模式 -->
- <mvc:annotation-driven/>
- <!-- 静态资源默认servlet配置 -->
- <mvc:default-servlet-handler/>
- <!-- 视图解析 -->
- <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
- <property name="viewResolvers">
- <list>
- <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <!-- <property name="prefix" value="/WEB-INF/jsp/"/>
- <property name="suffix" value=".jsp"/> -->
- </bean>
- </list>
- </property>
- <property name="defaultViews">
- <list>
- <!--json视图 -->
- <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
- <!--xml视图-->
- <!-- <bean class="org.springframework.sodc.servlet.view.xml.MarshallingView">-->
- <!-- <constructor-arg>-->
- <!-- <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">-->
- <!-- <property name="classesToBeBound">-->
- <!-- <list>-->
- <!-- <value>com.cn.my.entity.Course</value>-->
- <!-- <value>com.cn.my.entity.CourseList</value>-->
- <!-- </list>-->
- <!-- </property>-->
- <!-- </bean>-->
- <!-- </constructor-arg>-->
- <!-- </bean>-->
- </list>
- </property>
- </bean>
-
- <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
- <property name="favorParameter" value="true"/>
- <property name="parameterName" value="format"/>
- <property name="ignoreAcceptHeader" value="false"/>
- <property name="mediaTypes">
- <value>
- json=application/json
- xml=application/xml
- html=text/html
- </value>
- </property>
- <property name="defaultContentType" value="text/html"/>
- </bean>
-
- <!-- 配置jsp 显示ViewResolver -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
- <property name="prefix" value="/WEB-INF/views/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
-
- <!-- 解决 IE浏览器 不能正常解析 json 问题 -->
- <!-- <mvc:annotation-driven>-->
- <!-- <mvc:message-converters register-defaults="true">-->
- <!-- <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">-->
- <!-- <property name="supportedMediaTypes">-->
- <!-- <list>-->
- <!-- <value>text/plain;charset=utf-8</value>-->
- <!-- <value>application/json;charset=utf-8</value>-->
- <!-- <value>application/x-www-form-urlencoded</value>-->
- <!-- </list>-->
- <!-- </property>-->
- <!-- </bean>-->
- <!-- </mvc:message-converters>-->
- <!-- </mvc:annotation-driven>-->
-
- <!--配置静态资源映射,防止静态资源被拦截后导致controller返回的页面的js,css失效 -->
- <!-- <mvc:resources location="/static/" mapping="/static/**"></mvc:resources>-->
-
- </beans>
UserController.java
- package com.mimi.controller;
-
- import com.mimi.domain.BaseResp;
- import com.mimi.domain.User;
- import com.mimi.service.UserService;
- import com.mimi.util.Util;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
-
- @Controller
- @RequestMapping("/user")
- public class UserController {
- @Autowired
- UserService userService;
-
- @Autowired(required = true)
- private HttpServletRequest request;
-
- @RequestMapping("/login")
- @ResponseBody
- public BaseResp login(String userId, String pwd, @ModelAttribute("clientIpAddress") String clientIpAddress)
- {
- try {
- System.out.println("userId : "+userId +"pwd : " + pwd);
- // 校验用户名,密码,检验码,设置最后登陆时间和IP地址
- HttpSession session = request.getSession();
- User user = this.userService.checkLogin(userId, pwd);
- System.out.println(user);
- if (Util.isBlank(user)) {
- // 记录日志
- return BaseResp.getFailInstance("Account or Password error, please check your account.");
- }
- // 获取岗位等其他信息
- session.setAttribute("user", user);
- // session.setAttribute("emp", emp);
- // session.setAttribute("orgName", orgName);
- // session.setAttribute("rndNum", userId + sDate);
- session.setAttribute("clientIpAddress", clientIpAddress);
- // 记录日志
-
- return BaseResp.getSuccessInstance(user);
- } catch (Exception e) {
- e.printStackTrace();
- return BaseResp.getFailInstance();
- }
-
- }
- }
User.java
- package com.mimi.domain;
-
- public class User {
-
-
- private String userId;
- private String password;
- private String status;
-
- public String getUserId() {
- return userId;
- }
-
- public void setUserId(String userId) {
- this.userId = userId;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public String getStatus() {
- return status;
- }
-
- public void setStatus(String status) {
- this.status = status;
- }
-
- @Override
- public String toString() {
- return "User{" +
- "userId='" + userId + '\'' +
- ", password='" + password + '\'' +
- ", status='" + status + '\'' +
- '}';
- }
- }
BaseResp.java
- package com.mimi.domain;
-
- public class BaseResp {
-
- public final static Integer HTTP_OK = 200;
- public final static String MESSAGE_OK = "SUCCESS";
-
- public final static Integer HTTP_ERROR_500 = 500;
- public final static String MESSAGE_ERROR = "The system error, please wait.";
-
- public final static Integer HTTP_ERROR_501 = 501;
-
- private Integer retCode;
- private String message;
- private Object data;
-
- public BaseResp() { }
-
- public BaseResp(int retCode, String message, Object data) {
- this.retCode = retCode;
- this.message = message;
- this.data = data;
- }
-
- public static BaseResp getSuccessInstance() {
- return new BaseResp(HTTP_OK, MESSAGE_OK, null);
- }
-
- public static BaseResp getSuccessInstance(Object data) {
- return new BaseResp(HTTP_OK, MESSAGE_OK, data);
- }
-
- // 系统异常
- public static BaseResp getFailInstance() {
- return new BaseResp(HTTP_ERROR_500, MESSAGE_ERROR, null);
- }
-
- // 业务异常
- public static BaseResp getFailInstance(String errorMsg) {
- return new BaseResp(HTTP_ERROR_501, errorMsg, null);
- }
-
- public Integer getRetCode() {
- return retCode;
- }
-
- public void setRetCode(Integer retCode) {
- this.retCode = retCode;
- }
-
- public String getMessage() {
- return message;
- }
-
- public void setMessage(String message) {
- this.message = message;
- }
-
- public Object getData() {
- return data;
- }
-
- public void setData(Object data) {
- this.data = data;
- }
- }
UserService.java
- package com.mimi.service;
-
- import com.mimi.domain.User;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.dao.EmptyResultDataAccessException;
- import org.springframework.jdbc.core.BeanPropertyRowMapper;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.stereotype.Service;
-
- import javax.sql.DataSource;
-
- @Service
- public class UserService {
-
- @Autowired
- private JdbcTemplate jdbcTemplate;
-
- @Autowired
- private void setDataSource(DataSource dataSource) {
- this.jdbcTemplate = new JdbcTemplate(dataSource);
- }
-
- public User checkLogin(String userId, String pwd) {
- try {
- String sql = "select * from T_USER where userId=? and password=?";
- System.out.println("sql="+sql);
- final Object[] params = new Object[]{userId, pwd};
- return this.jdbcTemplate.queryForObject(sql, params, new BeanPropertyRowMapper<User>(User.class));
- } catch (EmptyResultDataAccessException e) {
- return null;
- }
- }
- }
这个是数据库连接参数的配置,这里就不说怎么生成这个context.xml,不知道的话可以看看我上一篇文章。
- <?xml version="1.0" encoding="UTF-8"?>
- <Context path="/">
- <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
- maxActive="20" maxIdle="3"
- name="jdbc/AppService" password="root123456"
- type="javax.sql.DataSource"
- url="jdbc:mysql://localhost:3306/myfb?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true"
- username="root"/>
- </Context>
和上一篇使用的是同一个数据库,访问的是同一张表。
Tomcat的配置,就是不说了,上篇文章已经说过了。
配置完Tomcat启动成功后会在 浏览器看到hello word。这里也不做前端页面了,使用一下 postman验证一下登陆接口:
其实 使用maven来构建项目,就是将Jar交给 pom.xml来引入,然后项目的目录结构有稍微的区别。还有就是在 Idea中使用Maven来构建,他会自动配置 Project Structure,不用我们再像之前那样手动的设置 Modules 、Facets、Artifacts等配置信息。其他的和之前我写过的 简单的apringMva的 Java Web一样。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。