ssm整合

SSM整合

1. web工程搭建初始化

1.1创建webapp工程

]ssm整合_第1张图片

1.2 编写工程信息

ssm整合_第2张图片

1.3POM文件

 <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>
  properties>

  <dependencies>
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.5.3version>
    dependency>
    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.47version>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>5.1.9.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>2.0.3version>
    dependency>
    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>druidartifactId>
      <version>1.1.16version>
    dependency>
    
    <dependency>
      <groupId>com.github.pagehelpergroupId>
      <artifactId>pagehelperartifactId>
      <version>5.1.2version>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>5.1.9.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>2.9.0version>
    dependency>
    
    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
      <scope>providedscope>
    dependency>

    
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.12version>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>5.1.9.RELEASEversion>
    dependency>
  dependencies>

  <build>
    
    <plugins>
      
      <plugin>
        <groupId>org.apache.tomcat.mavengroupId>
        <artifactId>tomcat7-maven-pluginartifactId>
        <version>2.1version>
        <configuration>
          <port>80port>
          <path>/path>
        configuration>
      plugin>
    plugins>
  build>
project>

2. 工程基础代码编写

2.1基本工程结构

ssm整合_第3张图片

2.2 基本pojo

1)Student对象
public class Student {
    private Integer id;
    private String name;
    private Integer age;
}
2) 通用返回对象
public class GeneralResult<T> {
    private T data;
    private Boolean flag;
    private String msg;
    private Integer code;
}

3. 初始化SpringMVC + Spring

3.1 web.xml编写

  • 过滤器
  • 中央控制器

<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_3_1.xsd"
         version="3.1">

    <filter>
        <filter-name>characterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>characterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    
    
    <servlet>
        <servlet-name>DispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath*:spring-mvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>

web-app>

3.2 resources下添加spring-mvc.xml配置文件

  • 包扫描
  • 静态资源放行
  • 注解驱动

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

    <mvc:annotation-driven/>

    <context:component-scan base-package="com.itheima"/>

    <mvc:default-servlet-handler/>

beans>

3.3 HelloController编写

  • 在Controller包下添加HelloController,目的测试springMVC是否配置成功
@Controller
public class HelloController {
    
    
    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "hello world";
    }
}

3.4 此处启动Tomcat访问是否能够成功获得响应

3.5目前工程目录结构

ssm整合_第4张图片

4. 整合mybatis

实现查询所有学生功能

4.1 完整工程结构

ssm整合_第5张图片

4.2 编写StudentDao接口

public interface StudentDao {
	List findAll();
}

4.3 StudentDao.xml映射文件

  • 注意此文件必须在对应的接口权限定报名下


<mapper namespace="com.itheima.dao.StudentDao">
    <select id="findAll" resultType="com.itheima.domain.Student">
        select * from student
    select>
mapper>

4.4 jdbc.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.200.129:3307/db1
jdbc.username=root
jdbc.password=123456

4.5 spring配置文件中添加mybatis相关的三个bean


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

    <mvc:annotation-driven/>
    <context:component-scan base-package="com.itheima"/>
    <mvc:default-servlet-handler/>


    
    <context:property-placeholder location="classpath:*.properties"/>

    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    bean>

    
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.itheima.domain"/>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.dao"/>
    bean>

beans>

4.6 SSM整合完成修改Controller测试

@Controller
public class HelloController {

    @Autowired
    private StudentDao studentDao;

    @RequestMapping("/hello")
    @ResponseBody
    public List<Student> hello() {
        return studentDao.findAll();
    }
}

加粗样式

你可能感兴趣的:(ssm整合)