idea搭建ssm框架

idea是目前的主流java开发软件,那么如何在idea上快速而准确的搭建一个可以运行的ssm框架获取会给一些小伙伴尤其是刚入门的小伙伴带来困难。
首先,来一个项目整体目录的截图
idea搭建ssm框架_第1张图片
ssm最重要的在于配置文件:
你可能看见不同人的项目拥有的配置文件数量不一样,这很正常

首先,看一下pom里面所需要的依赖



  org.springframework
  spring-webmvc
  5.1.9.RELEASE



  org.springframework
  spring-jdbc
  5.1.9.RELEASE



  org.springframework
  spring-aop
  5.0.11.RELEASE


  org.springframework
  spring-aspects
  5.0.11.RELEASE




  org.mybatis
  mybatis
  3.4.5




  org.mybatis
  mybatis-spring
  1.3.1




  mysql
  mysql-connector-java
  8.0.15




  com.mchange
  c3p0
  0.9.5.5



  
      jstl
      jstl
      1.2
  



  javax.servlet
  javax.servlet-api
  3.1.0



  org.projectlombok
  lombok
  1.18.6



  org.slf4j
  jcl-over-slf4j
  1.7.20



  junit
  junit
  4.12
  test

这是所需要配置的jar包,下面是资源的读取路径
medicals

  
    src/main/java
    
      **/*.xml
    
  
  
    src/main/resources
    
      *.xml
      *.properties
    
  

接下来让我们看一个mybatis的配置,mybatis主要用于持久层,读取实体类,建立日志等

<?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>

<typeAliases>
    <package name="com.medicals.entity"/>
</typeAliases>
</configuration>

接下来是spring.xml。spring主要用于整合,我们在这里直接将数据源和扫描mapper等写入

<?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-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!--整合mybatis-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/medical?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC" />
        <property name="user" value="root" />
        <property name="password" value="lht980519" />
        <!-- 初始化连接大小 -->
        <property name="initialPoolSize" value="5"></property>
        <!-- 连接池最大数量 -->
        <property name="maxPoolSize" value="10"></property>
    </bean>

    <!--配置myBatis sqlsessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath:com/medicals/mapper/*.xml"></property>
        <property name="configLocation" value="classpath:mybatisConfig.xml"></property>
    </bean>

    <!--扫描自定义的mapper接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.medicals.mapper"></property>
    </bean>
</beans>

下面是springmvc.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" 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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

     <!--启动注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--扫描业务代码-->
    <context:component-scan base-package="com.medicals"></context:component-scan>

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

</beans>

当然,web.xml也是必不可少的

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!--启动spring -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!--Spring MVC-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--字符编码过滤器-->
  <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>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*
  

  
  
    default
    *.js
  
  
    default
    *.css
  
  
    default
    *.jpg
  


ssm相关配置基本就完成了,有额外需要的自行添加。当你完成框架的搭建后可以先进行测试,如果能测通,则直接写相应的业务逻辑就行。

最后再贴一些注解的使用,希望对新手有用
idea搭建ssm框架_第2张图片
idea搭建ssm框架_第3张图片
idea搭建ssm框架_第4张图片
奉上头文件:

<?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">

如果有帮助,希望点个赞!!

你可能感兴趣的:(idea,框架,ssm,java)