创建SSM工程

1 篇文章 0 订阅
1 篇文章 0 订阅
本文介绍如何在IntelliJ IDEA中创建一个SSM(Spring、SpringMVC、MyBatis)项目。首先通过File->New->Project创建Maven项目,然后添加Web Application支持,设置web.xml。接着配置pom.xml,创建Java和resource文件夹,分别编写执行代码和配置文件,如controller、dao、service、database.properties等,并利用@Mapper注解简化mapper配置。
摘要由CSDN通过智能技术生成

IDEA左上角点击file->new->project;选择maven

next

 finish进入项目

 鼠标右键点击项目,点击Add Framework Support...,添加框架支持

 选择Web Application,版本为4.0 ,并勾选Create web.xml

 此时项目有了web文件夹,即前端页面和配置文件,也可创建文件夹存放静态资源(与WEB-INF同级目录),配置路径即可用。

首先配置pom.xml,此处主要使用maven管理项目依赖和配置文件的设置

Java文件夹编写执行代码:

controller:控制层

dao(mapper):持久层

pojo:实体类

service:业务层

resource文件夹下编写配置文件:

database.properties

#数据库驱动
jdbc.driver=com.mysql.jdbc.Driver
#数据库路径
jdbc.url=jdbc:mysql://localhost:3306/#库名?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=#用户名
jdbc.password=#密码

mybatis-config.xml

<?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>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <!--配置数据源,交给spring做-->
    <typeAliases>
        <package name="实体类路径"/>
    </typeAliases>
    <mappers>
        <mapper class="#mapper路径"/>
    </mappers>
</configuration>

mapper路径映射的持久层,使用@Mapper注解将mapper这个DAO交給Spring管理 ,不用再写mapper映射xxxMapper.xml文件,自动根据这个添加@Mapper注解的接口生成一个实现类

    @Select("select * from xxx where name=#{name};")
    List<Xxx> queryXxx(String name);
<?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><!-- 配置 -->
    <properties /><!-- 属性 -->
    <settings /><!-- 设置 -->
    <typeAliases /><!-- 类型命名 -->
    <typeHandlers /><!-- 类型处理器 -->
    <objectFactory /><!-- 对象工厂 -->
    <plugins /><!-- 插件 -->
    <environments><!-- 配置环境 -->
        <environment><!-- 环境变量 -->
            <transactionManager /><!-- 事务管理器 -->
            <dataSource /><!-- 数据源 -->
        </environment>
    </environments>
    <databaseIdProvider /><!-- 数据库厂商标识 -->
    <mappers /><!-- 映射器 -->
</configuration>

spring-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:context="http://www.springframework.org/schema/context"
       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">
<!--    关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
    <!-- 2.数据库连接池 -->
    <!--数据库连接池
        dbcp 半自动化操作 不能自动连接
        c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 配置连接池属性 -->
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>
    <!-- 3.配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--        注入数据库连接池-->
        <property name="dataSource" ref="dataSource"/>
<!--        配置mybatis全局配置文件,mybatis-config.xml-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

<!--    配置dao接口扫描包,动态的实现了Dao接口可以注入到Spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--        注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!--        要扫描的dao包-->
        <property name="basePackage" value="com.dao"/>
    </bean>
</beans>

spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--    注解驱动-->
    <mvc:annotation-driven/>
<!--    静态资源过滤-->
    <mvc:default-servlet-handler/>
<!--    扫描包:controller-->
    <context:component-scan base-package="com.controller"/>
<!--    视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--    前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
<!--    后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
<!--    静态资源路径-->
    <mvc:resources mapping="/static/**" location="/static/"/>
    <mvc:interceptors>
        <mvc:interceptor>
<!--    拦截器生效路径-->
            <mvc:mapping path="/user/**"/>
            <bean class="com.interceptor.LoginInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
    <!-- 定义文件上传解析器 -->
    <!-- 定义文件上传解析器 -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设定默认编码 -->
        <property name="defaultEncoding" value="UTF-8"></property>
        <!-- 设定文件上传的最大值 -->
        <property name="maxUploadSize" value="99999999"></property>
    </bean>
</beans>

spring-service.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: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">
<!--    扫描service下的包-->
    <context:component-scan base-package="com.kuang.service"/>

<!--    将我们的所有业务类,注入到spring,可以通过配置,或者注解实现-->
    <bean id="XxxServiceImpl" class="com.service.XxxServiceImpl">
        <property name="xxxMapper" ref="xxxMapper"/>
    </bean>
<!--    声明式事务配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--        注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--    结合aop实现事务的织入-->
    <!--    配置事务的通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--        给方法配置事务-->
        <!--        配置事务的传播性-->
        <tx:attributes>
            <tx:method name="add" propagation="REQUIRED"/>
            <tx:method name="delete" propagation="REQUIRED"/>
            <tx:method name="update" propagation="REQUIRED"/>
            <tx:method name="query" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--    配置事务切入-->
    <aop:config>
        <aop:pointcut id="txPointCut" expression="execution(* com.kuang.dao.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
</beans>

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

    <import resource="classpath:spring-dao.xml"/>
    <import resource="classpath:spring-service.xml"/>
    <import resource="classpath:spring-mvc.xml"/>

</beans>

XxxMapper.xml

resultType主要针对的是从数据库查询数据;parameterType 主要针对于将信息存入到数据库中(如:insert)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--绑定接口-->
<mapper namespace="com.dao.XxxMapper">
    #sql语句
    <select id="#XxxMapper的方法名绑定" resultType="Xxx">
        select * from xxx;
    </select>
</mapper>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,在IDEA创建一个非maven的ssm工程,你可以按照以下步骤进行操作: 1. 打开IDEA,点击"Create New Project"创建新项目。 2. 在左侧的菜单中选择"Java",然后点击"Next"。 3. 输入项目名称,例如"SSMProject01",然后点击"Next"。 4. 在"Project SDK"下拉菜单中选择你的Java版本,然后点击"Next"。 5. 在项目类型选择"None",然后点击"Next"。 6. 点击"Finish"完成项目的创建。 接下来,我们需要对项目进行一些配置: 1. 在项目结构中,可以看到项目的目录结构与Eclipse创建的项目有所不同。我们需要做一些调整。 2. 在项目根目录下创建"WEB-INF"文件夹,然后在"WEB-INF"下创建"lib"和"classes"两个文件夹。"lib"文件夹用于存放项目所需的jar包,"classes"文件夹用于存放编译后的文件。 3. 将ssm框架所需的jar包放入"lib"文件夹中。 然后,我们需要告诉IDEA关于lib和classes文件夹的位置: 1. 点击菜单栏中的"File",选择"Project Structure"。 2. 在弹出的窗口中,选择"Modules",然后选择你的项目。 3. 在右侧的选项卡中,选择"Dependencies"。 4. 点击右上角的"+"按钮,选择"JARs or directories"。 5. 在弹出的窗口中,选择你之前创建的"lib"文件夹,然后点击"OK"。 6. 再次点击右上角的"+"按钮,选择"JARs or directories"。 7. 在弹出的窗口中,选择你之前创建的"classes"文件夹,然后点击"OK"。 8. 点击"Apply"和"OK"保存配置。 最后,你可以编辑index.jsp文件来查看项目是否正常运行: 1. 打开index.jsp文件,你可以在"webapp"文件夹下找到它。 2. 编辑index.jsp文件,你可以根据需要修改文件中的内容。 以上就是使用IDEA创建一个ssm框架的步骤。如果你按照以上步骤进行操作,你应该能够成功创建一个ssm工程
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值