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

Posted yzbx12138

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssm简单的示例整合(eclipse+maven+ssm+oracle/mysql+jquery.js)相关的知识,希望对你有一定的参考价值。

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

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

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

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

 

 

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

技术图片

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

一。我的pom.xml:

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3   <modelVersion>4.0.0</modelVersion>
  4   <groupId>Maven</groupId>
  5   <artifactId>zxm02</artifactId>
  6   <packaging>war</packaging>
  7   <version>0.0.1-SNAPSHOT</version>
  8   <name>zxm Maven Webapp</name>
  9   <url>http://maven.apache.org</url>
 10   <dependencies>
 11     <dependency>
 12       <groupId>junit</groupId>
 13       <artifactId>junit</artifactId>
 14       <version>3.8.1</version>
 15       <scope>test</scope>
 16     </dependency>
 17     
 18     <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
 19     <dependency>
 20         <groupId>org.apache.commons</groupId>
 21         <artifactId>commons-dbcp2</artifactId>
 22         <version>2.6.0</version>
 23     </dependency>
 24     <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
 25     <dependency>
 26         <groupId>commons-logging</groupId>
 27         <artifactId>commons-logging</artifactId>
 28         <version>1.2</version>
 29     </dependency>
 30 
 31     <dependency>
 32         <groupId>org.apache.commons</groupId>
 33         <artifactId>commons-pool2</artifactId>
 34         <version>2.6.2</version>
 35     </dependency>
 36     
 37     <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
 38     <dependency>
 39         <groupId>com.fasterxml.jackson.core</groupId>
 40         <artifactId>jackson-core</artifactId>
 41         <version>2.9.9</version>
 42     </dependency>
 43     <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
 44 <dependency>
 45     <groupId>com.fasterxml.jackson.core</groupId>
 46     <artifactId>jackson-annotations</artifactId>
 47     <version>2.9.9</version>
 48 </dependency>
 49 
 50     <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
 51 <dependency>
 52     <groupId>com.fasterxml.jackson.core</groupId>
 53     <artifactId>jackson-databind</artifactId>
 54     <version>2.9.9</version>
 55 </dependency>
 56 
 57     
 58     <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
 59 <dependency>
 60     <groupId>org.mybatis</groupId>
 61     <artifactId>mybatis</artifactId>
 62     <version>3.4.6</version>
 63 </dependency>
 64 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
 65 <dependency>
 66     <groupId>org.mybatis</groupId>
 67     <artifactId>mybatis-spring</artifactId>
 68     <version>2.0.1</version>
 69 </dependency>
 70 
 71 <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
 72 <dependency>
 73     <groupId>org.springframework</groupId>
 74     <artifactId>spring-aop</artifactId>
 75     <version>4.1.6.RELEASE</version>
 76 </dependency>
 77 <dependency>
 78     <groupId>org.springframework</groupId>
 79     <artifactId>spring-aspects</artifactId>
 80     <version>4.1.6.RELEASE</version>
 81 </dependency>
 82 <dependency>
 83     <groupId>org.springframework</groupId>
 84     <artifactId>spring-beans</artifactId>
 85     <version>4.1.6.RELEASE</version>
 86 </dependency>
 87 <dependency>
 88     <groupId>org.springframework</groupId>
 89     <artifactId>spring-context</artifactId>
 90     <version>4.1.6.RELEASE</version>
 91 </dependency>
 92 <dependency>
 93     <groupId>org.springframework</groupId>
 94     <artifactId>spring-context-support</artifactId>
 95     <version>4.1.6.RELEASE</version>
 96 </dependency>
 97 <dependency>
 98     <groupId>org.springframework</groupId>
 99     <artifactId>spring-core</artifactId>
