前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【云+社区年度征文】最详细的 SSM整合(纯干货)包含(常用依赖),(如何整合)等

【云+社区年度征文】最详细的 SSM整合(纯干货)包含(常用依赖),(如何整合)等

原创
作者头像
CaesarChang张旭
修改2020-12-07 10:02:29
3140
修改2020-12-07 10:02:29
举报
文章被收录于专栏:悟道

By CaesarChang 张旭  合作: root121toor@gmail.com


1简介

分为Spring SpringMVC Mybatis三部分  ,

  1. [ Spring:替换创建对象的操作,servlet->service ] 
  2.   [ mybatis:持久化保存数据,控制bean->数据库 ]   
  3. [ springMVC :主要是控制请求,交给DispatcherServlet  然后在渲染视图]

2思路

1首先是创建环境,创建完了  ,编写对应的bean dao service controller  ,当然了推荐使用Mybatis反向生成工具(贼高效 方便简单) 不会的看之前的博客  https://blog.csdn.net/weixin_45434902/article/details/109494420   末尾部分

生成完毕样式为:

2开始你的业务,思路(我这里使用mapscanner可以省略dao的实现类

 :   2_1 先在service接口里面生命方法

               2_2然后在impl里面实现它 (调用dao,dao调用mapper映射)

           

3在页面里面发送请求,然后在controller里面接收,然后controller调用service方法,service调用dao,dao调用mapper,然后逐层返回给controller,controller再返回给页面

注意:增删改查上面一定要  事务注解 @Transactional



3 我常用的依赖:

1 pom的

代码语言:html
复制

<?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.zx</groupId>
    <artifactId>ssmDemo1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
 
    <name>ssmDemo1 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.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <springversion>5.0.8.RELEASE</springversion>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!-- 加入ServletAPI -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency> <!-- MySQL依赖 start -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency> <!-- 加入MyBatis 依赖 start -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency> <!-- 引入Spring(包含SpringMVC) 依赖 start -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springversion}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springversion}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${springversion}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${springversion}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${springversion}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springversion}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${springversion}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${springversion}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${springversion}</version>
        </dependency> <!-- 引用插件依赖:MyBatis整合Spring,如果mybatis版本在3.4及以上版本 mybatis-spring的版本要在1.3以上 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency> <!-- JSTL -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- 德鲁伊数据连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency> <!-- pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.6</version>
        </dependency> <!--处理json-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.5</version>
        </dependency> <!--javaee-->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency> <!--文件上传下载-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.1</version>
        </dependency>
        <dependency>
 
            <groupId>org.mybatis.generator</groupId>
 
            <artifactId>mybatis-generator-core</artifactId>
 
            <version>1.3.5</version>
 
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>
 
    <build>
        <finalName>ssmDemo1</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
 
                    <groupId>org.mybatis.generator</groupId>
 
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
 
                    <version>1.3.5</version>
 
                    <configuration>
 
                        <!--配置文件的路径-->
 
                        <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
 
                        <overwrite>true</overwrite>
 
                    </configuration>
 
                    <dependencies>
 
                        <dependency>
 
                            <groupId>org.mybatis.generator</groupId>
 
                            <artifactId>mybatis-generator-core</artifactId>
 
                            <version>1.3.5</version>
 
                        </dependency>
 
                    </dependencies>
 
                </plugin>
                <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>
    </build>
</project>

2 Mybatis的

代码语言:html
复制
<?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>
    <typeAliases>
        <package name="com.zx.bean"></package>
    </typeAliases>
 
    <plugins>
        <!-- PageHelper4.1.6 -->
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
 
</configuration>

3springde (需要在resource下创建mapper文件夹 为了省略dao的实现类,注解用mapperscanner扫描指定mapper包下的所有mapper,而不扫描别的mapper)

代码语言:html
复制
<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
>
<!--  八件事-->
<!--        1配置数据源-->
    <bean id="db" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8"></property>
        <property name="username" value="root"></property>
        <property name="password"  value="root"></property>
    </bean>
<!--    2创建sqlsessionfactory工厂-->
<bean id="fac" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="db"></property>
    <property name="configLocation" value="classpath:mybatis.xml"></property>
    <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!--         3创建MapperScannerConfigurer来 省略dao的实现类impl-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="fac"></property>
        <property name="basePackage" value="com.zx.dao"></property>
 
    </bean>
