新手如何搭建ssm框架(idea)

写这篇文章的目的是为了新手学习如何快速搭建ssm框架,用于刚进入公司快速搭建和平时的学习。
1 创建一个ssm的工程
在这里插入图片描述
2 导入pom坐标:

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>com.jijia</groupId>
    <artifactId>ssm_test</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>ssm_common</module>
        <module>ssm_dao</module>
        <module>ssm_domain</module>
        <module>ssm_system_service</module>
        <module>ssm_mannge_web</module>
    </modules>

    <!-- 集中定义依赖版本号 -->
    <properties>
        <!--测试包--><junit.version>4.12</junit.version>
        <!--spring的包--><spring.version>5.0.2.RELEASE</spring.version>
        <!--spring分页包--><pagehelper.version>5.1.2</pagehelper.version>
        <mybatis.version>3.4.5</mybatis.version>
        <!--mybatis的包--><mybatis.spring.version>1.3.0</mybatis.spring.version>
        <!--数据库的包--><mysql.version>5.1.32</mysql.version>
        <!--连接池的包--><druid.version>1.0.9</druid.version>
    </properties>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</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-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</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-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>
        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis.spring.version}</version>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!-- 连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>
</project>

3 建立各个工程的模块:
在这里插入图片描述
4 建立模块的关系:
依次是:web依赖service依赖dao依赖domian依赖common
在这里插入图片描述
5 插入测试company实体类:

package com.jijia.domain.system;

import java.io.Serializable;
import java.util.Date;
/**
 * 这是一个测试实体类
 * @author 马石林
 * @Company http://www.马石林.com
 */
public class Company implements Serializable {
    private String id;
    private String name;
    private Date expirationDate;
    private String address;
    private String licenseId;
    private String representative;
    private String phone;
    private String companySize;
    private String industry;
    private String remarks;
    private Integer state;
    private Double balance;
    private String city;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getExpirationDate() {
        return expirationDate;
    }

