SSM整合(拿来即用已测试)

SSM整合(拿来即用已测试)

一.准备工作:

1. 分析,画流程图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hy5oGMeg-1591789344035)(C:\Users\18035\OneDrive - 中国加油!武汉加油!\Desktop\markdown\img\Snipaste_2020-06-10_10-43-49.png)]

2.创建数据库

create database `ssmbulid`;
USE  `ssmbulid`;
CREATE TABLE IF NOT EXISTS `books`
(
   `id` INT(20) NOT NULL AUTO_INCREMENT COMMENT '编号',
   `name` VARCHAR(20) NOT NULL DEFAULT '书名' COMMENT '书名',
   `counts` INT(11) NOT NULL DEFAULT 0 COMMENT '数量',
   `detail` VARCHAR(200) NOT NULL DEFAULT '空' COMMENT '细节',
   PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8mb4;
INSERT	INTO books (`name`,`counts`,`detail`) VALUES('java',15,'从入门到放弃'),('python',12,'从入门到放弃'),('C',6,'从入门到放弃');
SELECT * FROM books;

3.创建一个项目导入依赖

创建一个空maven项目,并且导入依赖

需要的依赖有
    <dependencies>
            <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
       <!--数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!--数据源连接池-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
          <!--jsp-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
          <!--mybatis-->
          <dependency>
              <groupId>org.mybatis</groupId>
              <artifactId>mybatis</artifactId>
              <version>3.5.2</version>
          </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <!--spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!--lombok偷懒用,可以省区toString,set,get等操作-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
     </dependencies>
    <!--静态资源导出问题-->  
    <build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <excludes>
                <exclude>**/*.properties</exclude>
                <exclude>**/*.xml</exclude>
            </excludes>
            <filtering>false</filtering>
        </resource>
        <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>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
    </build>

4.测试连接数据库

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZCP77AMM-1591789344037)(C:\Users\18035\OneDrive - 中国加油!武汉加油!\Desktop\markdown\img\数据库.png)]

5.建立目录结构

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-plvLO54c-1591789344040)(C:\Users\18035\OneDrive - 中国加油!武汉加油!\Desktop\markdown\img\目录结构.png)]

二.开始配置文件

配置文件有(applicationContext.xml,mybatis-config.xml,mapper.xml,db.properties。。。)

在这里由于不想给applicationContext.xml太多东西,所以将一些配置文件分开放置,最后引入到applicationContext.xml(这也是很多公司的趋势)

1.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
        http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>
2.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>
<!--本来在此包配置数据源,别名,以及mapper的包,此时数据源交给spring做其余两个在这里配置-->
       <!--日志文件-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
     <!--别名-->
    <typeAliases>
        <package name="com.ssm.pojo"></package>
    </typeAliases>
    <!--mapper.xml-->
    <mappers>
        <mapper class="com.ssm.dao.BookMapper"/>
    </mappers>
