1.SpringMVC技术

1.1 技术介绍

  1. 一个MVC框架,在web模型中,MVC是一种很流行的框架,通过把ModelViewController分离,把较为复杂的web应用分成逻辑清晰的几部分,是为了简化开发,减少出错。
  2. 老李的理解是将原来传统的jsp模式开发,整合成不同的业务分层,更加合理的分离整合代码,减少耦合性。

1.2 技术注意点

1.2.1 @RequestParam注解

请求参数名和列对不上时,可以使用required=”false”来选择可以不用传参,defaultValue来默认请求参数。

  1. public String add(@RequestParam(value="id" required=false)Integer idxxxa, defaultValue="1"){
  2. ...
  3. }

1.2.2 设置请求和响应编码格式

修改web.xml

  1. <filter>
  2. <filter-name>encodingFilter</filter-name>
  3. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  4. <init-param>
  5. <param-name>encoding</param-name>
  6. <param-value>UTF-8</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>forceEncoding</param-name>
  10. <param-value>true</param-value>
  11. </init-param>
  12. </filter>
  13. <filter-mapping>
  14. <filter-name>encodingFilter</filter-name>
  15. <url-pattern>/*</url-pattern>
  16. </filter-mapping>

1.2.3 静态资源放行

修改spring-mvc.xml

  1. <mvc:resources mapping="/img/**" location="/img/" />
  2. <mvc:resources mapping="/js/**" location="/js/" />
  3. <mvc:resources mapping="/css/**" location="/css/" />
  4. <mvc:resources mapping="/html/**" location="/html/" />

1.2.4 全局更改编码

修改tomcat的server.xml

  1. <Connector URIEncoding="UTF-8" connectionTimeout="20000", port="8080" protocol="HTTP/1.1" ......>

1.2.5 @ResponseBody注解

加上这个注解后,是无法返回jsp页面,所以在springmvc中要根据@RestController

  1. @RequestMapping("/getStudent")
  2. @ResponseBody
  3. public String getStudentInfo(@RequestParam(value = "id", required = false) Long id,ModelMap model) {
  4. List<Student> student = studentService.getStudentById(id);
  5. System.out.println(student);
  6. model.addAttribute("student", student.get(0));
  7. return "student";
  8. }

1.3 技术具体demo文件路径

链接:https://pan.baidu.com/s/1j2-VIv-A5jNgX2WJiQaaAw
提取码:hm00

1.4 demo的UML图

image.png

image.png

1.5 demo的技术代码实现

1.5.1 pom的编写
  1. <!--统一jar包-->
  2. <properties>
  3. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  4. <spring.vision>5.0.2.RELEASE</spring.vision>
  5. <shiro.version>1.2.3</shiro.version>
  6. <junit.version>4.12</junit.version>
  7. <mysql.version>5.1.38</mysql.version>
  8. <mybatis.ehcache.version>1.1.0</mybatis.ehcache.version>
  9. <log4j.version>1.2.17</log4j.version>
  10. <slf4j.version>1.7.5</slf4j.version>
  11. <slf4j.log4j.version>1.7.12</slf4j.log4j.version>
  12. <mybatis.generator.version>1.3.2</mybatis.generator.version>
  13. <cglib.version>3.1</cglib.version>
  14. <asm.version>3.3.1</asm.version>
  15. <pagehelper.version>5.1.8</pagehelper.version>
  16. <mybatis.spring.version>1.3.2</mybatis.spring.version>
  17. <aspectjweaver.version>1.9.2</aspectjweaver.version>
  18. <c3p0.version>0.9.5.2</c3p0.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>commons-fileupload</groupId>
  23. <artifactId>commons-fileupload</artifactId>
  24. <version>1.2.1</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>javax.servlet</groupId>
  28. <artifactId>servlet-api</artifactId>
  29. <version>2.5</version>
  30. </dependency>
  31. <!--mybatis框架-->
  32. <dependency>
  33. <groupId>org.mybatis</groupId>
  34. <artifactId>mybatis</artifactId>
  35. <version>3.4.6</version>
  36. </dependency>
  37. <!-- JUnit单元测试工具 -->
  38. <dependency>
  39. <groupId>junit</groupId>
  40. <artifactId>junit</artifactId>
  41. <version>${junit.version}</version>
  42. <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
  43. <scope>test</scope>
  44. </dependency>
  45. <!--mysql连接池jar包-->
  46. <dependency>
  47. <groupId>mysql</groupId>
  48. <artifactId>mysql-connector-java</artifactId>
  49. <version>${mysql.version}</version>
  50. </dependency>
  51. <!--缓存-->
  52. <dependency>
  53. <groupId>org.mybatis.caches</groupId>
  54. <artifactId>mybatis-ehcache</artifactId>
  55. <version>${mybatis.ehcache.version}</version>
  56. </dependency>
  57. <!--打印日志-->
  58. <dependency>
  59. <groupId>log4j</groupId>
  60. <artifactId>log4j</artifactId>
  61. <version>${log4j.version}</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.slf4j</groupId>
  65. <artifactId>slf4j-api</artifactId>
  66. <version>${slf4j.version}</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>org.slf4j</groupId>
  70. <artifactId>slf4j-log4j12</artifactId>
  71. <version>${slf4j.log4j.version}</version>
  72. </dependency>
  73. <!--自动生成bean-->
  74. <dependency>
  75. <groupId>org.mybatis.generator</groupId>
  76. <artifactId>mybatis-generator-core</artifactId>
  77. <version>${mybatis.generator.version}</version>
  78. </dependency>
  79. <!--toString与重载方法过滤-->
  80. <dependency>
  81. <groupId>cglib</groupId>
  82. <artifactId>cglib</artifactId>
  83. <version>${cglib.version}</version>
  84. </dependency>
  85. <dependency>
  86. <groupId>asm</groupId>
  87. <artifactId>asm</artifactId>
  88. <version>${asm.version}</version>
  89. </dependency>
  90. <!--分页-->
  91. <dependency>
  92. <groupId>com.github.pagehelper</groupId>
  93. <artifactId>pagehelper</artifactId>
  94. <version>${pagehelper.version}</version>
  95. </dependency>
  96. <!--spring框架核心库-->
  97. <dependency>
  98. <groupId>org.springframework</groupId>
  99. <artifactId>spring-context</artifactId>
  100. <version>${spring.vision}</version>
  101. </dependency>
  102. <!-- spring-aop -->
  103. <dependency>
  104. <groupId>org.springframework</groupId>
  105. <artifactId>spring-aop</artifactId>
  106. <version>${spring.vision}</version>
  107. </dependency>
  108. <dependency>
  109. <groupId>org.springframework</groupId>
  110. <artifactId>spring-orm</artifactId>
  111. <version>${spring.vision}</version>
  112. </dependency>
  113. <!--Spring java数据库访问包,在本例中主要用于提供数据源 -->
  114. <dependency>
  115. <groupId>org.springframework</groupId>
  116. <artifactId>spring-jdbc</artifactId>
  117. <version>${spring.vision}</version>
  118. </dependency>
  119. <!--Spring Web-->
  120. <dependency>
  121. <groupId>org.springframework</groupId>
  122. <artifactId>spring-webmvc</artifactId>
  123. <version>${spring.vision}</version>
  124. </dependency>
  125. <dependency>
  126. <groupId>org.springframework</groupId>
  127. <artifactId>spring-web</artifactId>
  128. <version>${spring.vision}</version>
  129. </dependency>
  130. <dependency>
  131. <groupId>org.springframework</groupId>
  132. <artifactId>spring-context-support</artifactId>
  133. <version>${spring.vision}</version>
  134. </dependency>
  135. <!--spring和mybatis整合-->
  136. <dependency>
  137. <groupId>org.mybatis</groupId>
  138. <artifactId>mybatis-spring</artifactId>
  139. <version>${mybatis.spring.version}</version>
  140. </dependency>
  141. <!-- aspectjweaver 切面-->
  142. <dependency>
  143. <groupId>org.aspectj</groupId>
  144. <artifactId>aspectjweaver</artifactId>
  145. <version>${aspectjweaver.version}</version>
  146. </dependency>
  147. <!--最新版c3p0连接池-->
  148. <dependency>
  149. <groupId>com.mchange</groupId>
  150. <artifactId>c3p0</artifactId>
  151. <version>${c3p0.version}</version>
  152. </dependency>
  153. </dependencies>

1.5.2 数据源的配置 jdbc.properties
  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/stus?useUnicode=true&characterEncoding=utf8&useSSL=false
  3. jdbc.username=root
  4. jdbc.password=123
  5. #最大连接数
  6. maxPoolSize=100
  7. #最小连接数
  8. minPoolSize=20
  9. # 关闭连接后不自动commit
  10. autoCommitOnClose=false
  11. # 获取连接超时时间
  12. checkoutTimeout=10000
  13. # 初始化连接
  14. initialSize=30
  15. #driver default 指定由连接池所创建的连接的只读(read-only)状态。
  16. #如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix)
  17. defaultReadOnly=
  18. #driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
  19. #可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
  20. defaultTransactionIsolation=READ_UNCOMMITTED
  21. #连接数
  22. acquireIncrement=30

1.5.3 日志的配置 log4j.properties
  1. #---- global logging configuration
  2. #---- level: FATAL,ERROR,WARN,INFO,DEBUG
  3. #---- appender: console, file, mail
  4. ### set log levels ###
  5. log4j.rootLogger = DEBUG,console
  6. ### 输出到控制台 ###
  7. log4j.appender.console = org.apache.log4j.ConsoleAppender
  8. log4j.appender.console.Target = System.out
  9. log4j.appender.console.layout = org.apache.log4j.PatternLayout
  10. log4j.appender.console.layout.ConversionPattern = %d{yyyy-MM-dd HH\:mm\:ss} %5p %c{1}:%L - %m%n
  11. ### 输出到日志文件 ###
  12. log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
  13. log4j.appender.file.File = ${webapp.root}/WEB-INF/logs/platform.log
  14. log4j.appender.file.DatePattern=_yyyyMMdd'.log'
  15. #log4j.appender.file.Append = true
  16. #log4j.appender.file.Threshold = INFO
  17. log4j.appender.file.layout = org.apache.log4j.PatternLayout
  18. log4j.appender.file.layout.ConversionPattern =%-d{yyyy-MM-dd HH\:mm\:ss} [ %t\:%r ] - [ %p ] %m%n
  19. ### 打印SQL ###
  20. #log4j.logger.com.ibatis=DEBUG
  21. #log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
  22. #log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
  23. #log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
  24. log4j.logger.java.sql.Connection=DEBUG
  25. log4j.logger.java.sql.Statement=DEBUG
  26. log4j.logger.java.sql.PreparedStatement=DEBUG
  27. #log4j.logger.java.sql.ResultSet=DEBUG

1.5.4 web.xml配置
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <!-- 加载Spring容器配置 -->
  7. <listener>
  8. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  9. </listener>
  10. <!-- 设置Spring容器加载所有的配置文件的路径 -->
  11. <context-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath:applicationContext.xml,classpath:spring-mvc.xml</param-value>
  14. </context-param>
  15. <!-- 配置DispatcherServlet -->
  16. <servlet>
  17. <servlet-name>spring-mvc</servlet-name>
  18. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  19. <init-param>
  20. <param-name>contextConfigLocation</param-name>
  21. <param-value>classpath:applicationContext.xml,classpath:spring-mvc.xml</param-value>
  22. </init-param>
  23. <!-- 启动加载一次 -->
  24. <load-on-startup>1</load-on-startup>
  25. </servlet>
  26. <servlet-mapping>
  27. <servlet-name>spring-mvc</servlet-name>
  28. <!-- 默认匹配所有的请求 -->
  29. <url-pattern>/</url-pattern>
  30. </servlet-mapping>
  31. <!-- 防止Spring内存溢出监听器 -->
  32. <listener>
  33. <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  34. </listener>
  35. <!-- 解决工程编码过滤器 -->
  36. <filter>
  37. <filter-name>encodingFilter</filter-name>
  38. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  39. <init-param>
  40. <param-name>encoding</param-name>
  41. <param-value>UTF-8</param-value>
  42. </init-param>
  43. <init-param>
  44. <param-name>forceEncoding</param-name>
  45. <param-value>true</param-value>
  46. </init-param>
  47. </filter>
  48. <filter-mapping>
  49. <filter-name>encodingFilter</filter-name>
  50. <url-pattern>/*</url-pattern>
  51. </filter-mapping>
  52. <welcome-file-list>
  53. <welcome-file>index.jsp</welcome-file>
  54. </welcome-file-list>
  55. </web-app>

1.5.5 springmvc.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. xmlns:p="http://www.springframework.org/schema/p"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
  13. <!-- 自动扫描controller包下所有的类,成为spring mvc 的控制器-->
  14. <context:component-scan base-package="org.hikktn.controller" use-default-filters="false">
  15. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
  16. </context:component-scan>
  17. <!--视图解析器-->
  18. <!--定义JSP文件的位置-->
  19. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  20. <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"/>
  21. <property name="prefix" value="/WEB-INF/" />
  22. <property name="suffix" value=".jsp" />
  23. <property name="order" value="0"></property>
  24. </bean>
  25. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  26. <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"/>
  27. <property name="prefix" value="/html/" />
  28. <property name="order" value="10"></property>
  29. <property name="suffix" value=".html" />
  30. </bean>
  31. <!-- 视图解析器:通用视图名来解析视图(Excel) -->
  32. <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
  33. <property name="order" value="100"></property>
  34. </bean>
  35. <context:annotation-config/>
  36. <!-- 开启注解( 用于将对象转换为 JSON) -->
  37. <mvc:annotation-driven/>
  38. <!--处理静态资源-->
  39. <mvc:default-servlet-handler></mvc:default-servlet-handler>
  40. <!--
  41. 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd
  42. -->
  43. <mvc:resources mapping="/img/**" location="/img/" />
  44. <mvc:resources mapping="/js/**" location="/js/" />
  45. <mvc:resources mapping="/css/**" location="/css/" />
  46. <mvc:resources mapping="/html/**" location="/html/" />
  47. <!-- 定义文件上传解析器 -->
  48. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8">
  49. <!-- 设定文件上传的最大值为100MB,100*1024*1024 -->
  50. <property name="maxUploadSize">
  51. <value>104857600</value>
  52. </property>
  53. <property name="maxInMemorySize">
  54. <value>40960</value>
  55. </property>
  56. </bean>
  57. </beans>

1.5.6 applicationContext.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:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop.xsd">
  15. <!--引入属性文件,在配置中占位使用 -->
  16. <context:property-placeholder location="classpath:jdbc.properties"/>
  17. <!--配置C3P0数据源-->
  18. <bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  19. <!--连接数据库的基本信息-->
  20. <property name="user" value="${jdbc.username}" />
  21. <property name="password" value="${jdbc.password}" />
  22. <property name="jdbcUrl" value="${jdbc.url}" />
  23. <property name="driverClass" value="${jdbc.driver}" />
  24. <!--连接池的相应配置-->
  25. <property name="maxPoolSize" value="${maxPoolSize}"></property>
  26. <property name="minPoolSize" value="${minPoolSize}"></property>
  27. <property name="initialPoolSize" value="${initialSize}"></property>
  28. <property name="acquireIncrement" value="${acquireIncrement}"></property>
  29. <property name="autoCommitOnClose" value="${autoCommitOnClose}"></property>
  30. </bean>
  31. <!--配置mybatis的工厂-->
  32. <bean id="sessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
  33. <!--数据源-->
  34. <property name="dataSource" ref="ds"></property>
  35. <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
  36. <property name="configLocation" value="classpath:mybatis-config.xml"></property>
  37. <!--类的别名的包-->
  38. <property name="typeAliasesPackage" value="org.hikktn.model"></property>
  39. <!-- 扫描sql配置文件:mapper需要的xml文件 -->
  40. <property name="mapperLocations" value="classpath*:mapper/*.xml" />
  41. </bean>
  42. <!--spring和mybatis整合 (自动扫描映射接口)-->
  43. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  44. <!--指定ioc容器中的mybatis工厂的bean的名字-->
  45. <property name="sqlSessionFactoryBeanName" value="sessionFactoryBean"></property>
  46. <!--mapper所在的包-->
  47. <property name="basePackage" value="org.hikktn.dao"></property>
  48. </bean>
  49. <!--声明式事务管理-->
  50. <!--定义事物管理器,由spring管理事务 -->
  51. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  52. <property name="dataSource" ref="ds"></property>
  53. </bean>
  54. <!--支持注解驱动的事务管理,指定事务管理器 -->
  55. <!--事务属性-->
  56. <tx:advice id="advice" transaction-manager="transactionManager">
  57. <tx:attributes>
  58. <tx:method name="*" isolation="READ_COMMITTED" propagation="REQUIRED"/>
  59. <tx:method name="insert*" isolation="SERIALIZABLE" propagation="REQUIRED"></tx:method>
  60. </tx:attributes>
  61. </tx:advice>
  62. <!--配置事务(拦截service)-->
  63. <aop:config>
  64. <!--切入点表达式-->
  65. <aop:pointcut id="pc" expression="execution(* org.hikktn.service.*.*(..))"></aop:pointcut>
  66. <!--关联切入点表达式和事务属性-->
  67. <aop:advisor advice-ref="advice" pointcut-ref="pc"></aop:advisor>
  68. </aop:config>
  69. <!--使事务注解起作用-->
  70. <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
  71. <!--只扫描controller的包以外的包-->
  72. <context:component-scan base-package="org.hikktn">
  73. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
  74. </context:component-scan>
  75. <!--aspectj支持自动代理实现AOP功能-->
  76. <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
  77. <!-- 拦截器方式配置事物(这里可以不写,只是方便在advice的事务方便增加相应的事务配置) -->
  78. <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
  79. <tx:attributes>
  80. <tx:method name="add*" propagation="REQUIRED" />
  81. <tx:method name="append*" propagation="REQUIRED" />
  82. <tx:method name="insert*" propagation="REQUIRED" />
  83. <tx:method name="save*" propagation="REQUIRED" />
  84. <tx:method name="update*" propagation="REQUIRED" />
  85. <tx:method name="modify*" propagation="REQUIRED" />
  86. <tx:method name="edit*" propagation="REQUIRED" />
  87. <tx:method name="delete*" propagation="REQUIRED" />
  88. <tx:method name="remove*" propagation="REQUIRED" />
  89. <tx:method name="repair" propagation="REQUIRED" />
  90. <tx:method name="delAndRepair" propagation="REQUIRED" />
  91. <tx:method name="get*" propagation="SUPPORTS" />
  92. <tx:method name="find*" propagation="SUPPORTS" />
  93. <tx:method name="load*" propagation="SUPPORTS" />
  94. <tx:method name="search*" propagation="SUPPORTS" />
  95. <tx:method name="datagrid*" propagation="SUPPORTS" />
  96. <tx:method name="*" propagation="SUPPORTS" />
  97. </tx:attributes>
  98. </tx:advice>
  99. </beans>

1.5.7 model的准备
  1. package org.hikktn.model;
  2. public class Student {
  3. /**
  4. * 主键
  5. */
  6. private Long id;
  7. /**
  8. * 名称
  9. */
  10. private String name;
  11. /**
  12. * 年齡
  13. */
  14. private Long age;
  15. public Long getId() {
  16. return id;
  17. }
  18. public void setId(Long id) {
  19. this.id = id;
  20. }
  21. public String getName() {
  22. return name;
  23. }
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27. public Long getAge() {
  28. return age;
  29. }
  30. public void setAge(Long age) {
  31. this.age = age;
  32. }
  33. @Override
  34. public String toString() {
  35. return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}';
  36. }
  37. }

