ssm简单的示例整合(eclipse+maven+ssm+oracle/mysql+jquery.js)

开发工具我使用的是eclipse,毕竟从学java开始就用这个,比较熟悉

maven其实就是管理jar包的东西,与普通ssm相比也不用配置其他的xml

ssm是spring+springmvc+mybatis(重点的东西)

数据库我两个都试过了,更换数据库只需要改那个properties里的东西就行了

然后把我的目录结构拿出来 

ssm简单的示例整合(eclipse+maven+ssm+oracle/mysql+jquery.js)

其中红框内部的就是我们要写的,对了还有pom.xml

一。我的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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Maven</groupId>
  <artifactId>zxm02</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>zxm Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.6.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.6.2</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.9</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.9</version>
</dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.9</version>
</dependency>

    
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>12.1.0.1-atlassian-hosted</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.30</version>
</dependency>

    
    
    
  </dependencies>
  
  
  
  
  <build>
    <finalName>zxm02</finalName>
  </build>
</project>

pox.xml的东西不要全部复制粘贴,比如工程的名字就不一样,复制dependencies里的就行,

这里说一下,ojdbc6的jar包,因为版权原因,maven不能下载,需要先下载到本地再在引用(非maven的小伙伴可跳过)    友情链接:https://blog.csdn.net/qq_38263715/article/details/80698499

导包成功后,我们进行第二步

二。我的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
         id="WebApp_ID" version="3.1">


         
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:app.xml</param-value>
      </context-param>
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <servlet>
        <servlet-name>springmvc</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>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
      
         
</web-app>

其实我这里只有两个东西:1是加载app.xml(大多数人写的是applicationContext.xml,我简写了)

          2是加载springmvc.xml

    下面那个<servlet-mapping>是拦截请求用的,他会拦截所有以.do结尾的url,这个可以自己选,*.什么都可以

既然要加载这两个文件,我们就要写出来,要不然会报错的

三。app.xml

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

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

    <context:property-placeholder location="classpath:mysql.properties"/>
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    
    
    
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:resource/userMapper.xml" />
    </bean>   
    
    

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

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

</beans>

这里配置了包扫描,连接数据库的,有几处用到了包路径的名字!!仔细改成你的路径(4处)

四。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:mvc="http://www.springframework.org/schema/mvc"
    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-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
        
<!-- 开启controller注解支持 -->
    <!-- 注意事项请参考:http://jinnianshilongnian.iteye.com/blog/1762632 -->
    <!-- 扫描注解的,context是负责扫描的 -->
    <context:component-scan base-package="com.controller" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan>
    <!--使用mvc:annotation-driven代替上边注解映射器和注解适配器 配置 如果使用mvc:annotation-driven就不用配置上面的
        RequestMappingHandlerMapping和RequestMappingHandlerAdapter-->
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <!-- 配置视图解析器    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置jsp路径的前缀  -->
        <property name="prefix" value="/WEB-INF/view/"/>
        <!--配置jsp路径的后缀  -->
        <property name="suffix" value=".jsp"/>
    </bean>
    
    
    
     <bean id="mappingJacksonHttpMessageConverter"
       class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
       <property name="supportedMediaTypes">
          <list>
             <value>app/json;charset=UTF-8</value>
          </list>
       </property>
    </bean>
    
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
       <property name="messageConverters">
          <list>
             <!--json视图拦截器,读取到@ResponseBody的时候去配置它 -->
             <ref bean="mappingJacksonHttpMessageConverter" />
          </list>
       </property>
    </bean>
    
 
</beans>

这里配置了controller的扫描和一个试图解析器,就是controller不用@ResponseBody时,返回页面的时候,会用到他,加一个前缀,加一个后缀

下面那俩配完了就允许返回java类的数据

五。这两个xml我们写完了,我们再连接一个数据库

db.properties(oracle)

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=zxm
jdbc.password=zxm

mysql.properties(mysql)

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/database01useUnicode=true&characterEncoding=UTF8
jdbc.username=root
jdbc.password=root

我也不打码了,这个数据库,用户名,密码要改成你们的

mysql的我加了一个转码的

六。下一步是controller   dao   entity   service  service_impl  usermapper.xml

这几个分别是 控制层  dao层  实体类  服务层  实现服务层  和  写语句的表

0.首先建一个实体类(要与数据库里的属性相对应)

ssm简单的示例整合(eclipse+maven+ssm+oracle/mysql+jquery.js)

1.controller    被。do拦截的页面先来这里(a是我测试用的)

ssm简单的示例整合(eclipse+maven+ssm+oracle/mysql+jquery.js)

 2.然后走service的方法

ssm简单的示例整合(eclipse+maven+ssm+oracle/mysql+jquery.js)

3.然后impl实现这个接口

ssm简单的示例整合(eclipse+maven+ssm+oracle/mysql+jquery.js)

 4.再去找dao

ssm简单的示例整合(eclipse+maven+ssm+oracle/mysql+jquery.js)

 5.dao里的名字与usermapper.xml的id相对应,去找usermapper.xml

ssm简单的示例整合(eclipse+maven+ssm+oracle/mysql+jquery.js)

 6.至此,后台的东西就写完了,框架也搭完了,去写一个jsp文件,看好不好使

ssm简单的示例整合(eclipse+maven+ssm+oracle/mysql+jquery.js)

 7.成功

ssm简单的示例整合(eclipse+maven+ssm+oracle/mysql+jquery.js)

改错的时候,尽量记住都改了哪里,不然一个项目可能越改错的越多。。。。

祝福大家都可以一次配成功0.0  yzbx12138

相关推荐