SSM项目整合

1.准备所需要的jar包

链接:https://pan.baidu.com/s/1y8KdlaZNM_KZeaniypQ10g
提取码:cu9s

2.新建动态web工程,目录结构为

3.配置web.xml,将Spring容器配置在web.xml中

    <servlet>
        <servlet-name>spring4</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>spring4</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
View Code

4.注意几个需要加注解的地方

   一、Service层中,类上加上注解@Service;属性上 private TeacherrDao teacherDao 加上注解@Autowired或者@Resource,作用是自动装配。

  二、Controller层中和service层类似,类上加上注解@Controller;属性上private TeacherService teacherService; 加上注解@Autowired或者@Resource。

5.mapper文件注意namespace,指向的是Dao的地址

<?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.dao.TeacherDao">
    
    <select id="getTeacherById" parameterType="int" resultType="com.entity.Teacher">
        select * from teacher where tid = #{tid}
    </select>
    
    <select id="getallteachers" resultType="com.entity.Teacher">
        select * from teacher
    
</mapper>
View Code

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

    <context:component-scan base-package="com.*"/>
    <mvc:annotation-driven/>
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="Url" value="jdbc:mysql://localhost:3306/mybatis"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
    
        <!-- 扫描mybatis的配置结合上面的DataSource,会生成一个sqlFactory-->     
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation">
            <value>classpath:myBatisConfig.xml</value>
        </property>
    </bean>
    
    
    
    <!-- 配置 转换器,对于在basePackage设置的包(包括子包)下的接口类,  
    如果接口类的全类名在Mapper.xml文件中和定义过命名空间一致, 将被转换成spring的BEAN,  
    在调用的地方通过@Autowired方式将可以注入接口实例 -->    
    <!-- 通过上面的sqlFactory再扫描接口包,生成一个接口的实现类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dao"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    
    
    
    
        <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/teacher/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    



   
    

</beans>
View Code

7.mybatisConfig.xml文件,作用是将TeacherMapper.xml文件引入到mybatis配置中

<?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>
    
    <mappers>
        <mapper resource="com/entity/TeacherMapper.xml" />
    </mappers>
</configuration>
View Code

8.使用responsebody直接时,需要引入Jackson的jar包,导入不全会报406错误!网盘中可以下载。

 

posted @ 2019-02-27 15:36  Dreamvv  阅读(76)  评论(0编辑  收藏  举报