程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(3)

SSM框架配置

发布于2021-06-12 13:50     阅读(864)     评论(0)     点赞(14)     收藏(2)


SSM包括:Mybatis,Spring,SpringMVC

目录

准备工作

配置Mybatis

配置Spring

配置SpringMVC

整合


准备工作

导入各种依赖

  1. <!--依赖:
  2. junit,数据库驱动,链接池,servlet,jsp,mybatis,mybatis-spring,spring
  3. -->
  4. <dependencies>
  5. <dependency>
  6. <groupId>junit</groupId>
  7. <artifactId>junit</artifactId>
  8. <version>4.13.2</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>mysql</groupId>
  12. <artifactId>mysql-connector-java</artifactId>
  13. <version>8.0.24</version>
  14. </dependency>
  15. <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
  16. <!-- 数据库链接池 c3p0 dbcp-->
  17. <dependency>
  18. <groupId>com.mchange</groupId>
  19. <artifactId>c3p0</artifactId>
  20. <version>0.9.5.5</version>
  21. </dependency>
  22. <!--servlet-->
  23. <dependency>
  24. <groupId>javax.servlet</groupId>
  25. <artifactId>servlet-api</artifactId>
  26. <version>2.5</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>javax.servlet.jsp</groupId>
  30. <artifactId>jsp-api</artifactId>
  31. <version>2.2</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>javax.servlet</groupId>
  35. <artifactId>jstl</artifactId>
  36. <version>1.2</version>
  37. </dependency>
  38. <!--mybatis-->
  39. <dependency>
  40. <groupId>org.mybatis</groupId>
  41. <artifactId>mybatis</artifactId>
  42. <version>3.5.7</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.mybatis</groupId>
  46. <artifactId>mybatis-spring</artifactId>
  47. <version>2.0.6</version>
  48. </dependency>
  49. <!--spring-->
  50. <dependency>
  51. <groupId>org.springframework</groupId>
  52. <artifactId>spring-webmvc</artifactId>
  53. <version>5.3.6</version>
  54. </dependency>
  55. <dependency>
  56. <groupId>org.springframework</groupId>
  57. <artifactId>spring-jdbc</artifactId>
  58. <version>5.3.6</version>
  59. </dependency>
  60. <!--测试-->
  61. <dependency>
  62. <groupId>junit</groupId>
  63. <artifactId>junit</artifactId>
  64. <version>4.13.2</version>
  65. </dependency>
  66. </dependencies>

配置资源导出问题,防止项目只识别resources目录下的配置文件

  1. <!--静态资源导出问题-->
  2. <build>
  3. <resources>
  4. <resource>
  5. <directory>src/main/resources</directory>
  6. <includes>
  7. <include>**/*.properties</include>
  8. <include>**/*.xml</include>
  9. </includes>
  10. <filtering>false</filtering>
  11. </resource>
  12. <resource>
  13. <directory>src/main/java</directory>
  14. <includes>
  15. <include>**/*.properties</include>
  16. <include>**/*.xml</include>
  17. </includes>
  18. <filtering>false</filtering>
  19. </resource>
  20. </resources>
  21. </build>

添加WEB支持

在WEB支持中导入lib依赖(创建lib文件夹,并导入依赖)

为各个层(数据层,服务层,控制层)创建文件夹

在IDEA中添加数据库

 

配置Mybatis

创建数据库链接的配置文件 ,创建配置文件      database.properties

  1. jdbc.driver=com.mysql.cj.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?serverTimezone=UTC
  3. jdbc.username=root
  4. jdbc.password=root

配置数据库链接池:主要作用于dao层(数据层),创建配置文件      spring-dao.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. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  7. <!--1.管理数据库配置文件-->
  8. <context:property-placeholder location="classpath:database.properties"/>
  9. <!--2.链接池
  10. dbcp:半自动化操作,不能自动链接
  11. c3p0:自动化操作(自动化加载配置文件,并且可以自动设置到对象中!)
  12. -->
  13. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  14. <property name="driverClass" value="${jdbc.driver}"/>
  15. <property name="jdbcUrl" value="${jdbc.url}"/>
  16. <property name="user" value="${jdbc.username}"/>
  17. <property name="password" value="${jdbc.password}"/>
  18. </bean>
  19. <!--3.sqlSessionFactory-->
  20. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  21. <property name="dataSource" ref="dataSource"/>
  22. <!--绑定Mybatis的配置文件-->
  23. <property name="configLocation" value="classpath:mybatis-config.xml"/>
  24. </bean>
  25. <!--配置dao接口扫描包,动态的实现了Dao接口可以注入到Spring容器中-->
  26. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  27. <!--注入sqlSessionFactory-->
  28. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  29. <!--要扫描的到包-->
  30. <property name="basePackage" value="com.zsy.dao"/>
  31. </bean>
  32. </beans>

