小菜鸟之SSM框架
- 1 # SSM框架
- 2
- 3 # **什么是框架**
- 4
- 5 就是模板,将一些基础性的程序和代码由框架模板提供,然后程序员补充和业务需求相关的代码。
- 6
- 7 # **ssm框架组成**
- 8
- 9 s: springMvc 子框架 代替servlet类,control层
- 10
- 11 s: spring 框架系列
- 12
- 13 基本作用:创建对象(控制反转和依赖注入)
- 14
- 15 m: mybatis 框架 代替dao包,和数据库进行交互(jdbc技术),表 示model层(service包和dao包)
- 16
- 17 **补充**
- 18
- 19 view层:视图层,还是使用jsp实现
- 20
- 21 # **项目演示**
- 22
- 23 ## 目标:用户管理项目
- 24
- 25 ## 操作步骤:
- 26
- 27 ### 1、创建maven项目,并配置tomcat服务器
- 28
- 29 **配置tomcat时无artifact选项解决方案**
- 30
- 31 <img src="img/1573611676801.png" alt="1573611676801" style="zoom:33%;" />
- 32
- 33
- 34
- 35 打开
- 36
- 37 <img src="img/1573611734587.png" alt="1573611734587" style="zoom:33%;" />
- 38
- 39 选择
- 40
- 41 <img src="img/1573611802769.png" alt="1573611802769" style="zoom:33%;" />
- 42
- 43
- 44
- 45 ### 2、创建java和resources目录,并mark一下,新建文件夹变色
- 46
- 47 
- 48
- 49 java文件夹:存放程序员编写的java代码
- 50
- 51 resources文件夹:存放配置文件(和ssm框架相关)
- 52
- 53 ### 3、myBatis框架(替代jdbc技术)
- 54
- 55 #### 3.1 导入和mybatis相关的依赖(包)
- 56
- 57 mybatis
- 58
- 59 mybatis-spring
- 60
- 61 mysql-connector-java
- 62
- 63 ```xml
- 64 <!--mybatis-->
- 65 <dependency>
- 66 <groupId>org.mybatis</groupId>
- 67 <artifactId>mybatis</artifactId>
- 68 <version>3.5.2</version>
- 69 </dependency>
- 70 <!--mybatis-spring 二者的整合-->
- 71 <dependency>
- 72 <groupId>org.mybatis</groupId>
- 73 <artifactId>mybatis-spring</artifactId>
- 74 <version>2.0.2</version>
- 75 </dependency>
- 76 <!--mysql-connector-java 用于java程序连接mysql数据库-->
- 77 <dependency>
- 78 <groupId>mysql</groupId>
- 79 <artifactId>mysql-connector-java</artifactId>
- 80 <version>8.0.17</version>
- 81 </dependency>
- 82 ```
- 83
- 84 #### 3.2 在resources文件夹中创建文件mybatis-config.xml
- 85
- 86 ```xml
- 87 <?xml version="1.0" encoding="UTF-8" ?>
- 88 <!DOCTYPE configuration
- 89 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- 90 "http://mybatis.org/dtd/mybatis-3-config.dtd">
- 91 <configuration>
- 92 <!--该文件中可以对mybatis框架进行一些配置设置-->
- 93
- 94 </configuration>
- 95 ```
- 96
- 97 ### 4.spring框架
- 98
- 99 #### 4.1添加和spring相关的常用的依赖
- 100
- 101 spring-beans
- 102
- 103 spring-context/spring-context-support
- 104
- 105 spring-core
- 106
- 107 spring-expression
- 108
- 109 spring-jdbc
- 110
- 111 spring-tx
- 112
- 113 spring-web
- 114
- 115 commons-dbcp
- 116
- 117 ```xml
- 118 <dependency>
- 119 <groupId>org.springframework</groupId>
- 120 <artifactId>spring-beans</artifactId>
- 121 <version>5.1.8.RELEASE</version>
- 122 </dependency>
- 123 <dependency>
- 124 <groupId>org.springframework</groupId>
- 125 <artifactId>spring-context</artifactId>
- 126 <version>5.1.8.RELEASE</version>
- 127 </dependency>
- 128 <dependency>
- 129 <groupId>org.springframework</groupId>
- 130 <artifactId>spring-context-support</artifactId>
- 131 <version>5.0.10.RELEASE</version>
- 132 </dependency>
- 133 <dependency>
- 134 <groupId>org.springframework</groupId>
- 135 <artifactId>spring-core</artifactId>
- 136 <version>5.1.8.RELEASE</version>
- 137 </dependency>
- 138 <dependency>
- 139 <groupId>org.springframework</groupId>
- 140 <artifactId>spring-expression</artifactId>
- 141 <version>5.1.8.RELEASE</version>
- 142 </dependency>
- 143 <dependency>
- 144 <groupId>org.springframework</groupId>
- 145 <artifactId>spring-jdbc</artifactId>
- 146 <version>5.0.10.RELEASE</version>
- 147 </dependency>
- 148 <dependency>
- 149 <groupId>org.springframework</groupId>
- 150 <artifactId>spring-tx</artifactId>
- 151 <version>5.1.8.RELEASE</version>
- 152 </dependency>
- 153 <dependency>
- 154 <groupId>org.springframework</groupId>
- 155 <artifactId>spring-web</artifactId>
- 156 <version>5.1.8.RELEASE</version>
- 157 </dependency>
- 158
- 159 <!--commons-dbcp 用于创建dataSource数据源-->
- 160 <dependency>
- 161 <groupId>commons-dbcp</groupId>
- 162 <artifactId>commons-dbcp</artifactId>
- 163 <version>1.4</version>
- 164 </dependency>
- 165 ```
- 166
- 167
- 168
- 169 #### 4.2在resources文件夹创建applicationContext.xml文件,用于对spring框架配置的设置
- 170
- 171 ```xml
- 172 <?xml version="1.0" encoding="UTF-8"?>
- 173 <beans xmlns="http://www.springframework.org/schema/beans"
- 174 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 175 xmlns:context="http://www.springframework.org/schema/context"
- 176 xsi:schemaLocation="http://www.springframework.org/schema/beans
- 177 http://www.springframework.org/schema/beans/spring-beans.xsd
- 178 http://www.springframework.org/schema/context
- 179 https://www.springframework.org/schema/context/spring-context.xsd">
- 180 <!--用于spring框架的配置设置-->
- 181
- 182 </beans>
- 183 ```
- 184
- 185 ### 5.springmvc框架
- 186
- 187 #### 5.1添加依赖(jar包)
- 188
- 189 spring-mvc
- 190
- 191 ```xml
- 192 <!--springmvc的依赖-->
- 193 <dependency>
- 194 <groupId>org.springframework</groupId>
- 195 <artifactId>spring-webmvc</artifactId>
- 196 <version>5.1.5.RELEASE</version>
- 197 </dependency>
- 198 ```
- 199
- 200
- 201
- 202 #### 5.2在resources目录下创建springmvc-servlet.xml文件,对springmvc进行的配置设置
- 203
- 204 ```xml
- 205 <?xml version="1.0" encoding="UTF-8"?>
- 206 <beans xmlns="http://www.springframework.org/schema/beans"
- 207 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 208 xmlns:mvc="http://www.springframework.org/schema/mvc"
- 209 xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
- 210 http://www.springframework.org/schema/beans/spring-beans.xsd
- 211 http://www.springframework.org/schema/context
- 212 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
- 213 <!--该文件用于对springmc框架的配置设置-->
- 214
- 215
- 216 </beans>
- 217 ```
- 218
- 219 #### **注意处理**
- 220
- 221 
- 222
- 223
- 224
- 225 ### 6.在java目录下创建一些基本的包
- 226
- 227 dao :用于连接数据库
- 228
- 229 service:用于进行业务逻辑的处理
- 230
- 231 controller:用于对请求的派发和处理
- 232
- 233 pojo:将数据库的表名定义为类名,字段定义为属性,此类放在此包中
- 234
- 235 interceptor:拦截器包,使用的场景:处理非法登陆。
- 236
- 237 util:工具包,存放一些工具类,比如jdbcUtil,分页插件类等等。
- 238
- 239 test:测试包,用于测试,junit等进行测试。
- 240
- 241 <img src="img/1573526271379.png" alt="1573526271379" style="zoom:33%;" />
- 242
- 243 ### 7.pojo中类的创建和编写
- 244
- 245 根据数据库中的表创建对应类,好处是通过操作pojo类,来代替操作数据库中的表(对象关系映射orm技术)
- 246
- 247 ```java
- 248 //表名对应类名
- 249 public class User {
- 250
- 251 //表字段对应类属性
- 252 private int id;
- 253 private String username;
- 254 private String password;
- 255
- 256 public int getId() {
- 257 return id;
- 258 }
- 259
- 260 public void setId(int id) {
- 261 this.id = id;
- 262 }
- 263
- 264 public String getUsername() {
- 265 return username;
- 266 }
- 267
- 268 public void setUsername(String username) {
- 269 this.username = username;
- 270 }
- 271
- 272 public String getPassword() {
- 273 return password;
- 274 }
- 275
- 276 public void setPassword(String password) {
- 277 this.password = password;
- 278 }
- 279 }
- 280
- 281 ```
- 282
- 283 ### 8、在resources目录下,创建database.properties文件,该文件中编写:driver,url,username,password等连接数据库的参数
- 284
- 285 ```xml
- 286 driver=com.mysql.jdbc.Driver
- 287 url=jdbc:mysql://127.0.0.1:3306/school?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
- 288 user=root
- 289 password=root
- 290 ```
- 291
- 292 ### 9、进行java应用项目连接数据库
- 293
- 294 **在applicationContext.xml文件下**
- 295
- 296 ```xml
- 297 <!--1 引入database.properties文件-->
- 298 <context:property-placeholder location="classpath:database.properties"/>
- 299
- 300 <!--2 创建数据源-->
- 301 <!--基本数据源(配置数据源)-->
- 302 <!-- 配置数据源,核心类:基本数据源BasicDataSource类 -->
- 303 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
- 304 <property name="driverClassName" value="${driver}"/>
- 305 <property name="url" value="${url}"/>
- 306 <property name="username" value="${user}"/>
- 307 <property name="password" value="${password}"/>
- 308 </bean>
- 309
- 310 <!--3 对数据源进行事务管理-->
- 311 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- 312 <property name="dataSource" ref="dataSource"/>
- 313 </bean>
- 314
- 315 <!--4 配置mybatis核心类SqlSessionFactoryBean,引入数据源,引入mybatis-config.xml-->
- 316 <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
- 317 <property name="dataSource" ref="dataSource"/>
- 318 <property name="configLocation" value="classpath:mybatis-config.xml"/>
- 319 </bean>
- 320 ```
- 321
- 322 ### 10、在dao包中创建接口和mapper文件,在spring配置文件中配置
- 323
- 324 在mapper文件中编写sql语句
- 325
- 326 <img src="img/1573530423150.png" alt="1573530423150" style="zoom:25%;" />
- 327
- 328 #### **a 创建接口UserMapper**
- 329
- 330 ```java
- 331 public interface UserMapper {
- 332 //登陆功能
- 333 public User login(@Param("xiaomao")String username,@Param("xiaogou")String password);
- 334 }
- 335 ```
- 336
- 337
- 338
- 339 #### **b 创建UserMapper文件**
- 340
- 341 ```xml
- 342 <?xml version="1.0" encoding="UTF-8" ?>
- 343 <!DOCTYPE mapper
- 344 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- 345 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- 346 <mapper namespace="com.baidu.dao.UserMapper">
- 347 <!--namespace 赋值:对应的接口全路径-->
- 348 <!--select * from user where username=? and password=?-->
- 349 <!--id 填写 对应的接口方法名, resultType 填写接口方法的返回类型-->
- 350 <!--登陆方法-->
- 351 <select id="login" resultType="com.baidu.pojo.User">
- 352 select * from user where username=#{xiaomao} and password=#{xiaogou}
- 353 </select>
- 354 </mapper>
- 355 ```
- 356
- 357 #### c 在applicationContext中配置对mapper文件扫描
- 358
- 359 ```xml
- 360 <!--映射扫描配置器(引入sql映射文件)-->
- 361 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- 362 <property name="basePackage" value="com.baidu.dao"/>
- 363 </bean>
- 364 ```
- 365
- 366 #### d 在pom.xml中<build>内添加
- 367
- 368 **将src/main/java目录下的所有*.xml文件编译到class内,不然报错:找不到mapper文件**
- 369
- 370 ```xml
- 371 <build>
- 372 <!--将java目录下的所有*.xml文件编译到class内,不然报错:找不到mapper文件-->
- 373 <resources>
- 374 <resource>
- 375 <directory>src/main/java</directory>
- 376 <includes>
- 377 <include>**/*.xml</include>
- 378 </includes>
- 379 <filtering>false</filtering>
- 380 </resource>
- 381 </resources>
- 382 </build>
- 383 ```
- 384
- 385
- 386
- 387 ### 11、在service包中创建接口和实现类,编写业务逻辑代码
- 388
- 389 #### 接口
- 390
- 391 ```java
- 392 public interface UserService {
- 393 //登陆功能
- 394 public boolean login(String username,String password);
- 395 }
- 396 ```
- 397
- 398 #### 实现类
- 399
- 400 ```java
- 401 public class UserServiceImpl implements UserService {
- 402
- 403 @Resource//此时报错,缺少依赖:javax.annotation-api
- 404 private UserMapper userMapper=null;
- 405
- 406 //登陆方法
- 407 public boolean login(String username, String password) {
- 408 User user = userMapper.login(username, password);
- 409 boolean flag=false;
- 410 if(user!=null){//用户存在
- 411 flag=true;
- 412 }
- 413 return flag;
- 414 }
- 415 }
- 416 ```
- 417
- 418 #### **补充支持@Resource注解的依赖**
- 419
- 420 ```xml
- 421 <!--@Resource-->
- 422 <dependency>
- 423 <groupId>javax.annotation</groupId>
- 424 <artifactId>javax.annotation-api</artifactId>
- 425 <version>1.3.1</version>
- 426 </dependency>
- 427 ```
- 428
- 429
- 430
- 431 ### 12、在applicationContext.xml文件中对组件进行定义和扫描
- 432
- 433 #### **组件扫描**
- 434
- 435 ##### a、定义组件
- 436
- 437 编写userController类,将@Controller 放在controller包的类上
- 438
- 439 ```java
- 440 @Controller
- 441 public class UserController {
- 442 //编写对请求的派发和处理
- 443
- 444 }
- 445 ```
- 446
- 447 @Service 放在service包的实现类上
- 448
- 449 ```java
- 450 public interface UserService {
- 451 //登陆功能
- 452 public boolean login(String username,String password);
- 453 }
- 454 ```
- 455
- 456 ```
- 457 @Service
- 458 public class UserServiceImpl implements UserService {
- 459
- 460 @Autowired//@Resource同一功能
- 461 private UserMapper userMapper=null;
- 462
- 463 //登陆方法
- 464 public boolean login(String username, String password) {
- 465 User user = userMapper.login(username, password);
- 466 boolean flag=false;
- 467 if(user!=null){//用户存在
- 468 flag=true;
- 469 }
- 470 return flag;
- 471 }
- 472 }
- 473 ```
- 474
- 475
- 476
- 477 @Repository 放在dao包的实现类上(如果没有实现类,就省略)
- 478
- 479 ```java
- 480 public interface UserMapper {
- 481 //登陆功能
- 482 public User login(@Param("xiaomao") String username, @Param("xiaogou") String password);
- 483 }
- 484 ```
- 485
- 486 ##### b、在applicationContext.xml容器中配置,使组件生效
- 487
- 488 ```xml
- 489 <!--组件浏览-->
- 490 <context:component-scan base-package="com.baidu.service"/>
- 491 <context:component-scan base-package="com.baidu.dao"/>
- 492
- 493 <!--注解配置-->
- 494 <context:annotation-config/>
- 495 ```
- 496
- 497 ##### c、在springmvc-serlvet.xml容器中配置,使组件生效
- 498
- 499 ```java
- 500 <!-- 支持组件-->
- 501 <context:component-scan base-package="com.baidu.controller"/>
- 502 ```
- 503
- 504 ### 13、配置web.xml文件
- 505
- 506 #### a."<web-app>"中添加属性
- 507
- 508 ```xml
- 509 <web-app
- 510 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 511 xmlns="http://java.sun.com/xml/ns/javaee"
- 512 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- 513 id="WebApp_ID" version="3.0">
- 514 ```
- 515
- 516 #### b、配置监听器
- 517
- 518 ```xml
- 519 <!-- 配置监听器 -->
- 520 <listener>
- 521 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- 522 </listener>
- 523 ```
- 524
- 525 #### c、引入spring配置文件
- 526
- 527 ```xml
- 528 <!--引入spring的配置文件,制定文件的位置-->
- 529 <context-param>
- 530 <param-name>contextConfigLocation</param-name>
- 531 <param-value>classpath:applicationContext.xml</param-value>
- 532 </context-param>
- 533 ```
- 534
- 535 #### d、配置DispatcherServlet(类似于“总机调度”)
- 536
- 537 ```xml
- 538 <!--配置DispatcherServlet-->
- 539 <servlet>
- 540 <servlet-name>DispatcherServlet</servlet-name>
- 541 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- 542 <init-param>
- 543 <param-name>contextConfigLocation</param-name>
- 544 <param-value>classpath:springmvc-servlet.xml</param-value>
- 545 </init-param>
- 546 <load-on-startup>1</load-on-startup>
- 547 </servlet>
- 548 <servlet-mapping>
- 549 <servlet-name>DispatcherServlet</servlet-name>
- 550 <!--所有的请求都要被拦截,然后进行转发-->
- 551 <url-pattern>/</url-pattern>
- 552 </servlet-mapping>
- 553 ```
- 554
- 555 ### 14、配置springmvc-servlet.xml
- 556
- 557 #### a、配置对组件@Controller的扫描(上述已经操作)
- 558
- 559 ```xml
- 560 <!-- 支持组件-->
- 561 <context:component-scan base-package="com.baidu.controller"/>
- 562 ```
- 563
- 564 #### b、支持注解
- 565
- 566 (@RequestMapping,@RequestParam等)
- 567
- 568 ```xml
- 569 <!--支持注解-->
- 570 <mvc:annotation-driven/>
- 571 ```
- 572
- 573 #### c、配置视图解析器
- 574
- 575 - /WEB-INF/下创建jsp文件夹
- 576
- 577 - 通过逻辑视图名(jsp文件的文件名)找到真实的视图名(jsp文件的全名)
- 578
- 579 举例: 真实的视图名 index.jsp 《---逻辑视图名 jsp
- 580
- 581 比如: /WEB-INF/jsp/index.jsp
- 582
- 583 ```xml
- 584 <!--配置视图解析器-->
- 585 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- 586 <property name="prefix" value="/WEB-INF/jsp/"/>
- 587 <property name="suffix" value=".jsp"/>
- 588 </bean>
- 589 ```
- 590
- 591 #### d、引入静态资源
- 592
- 593 - 在webapp下创建statics文件夹
- 594 - 在springmvc-servlet.xml配置文件中添加
- 595
- 596 ```xml
- 597 <!--引入静态资源--><mvc:resources mapping="/statics/**" location="/statics/"/>
- 598 ```
- 599
- 600 ### 15、在controller类中创建控制器方法
- 601
- 602 ```java
- 603 /**
- 604 * @author 四两数字先生(CSDN/公众号)
- 605 */
- 606 @Controller
- 607 @RequestMapping("/user")
- 608 public class UserController {
- 609
- 610 //private UserService userService=new UserServiceImpl();
- 611 @Resource //自动装配
- 612 private UserService userService;
- 613
- 614 @RequestMapping("/index")
- 615 public String index(){
- 616 return "index";//index就是逻辑视图名
- 617 }
- 618 @RequestMapping("/login")
- 619 public String login(@RequestParam(value = "xiaogou",required = false) String username,
- 620 @RequestParam(value = "xiaomao",required = false) String password){
- 621 boolean flag = this.userService.login(username, password);
- 622 if(flag){//用户存在
- 623 return "success";
- 624 }
- 625 return "failure";
- 626 }
- 627 }
- 628 ```
- 629
- 630 ### 16、编写jsp文件index.jsp
- 631
- 632 a.编写登陆表单
- 633
- 634 ```html
- 635 <form method="get" action="<%=request.getContextPath() %>/user/login">
- 636 用户名:<input type="text" name="xiaogou" value="">
- 637 密码:<input type="password" name="xiaomao" value="">
- 638 <input type="submit" name="submit" value="登陆">
- 639 </form>
- 640 ```
- 641
- 642 b.添加 sevlet-api依赖
- 643
- 644 ```xml
- 645 <!--servlet-api-->
- 646 <dependency>
- 647 <groupId>javax.servlet</groupId>
- 648 <artifactId>javax.servlet-api</artifactId>
- 649 <version>4.0.1</version>
- 650 </dependency>
- 651 ```
- 652
- 653 b.编写登陆成功页面success.jsp
- 654
- 655 ```
- 656 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- 657 <html>
- 658 <head>
- 659 <title>登陆成功</title>
- 660 </head>
- 661 <body>
- 662 <h1>登陆成功</h1>
- 663 </body>
- 664 </html>
- 665 ```
- 666
- 667
- 668
- 669 c.编写登陆失败页面failure.jsp
- 670
- 671 ```
- 672 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- 673 <html>
- 674 <head>
- 675 <title>登陆失败</title>
- 676 </head>
- 677 <body>
- 678 <h1>登陆失败</h1>
- 679 </body>
- 680 </html>
- 681 ```
- 682
- 683 # Spring的两大核心技术
- 684
- 685 ## 控制反转/依赖注入
- 686
- 687 1、添加依赖
- 688
- 689 spring-core
- 690
- 691 spring-context
- 692
- 693 spring-beans
- 694
- 695 spring-expression
- 696
- 697 日志依赖
- 698
- 699 log4j
- 700
- 701 commons-logging
- 702
- 703
- 704
- 705 2、编写代码
- 706
- 707 验证控制反转/依赖注入
- 708
- 709 applicationContext.xml
- 710
- 711 ```xml
- 712 <?xml version="1.0" encoding="UTF-8"?>
- 713 <beans xmlns="http://www.springframework.org/schema/beans"
- 714 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 715 xmlns:context="http://www.springframework.org/schema/context"
- 716 xsi:schemaLocation="http://www.springframework.org/schema/beans
- 717 http://www.springframework.org/schema/beans/spring-beans.xsd
- 718 http://www.springframework.org/schema/context
- 719 https://www.springframework.org/schema/context/spring-context.xsd">
- 720
- 721 <bean id="person1" class="com.baidu.Person">
- 722 <property name="name" value="张三"/>
- 723 </bean>
- 724
- 725
- 726 </beans>
- 727 ```
- 728
- 729 person.java
- 730
- 731 ```java
- 732 /**
- 733 * @author 四两数字先生(CSDN/公众号)
- 734 */
- 735 public class Person {
- 736 String name;
- 737
- 738 public String getName() {
- 739 return name;
- 740 }
- 741
- 742 public void setName(String name) {
- 743 this.name = name;
- 744 }
- 745
- 746 //speak方法
- 747 public void speak(){
- 748 System.out.println(name+"speak hello world");
- 749 }
- 750 }
- 751
- 752 ```
- 753
- 754 MyTest.java
- 755
- 756 ```java
- 757 /**
- 758 * @author 四两数字先生(CSDN/公众号)
- 759 */
- 760 public class MyTest {
- 761
- 762 @Test
- 763 public void test1(){
- 764 /**
- 765 * 通过代码创建对象
- 766 * 通过代码给属性赋值
- 767 */
- 768 //创建对象
- 769 Person person=new Person();
- 770 //给属性赋值
- 771 person.setName("张三");
- 772 //调用speak方法输出
- 773 person.speak();
- 774
- 775 }
- 776
- 777 @Test
- 778 public void test2(){
- 779 /**
- 780 * 通过spring技术之一:控制反转/依赖注入实现来实现
- 781 * 1 由spring容器创建对象
- 782 * 2 由spring容器给属性赋值
- 783 *
- 784 * 控制反转/依赖注入:
- 785 * 控制权正转:通过java代码创建对象和给属性赋值
- 786 * 控制权反转: 将创建对象和给属性赋值的权利反转给spring容器;
- 787 * 控制反转就等价于依赖注入
- 788 *
- 789 */
- 790 //调用speak方法输出
- 791 //通过spring配置文件,获得对象ApplicationContext
- 792 ApplicationContext applicationContext =
- 793 new ClassPathXmlApplicationContext
- 794 ("applicationContext.xml");
- 795
- 796 Person person1 = (Person) applicationContext.getBean("person1");
- 797
- 798 person1.speak();
- 799
- 800 }
- 801 }
- 802
- 803 ```
- 804
- 805 ## 面向切面编程技术
- 806
- 807 在编写代码的过程中,有些代码和业务相关(比如增删改查功能),有些代码和业务没有直接关系,但是是必不可少(日志,安全权限等)
- 808
- 809 1、添加依赖
- 810
- 811 spring-core
- 812
- 813 spring-context
- 814
- 815 spring-beans
- 816
- 817 spring-expression
- 818
- 819 **和日志相关**
- 820
- 821 commons-logging
- 822
- 823 log4j
- 824
- 825 **和aop相关**
- 826
- 827 spring-aop
- 828
- 829 aopalliance
- 830
- 831 aspectjweave
- 832
- 833 # springMvc
- 834
- 835 ## **参数传递(View to Controller)**
- 836
- 837 ### **url含参传递(@RequestParam)**
- 838
- 839 ```java
- 840 @Controller
- 841 public class IndexController{
- 842
- 843 @RequestMapping(“/welcome”)
- 844
- 845 public String index(@RequestParam(value=”username”, required=false)String username){
- 846
- 847
- 848 }
- 849
- 850 }
- 851 ```
- 852
- 853 ### **更精确的url含参传递(@RequestMapping)**
- 854
- 855 注意:http请求信息不仅不含请求url地址,还包含请求方法/请求参数/请求头
- 856
- 857 ```
- 858 @RequestMapping(value=”/welcome”, method=RequestMethod.GET, params=”username”)
- 859
- 860 public String index(@RequestParam String username){
- 861
- 862 logger.info(username);
- 863
- 864 return “index”;
- 865
- 866 }
- 867 ```
- 868
- 869 ## **参数传递(Controller to View)**
- 870
- 871 ### **ModelAndView实现参数传递**
- 872
- 873 ```java
- 874 Controller
- 875
- 876 @Request Mapping(“/welcome”)
- 877 public String index(@RequestParam(value=”username”,required=false) String username){
- 878 logger.info();
- 879 ModelAndView m=new ModelAndView();
- 880 m.addObject(username);
- 881 m.setViewName(“index”);
- 882 Return m;
- 883 }
- 884 ```
- 885
- 886 index.jsp 获得容器中的参数
- 887
- 888 ```html
- 889 <h2 >modelAndView(key:default)--->${string}</h2>
- 890 ```
- 891
- 892 ### **Model实现参数传递**
- 893
- 894 需要使用到el表达式需要添加什么?
- 895
- 896 web.xml需要修改?
- 897
- 898 添加 web-app 后面的属性内容才能支持el表达式
- 899
- 900 ```xml
- 901 <?xml version="1.0" encoding="UTF-8"?>
- 902
- 903 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 904 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- 905 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- 906 version="3.0">
- 907 </web-app>
- 908 ```
- 909
- 910 Controller层
- 911
- 912 ```java
- 913 @RequestMapping(“/welcome”)
- 914 public String index(@RequestParam String username, Model model){
- 915 logger.info();
- 916
- 917 //1
- 918 model.addAttribute(“username”,username);
- 919
- 920 //2
- 921 model.addAttribute(username);
- 922
- 923 //3
- 924 User user= new User();
- 925 user.setUserName(username); model.addAttribute(“currentUserName”,user);
- 926 Return “index”;
- 927
- 928 }
- 929 ```
- 930
- 931 Index.jsp
- 932
- 933 ```html
- 934 <h1>model(key:username)----> ${username}</h1>
- 935
- 936 <h1>model(key:default)-------> ${string}</h1>
- 937
- 938 <h1>model(key:currentUserName)---->${currentUseName.userName}</h1>
- 939 ```
- 940
- 941 ### **Map实现参数传递**
- 942
- 943 Controller
- 944
- 945 注意:Map<String,Object> model 必须以形参的方式引入
- 946
- 947 ```java
- 948 @RequestMapping(“/welcome”)
- 949 public String index(@RequestParam(value=”username”) String username,Map<String,Object> map){
- 950 map.put(“username”,username);
- 951 return “index”;
- 952 }
- 953 ```
- 954
- 955 # Ajax技术
- 956
- 957 1 register.jsp
- 958
- 959 ```java
- 960 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- 961 <html>
- 962 <head>
- 963 <title>注册</title>
- 964 <script src="statics/js/jquery-3.4.1.js"></script>
- 965 <script>
- 966 $(document).ready(function () {
- 967 $("#name").blur(function () {
- 968 var name=this.value;
- 969 if(name==null || name==""){
- 970 $("#nameDiv").html("用户名不能为空");
- 971 }else {
- 972 $.ajax({
- 973
- 974 "url" :"<%=request.getContextPath()%>/AjaxServlet",//action
- 975 "data":"name1="+name, //含参 &name=lisi
- 976 "type" :"get",//method 请求方式
- 977
- 978 "success":callBack,//返回结果成功,写函数名
- 979 "dataType":"text",//返回的数据类型
- 980 "error": function () {
- 981 alert("返回结果失败");
- 982 }
- 983 });
- 984
- 985 function callBack(data) {//返回的结果
- 986 if(data=="true"){//== 字符串
- 987 $("#nameDiv").html("该用户已被使用");
- 988 }else{
- 989 $("#nameDiv").html("该用户可以使用");
- 990 }
- 991 }
- 992 }
- 993 });
- 994 });
- 995 </script>
- 996 </head>
- 997 <body>
- 998 <form action="" method="">
- 999 <%--请求方式
- 1000 get 表单中参数会出现url路径中
- 1001 post 表单的参数不会出现在url路径中,更加安全
- 1002 --%>
- 1003 <h1>注册页面</h1>
- 1004 姓名:<input type="text" name="username" value="" id="name">
- 1005 <div id="nameDiv"></div>
- 1006 密码:<input type="password" name="password" value="">
- 1007 再次确认密码:<input type="password" name="password" value="">
- 1008 <input type="submit" name="register" value="注册">
- 1009 </form>
- 1010 </body>
- 1011 </html>
- 1012 ```
- 1013
- 1014 2 AjaxServlet.jsp
- 1015
- 1016 ```java
- 1017 package com.baidu.servlet;
- 1018
- 1019 import com.baidu.service.UserService;
- 1020 import com.baidu.service.UserServiceImpl;
- 1021
- 1022 import javax.servlet.ServletException;
- 1023 import javax.servlet.annotation.WebServlet;
- 1024 import javax.servlet.http.HttpServlet;
- 1025 import javax.servlet.http.HttpServletRequest;
- 1026 import javax.servlet.http.HttpServletResponse;
- 1027 import java.io.IOException;
- 1028 import java.io.PrintWriter;
- 1029
- 1030 /**
- 1031 * @author 四两数字先生(CSDN/公众号)
- 1032 */
- 1033 public class AjaxServlet extends HttpServlet {
- 1034
- 1035
- 1036 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- 1037 this.doGet(request,response);
- 1038 }
- 1039
- 1040 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- 1041 //编写代码
- 1042 //防止中文乱码
- 1043 request.setCharacterEncoding("utf-8");
- 1044 String name = request.getParameter("name1");//name是哪里
- 1045 boolean flag=false;
- 1046
- 1047 //模拟数据库:假设数据库中存在用户名"admin"
- 1048 if(name.equals("admin")){
- 1049 flag=true;//boolean 型返回前端时自动变成字符串"true"
- 1050 }
- 1051
- 1052 response.setContentType("text/html;charset=utf-8");
- 1053 PrintWriter writer = response.getWriter();
- 1054 writer.print(flag);//将flag返回给前端
- 1055 writer.flush();
- 1056 writer.close();
- 1057
- 1058 }
- 1059 }
- 1060 ```
- 1061
- 1062 3 web.xml
- 1063
- 1064 ```xml
- 1065 <!DOCTYPE web-app PUBLIC
- 1066 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- 1067 "http://java.sun.com/dtd/web-app_2_3.dtd" >
- 1068
- 1069 <web-app
- 1070 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 1071 xmlns="http://java.sun.com/xml/ns/javaee"
- 1072 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- 1073 id="WebApp_ID" version="3.0"
- 1074 >
- 1075 <display-name>Archetype Created Web Application</display-name>
- 1076
- 1077 <servlet>
- 1078 <servlet-name>AjaxServlet</servlet-name>
- 1079 <servlet-class>com.baidu.servlet.AjaxServlet</servlet-class>
- 1080 </servlet>
- 1081 <servlet-mapping>
- 1082 <servlet-name>AjaxServlet</servlet-name>
- 1083 <url-pattern>/AjaxServlet</url-pattern>
- 1084 </servlet-mapping>
- 1085
- 1086 <servlet>
- 1087 <servlet-name>MyServlet</servlet-name>
- 1088 <servlet-class>com.baidu.servlet.MyServlet</servlet-class>
- 1089 </servlet>
- 1090 <servlet-mapping>
- 1091 <servlet-name>MyServlet</servlet-name>
- 1092 <url-pattern>/MyServlet</url-pattern>
- 1093 </servlet-mapping>
- 1094 </web-app>
- 1095 ```
- 1096
- 1097 # Json格式数据
- 1098
- 1099 ```html
- 1100 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- 1101 <html>
- 1102 <head>
- 1103 <title>Title</title>
- 1104 <script src="statics/js/jquery-3.4.1.js"></script>
- 1105 <script>
- 1106 $(document).ready(function() {
- 1107
- 1108 //1、定义JSON格式的user对象,并在div中输出
- 1109 var user = {
- 1110 "id" : 1,
- 1111 "name" : "张三",
- 1112 "pwd" : "000"
- 1113 };
- 1114 $("#objectDiv").append("ID:" + user.id + "<br>")
- 1115 .append("用户名:" + user.name + "<br>")
- 1116 .append("密码:" + user.pwd + "<br>");
- 1117
- 1118 //2、定义JSON格式的字符串数组,在ul中输出
- 1119 var ary = [ "中", "美", "俄" ];
- 1120 var $ary = $(ary);
- 1121 var $ul = $("#arrayUl"); // 展示数据的ul元素
- 1122 $ary.each(function() {
- 1123 $ul.append("<li>"+this+"</li>");
- 1124 });
- 1125
- 1126 //3、定义JSON格式的字符串数组,在select中输出
- 1127 var $sel = $("#arraySel"); // 展示数据的select元素
- 1128 $ary.each(function(i) {//index 索引
- 1129 /**
- 1130 * <select name=“国籍” id=“arraySel”>
- 1131 * <option value=1>中</option>
- 1132 * <option value=2>美</option>
- 1133 * <option value=3>俄</option>
- 1134 * </select>
- 1135 */
- 1136 //"<option value='"+(i+1)+"'>"
- 1137 $sel.append("<option value='"+(i+1)+"'>"+this+"</option>");
- 1138 });
- 1139
- 1140 //4、定义JSON格式的user对象数组,并使用<table>输出
- 1141 var userArray = [ {
- 1142 "id" : 2,
- 1143 "name" : "admin",
- 1144 "pwd" : "123"
- 1145 }, {
- 1146 "id" : 3,
- 1147 "name" : "詹姆斯",
- 1148 "pwd" : "11111"
- 1149 }, {
- 1150 "id" : 4,
- 1151 "name" : "梅西",
- 1152 "pwd" : "6666"
- 1153 } ];
- 1154 /**<div id="objectArrayDiv">
- 1155 * <table border='1'>
- 1156 * <tr><td>ID</td><td>用户名</td><td>密码</td></tr>
- 1157 * <tr><td>2</td><td>admin</td><td>123</td></tr>
- 1158 * <tr><td>3</td><td>詹姆斯</td><td>11111</td></tr>
- 1159 * <tr><td>4</td><td>梅西</td><td>666</td></tr>
- 1160 * </table>
- 1161 * <div>
- 1162 * 我是中国人我爱中国我爱人民人民爱我
- 1163 */
- 1164 var $table = $("<table border='1'></table>").append(
- 1165 "<tr><td>ID</td><td>用户名</td><td>密码</td></tr>");
- 1166 $(userArray).each(function() {
- 1167 $table.append("<tr><td>" + this.id + "</td><td>"
- 1168 + this.name + "</td><td>"
- 1169 + this.pwd + "</td></tr>");
- 1170 });
- 1171 $("#objectArrayDiv").append($table);
- 1172
- 1173 });
- 1174 </script>
- 1175 </head>
- 1176 <body>
- 1177 <%--div中呈现普通对象--%>
- 1178 <div id="objectDiv"></div>
- 1179 <%--无序列表形式呈现数组--%>
- 1180 <div>
- 1181 <ul id="arrayUl">
- 1182
- 1183 </ul>
- 1184 </div>
- 1185 <%--下拉列表形式呈现数组--%>
- 1186 <div>
- 1187 <select id="arraySel"></select>
- 1188 </div>
- 1189 <%--表格形式呈现对象数组--%>
- 1190 <div id="objectArrayDiv">
- 1191
- 1192 </div>
- 1193 </body>
- 1194 </html>
- 1195 ```
- 1196
- 1197 # SpringMVC参数传递
- 1198
- 1199 ```java
- 1200 @Controller
- 1201 @RequestMapping("/user")
- 1202 public class UserController {
- 1203
- 1204 //private UserService userService=new UserServiceImpl();
- 1205 @Resource //自动装配
- 1206 private UserService userService;
- 1207
- 1208 //无参数的url :localhost:8080/项目名/user/index
- 1209 @RequestMapping("/index")
- 1210 public String index(){
- 1211 return "index";//index就是逻辑视图名
- 1212 }
- 1213 //含参的url:localhost:8080/项目名/user/login?xiaogou=lisi&xiaomao=lisi
- 1214 @RequestMapping(value = "/login")
- 1215 public String login(@RequestParam(value = "xiaogou",required = false) String username,
- 1216 @RequestParam(value = "xiaomao",required = false) String password){
- 1217 boolean flag = this.userService.login(username, password);
- 1218 if(flag){//用户存在
- 1219 return "success";
- 1220 }
- 1221 return "failure";
- 1222 }
- 1223 //含参的url:localhost:8080/项目名/login?xiaogou=lisi&xiaomao=lisi
- 1224 //更精确的含参url:method = RequestMethod.POST
- 1225 @RequestMapping(value = "/login1" ,method = RequestMethod.POST)
- 1226 public String login1(@RequestParam(value = "xiaogou",required = false) String username,
- 1227 @RequestParam(value = "xiaomao",required = false) String password){
- 1228 boolean flag = this.userService.login(username, password);
- 1229 if(flag){//用户存在
- 1230 return "success";
- 1231 }
- 1232 return "failure";
- 1233 }
- 1234
- 1235 //Controller层传递到View
- 1236 //1 ModelAndView 作为返回对象
- 1237 @RequestMapping("/index1")
- 1238 public ModelAndView index1(){
- 1239 User user = new User();
- 1240 user.setId(1);
- 1241 user.setUsername("zhangsan");
- 1242 user.setPassword("123456");
- 1243
- 1244 ModelAndView modelAndView = new ModelAndView();
- 1245 //存放数据
- 1246 modelAndView.addObject("user",user);//key-value
- 1247 //设置逻辑视图名
- 1248 modelAndView.setViewName("nankeyuan");
- 1249 return modelAndView;
- 1250 }
- 1251
- 1252 //Model 作为一个容器,保存数据
- 1253 @RequestMapping("/index2")
- 1254 public String index2(Model model){
- 1255 model.addAttribute("str","hello model");
- 1256 //字符串类型 --key:string
- 1257 model.addAttribute("hello model2");
- 1258 return "nankeyuan";
- 1259 }
- 1260
- 1261 //Map作为容器,保存数据
- 1262 @RequestMapping("/index3")
- 1263 public String index3(Map<String,Object> map){
- 1264 map.put("mapStr","map demo");
- 1265 return "nankeyuan";
- 1266 }
- 1267 }
- 1268
- 1269 ```
- 1270
- 1271 # 字符乱码过滤器
- 1272
- 1273 web.xml
- 1274
- 1275 ```
- 1276 <filter>
- 1277 <filter-name>characterFilter</filter-name>
- 1278 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- 1279 <!-- 设置过滤器中的属性值 -->
- 1280 <init-param>
- 1281 <param-name>encoding</param-name>
- 1282 <param-value>UTF-8</param-value>
- 1283 </init-param>
- 1284 <!-- 启动过滤器 -->
- 1285 <init-param>
- 1286 <param-name>forceEncoding</param-name>
- 1287 <param-value>true</param-value>
- 1288 </init-param>
- 1289 </filter>
- 1290 <filter-mapping>
- 1291 <filter-name>characterFilter</filter-name>
- 1292 <!--表示项目名下的所有url都被拦截过滤-->
- 1293 <url-pattern>/*</url-pattern>
- 1294 </filter-mapping>
- 1295
- 1296 ```
- 1297
- 1298 ## Restful风格路径
- 1299
- 1300 ```java
- 1301 //url中含参传递 restful风格路径
- 1302 //localhost:8080/userPro-ssm/user/index6/lisi 等价(?username=lisi)
- 1303 @RequestMapping("/index6/{username}")
- 1304 public String index6(@PathVariable(name = "username") String username){
- 1305 System.out.println("用户名为"+username);
- 1306 return "nankeyuan";
- 1307 }
- 1308 ```
- 1309
- 1310
小菜鸟之SSM框架的更多相关文章
- (SSM框架)实现小程序图片上传(配小程序源码)
阅读本文约"2分钟" 又是一个开源小组件啦! 因为刚好做到这个小功能,所以就整理了一下,针对微信小程序的图片(文件)上传! 原业务是针对用户反馈的图片上传.(没错,本次还提供小程序 ...
- ssm框架与shiro的整合小demo,用idea开发+maven管理
shiro安全框架是目前为止作为登录注册最常用的框架,因为它十分的强大简单,提供了认证.授权.加密和会话管理等功能 . shiro能做什么? 认证:验证用户的身份 授权:对用户执行访问控制:判断用户是 ...
- 菜鸟 ssm 框架的学习之路
跟着老师学习了两个月的java语言,现在学习到了框架的部分,一直想在博客上写点东西的,只是自己一直没有时间,其实到底也是懒,鲁迅说过:"时间就像海绵里的水,只要愿意去挤还是有的", ...
- SSM框架学习之高并发秒杀业务--笔记2-- DAO层
上节中利用Maven创建了项目,并导入了所有的依赖,这节来进行DAO层的设计与开发 第一步,创建数据库和表. 首先分析业务,这个SSM匡济整合案例是做一个商品的秒杀系统,要存储的有:1.待秒杀的商品的 ...
- SSM框架——以注解形式实现事务管理
上一篇博文<SSM三大框架整合详细教程>详细说了如何整合Spring.SpringMVC和MyBatis这三大框架.但是没有说到如何配置mybatis的事务管理,在编写业务的过程中,会需要 ...
- 百度富文本编辑器ueditor在jsp中的使用(ssm框架中的应用)
折腾了一下午终于把百度富文本编辑器ueditor搞定了! 项目地址:https://github.com/724888/lightnote_new 首先我参考了一个ueditor的demo ...
- 整合最优雅SSM框架:SpringMVC + Spring + MyBatis
我们看招聘信息的时候,经常会看到这一点,需要具备SSH框架的技能:而且在大部分教学课堂中,也会把SSH作为最核心的教学内容. 但是,我们在实际应用中发现,SpringMVC可以完全替代Struts,配 ...
- 最优雅SSM框架:SpringMVC + Spring + MyBatis
在写代码之前我们先了解一下这三个框架分别是干什么的? 相信大以前也看过不少这些概念,我这就用大白话来讲,如果之前有了解过可以跳过这一大段,直接看代码! SpringMVC:它用于web层,相当于con ...
- 手把手教你 基础 整合最优雅SSM框架:SpringMVC + Spring
我们看招聘信息的时候,经常会看到这一点,需要具备SSH框架的技能:而且在大部分教学课堂中,也会把SSH作为最核心的教学内容. 但是,我们在实际应用中发现,SpringMVC可以完全替代Struts,配 ...
随机推荐
- P3469 割点的应用
https://www.luogu.org/problem/P3469 题目就是说封锁一个点,会导致哪些点(对)连不通: 用tarjan求割点,如果这个点是割点,那么不能通行的点对数就是(乘法法则)儿 ...
- Qt读写Json格式配置文件
头文件Config.h #pragma once #include <QVariantMap> class Config { public: Config(const QString &a ...
- tomcat发布web项目
转:https://www.cnblogs.com/skyblue-li/p/7888951.html Tomcat是一种Web服务器,我们自己做好了一个Web项目,就可以通过Tomcat来发布.服务 ...
- mysql 使用service mysqld start 提示未识别服务 进入/etc/rc.d/init.d 下面未发现有mysqld解决方法
1.执行whereis mysql会有如下打印: mysql: /usr/bin/mysql /usr/lib64/mysql /usr/include/mysql /usr/share/mysql ...
- EntityFramework Core Code First 已有数据库
问题场景:我已经有一个数据库,想用 EF core Code First,怎么办? 首先,可以参考微软的API文档:通过现有数据库在 ASP.NET Core 上开始使用 EF Core, 这一步可以 ...
- vue devtools无法使用
vue devtools无法使用 一.总结 一句话总结: 没显示vue devtools调试工具的原因是用了生产环境的版本或是压缩的vue版本,或是没有勾选:允许访问文件网址 二.vue调试工具Dev ...
- lareval重命名created_at和updated_at字段
lareval重命名created_at和updated_at字段 一.总结 一句话总结: 要改变created_at和updated_at的名称,模型和数据迁移里面都需要改变 在模型中指定数据类型之 ...
- Flutter移动电商实战 --(32)列表页_小类高亮交互效果制作
点击大类右侧的横向的小类红色显示当前的小类别 解决之前溢出的问题: 先解决一个bug,之前右侧的这里设置的高度是1000,但是有不同的虚拟机和手机设别的问题造成了溢出的问题 Expaned是有伸缩能力 ...
- The first one spawns an additional process forwarding requests to a series of workers (think about it as a form of shield, at the same level of apache or nginx), while the second one sets workers to n
Things to know (best practices and “issues”) READ IT !!! — uWSGI 2.0 documentationhttps://uwsgi-docs ...
- UNIX 历史问题 分布式系统的Thundering Herd效应 惊群效应
https://uwsgi-docs.readthedocs.io/en/latest/articles/SerializingAccept.html One of the historical pr ...