发布于2021-06-12 13:50 阅读(864) 评论(0) 点赞(14) 收藏(2)
SSM包括:Mybatis,Spring,SpringMVC
目录
导入各种依赖
- <!--依赖:
- junit,数据库驱动,链接池,servlet,jsp,mybatis,mybatis-spring,spring
- -->
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.13.2</version>
- </dependency>
-
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.24</version>
- </dependency>
-
-
- <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
- <!-- 数据库链接池 c3p0 dbcp-->
- <dependency>
- <groupId>com.mchange</groupId>
- <artifactId>c3p0</artifactId>
- <version>0.9.5.5</version>
- </dependency>
-
- <!--servlet-->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.5</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.2</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- </dependency>
-
- <!--mybatis-->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.5.7</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>2.0.6</version>
- </dependency>
-
- <!--spring-->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>5.3.6</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>5.3.6</version>
- </dependency>
-
- <!--测试-->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.13.2</version>
- </dependency>
- </dependencies>
配置资源导出问题,防止项目只识别resources目录下的配置文件
- <!--静态资源导出问题-->
- <build>
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <includes>
- <include>**/*.properties</include>
- <include>**/*.xml</include>
- </includes>
- <filtering>false</filtering>
- </resource>
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.properties</include>
- <include>**/*.xml</include>
- </includes>
- <filtering>false</filtering>
- </resource>
- </resources>
- </build>
添加WEB支持
在WEB支持中导入lib依赖(创建lib文件夹,并导入依赖)
为各个层(数据层,服务层,控制层)创建文件夹
在IDEA中添加数据库
创建数据库链接的配置文件 ,创建配置文件 database.properties
- jdbc.driver=com.mysql.cj.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?serverTimezone=UTC
- jdbc.username=root
- jdbc.password=root
配置数据库链接池:主要作用于dao层(数据层),创建配置文件 spring-dao.xml
- <?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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
-
- <!--1.管理数据库配置文件-->
- <context:property-placeholder location="classpath:database.properties"/>
-
- <!--2.链接池
- dbcp:半自动化操作,不能自动链接
- c3p0:自动化操作(自动化加载配置文件,并且可以自动设置到对象中!)
- -->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="${jdbc.driver}"/>
- <property name="jdbcUrl" value="${jdbc.url}"/>
- <property name="user" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.password}"/>
- </bean>
-
- <!--3.sqlSessionFactory-->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <!--绑定Mybatis的配置文件-->
- <property name="configLocation" value="classpath:mybatis-config.xml"/>
- </bean>
-
-
- <!--配置dao接口扫描包,动态的实现了Dao接口可以注入到Spring容器中-->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <!--注入sqlSessionFactory-->
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
- <!--要扫描的到包-->
- <property name="basePackage" value="com.zsy.dao"/>
- </bean>
-
- </beans>
配置Mybatis操作数据库配置,创建配置文件 mybatis-config.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <!--configuration核心配置文件-->
- <configuration>
-
- <!--配置数据源,交给Spring去做-->
-
- <!--取别名-->
- <typeAliases>
- <package name="com.zsy.pojo"/>
- </typeAliases>
-
- <mappers>
- <mapper class="com.zsy.dao.BookMapper"/>
- </mappers>
- </configuration>
service层主要基于代理模式,创建配置文件 spring-service.xml
- <?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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- https://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- https://www.springframework.org/schema/context/spring-context.xsd">
-
-
- <!--1.扫描service下的包-->
- <context:component-scan base-package="com.zsy.service"/>
-
- <!--2.将我们的所有业务类,注入到Spring,可以通过配置,或者注解实现-->
- <bean id="BookServiceImpl" class="com.zsy.service.BookServiceImpl">
- <property name="bookMapper" ref="bookMapper"/>
- </bean>
-
- <!--3.声明式事务配置-->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <!--注入数据源,数据源来自dao层-->
- <property name="dataSource" ref="dataSource"/>
- </bean>
-
- </beans>
在web支持下的WEB-INF文件夹下创建jsp文件夹,并创建springmvc的配置文件 spring-mvc.xml
- <?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:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- https://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/mvc
- https://www.springframework.org/schema/mvc/spring-mvc.xsd
- http://www.springframework.org/schema/context
- https://www.springframework.org/schema/context/spring-context.xsd">
-
- <!--1.注解驱动-->
- <mvc:annotation-driven/>
- <!--2.静态资源过滤-->
- <mvc:default-servlet-handler/>
- <!--3.扫描包:controller-->
- <context:component-scan base-package="com.zsy.controller"/>
- <!--4.视图解析器-->
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
-
- </beans>
对web支持进行修改,使对URL的处理交给DispatcherServlet处理,修改 web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0">
-
- <!--DispatchServlet-->
- <servlet>
- <servlet-name>springmvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <!--需要处理的是总的配置文件而单是controller层的配置文件,所以不是spring-mvc.xml-->
- <param-value>classpath:applicationContext.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
-
- <!--乱码过滤-->
- <filter>
- <filter-name>encoding</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>encoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <session-config>
- <session-timeout>15</session-timeout>
- </session-config>
-
- </web-app>
整合dao,service,controller层的各个配置文件,创建配置文件 applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- https://www.springframework.org/schema/beans/spring-beans.xsd">
-
- <import resource="classpath:spring-dao.xml"/>
- <import resource="classpath:spring-service.xml"/>
- <import resource="classpath:spring-mvc.xml"/>
-
- </beans>
注:各个配置文件均在resources目录下
原文链接:https://blog.csdn.net/ye_fei_shuang/article/details/117770483
作者:天使之恋
链接:http://www.javaheidong.com/blog/article/222187/2c30ea3c58244499e9a3/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!