配置Mybatis操作数据库配置,创建配置文件         mybatis-config.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <!--configuration核心配置文件-->
  6. <configuration>
  7. <!--配置数据源,交给Spring去做-->
  8. <!--取别名-->
  9. <typeAliases>
  10. <package name="com.zsy.pojo"/>
  11. </typeAliases>
  12. <mappers>
  13. <mapper class="com.zsy.dao.BookMapper"/>
  14. </mappers>
  15. </configuration>

 

配置Spring

service层主要基于代理模式,创建配置文件     spring-service.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. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. https://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. https://www.springframework.org/schema/context/spring-context.xsd">
  9. <!--1.扫描service下的包-->
  10. <context:component-scan base-package="com.zsy.service"/>
  11. <!--2.将我们的所有业务类,注入到Spring,可以通过配置,或者注解实现-->
  12. <bean id="BookServiceImpl" class="com.zsy.service.BookServiceImpl">
  13. <property name="bookMapper" ref="bookMapper"/>
  14. </bean>
  15. <!--3.声明式事务配置-->
  16. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  17. <!--注入数据源,数据源来自dao层-->
  18. <property name="dataSource" ref="dataSource"/>
  19. </bean>
  20. </beans>

 

配置SpringMVC

在web支持下的WEB-INF文件夹下创建jsp文件夹,并创建springmvc的配置文件     spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. https://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/mvc
  9. https://www.springframework.org/schema/mvc/spring-mvc.xsd
  10. http://www.springframework.org/schema/context
  11. https://www.springframework.org/schema/context/spring-context.xsd">
  12. <!--1.注解驱动-->
  13. <mvc:annotation-driven/>
  14. <!--2.静态资源过滤-->
  15. <mvc:default-servlet-handler/>
  16. <!--3.扫描包:controller-->
  17. <context:component-scan base-package="com.zsy.controller"/>
  18. <!--4.视图解析器-->
  19. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  20. <property name="prefix" value="/WEB-INF/jsp/"/>
  21. <property name="suffix" value=".jsp"/>
  22. </bean>
  23. </beans>

对web支持进行修改,使对URL的处理交给DispatcherServlet处理,修改      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. <!--DispatchServlet-->
  7. <servlet>
  8. <servlet-name>springmvc</servlet-name>
  9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10. <init-param>
  11. <param-name>contextConfigLocation</param-name>
  12. <!--需要处理的是总的配置文件而单是controller层的配置文件,所以不是spring-mvc.xml-->
  13. <param-value>classpath:applicationContext.xml</param-value>
  14. </init-param>
  15. <load-on-startup>1</load-on-startup>
  16. </servlet>
  17. <servlet-mapping>
  18. <servlet-name>springmvc</servlet-name>
  19. <url-pattern>/</url-pattern>
  20. </servlet-mapping>
  21. <!--乱码过滤-->
  22. <filter>
  23. <filter-name>encoding</filter-name>
  24. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  25. <init-param>
  26. <param-name>encoding</param-name>
  27. <param-value>utf-8</param-value>
  28. </init-param>
  29. </filter>
  30. <filter-mapping>
  31. <filter-name>encoding</filter-name>
  32. <url-pattern>/*</url-pattern>
  33. </filter-mapping>
  34. <session-config>
  35. <session-timeout>15</session-timeout>
  36. </session-config>
  37. </web-app>

 

整合

整合dao,service,controller层的各个配置文件,创建配置文件  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. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. https://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <import resource="classpath:spring-dao.xml"/>
  7. <import resource="classpath:spring-service.xml"/>
  8. <import resource="classpath:spring-mvc.xml"/>
  9. </beans>

注:各个配置文件均在resources目录下

 

 

原文链接:https://blog.csdn.net/ye_fei_shuang/article/details/117770483



所属网站分类: 技术文章 > 博客

作者:天使之恋

链接:http://www.javaheidong.com/blog/article/222187/2c30ea3c58244499e9a3/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

14 0
收藏该文
已收藏

评论内容:(最多支持255个字符)