整合一个基础SSM项目(实训笔记)

文章目录

  • 1 建包controller,mapper,pojo,service,utils
  • 2 创建配置文件 web.xml、applicationContext.xml、mybatis-config.xml
  • 3 配置web.xml
  • 4 配置mybatis-config.xml -- 别名
  • 5 配置springmvc-servlet.xml
    • 5.1 注解扫描
    • 5.2 核心三要素 (handlerMapping,handlerAdapt,视图解析器)
    • 5.3 处理静态资源
  • 6 配置spring-mapper.xml
    • 6.1 注解扫描
    • 6.2 配置数据源
    • 6.3 sqlSessionFactory
    • 6.4 动态扫描mapper
  • 7 配置spring-service.xml
    • 7.1 注解扫描
    • 7.2 配置声明式事务
  • 8.创建 mapper接口 还有 mapper.xml
  • 9.测试 mvc ,mybatis

1 建包controller,mapper,pojo,service,utils

整合一个基础SSM项目(实训笔记)_第1张图片

2 创建配置文件 web.xml、applicationContext.xml、mybatis-config.xml

整合一个基础SSM项目(实训笔记)_第2张图片
整合一个基础SSM项目(实训笔记)_第3张图片

3 配置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_4_0.xsd"
         version="4.0">

  
  <servlet>

    <servlet-name>DispatcherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

    
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:applicationContext.xmlparam-value>
    init-param>

    
    <load-on-startup>1load-on-startup>

  servlet>

  <servlet-mapping>
    <servlet-name>DispatcherServletservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>

  
  <filter>
    <filter-name>encodingfilter-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>encodingfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>

  
  <session-config>
    <session-timeout>15session-timeout>
  session-config>

  
  <welcome-file-list>
    <welcome-file>index.jspwelcome-file>
  welcome-file-list>

web-app>

4 配置mybatis-config.xml – 别名


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    
    <typeAliases>
        <package name="com.cxj.pojo"/>
    typeAliases>
configuration>

5 配置springmvc-servlet.xml

头文件


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

5.1 注解扫描

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

5.2 核心三要素 (handlerMapping,handlerAdapt,视图解析器)


    <mvc:annotation-driven/>

    
    <bean id="InternalResourceViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    bean>

5.3 处理静态资源


    <mvc:default-servlet-handler/>

6 配置spring-mapper.xml

头文件:


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

6.1 注解扫描


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

6.2 配置数据源

 
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/数据库名称?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true" />
        <property name="username" value="root" />
        <property name="password" value="密码" />
        <property name="maxActive" value="10" />
        <property name="minIdle" value="2" />
        <property name="maxWait" value="15000" />
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <property name="minEvictableIdleTimeMillis" value="180000" />
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="600" />
        <property name="logAbandoned" value="true" />
        <property name="filters" value="stat" />
    bean>

6.3 sqlSessionFactory


    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    bean>

6.4 动态扫描mapper

 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        
        <property name="basePackage" value="com.cxj.mapper"/>
    bean>

7 配置spring-service.xml

头文件


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

7.1 注解扫描


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

7.2 配置声明式事务


    <bean id="transactionManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    bean>

注册bean

   
    <bean id="GoodsServiceImpl" class="com.cxj.service.GoodsServiceImpl">
        <property name="GoodsMapper" ref="goodsMapper"/>
    bean>

8.创建 mapper接口 还有 mapper.xml

创建接口:

public interface GoodsMapper {
    /**
     * 查询所有
     */
    List<Goods> queryAll();
}

GoodsMapper.xml


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cxj.mapper.GoodsMapper">
    <select id="queryAll" resultType="goods">
        select * from goods;
    select>
mapper>

9.测试 mvc ,mybatis

测试类:

public class Application {
    public static void main(String[] args) {
        //ClassPathXmlApplicationContext拿到的bean必须是在xml中注册的
        //如果是注解注册bean, 那你就要用AnnotationConfigApplicationContext 来映射
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        GoodsServiceImpl goodsServiceImpl = context.getBean("GoodsServiceImpl", GoodsServiceImpl.class);
        for (Goods goods : goodsServiceImpl.queryAll()) {
            System.out.println(goods);
        }

    }
}

测试结果:
整合一个基础SSM项目(实训笔记)_第4张图片

你可能感兴趣的:(知识点,java,spring,xml,java)