码迷,mamicode.com
首页 > 其他好文 > 详细

ssm简单整合

时间:2020-07-27 09:41:28      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:root   arch   tab   mybaits   stl   htm   rac   配置文件的路径   doctype   

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.zut</groupId>
<artifactId>ssmone</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>ssmone Maven Webapp</name>
<!-- FIXME change it to the project‘s website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>compile</scope>
</dependency>
<!--读取Excel-->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>


<!--HttpServletRequest-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--dbcp-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>ojdbc</groupId>
<artifactId>ojdbc</artifactId>
<version>14</version>
</dependency>

<!-- 添加spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>

<!-- 添加spring-test的依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- cglib的依赖 -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
<!-- aspectjweaver的依赖 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>

<!-- dbcp依赖包 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>

<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.6</version>
</dependency>
<!-- mysql连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<!-- 整合包 spring5 要求 mybatis-spring 1.3以上版本 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

</dependencies>

<build>
<finalName>ssmone</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>

<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>

</build>
</project>


web.xml:

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--配置spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--设置配置文件的路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name/>
<param-value/>
</context-param>
<!-- 前端控制器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化springmvc核心配置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<!-- / 拦截所有 除了jsp -->
<url-pattern>/</url-pattern>
<url-pattern>*.jpg</url-pattern>
<url-pattern>*.js</url-pattern>
<url-pattern>*.css</url-pattern>
<url-pattern>*.png</url-pattern>
<url-pattern>*.gif</url-pattern>
<url-pattern>*.json</url-pattern>
<url-pattern>*.html</url-pattern>
<url-pattern>*.htm</url-pattern>
<url-pattern>*.swf</url-pattern>
</servlet-mapping>
<!-- 解决中文乱码问题 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<!-- 拦截所有,包括jsp -->
<url-pattern>/*</url-pattern>
</filter-mapping>


</web-app>

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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<!-- 启动扫描注解 -->
<context:component-scan base-package="com.zut.*">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!-- 引入配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>

<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybaits-config.xml"></property>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath*:com/zut/dao/mapper/*.xml"></property>
<property name="typeAliasesPackage" value="com.zut.domain"></property>

</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zut.dao"/>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- <tx:annotation-driven transaction-manager="transactionManager"/>-->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >

<property name="basePackage" value="com.zut.dao.mapper"></property>
</bean>

</beans>

db.properties:

###对jdbc中的相关属性进行封装 key=value
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123
log4j.properties:

# 全局日志配置     如果进入开发阶段,改为ERROR或者INFO
log4j.rootLogger=DEBUG, stdout
# 控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

mybaits-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>
<!--配置全局属性-->
<settings>
<setting name="logImpl" value="LOG4J" />
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>

springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!--开启注解扫描,只扫描Controller注解-->
<context:component-scan base-package="com.zut.controller" />

<!--视图解析器-->
<bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--开启注解SpringMVC框架注解的支持-->
<mvc:annotation-driven/>
<!--拦截器-->

<mvc:resources mapping="/css/**" location="/WEB-INF/static/css/"></mvc:resources>
<mvc:resources mapping="/js/**" location="/WEB-INF/static/js/"></mvc:resources>
<mvc:resources mapping="/img/**" location="/WEB-INF/static/img/"></mvc:resources>
<mvc:resources mapping="/mv/**" location="/WEB-INF/static/mv/"/>


</beans>



 

 技术图片

 

 

 

 

一、SpringMVC

File->New->maven->webapp

解决maven加载过慢问题:

archetypeCatalog
internal

 

在main下面创建文件夹:

Java :右键->make directory as->Test Sources Boot

 

Resources : 右键->make directory as->Test resources Boot

 

 

刷新项目:

右键->maven->Reimport

 

服务器部署:

技术图片

 

 



流程总结:

 技术图片

 

 

 

 

1、注解的使用:

 

@Controller  控制层

@Controller用于标记在一个类是控制类,使用它标记的类就是一个SpringMvc Controller对象,不需要再实现Controller接口

 

@Autowired

@RequestMapping

标记在方法上,使该方法成为一个请求处理方法,会在程序接受到对应请求的URL请求时被调用

Idea创建maven

 技术图片

 

 

缺少webapp目录的解决方法:

https://www.cnblogs.com/oldzhang1222/p/10429827.html

部署tomcat

https://blog.csdn.net/daxia_2016/article/details/81265282

Information:Internal caches are corrupted or have outdated format, forcing project rebuild等拒绝访问异常之处理办法:

如果是MAVEN项目的话看能不能在IDEA的Terminal里面,在最底下的菜单栏中点击打开,输入指令 MVN CLEAN(前提是配置了环境变量)。

如果不能删除的话,那么就需要手动删除了,这个时候找到你的trunk文件找到target目录手动删除即可。

然后如图所示,删除掉IDEA的缓存,重启IDEA后重新编译项目便可解决。

 

Idea创建SSM web项目:

https://www.cnblogs.com/xiaofeng88/p/10353152.html

 

 

部署jar包:

https://www.cnblogs.com/kongxiaoshuang/p/10689564.html

 

2、拦截器和过滤器的区别:

    过滤器是servlet规范中的一部分,任何java web工程都可以使用

    拦截器是springMVC框架自己的,只有使用springMVC框架的工程才使用

    过滤器在url-pattern中配置了/*之后,可以对所有要访问的资源拦截

    拦截器它是只会拦截访问的控制器方法,如果访问的是jsp,html,css,image或者js是不会进行拦截的

   

 

 

二、SSM整合

ssm各package的含义

https://www.cnblogs.com/yanxi1900/p/8533562.html

持久层:DAO层(mapper)

主要是做数据持久层的工作,负责与数据库进行联络

         Dao层的设计首先设计DAO的接口

         然后在spring的配置文件中定义此接口的实现类

业务层:Service层

         主要负责业务模块的逻辑应用设计

         首先设计接口,再设计实现的类

表现层:Controller层

         负责具体的业务模块流程的控制

         调用service层的接口来控制业务流程

View层:

         与控制层结合比较紧密,主要实现jsp页面

 

 

 

 

 

1、整合思路: view                    service                 dao

 技术图片

 

 

三层分别测试,再整合

 

2、搭建环境:

 

创建数据库:

create database ssm;

use ssm;

create table account(

id int primary key auto_increment,

name varchar(20),

money double

);

 

创建maven

 技术图片

 

 技术图片

 

 技术图片

 

 

 

 

 

添加标签(+):

archetypeCatalog
internal

测试spring框架:

配置applicationContext.xml

配置web.xml

配置log4j.properties

测试springmvc框架

配置springmvc.xml

部署tomcat

 技术图片

 

 技术图片

 

 技术图片

 

 技术图片

 

 

 

 

 

 

 

 

 

Spring整合springmvc

整合思路:

 技术图片

 

 

 

 

Spring整合mybatis:

配置mybatis-config.xml

右键->new->file->mybatis-config.xml

 

 

有关页面跳转的若干问题解决:

 

1、 WEB-INF外跳入外:

只需要<a href=”xx.jsp”>

2、 WEB-INF外跳入内:

需要加上类名上的RequestMapping(“/xx”)

和方法上的RequestMapping(“/yy”)

3、 WEB-INF内跳入外:

需要重定向:return “redirect:/xx”

4、 WEB-INF内跳入内:

直接加上方法上的RequestMapping(“/aa“)

    即可,不必加类上的

 

 注:以上为个人总结,如有错误和不足,请多指教



ssm简单整合

标签:root   arch   tab   mybaits   stl   htm   rac   配置文件的路径   doctype   

原文地址:https://www.cnblogs.com/dre-52yao/p/13382942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!