    public void setExpirationDate(Date expirationDate) {
        this.expirationDate = expirationDate;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getLicenseId() {
        return licenseId;
    }

    public void setLicenseId(String licenseId) {
        this.licenseId = licenseId;
    }

    public String getRepresentative() {
        return representative;
    }

    public void setRepresentative(String representative) {
        this.representative = representative;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getCompanySize() {
        return companySize;
    }

    public void setCompanySize(String companySize) {
        this.companySize = companySize;
    }

    public String getIndustry() {
        return industry;
    }

    public void setIndustry(String industry) {
        this.industry = industry;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public Double getBalance() {
        return balance;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Company{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", expirationDate=" + expirationDate +
                ", address='" + address + '\'' +
                ", licenseId='" + licenseId + '\'' +
                ", representative='" + representative + '\'' +
                ", phone='" + phone + '\'' +
                ", companySize='" + companySize + '\'' +
                ", industry='" + industry + '\'' +
                ", remarks='" + remarks + '\'' +
                ", state=" + state +
                ", balance=" + balance +
                ", city='" + city + '\'' +
                '}';
    }
}

6 编写dao层的配置文件和接口:
dao的接口(CompanyDao):

package com.jijia.system.dao;

import com.jijia.domain.system.Company;

import java.util.List;

/**
 * dao的测试接口
 * @author 马石林
 * @Company http://www.马石林.com
 */
public interface CompanyDao {
    List<Company> findAll();
}

properties连接(db.properties)(自己电脑的数据库):

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/saas-export-ee100?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=1234

spring的整合mybatis(applicationContext-dao.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       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-4.2.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <!--创建容器的包扫描-->
    <context:component-scan base-package="com.itheima">
        <!--Controller注解不需要spring扫描,由springmvc扫描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
    </context:component-scan>

    <!--指定连接数据库的properties文件的位置-->
    <context:property-placeholder location="classpath*:properties/db.properties"></context:property-placeholder>

    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--配置SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--配置别名-->
        <property name="typeAliasesPackage" value="com.itheima.domain"></property>
    </bean>

    <!--配置dao接口创建代理实现类的扫描器-->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定dao接口所在的包-->
        <property name="basePackage" value="com.itheima.dao"></property>
    </bean>
</beans>

同目录下的xml配置文件(com/jijia/system/dao/CompanyDao.xml):

<?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.jijia.dao.system.CompanyDao">

    <!--建立实体类属性和数据库表中列名的对应关系-->
    <resultMap id="BaseResultMap" type="company">
        <id     column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="expiration_date" property="expirationDate"></result>
        <result column="address" property="address"></result>
        <result column="license_id" property="licenseId"></result>
        <result column="representative" property="representative"></result>
        <result column="phone" property="phone"></result>
        <result column="company_size" property="companySize"></result>
        <result column="industry" property="industry"></result>
        <result column="remarks" property="remarks"></result>
        <result column="state" property="state"></result>
        <result column="balance" property="balance"></result>
        <result column="city" property="city"></result>
    </resultMap>

    <!--配置查询所有-->
    <select id="findAll" resultMap="BaseResultMap">
        select * from ss_company;
    </select>
</mapper>

7.service整合spring配置文件(applicationContext-dao.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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

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

    <!--配置事务的通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--配置事务的属性-->
        <tx:attributes>
            <tx:method name="*" read-only="false" propagation="REQUIRED"/>
            <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
        </tx:attributes>
    </tx:advice>

    <!--配置aop 包括配置切入点表达式,事务通知和切入点表达式的关联-->
    <aop:config>
        <!--配置切入点表达式-->
        <aop:pointcut id="pt1" expression="execution(* com.jijia.service.*.impl.*.*(..))"></aop:pointcut>
        <!--建立切入点表达式和事务的关联-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
    </aop:config>
</beans>

编写service和service实现类测试类:
CompanyService

package com.jijia.service;

import com.jijia.domain.system.Company;

import java.util.List;

public interface CompanyService {
    /**
     * 查询所有企业
     * @return
     */
    List<Company> findAll();
}

CompanyServiceImpl:

package com.jijia.service.impl;

        import com.jijia.domain.system.Company;
        import com.jijia.service.CompanyService;
        import com.jijia.system.dao.CompanyDao;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;

        import java.util.List;
@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    private CompanyDao companyDao;
    public List<Company> findAll() {
        return companyDao.findAll();
    }
}

8 编写Controller层测试类和配置文件:
spring整合springmvc(springmvc.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:aop="http://www.springframework.org/schema/aop"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <!--指定springmvc创建容器时要扫描的包-->
    <context:component-scan base-package="com.jijia.web"></context:component-scan>

    <!--配置视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--开启注解的支持-->
    <mvc:annotation-driven/>
</beans>

测试Controller:

package com.jijia.web.controller;

import com.jijia.domain.system.Company;
import com.jijia.service.system.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 企业的控制器
 * @author 黑马程序员
 * @Company http://www.jijia.com
 */
@Controller
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;
    /**
     * 获取企业列表
     * @return
     */
    @RequestMapping(value="/list",name = "查询企业列表")
    public String list(HttpServletRequest request){
        //1.查询企业列表
        List<Company> companyList = companyService.findAll();
        //2.存入请求域中
        request.setAttribute("companyList",companyList);
        //3.前往列表页面
        return "company/company-list";
    }
}

编写测试页面:
在这里插入图片描述
web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <!-- 监听器监听其他的spring配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 解决post乱码 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <!-- Shiro Security filter  filter-name这个名字的值将来还会在spring中用到
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
      -->
</web-app>

index.jsp:

<%@page contentType="text/html;UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
    <title>临时主页</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/company/list.do">
    <h2>企业列表</h2>
</a>
</body>
</html>

company-list.jsp:

<%@page pageEncoding="UTF-8" language="java" contentType="text/html;UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>临时企业列表</title>
</head>
<body>
<table>
    <tr>
        <th class="">序号</th>
        <th class="sorting">企业名称</th>
        <th class="sorting">所在地</th>
        <th class="sorting">地址</th>
        <th class="sorting">企业法人</th>
        <th class="sorting">联系方式</th>
        <th class="sorting">所属行业</th>
        <th class="sorting">状态</th>
        <th class="sorting">余额</th>
        <th class="text-center">操作</th>
    </tr>
    <!--遍历集合,显示企业列表内容-->
    <c:forEach items="${companyList}" var="item" varStatus="vs">
        <tr>
            <td><input name="ids" value="${item.id}" type="checkbox"> ${vs.count}</td>
            <td>
                    ${item.name}
            </td>
            <td>${item.city}</td>
            <td>${item.address}</td>
            <td>${item.representative}</td>
            <td>${item.phone}</td>
            <td>${item.industry}</td>
            <td>${item.state ==0?'未审核':'已审核'}</td>
            <td class="text-center">${item.balance}</td>
            <td class="text-center">
                <button type="button" class="btn bg-olive btn-xs" onclick='location.href="${pageContext.request.contextPath}/company/toUpdate.do?id=${item.id}"'>编辑</button>
            </td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

测试成功。

注意事项:数据库账号和密码是对的,表名是对的,dao和dao.xml包结构相对应。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于简化Spring应用程序开发的框架。它提供了一套开箱即用的配置,可以快速搭建基于Spring的应用程序。SSM框架是指Spring + SpringMVC + MyBatis组合使用的一种开发框架。下面是关于如何使用Spring Boot搭建SSM框架的步骤: 1. 首先,我们需要创建一个Maven项目。可以通过在IDE中选择创建Maven项目的方式来创建一个新的Maven项目。 2. 在pom.xml文件中添加Spring Boot和SSM的相关依赖。在这里,我们需要引入spring-boot-starter-web,spring-boot-starter-data-jpa,mybatis-spring-boot-starter等依赖。 3. 创建一个Spring Boot的主类,使用@SpringBootApplication注解进行标记。这个类将作为整个应用的入口。 4. 创建一个控制器类,使用@RestController注解进行标记,并编写一些API接口方法。 5. 创建一个实体类,用于映射数据库中的表结构。 6. 创建一个DAO接口,使用@Mapper注解进行标记,并在方法上使用@Select、@Insert、@Update等注解编写SQL语句。 7. 创建一个Service类,使用@Service注解进行标记,并在类中注入DAO接口的实现。 8. 创建一个配置类,用于配置数据库连接等信息。 9. 在配置文件application.properties或application.yml中配置数据库连接信息。 10. 运行Spring Boot应用程序,可以通过访问定义的API接口来进行测试。 通过以上步骤,我们就可以使用Spring Boot搭建SSM框架。Spring Boot提供了自动配置的功能,可以帮助我们快速集成SSM框架所需的组件,并且简化了开发过程。使用Spring Boot搭建SSM框架可以提高开发效率和代码质量,同时还能够实现更好的性能和可扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值