1.5.8 dao的编写
  1. @Repository
  2. public interface StudentDao {
  3. List<Student> getStudentById(@Param("id")Long id);
  4. }

1.5.9 service的编写
  1. public interface StudentService {
  2. List<Student> getStudentById(Long id);
  3. }

1.5.10 service实现类的编写
  1. @Service
  2. public class StudentServiceImpl implements StudentService {
  3. @Autowired
  4. private StudentDao studentDao;
  5. public List<Student> getStudentById(Long id) {
  6. List<Student> student = studentDao.getStudentById(id);
  7. return student;
  8. }
  9. }

1.5.11 Controller的编写
  1. @Controller
  2. public class StudentController {
  3. @Autowired
  4. private StudentService studentService;
  5. @RequestMapping(value = "/hello")
  6. public String hello(ModelMap model) {
  7. model.addAttribute("message", "Spring 3 MVC Hello World");
  8. return "index";
  9. }
  10. @RequestMapping("/getStudent")
  11. public String getStudentInfo(@RequestParam(value = "id", required = false) Long id,ModelMap model) {
  12. List<Student> student = studentService.getStudentById(id);
  13. System.out.println(student);
  14. model.addAttribute("student", student.get(0));
  15. return "student";
  16. }
  17. }

1.5.12 启动访问路径

