|
| 1 | +# Maven + Spring + SpringMVC + Mybatis 整合 |
| 2 | + |
| 3 | +通过这篇文章你可以获取的内容: |
| 4 | +1. 整个配置流程。 |
| 5 | +1. 对于每一个配置项,Spring 到底帮我们做了什么? |
| 6 | + |
| 7 | +本文假设读者: |
| 8 | + |
| 9 | +1. 未使用 IDEA 进行过 Web 项目开发 |
| 10 | +1. 未使用过 Maven 进行项目构建 |
| 11 | +1. 已学习过 Spring、SpringMVC、Mybatis |
| 12 | + |
| 13 | +环境搭建: |
| 14 | + |
| 15 | +- IDE: `IntelliJ IDEA 2017.1.1(Ultimate)` |
| 16 | +- Web Server: `Tomcat 8.0.43` |
| 17 | +- Spring: `4.3.7.RELEASE` |
| 18 | +- Mybatis: `3.4.4` |
| 19 | + |
| 20 | +如果需要示例代码,可以参考 [SSM-Demo](https://github.com/c-rainstorm/ssm-Demo). |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +<!-- TOC --> |
| 25 | + |
| 26 | +- [Maven + Spring + SpringMVC + Mybatis 整合](#maven--spring--springmvc--mybatis-整合) |
| 27 | + - [Spring && SpringMVC](#spring--springmvc) |
| 28 | + - [工程搭建](#工程搭建) |
| 29 | + - [新建 Web Application](#新建-web-application) |
| 30 | + - [添加 Maven 支持](#添加-maven-支持) |
| 31 | + - [工程目录简介](#工程目录简介) |
| 32 | + - [代码实现](#代码实现) |
| 33 | + - [View 层](#view-层) |
| 34 | + - [Controller](#controller) |
| 35 | + - [Service](#service) |
| 36 | + - [配置文件编写](#配置文件编写) |
| 37 | + - [运行测试](#运行测试) |
| 38 | + - [Spring && SpringMVC && Mybatis](#spring--springmvc--mybatis) |
| 39 | + - [代码实现](#代码实现-1) |
| 40 | + - [配置文件编写](#配置文件编写-1) |
| 41 | + - [运行测试](#运行测试-1) |
| 42 | + - [参考](#参考) |
| 43 | + |
| 44 | +<!-- /TOC --> |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## Spring && SpringMVC |
| 49 | + |
| 50 | +### 工程搭建 |
| 51 | + |
| 52 | +#### 新建 Web Application |
| 53 | + |
| 54 | +1. File -> new -> project.. |
| 55 | +1. Java -> Web Application |
| 56 | + -  |
| 57 | + |
| 58 | +#### 添加 Maven 支持 |
| 59 | + |
| 60 | +1. 右键工程名,点击 Add Framwork Support.. |
| 61 | + -  |
| 62 | +1. 配置编译器源版本及目标字节码版本 |
| 63 | + - 在 `pom.xml` 配置文件中添加以下配置。 |
| 64 | + - **Tips:不添加的话目标字节码版本默认使用 `JDK1.5`** |
| 65 | + ```xml |
| 66 | + <properties> |
| 67 | + <maven.compiler.source>1.8</maven.compiler.source> |
| 68 | + <maven.compiler.target>1.8</maven.compiler.target> |
| 69 | + </properties> |
| 70 | + ``` |
| 71 | + |
| 72 | +#### 工程目录简介 |
| 73 | + |
| 74 | +``` |
| 75 | +. |
| 76 | +|-- pom.xml // Maven 配置文件 |
| 77 | +|-- src |
| 78 | +| |-- main |
| 79 | +| | |-- java // Java 代码存放位置 |
| 80 | +| | `-- resources // 配置文件存放位置 |
| 81 | +| `-- test |
| 82 | +| `-- java // 测试类存放位置 |
| 83 | +`-- web // 存放 View 层用到的文件,会直接部署到服务器 |
| 84 | + |-- index.jsp |
| 85 | + `-- WEB-INF |
| 86 | + `-- web.xml // Web 应用程序的 Web 组件的配置和部署信息 |
| 87 | +``` |
| 88 | +
|
| 89 | +
|
| 90 | +### 代码实现 |
| 91 | +
|
| 92 | +我们以一个最简单的用户登陆操作来说明配置的过程。 |
| 93 | +
|
| 94 | +#### View 层 |
| 95 | +
|
| 96 | +1. form 表单 |
| 97 | + ```html |
| 98 | + <!-- 1. 对应控制层 URL 为 /user/checkLogin --> |
| 99 | + <!-- 2. input 标签中的 class 属性在 JS 中进行元素定位需要用到 --> |
| 100 | + <!-- 3. input 标签中的 name 属性在控制层中提取表单数据时需要用到,即必须与控制层中对应方法的参数名或参数实体类中的域的名称一致,否则获取不到数据 --> |
| 101 | + <form action="${pageContext.request.contextPath}/user/checkLogin" method="post"> |
| 102 | + username: <input type="text" class="username" name="username" placeholder="username..."><br> |
| 103 | + password: <input type="password" class="password" name="password" placeholder="password..."><br> |
| 104 | + <input type="submit" value="submit"> |
| 105 | + </form> |
| 106 | + ``` |
| 107 | +1. 使用 JS 在传输之前进行加密 |
| 108 | + ```js |
| 109 | + $(document).ready(function () { |
| 110 | + // 以为网络传输时使用明文,所以直接将密码提交到后台的方式很不安全,所以在传输之前就进行加密。 |
| 111 | + // 在这里加密方法用的是 MD5 。你也可以选择其他的加密方式,加密用的 JS 库应该很容易着, |
| 112 | + // 这里我用的是 [JavaScript-MD5](https://github.com/blueimp/JavaScript-MD5)。 |
| 113 | + $("form").on("submit", function () { |
| 114 | + var pass = $(".password"); |
| 115 | + pass.val(md5(pass.val())); |
| 116 | + }) |
| 117 | + }) |
| 118 | + ``` |
| 119 | +
|
| 120 | +#### Controller |
| 121 | +
|
| 122 | +1. 因为 Controller 需要用到 Spring 的注解,所以需要先添加 Maven 依赖。 |
| 123 | + ```xml |
| 124 | + <properties> |
| 125 | + <org.springframework.version>4.3.7.RELEASE</org.springframework.version> |
| 126 | + </properties> |
| 127 | +
|
| 128 | + <dependency> |
| 129 | +
|
| 130 | + <dependencies> |
| 131 | + <dependency> |
| 132 | + <groupId>org.springframework</groupId> |
| 133 | + <artifactId>spring-core</artifactId> |
| 134 | + <version>${org.springframework.version}</version> |
| 135 | + </dependency> |
| 136 | + <dependency> |
| 137 | + <groupId>org.springframework</groupId> |
| 138 | + <artifactId>spring-beans</artifactId> |
| 139 | + <version>${org.springframework.version}</version> |
| 140 | + </dependency> |
| 141 | + <dependency> |
| 142 | + <groupId>org.springframework</groupId> |
| 143 | + <artifactId>spring-orm</artifactId> |
| 144 | + <version>${org.springframework.version}</version> |
| 145 | + </dependency> |
| 146 | + <dependency> |
| 147 | + <groupId>org.springframework</groupId> |
| 148 | + <artifactId>spring-context</artifactId> |
| 149 | + <version>${org.springframework.version}</version> |
| 150 | + </dependency> |
| 151 | + <dependency> |
| 152 | + <groupId>org.springframework</groupId> |
| 153 | + <artifactId>spring-web</artifactId> |
| 154 | + <version>${org.springframework.version}</version> |
| 155 | + </dependency> |
| 156 | + <dependency> |
| 157 | + <groupId>org.springframework</groupId> |
| 158 | + <artifactId>spring-webmvc</artifactId> |
| 159 | + <version>${org.springframework.version}</version> |
| 160 | + </dependency> |
| 161 | + <dependency> |
| 162 | + <groupId>org.springframework</groupId> |
| 163 | + <artifactId>spring-aop</artifactId> |
| 164 | + <version>${org.springframework.version}</version> |
| 165 | + </dependency> |
| 166 | + <dependency> |
| 167 | + <groupId>org.apache.logging.log4j</groupId> |
| 168 | + <artifactId>log4j-api</artifactId> |
| 169 | + <version>2.8.2</version> |
| 170 | + </dependency> |
| 171 | + <dependency> |
| 172 | + <groupId>org.apache.logging.log4j</groupId> |
| 173 | + <artifactId>log4j-core</artifactId> |
| 174 | + <version>2.8.2</version> |
| 175 | + </dependencies> |
| 176 | + ``` |
| 177 | +1. Controller |
| 178 | + ```java |
| 179 | + package com.github.crainstorm.oss.user.action; |
| 180 | +
|
| 181 | + @Controller |
| 182 | + @RequestMapping("/user") |
| 183 | + public class UserAction { |
| 184 | +
|
| 185 | + private static Logger logger = LogManager.getLogger(UserAction.class); |
| 186 | + @Autowired |
| 187 | + private UserService service; |
| 188 | +
|
| 189 | + @RequestMapping("/checkLogin") |
| 190 | + public String checkLogin(User user) { |
| 191 | + logger.trace("enter checkLogin..."); |
| 192 | + if (logger.isDebugEnabled()) { |
| 193 | + logger.debug(user); |
| 194 | + } |
| 195 | + if (service.loginSuccess(user)) { |
| 196 | + logger.info("login success..."); |
| 197 | + return "redirect:/index.jsp"; |
| 198 | + } else { |
| 199 | + logger.info("login failed..."); |
| 200 | + return "login/user"; |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + ``` |
| 205 | +1. 用到了 `log4j2`, 简单配置一下 |
| 206 | +
|
| 207 | + ```xml |
| 208 | + <?xml version="1.0" encoding="UTF-8"?> |
| 209 | +
|
| 210 | + <Configuration status="WARN"> |
| 211 | + <Appenders> |
| 212 | + <Console name="Console" target="SYSTEM_OUT"> |
| 213 | + <PatternLayout pattern="%d [%t] %-5level %logger{36} - %msg%n"/> |
| 214 | + </Console> |
| 215 | + </Appenders> |
| 216 | + <Loggers> |
| 217 | + <!-- 类寻找 Logger 时会根据包名一层一层向上找,如果没有找到,则默认使用 Root --> |
| 218 | + <Root level="trace"> |
| 219 | + <AppenderRef ref="Console"/> |
| 220 | + </Root> |
| 221 | + </Loggers> |
| 222 | + </Configuration> |
| 223 | + ``` |
| 224 | +
|
| 225 | +##### Service |
| 226 | +
|
| 227 | +
|
| 228 | +```java |
| 229 | +package com.github.crainstorm.oss.user.service; |
| 230 | +
|
| 231 | +public interface UserService { |
| 232 | + boolean loginSuccess(User user); |
| 233 | +} |
| 234 | +``` |
| 235 | + |
| 236 | +```java |
| 237 | +package com.github.crainstorm.oss.user.service; |
| 238 | + |
| 239 | +@Service |
| 240 | +public class UserServiceImpl implements UserService { |
| 241 | + private static final Logger LOGGER = LogManager.getLogger(UserServiceImpl.class); |
| 242 | + |
| 243 | + // 因为先配置 Spring + SpringMVC, 所以 dao 层先不管 |
| 244 | + public boolean loginSuccess(User user) { |
| 245 | + return true; |
| 246 | + } |
| 247 | +} |
| 248 | +``` |
| 249 | + |
| 250 | +### 配置文件编写 |
| 251 | +### 运行测试 |
| 252 | + |
| 253 | +## Spring && SpringMVC && Mybatis |
| 254 | + |
| 255 | +### 代码实现 |
| 256 | +### 配置文件编写 |
| 257 | +### 运行测试 |
| 258 | + |
| 259 | +## 参考 |
0 commit comments