项目一spring+springMVC+mybatis框架的配置与使用

时间:2022-08-13 05:11:21

1.使用maven导入配置SSM框架所需的jar包,在项目的pom.xml文件中加入

 <!--添加spring的依赖-->
        <dependency>
            <artifactId>spring-beans</artifactId>
            <groupId>org.springframework</groupId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <artifactId>spring-context</artifactId>
            <groupId>org.springframework</groupId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <artifactId>spring-tx</artifactId>
            <groupId>org.springframework</groupId>
            <version>${spring.version}</version>
        </dependency>
 <!-- 添加spring/springMVC的依赖   开始-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- 添加spring/springMVC的依赖   结束-->
<!-- 添加mybatis-spring的整合依赖-->
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis-spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.6</version>
        </dependency>

2.配置spring所需的applicationContext.xml,springmvc.xnl,在这个项目里不需要对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: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-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <context:component-scan base-package="com.data">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Component" />
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Repository" />
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Service" />
    </context:component-scan>

    <!-- 加载配数据源配置文件 db.properties -->
    <!--<context:property-placeholder location="db.properties"/>-->

    <!-- 配置 C3P0 数据源 -->
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/dataUp?useSSL=false&amp;useAffectedRows=true&amp;serverTimezone=UTC "/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="2"/>
    </bean>

    <!-- 事务管理器 (JDBC) -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 6.开启事务注解-->
    <tx:annotation-driven/>

    <!-- spring 通过 sqlSessionFactoryBean 获取 sqlSessionFactory 工厂类 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 扫描 po 包,使用别名 -->
        <property name="typeAliasesPackage" value="com.data.model"/>
        <!-- 扫描映射文件 -->
        <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/>
    </bean>
    <!-- 自动生成dao,mapper-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="classpath:mybatis/mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
    </bean>

    <!-- 配置扫描 dao 包,动态实现 dao 接口,注入到 spring 容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.data.dao" />
        <!-- 注意使用 sqlSessionFactoryBeanName 避免出现spring 扫描组件失效问题 -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
</beans>
<?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"
       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-4.3.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 只需要扫描包中的 Controller 注解 -->
    <context:component-scan base-package="com.data.controller">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!-- 启动 mvc 注解驱动 -->
    <mvc:annotation-driven/>

    <!-- 2.配置映射处理和适配器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

    <!--支持文件上传-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

    <!-- 配置视图解析器 ,对所有的访问和视图返回添加下面的前缀和后缀-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 静态资源处理 -->
    <mvc:default-servlet-handler/>

    <mvc:resources mapping="/js/**" location="/WEB-INF/js/"/>
    <mvc:resources mapping="/css/**" location="/WEB-INF/css/"/>
    <mvc:resources mapping="/images/**" location="/WEB-INF/images/"/>
    <mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts"/>
    <mvc:resources mapping="/Scripts/**" location="/WEB-INF/Scripts"/>
</beans>

项目中使用到的有关SSM的注解

1.@Controller ,添加在类的声明外面,表示这个类隶属于控制层

@Controller和@RestController的区别?

知识点:@RestController注解相当于@ResponseBody + @Controller合在一起的作用。

1) 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。

2) 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
    如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。

2.@Service,service一般会分成接口类和实现类,在两个类中都要加上,并且在实现类里要加上service的名字,例如

 

@Service("adminService")
public class adminServiceImpl implements adminService 

 

3.@Repository,表示dao层

4.@Autowired Spring容器中所有类型匹配的bean都被注入进来,并且如果bean有@Order注解或者实现Order接口,按照Order的先后顺序注入;

 

5.@RequestMapping

 

RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

 

RequestMapping注解有六个属性,下面我们把她分成三类进行说明。

1)value, method;

 

value:     指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);

 

method:  指定请求的method类型, GET、POST、PUT、DELETE等;

2)、 consumes,produces;

 

consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;

 

produces:    指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;

3)、 params,headers;

 

params: 指定request中必须包含某些参数值是,才让该方法处理。

 

headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。

6.@ResponseBody

该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。

使用时机:

      返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;

7.@RequestParam

GET和POST请求传的参数会自动转换赋值到@RequestParam 所注解的变量上

示例:

@RequestMapping("dataMange")
@Controller
public class dataMangeController {
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestParam(value = "edit_data_id",required = false) String dataID,
                         @RequestParam(value = "edit_dataName",required = false) String dataName,
                         @RequestParam(value = "edit_people",required = false) String people,
                         @RequestParam(value = "edit_telphone",required = false) String telphone,
                         @RequestParam(value = "edit_des",required = false) String des){
        dataModel datamodel = new dataModel(dataID,dataName,people,telphone,des,null,null,null);
        dataservice.updatedataByModel(datamodel);
        return "ok";
    }
}