自学内容网 自学内容网

SSM— spring,springMVC,mybatis整合

前言

SSM整合主要是将mybatis交给spring管理;将spring容器交给tomcat启动;对于web的请求和访问交给springmvc处理

没啥好说的,主要是配置文件

依赖

    <dependencies>
        <!--测试依赖2个-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.23</version>
        </dependency>

        <!--工具依赖2个-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.36</version>
        </dependency>

        <!--web相关依赖  5个-->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.25</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.14.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.15.2</version>
        </dependency>

        <!--数据库依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.21</version>
        </dependency>

        <!--mybatis相关依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>
        <!--spring-ioc依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.25</version>
        </dependency>
        <!--分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.2.1</version>
        </dependency>
    </dependencies>

spring.xml-配置spring和mybatis

<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--我们是要通过spring配置文件,完成service层以及Mapper层对象的创建-->
    <!--service层的类会在其上添加@Service注解,直接扫包就能创建bean-->
    <context:component-scan base-package="com.wngz.ssm.service"/>
    <!--创建数据访问层对象之前,必须链接数据库,就要用到数据源,必须知道连接的数据库信息-->
    <!--要知道所连接的数据库信息,必须导入数据库配置文件db.properties-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--配置druid数据源-->
    <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--如何创建Mapper实例呢,需要用到SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.wngz.ssm.entity"/>

        <property name="dataSource" ref="dataSource"/>

        <property name="mapperLocations" value="classpath:mappers/*.xml"/>

        <property name="configuration">
            <bean class="org.apache.ibatis.session.Configuration">
<!--                <property name="logImpl" value="org.apache.ibatis.logging.stdout.StdOutImpl"/>-->
                <property name="mapUnderscoreToCamelCase" value="true"/>
            </bean>
        </property>

        <!--配置mybatis插件-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            <!--指定用哪个数据库-->
                            helperDialect=mysql
                            <!--是否合理化,当输入的页码大于最大页数时,返回最后一页-->
                            reasonable=true
                            <!--当页码为0时,查询全部-->
                            pageSizeZero=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
    <!--扫描mapper接口所在的包,使用上面的SqlSessionFactoryBean来创建Mappers接口实例-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.wngz.ssm.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <!--配置事务管理器-->
    <!--务必注意,这个DataSourceTransactionManager类的bean,它的id一定要设为transactionManager-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--mysql的事务归根结底还是要使用Connection。所以这里还需要配置数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--启动事务管理, transaction-->
    <tx:annotation-driven/>
</beans>

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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.wngz.ssm.controller"></context:component-scan>
    <!--    注册springmvc相关注解-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--    让静态资源文件交给默认servlet处理-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>

db.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/community?serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=123456

mappers/*.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wngz.ssm.mapper.UserInfoMapper">
    <!-- mybatis映射文件-->
    <select id="selectByName" resultType="com.wngz.ssm.entity.UserInfo">
        select id,
               username,
               password,
               type,
               remarks,
               create_time as createTime,
               status
        from userinfo
        where username = #{username}
          and status = 1
    </select>
</mapper>


原文地址:https://blog.csdn.net/wyh2943455568/article/details/143465934

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!