100     <version>4.1.6.RELEASE</version>
101 </dependency>
102 <dependency>
103     <groupId>org.springframework</groupId>
104     <artifactId>spring-expression</artifactId>
105     <version>4.1.6.RELEASE</version>
106 </dependency>
107 <dependency>
108     <groupId>org.springframework</groupId>
109     <artifactId>spring-jdbc</artifactId>
110     <version>4.1.6.RELEASE</version>
111 </dependency>
112 <dependency>
113     <groupId>org.springframework</groupId>
114     <artifactId>spring-test</artifactId>
115     <version>4.1.6.RELEASE</version>
116 </dependency>
117 <dependency>
118     <groupId>org.springframework</groupId>
119     <artifactId>spring-tx</artifactId>
120     <version>4.1.6.RELEASE</version>
121 </dependency>
122 <dependency>
123     <groupId>org.springframework</groupId>
124     <artifactId>spring-web</artifactId>
125     <version>4.1.6.RELEASE</version>
126 </dependency>
127 <dependency>
128     <groupId>org.springframework</groupId>
129     <artifactId>spring-webmvc</artifactId>
130     <version>4.1.6.RELEASE</version>
131 </dependency>
132 
133 <dependency>
134     <groupId>com.oracle</groupId>
135     <artifactId>ojdbc6</artifactId>
136     <version>12.1.0.1-atlassian-hosted</version>
137 </dependency>
138 
139 <dependency>
140     <groupId>mysql</groupId>
141     <artifactId>mysql-connector-java</artifactId>
142     <version>5.1.30</version>
143 </dependency>
144 
145     
146     
147     
148   </dependencies>
149   
150   
151   
152   
153   <build>
154     <finalName>zxm02</finalName>
155   </build>
156 </project>

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

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

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

二。我的web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4          xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
 5          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
 6          id="WebApp_ID" version="3.1">
 7 
 8 
 9          
10       <context-param>
11           <param-name>contextConfigLocation</param-name>
12           <param-value>classpath:app.xml</param-value>
13       </context-param>
14       <listener>
15           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
16       </listener>
17       
18       <servlet>
19         <servlet-name>springmvc</servlet-name>
20         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
21         <init-param>
22             <param-name>contextConfigLocation</param-name>
23             <param-value>classpath:springmvc.xml</param-value>
24         </init-param>
25         <load-on-startup>1</load-on-startup>
26     </servlet>
27     
28     
29     <servlet-mapping>
30         <servlet-name>springmvc</servlet-name>
31         <url-pattern>*.do</url-pattern>
32     </servlet-mapping>
33     
34       
35          
36 </web-app>

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

            2是加载springmvc.xml

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

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

三。app.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 3     xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" 
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 8         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 9         http://www.springframework.org/schema/mvc 
10         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
11         http://www.springframework.org/schema/context 
12         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
13         http://www.springframework.org/schema/aop 
14         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
15         http://www.springframework.org/schema/tx 
16         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
17         
18 
19     <context:component-scan base-package="com.impl"></context:component-scan>
20         
21 
22     <context:property-placeholder location="classpath:mysql.properties"/>
23     <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
24         destroy-method="close">
25         <property name="driverClassName" value="${jdbc.driverClassName}" />
26         <property name="url" value="${jdbc.url}" />
27         <property name="username" value="${jdbc.username}" />
28         <property name="password" value="${jdbc.password}" />
29     </bean>
30     
31     
32     
33     
34     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
35         <property name="dataSource" ref="dataSource" />
36         <property name="mapperLocations" value="classpath:resource/userMapper.xml" />
37     </bean>   
38     
39     
40 
41     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
42         <property name="basePackage" value="com.dao"/>
43         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
44     </bean>
45 
46     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
47         <property name="dataSource" ref="dataSource"/>    
48     </bean>
49 
50 </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)

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

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

mysql的我加了一个转码的

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

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

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

技术图片

 

 

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

技术图片

 

 2.然后走service的方法

技术图片

3.然后impl实现这个接口

技术图片

 

 4.再去找dao

技术图片

 

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

技术图片

 

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

技术图片

 

 7.成功

技术图片

 

 

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

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

 

 

 

 

 

 

 

 

 

 

 

以上是关于ssm简单的示例整合(eclipse+maven+ssm+oracle/mysql+jquery.js)的主要内容,如果未能解决你的问题,请参考以下文章

在Eclipse中利用maven整合搭建ssm框架

Maven+SSM整合.doc

SSM项目整合,适合新手

ssm框架整合

SSM框架整合

SSM-maven 简单整合