SSM(Spring+SpringMVC+Mybatis)整合maven

一.开发环境准备

idea+maven+Tomcat+jdk1.8

二.idea新建Maven项目

File --> New --> Project -->Maven,并且选择一个web项目
在这里插入图片描述

三.创建项目结构

java代码包括:具体包含控制层,业务层(ipml具体实现),数据访问层,以及对应的实体类;
resource配置包括:
(1).mapper文件下下的*mapper.xml:主要对应dao层的mapper实现,采用命名空间+方法名定位到每一条对应的sql语句;
(2).applicationContext.xml:Spring上下文配置
(3).db.properties:数据库连接的驱动,对应地址,用户名,密码等配置
(4).mybatis-config.xml:配置mybatis
(5).spring-mvc.xml:配置mvc
在这里插入图片描述

四.具体配置实现以及对应的maven依赖

1.mapper下*mapper.xml配置:

在这里插入图片描述
注意事项:
(1).每个mapper对应的命名空间需要和dao层下的mapper位置对应,否则会找不到mapper报错。
(2).传入的参数类型,以及数据库查询返回的类型应该和dao层的一致

2.Spring上下文applicationContext.xml

applicationContext.xml主要用来管理一些静态资源的配置如db.properties,配置数据源,对事务的管理以及对一些框架的整合等等。具体如下:
(1).静态资源的加载:<context:property-placeholder location=“xxx”/>
(2).数据源的配置:见下列代码
(3).事务管理:<tx:annotation-driven transaction-manager=“transactionManager” />
(4).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:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation=
               "
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            "
>
    <!--加载数据库配置-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置c3p0连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="minPoolSize" value="1"></property>
        <property name="maxPoolSize" value="5"></property>
        <property name="initialPoolSize" value="1"></property>
        <property name="acquireIncrement" value="1"></property>
    </bean>

    <!--配置事务管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 注解方式配置事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 配置 Mybatis 的 SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定数据库环境 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 指定Mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!-- 指定Mybatis映射文件,*.xml 会匹配所有xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>

    <!-- 扫描生成所有dao层 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.linbin.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
</beans>

所需的对应依赖如下:
(1).c3p0连接池以及mysql:

<dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.17</version>
    </dependency>

(2).spring事务管理

<!--配置spring事务管理-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.3.14.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.14.RELEASE</version>
    </dependency>

(3).mybatis依赖

<!--配置mybatis依赖-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.1</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>

3.db.properties配置

该配置文件主要配置了一些数据库信息,包括驱动,数据库地址,user,password等,比较简单。

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
jdbc.user=root
jdbc.password=123456

4.mybatis-config.xml

该配置文件主要配置mybatis的信息,可以配置事务日志,表的别名,或者mapper对应的映射(当然在applicationContext.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="cacheEnabled" value="true"/>
        <setting name="defaultStatementTimeout" value="3000"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!-- 代理 -->
        <setting name="proxyFactory" value="CGLIB"/>
        <setting name="lazyLoadingEnabled" value="true"/>
    </settings>
</configuration>

5.spring-mvc.xml

主要配置了mvc三层结构,包括开启注解扫描,配置对应的视图解析器等等。

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

    <!-- 开启注解 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 让扫描spring扫描这个包下所有的类,让标注spring注解的类生效 -->
    <context:component-scan base-package="com.linbin.*"></context:component-scan>

    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/" />
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

此处有个需要注意的地方:因为SpringMVC无法自主返回json对象,所以应该加上对应依赖来转换,否则会报错
在这里插入图片描述
对应的依赖如下:
(1).mvc依赖:

<!--添加spring-mvc依赖--><!--webmvc中包含了spring的包,因此spring不用再导入新的依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.1.9.RELEASE</version>
    </dependency>

(2).mvc返回json转化依赖:

<!--配置SpringMvc返回json-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.5</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.5</version>
    </dependency>

6.web.xml

web.xml主要需要配置前端控制器以及对应的监听器,前端控制前主要决定了url请求的去向,根据对应的RequestMapping调用不同的Controller来处理业务,spring监听器把spring交由web项目来管理。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    <!--配置前端控制器-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置初始化参数-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!--配置启动时机-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <!--配置spring监听器-->
    <listener>
        <description>启动spring</description>
        <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>
</web-app>
        

五.测试

将项目用Tomcat跑起来之后,输入对应的url,如果能够返回对应的数据库结果证明成功;
在这里插入图片描述
至此ssm整合maven完成。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值