http://localhost:8080/hello
http://localhost:8080/getStudent?id=1
image.png

1.5.13 jsp的编写

index.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>Title</title>
  5. </head>
  6. <body>
  7. <span>Hello World!</span>
  8. <p>message:${message}</p>
  9. </body>
  10. </html>

student.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>Title</title>
  5. </head>
  6. <body>
  7. <p>学生信息:</p>
  8. <p>学生姓名:${student.name}</p>
  9. <p>学生年龄:${student.age}</p>
  10. </body>
  11. </html>

1.6 对应博客地址

1.7 环境准备

1.7.1 在idea上准备tomcat

https://blog.csdn.net/wsjzzcbq/article/details/89463304

1.7.2 idea上配置webapp

https://blog.csdn.net/qq_44575680/article/details/89449581

1.7.3 idea上移除或加入jar包

image.png
解释:左侧的available elements里面,如果出现jar包,则直接手动拖动lib目录下,并移除lib目录下报红的jar包,报红的jar包代表不存在。这里主要是当你的jar包出现冲突,某个类找不到的时候,你需要更改pom,但是maven依赖成功了,问题还是没有解决,这时你就需要手动排除掉一些错误引入的jar包,保证正确的jar包引入。

1.7.4 配置详解

1.7.4.1 数据库的数据源配置解释
  1. 本次使用的C3P0作为练习,即在applicationContext.xml中配置
  1. <!--引入属性文件,在配置中占位使用 -->
  2. <context:property-placeholder location="classpath:jdbc.properties"/>
  3. <!--配置C3P0数据源-->
  4. <bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  5. <!--连接数据库的基本信息-->
  6. <property name="user" value="${jdbc.username}" />
  7. <property name="password" value="${jdbc.password}" />
  8. <property name="jdbcUrl" value="${jdbc.url}" />
  9. <property name="driverClass" value="${jdbc.driver}" />
  10. <!--连接池的相应配置-->
  11. <property name="maxPoolSize" value="${maxPoolSize}"></property>
  12. <property name="minPoolSize" value="${minPoolSize}"></property>
  13. <property name="initialPoolSize" value="${initialSize}"></property>
  14. <property name="acquireIncrement" value="${acquireIncrement}"></property>
  15. <property name="autoCommitOnClose" value="${autoCommitOnClose}"></property>
  16. </bean>

这样就能连接数据库。

1.7.4.2 applicationContext上下文配置

下面的模板,只需要更改红框起来的地方,其他可以不用修改。
image.pngimage.png