ssm整合步骤

IDEA整合SSM

什么是ssm?

Spring + SpringMVC + Mybatis

ssm整合步骤_第1张图片

1.整合工程

1.创建Maven工程

方式一:使用Web模板

ssm整合步骤_第2张图片

方式二:

ssm整合步骤_第3张图片

1.在main目录下新建目录webapp

ssm整合步骤_第4张图片

2.建立web工程

ssm整合步骤_第5张图片

ssm整合步骤_第6张图片

注意路径问题

3.配置 Tomcat

ssm整合步骤_第7张图片

基本目录结构就搭建完毕

4.Maven导入相关依赖

Spring

SpringMVC

Mybatis

数据库连接池,数据库连接驱动

日志,测试单元

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <repositories>
        <repository>
            <id>aliyunmavenid>
            <name>aliyun mavenname>
            <url>https://maven.aliyun.com/repository/centralurl>
            <layout>defaultlayout>
            
            <releases>
                <enabled>trueenabled>
            releases>
            
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
    repositories>
    <groupId>com.august.ssmgroupId>
    <artifactId>SSM_01_CRUDartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>warpackaging>
    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <version.spring>5.3.15version.spring>
    properties>

    <build>
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
            <resource>
                <directory>src/main/resources/mapperdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
        resources>
    build>

    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${version.spring}version>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>${version.spring}version>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>${version.spring}version>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${version.spring}version>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.6version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>2.0.6version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.2.8version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.22version>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>4.0.1version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelperartifactId>
            <version>5.3.0version>
        dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.13.1version>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13.1version>
            <scope>testscope>
        dependency>

        






    dependencies>
project>

5.创建MVC模型

bean、dao、service、controller、utils、test

6.配置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">
    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
    <servlet>
        <servlet-name>DispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc.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>CharacterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>utf-8param-value>
        init-param>
        <init-param>
            <param-name>forceRequestEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
        <init-param>
            <param-name>forceResponseEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    
    <filter>
        <filter-name>HiddenHttpMethodFilterfilter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
web-app>

注意:web.xml引用到Spring、SpringMVC的xml文件,所以在类路径下创建applicationContext.xmlspring-mvc.xml

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

    
    <context:component-scan base-package="com.august" use-default-filters="false">
        
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>
    
    
    <mvc:default-servlet-handler/>
    
    <mvc:annotation-driven/>
beans>

8.配置applicationContext.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: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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <context:component-scan base-package="com.august">
        
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>
    
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" lazy-init="false">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        
        <property name="initialSize" value="${jdbc.initialSize}"/>
        
        <property name="maxActive" value="${jdbc.maxActive}"/>
        
        <property name="minIdle" value="${jdbc.minIdle}"/>
        
        <property name="maxWait" value="${jdbc.maxWait}"/>

        
        <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
        
        <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
    bean>

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

    
    <bean id="sourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
        <property name="dataSource" ref="dataSource"/>
    bean>
    
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* com.august.service..*(..))"/>
        
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    aop:config>
    
    <tx:advice id="txAdvice">
        <tx:attributes>
            
            <tx:method name="*"/>
            
            <tx:method name="get*" read-only="true"/>
        tx:attributes>
    tx:advice>
    
beans>

注意:applicationContext.xml引用jdbc.propertiesmybatis-config.xml文件,所以在类路径下创建.

jdbc.properties

jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmcrud?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456
jdbc.initialSize=5
jdbc.maxActive=20
jdbc.minIdle=3
jdbc.maxWait=0
jdbc.timeBetweenEvictionRunsMillis=0
jdbc.minEvictableIdleTimeMillis=0

9.配置mybatis-config.xml


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

<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true" />
    settings>
    <typeAliases>
        <package name="com.august.bean"/>
    typeAliases>
configuration>

基本框架搭建结束.

2.编写OPM数据库和Java类关系映射

1.创建SQL

员工类Sql

CREATE TABLE `tab_emp` (
  `emp_id` int NOT NULL AUTO_INCREMENT,
  `emp_name` varchar(32) DEFAULT NULL,
  `gender` char(1) DEFAULT NULL,
  `email` varchar(32) DEFAULT NULL,
  `d_id` int DEFAULT NULL,
  PRIMARY KEY (`emp_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

部门类Sql

CREATE TABLE `tab_dept` (
  `dept_id` int NOT NULL AUTO_INCREMENT,
  `dept_name` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`dept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

2.创建对应JavaBean

public class Employees {
    private Integer empId;
    private String empName;
    private String gender;
    private String email;
    private Integer dId;
    private Department dept;
}

public class Department {
    private Integer deptId;
    private String deptName;
}

3.创建对应的dao接口和对应的SQL映射文件

ssm整合步骤_第8张图片

EmployeesMapper.xml


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.august.dao.EmployeesMapper">
    
    <select id="queryAllEmployees" resultType="employees">
        SELECT
        <include refid="empFieldSql"/>
        FROM tab_emp
    select>
    
    <resultMap id="empResultMap" type="employees">
        <id property="empId" column="emp_id"/>
        <result property="empName" column="emp_name"/>
        <result property="email" column="email"/>
        <result property="gender" column="gender"/>
        <result property="deptId" column="dept_id"/>
        
        <association property="deptName" javaType="department">
            <id property="deptId" column="dept_id"/>
            <result property="deptName" column="dept_name"/>
        association>
    resultMap>
    <select id="queryAllEmployeesWithDepartment" resultMap="empResultMap">
        SELECT
        <include refid="empWithFieldSql"/>
        FROM tab_emp e
        LEFT JOIN tab_dept d
        ON e.d_id = d.dept_id
    select>
    
    <select id="queryEmployeesById" resultType="employees">
        SELECT
        <include refid="empFieldSql"/>
        FROM tab_emp where emp_id = #{empId}
    select>
    
    <select id="queryEmployeesByIdWithDepartment" resultMap="empResultMap">
        SELECT
        <include refid="empWithFieldSql"/>
        FROM tab_emp e
        LEFT JOIN tab_dept d
        ON e.d_id = d.dept_id
        WHERE e.emp_id =#{empId}
    select>
    
    <delete id="deleteEmployeesById">
        DELETE
        FROM tab_emp
        WHERE emp_id = #{empId}
    delete>

    
    <insert id="addEmployees" parameterType="employees" useGeneratedKeys="true"  keyProperty="empId">
        INSERT INTO tab_emp(emp_name, email, gender, d_id)
        VALUES (#{empName}, #{email}, #{gender}, #{dId})
    insert>

    
    <sql id="empFieldSql">
        emp_id
        ,emp_name,email,gender,d_id
    sql>
    <sql id="empWithFieldSql">
        e.emp_id,e.emp_name,e.email,gender,d.dept_id,d.dept_name
    sql>
mapper>

DepartmentMapper.xml同理

4.搭建前端框架

结束!

你可能感兴趣的:(intellij-idea,maven,java)