<!--       4配置事务-->
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="db"></property>
</bean>
<!--        5启用MVC注解-->
    <mvc:annotation-driven></mvc:annotation-driven>
<!--               6配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"></property>
        <property name="prefix" value="/"></property>
    </bean>
<!--                    7扫描注解包  @controller @service可以使用-->
    <context:component-scan base-package="com.zx"></context:component-scan>
<!--                            8配置静态资源访问-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
 
 
 
</beans>

4web.xml的(默认好像为为2.3, 一定改为2.5)

代码语言:html
复制
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
 
 
  <display-name>Archetype Created Web Application</display-name>
 
<!--配置前端控制器:-->
<servlet>
  <servlet-name>aa</servlet-name>
  <servlet-class>
    org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </init-param>
</servlet>
<servlet-mapping>
  <servlet-name>aa</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
<!--  处理post乱码-->
<filter>
  <filter-name>bb</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>bb</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

5 log4j的

代码语言:html
复制
<?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>
    <typeAliases>
        <package name="com.zx.bean"></package>
    </typeAliases>
 
    <plugins>
        <!-- PageHelper4.1.6 -->
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
 
</configuration>

6mybatis-generator的(需要在resource下创建mapper文件夹 为了省略dao的实现类,注解用mapperscanner扫描指定mapper包下的所有mapper,而不扫描别的mapper)

代码语言:html
复制
<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE generatorConfiguration
 
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 
<!-- 配置生成器 -->
 
<generatorConfiguration>
 
    <!--数据库驱动jar -->
 
    <classPathEntry
 
            location="/Volumes/mydata/Maven/apache-maven-3.6.3/my_repository/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar" />
 
    <context id="MyBatis" targetRuntime="MyBatis3"><!--去除注释 -->
 
        <commentGenerator>
 
            <property name="suppressAllComments" value="true" />
 
        </commentGenerator>
 
        <!--数据库连接 -->
 
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
 
                        connectionURL="jdbc:mysql://localhost:3306/test"
 
                        userId="root"
 
                        password="root">
 
        </jdbcConnection>
 
        <!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建
        使用Maven生成在target目录下,会自动创建) -->
 
        <javaModelGenerator targetPackage="com.zx.bean"
 
                            targetProject="/Volumes/mydata/IDEA/SSM/src/main/java">
 
            <property name="trimStrings" value="true" />
 
        </javaModelGenerator>
 
        <!--生成SQLmapper文件 -->
 
        <sqlMapGenerator targetPackage="mapper"
 
                         targetProject="/Volumes/mydata/IDEA/SSM/src/main/resources">
 
        </sqlMapGenerator>
 
        <!--生成Dao文件,生成接口 -->
 
        <javaClientGenerator type="XMLMAPPER"
 
                             targetPackage="com.zx.dao"
 
                             targetProject="/Volumes/mydata/IDEA/SSM/src/main/java">
 
        </javaClientGenerator>
 
        <table tableName="bills" enableCountByExample="false"
 
               enableUpdateByExample="false" enableDeleteByExample="false"
 
               enableSelectByExample="false" selectByExampleQueryId="false">
 
        </table>
 
        <table tableName="billtype" enableCountByExample="false"
 
               enableUpdateByExample="false" enableDeleteByExample="false"
 
               enableSelectByExample="false" selectByExampleQueryId="false">
 
        </table>
 
        <table tableName="subject" enableCountByExample="false"
 
               enableUpdateByExample="false" enableDeleteByExample="false"
 
               enableSelectByExample="false" selectByExampleQueryId="false">
 
        </table>
 
    </context>
 
</generatorConfiguration>


4下面是一个我写的SSM整合项目(简单练手用可以研究下 码云开源)

地址:

码云开源地址https://gitee.com/SSM_FinancialSystem/ssm_-financial-system 点我进入

记得点个star呗么么哒


  • 拜了个拜

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1简介
  • 2思路
  • 3 我常用的依赖:
    • 1 pom的
      • 2 Mybatis的
        • 3springde (需要在resource下创建mapper文件夹 为了省略dao的实现类,注解用mapperscanner扫描指定mapper包下的所有mapper,而不扫描别的mapper)
        • 4下面是一个我写的SSM整合项目(简单练手用可以研究下 码云开源)
        相关产品与服务
        云数据库 MySQL
        腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档