</configuration>
3.db.properties
db.username=root
db.password=root
db.driverClass=com.mysql.jdbc.Driver
db.jdbcUrl=jdbc:mysql://localhost:3306/ssmbulid?useUnicode=true&useSSL=true&characterEncoding=utf8
#如果用mysql8.0+还需要配置时区
#url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=true
4.mapper.xml(此文件根据个人情况配置(放在dao层))
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ssm.dao.BookMapper"><!--dao或mapper包层位置-->
    <!--这里记得id名字要和mapper名字一致-->
    <!--增-->
    <insert id="addbook" parameterType="Books">
        insert into ssmbulid.books (name, counts, detail) values (#{name},#{counts},#{detail});
    </insert>
    <!--删-->
    <delete id="deletBook" parameterType="id">
        delete from ssmbulid.books where id =#{id};
    </delete>
    <!--改-->
    <update id="updateBook" parameterType="Books">
        update ssmbulid.books set books.name =#{name},books.counts=#{counts},books.detail=#{detail} where id=#{id};
    </update>
    <!--查一本书-->
    <select id="selectOneBook" parameterType="int" resultType="Books">
        select * from where id=#{id};
    </select>
    <!--查所有书-->
    <select id="selectAllBook" resultType="Books">
        select  * from books;
    </select>
</mapper>
5.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:cotext="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 https://www.springframework.org/schema/context/spring-context.xsd">
    <!--关联数据库配置文件(这个是spring专门配置数据库配置文件的)-->
    <cotext:property-placeholder location="classpath:db.properties"/>
    <!--连接数据池:
            c3p0自动化连接,(自动化加载配置文件,并且设置到对象中)
            dbcp半自动化,不能自动连接
            druid
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${db.driverClass}"></property>
        <property name="jdbcUrl" value="${db.jdbcUrl}"></property>
        <property name="user" value="${db.name}"></property>
        <!--记住这里的user,不是name,每个数据源叫法不一样,不能乱起名字-->
        <property name="password" value="${db.password}"></property>
        <!--c3p0连接池私有属性-->
        <property name="maxPoolSize" value="30"></property>
        <property name="minPoolSize" value="10"></property>
        <!--连接关闭时不自动commit -->
        <property name="autoCommitOnClose" value="false"></property>

        <!--超时时间-->
        <property name="checkoutTimeout" value="10000"></property>

        <!--从数据库获取新连接失败后重复尝试的次数。-->
        <property name="acquireRetryAttempts" value="30"></property>
    </bean>
    
    <!--sqlessionFactory-->
    <bean id="sqlessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--绑定mybatis-config.xml配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
    
<!--配置dao接口扫描包,动态实现了dao接口注入到Spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!--注入sqlessionFactory-->
    <property name="SqlSessionFactoryBeanName" value="sqlessionFactory"></property>
    <!--扫描要扫描的包-->
    <property name="basePackage" value="com.ssm.dao"></property>
</bean>


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

    <!--导入dao包-->
   <import resource="classpath:spring-dao.xml"/>
    <!--配置扫描包-->
    <context:component-scan base-package="com.ssm.service"/>
    <!--将所有业务类,注入到spring,可以通过配置或者注解实现-->
    <bean id="BookServiceImpl" class="com.ssm.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>
    <!--声明式事务配置-->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
     <property name="dataSource" ref="dataSource"></property>
       <!--aop实现事务织入-->
        <!-- 配置事务通知 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!-- 以方法为单位,指定方法应用什么事务属性 isolation:隔离级别 propagation:传播行为 read-only:是否只读 -->
                <!--<tx:method name="save*" isolation="REPEATABLE_READ"-->
                           <!--propagation="REQUIRED" read-only="false" />-->
                <!--<tx:method name="persist*" isolation="REPEATABLE_READ"-->
                           <!--propagation="REQUIRED" read-only="false" />-->
                <!--<tx:method name="update*" isolation="REPEATABLE_READ"-->
                           <!--propagation="REQUIRED" read-only="false" />-->
                <!--<tx:method name="modify*" isolation="REPEATABLE_READ"-->
                           <!--propagation="REQUIRED" read-only="false" />-->
                <!--<tx:method name="delete*" isolation="REPEATABLE_READ"-->
                           <!--propagation="REQUIRED" read-only="false" />-->
                <!--<tx:method name="remove*" isolation="REPEATABLE_READ"-->
                           <!--propagation="REQUIRED" read-only="false" />-->
                <!--<tx:method name="get*" isolation="REPEATABLE_READ"-->
                           <!--propagation="REQUIRED" read-only="true" />-->
                <!--<tx:method name="find*" isolation="REPEATABLE_READ"-->
                           <!--propagation="REQUIRED" read-only="true" />-->
                <!--<tx:method name="transfer" isolation="REPEATABLE_READ"-->
                           <!--propagation="REQUIRED" read-only="false" />-->
            <!--</tx:attributes>-->
            <tx:method name="*" propagation="REQUIRED"  />
            </tx:attributes>
        </tx:advice>

        <!-- 配置织入 -->
        <aop:config>
            <!-- 配置切点表达式 -->
            <aop:pointcut expression="execution(* com.ssm.dao.*.*(..))" id="txPc" />
            <!-- 配置切面 : 通知+切点 advice-ref:通知的名称 pointcut-ref:切点的名称 -->
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
        </aop:config>
    </bean>
</beans>

配置到这里要注意将所有dao,和service的层联系起来,可以用,也可以让idea帮忙放到applicationContext.xml文件下:

在这里插入图片描述

接着要加入webmvc框架支持:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0L4kZCw6-1591789344044)(C:\Users\18035\OneDrive - 中国加油!武汉加油!\Desktop\markdown\img\加mvc.png)]

1.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">
    <!--配置springmvc支持-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <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过期时间-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
</web-app>

2.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
       http://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">

    <!--注解驱动-->
    <mvc:annotation-driven/>
    <!--静态资源过滤-->
    <mvc:default-servlet-handler/>
    <!--扫描包-->
    <context:component-scan base-package="com.ssm.controller"/>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
     </bean>
</beans>

将是所有dao,service,mvc.xml都放到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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:spring-dao.xml"></import>
<import resource="classpath:spring-service.xml"></import>
<import resource="classpath:spring-mvc.xml"></import>
</beans>

此时的配置文件问题解决了,还有些小问题

配置完之后的问题

1. 配置完成后发现service不存在,于是出现如下排错推导:(解决方法是在web.xml文件中,将原来的spring-mvc.xml,变为applicationContext.xml)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DmsD4m1p-1591789344045)(C:\Users\18035\OneDrive - 中国加油!武汉加油!\Desktop\markdown\img\报错.png)]

2.记着导lib包

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zTizAeI8-1591789344047)(C:\Users\18035\OneDrive - 中国加油!武汉加油!\Desktop\markdown\img\导入lib包.png)]

3.记着数据库与pojo下名字保持一致

总结

来来回回配置三个多小时,不过下次直接从这里取,肯定会快很多,还是觉得这配置够繁琐的,接下来就是做ssm项目,然后直接springBoot了,即使再麻烦,路还是要一步一步走,加油!!!!!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值