资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】

一. 框架详情
  1. Spring 是一个轻量级的Java开发框架,它是为了解决企业应用开发的复杂性而创建的。Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

    创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计、网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的龙山网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

  2. SpringMVC 属于SpringFrameWork的后续产品,分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。

  3. MyBatis 是一个基于Java的持久层框架。MyBatis提供的持久层框架包括SQL Maps和Data Access Objects(DAO)它消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java映射成数据库中的记录。

二. 创建Maven项目
  1. Eclipse中用Maven创建项目

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】
     

  2. 按默认Next

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】
     

  3. 找到maven-archetype-webapp后,点击next

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】
     

  4. 填写相应的信息,GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构。ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。Package填了默认给你建一个包,不写也可以。

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】
     

  5. 刚建好的目录如下

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】
     

  6. Maven规定必须添加以下Source Folder:
    src/main/resources
    src/main/java
    src/test/resources
    src/test/java
    在这步之前最好先项目上右键选择properties,然后点击java build path,在Librarys下,编辑JRE System Library,选择workspace default jre。

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】
     

  7. 分别修改输出路径为,对应关系如下:

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】
     

  8. 将项目转换成Dynamic Web Project,在项目上右键Properties,在左侧选择 Project Facets。

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】
     

  9. 设置部署时的文件发布路径,删除test的两项,因为test是测试使用,并不需要部署。
    设置将Maven的jar包发布到lib下。Add -> Java Build Path Entries -> Maven Dependencies -> Finish

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】
     

    三. Maven引入需要的JAR包

    Xml代码  手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】

     
    四. 相关配置文件配置,整合SSM框架

    web.xml

    Java代码  手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】

     
    spring.xml

    Java代码  手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】

     

    为了代替手工使用 SqlSessionDaoSupport 或 SqlSessionTemplate 编写数据访问对象 (DAO)的代码,MyBatis-Spring 提供了一个动态代理的实现:MapperFactoryBean。这个类 可以让你直接注入数据映射器接口到你的 service 层 bean 中。当使用映射器时,你仅仅如调 用你的 DAO 一样调用它们就可以了,但是你不需要编写任何 DAO 实现的代码,因为 MyBatis-Spring 将会为你创建代理。

    spring-mybatis.xml

    Java代码  手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】

     
    spring-mvc.xml

    Java代码  手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】

     
    log4j.properties

    Sql代码  手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】


    jdbc.properties

    Java代码  手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】

     

    五. 利用MyBatis Generator自动创建实体类、映射文件以及DAO接口

    MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生成器自动生成实体类、DAO接口和Mapping映射文件。这样可以省去很多的功夫,将生成的代码copy到项目工程中即可。
    生成代码需要的文件和jar并建立如下目录结构:

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】

    在generatorl.xml中配置相关的数据库连接,已经数据库表:

    Xml代码  手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】

    打开CMD窗口 进入该目录结构,输入命令行:

    java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite


    运行完成后会生成相应的dao mapper 和model,是不是很爽。
     

    六. 建立Service层以及conrorller层

    Java代码  手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】

     

    Java代码  手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】

     

    Java代码  手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】

     

    七. 建立测试类

    Java代码  手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】

    1. import java.util.List;  

    2.   

    3. import org.apache.log4j.LogManager;  

    4. import org.apache.log4j.Logger;  

    5. import org.junit.Test;  

    6. import org.junit.runner.RunWith;  

    7. import org.springframework.beans.factory.annotation.Autowired;  

    8. import org.springframework.test.context.ContextConfiguration;  

    9. import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;  

    10.   

    11.   

    12.   

    13.   

    14. @RunWith(SpringJUnit4Cla***unner.class)   

    15. @ContextConfiguration(locations = {"classpath:spring.xml"})  

    16. public class TestMybatis {  

    17.   

    18.     @Autowired  

    19.     private OtherServiceI service;  

    20.   

    21.     @Test  

    22.     public void test() {  

    23.            String list=service.getOterList();  

    24.            logger.info(list);  

    25.     }  

    26.       

    27. }  

    28. package com.yingjun.test.controller;  

    29. import org.springframework.beans.factory.annotation.Autowired;  

    30. import org.springframework.stereotype.Controller;  

    31. import org.springframework.web.bind.annotation.RequestMapping;  

    32. import org.springframework.web.bind.annotation.ResponseBody;  

    33.   

    34. import com.alibaba.fastjson.JSON;  

    35. import com.yingjun.test.service.OtherServiceI;  

    36. import com.yingjun.test.service.StockStatusServiceI;  

    37. @Controller  

    38. @RequestMapping(value = "/")  

    39. public class TSSController {  

    40.       

    41.     @Autowired  

    42.     private OtherServiceI otherService;  

    43.       

    44.     @RequestMapping(value="/getOtherList",produces="text/html;charset=UTF-8" )   

    45.     @ResponseBody  

    46.     private String getOtherList(){  

    47.         String json=otherService.getOterList();  

    48.         return json;  

    49.     }  

    50. }  

    51. package com.yingjun.test.service;  

    52. import java.util.ArrayList;  

    53. import java.util.Arrays;  

    54. import java.util.HashSet;  

    55. import java.util.List;  

    56. import java.util.Map;  

    57. import java.util.Set;  

    58.   

    59. import org.springframework.beans.factory.annotation.Autowired;  

    60. import org.springframework.stereotype.Service;  

    61.   

    62. import com.alibaba.fastjson.JSON;  

    63. import com.alibaba.fastjson.serializer.SerializerFeature;  

    64. import com.yingjun.test.dao.OtherListMapper;  

    65. import com.yingjun.test.model.OtherList;  

    66. import com.yingjun.test.model.OtherListDomain;  

    67. @Service  

    68. public class OtherServiceImpl implements OtherServiceI {  

    69.   

    70.       

    71.     @Autowired  

    72.     private OtherListMapper otherListMapper;  

    73.       

    74.     @Override  

    75.     public String getOterList() {  

    76.         Set set=new HashSet();  

    77.         List list=otherListMapper.selectAll();  

    78.         List jsonList=new ArrayList();  

    79.         for(OtherList other:list){  

    80.             String title=other.getTitle();  

    81.             if(set.contains(title)){  

    82.                 continue;  

    83.             }else{  

    84.                 List t_list=new ArrayList();  

    85.                 for(OtherList data:list){  

    86.                     if(title.equals(data.getTitle())){  

    87.                         t_list.add(data);  

    88.                     }  

    89.                 }  

    90.                 OtherListDomain domain=new OtherListDomain();  

    91.                 domain.setTitle(title);  

    92.                 domain.setItems(t_list);  

    93.                 jsonList.add(domain);  

    94.                 set.add(other.getTitle());  

    95.             }  

    96.         }  

    97.         return JSON.toJSONString(jsonList, SerializerFeature.WriteMapNullValue);  

    98.     }  

    99.   

    100. }  

    1. package com.yingjun.test.service;  

    2.   

    3. public interface OtherServiceI {  

    4.       

    5.     public String getOterList();  

    6.   

    7. }  

    8.     

    9.   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"    

    10.   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  

    11.     

    12.   

    13.       

    14.       

    15.       

    16.           

    17.               

    18.               

    19.               

    20.           

    21.           

    22.         

    23.             connectionURL="jdbc:mysql://192.168.1.194:3306/noc"   

    24.             userId="root" password="root">  

    25.           

    26.           

    27.               

    28.           

    29.           

    30.         

    31.             targetProject="src">  

    32.               

    33.               

    34.           

    35.           

    36.         

    37.             targetProject="src">  

    38.               

    39.           

    40.           

    41.         

    42.              targetProject="src">  

    43.                

    44.           

    45.           

    46.         

    47.             enableCountByExample="fasle" enableUpdateByExample="false"  

    48.             enableDeleteByExample="false" enableSelectByExample="false"  

    49.             selectByExampleQueryId="false" >  

    50.           

    51.       

    52.     

    1. log4j.rootLogger=info, console, debug, app, error  

    2.   

    3. ###Console ###  

    4. log4j.appender.console = org.apache.log4j.ConsoleAppender  

    5. log4j.appender.console.Target = System.out  

    6. log4j.appender.console.layout = org.apache.log4j.PatternLayout  

    7. log4j.appender.console.layout.ConversionPattern = %d %p[%C:%L]- %m%n  

    8.   

    9. ### debug ###    

    10. log4j.appender.debug = org.apache.log4j.DailyRollingFileAppender  

    11. log4j.appender.debug.File = log/debug.log  

    12. log4j.appender.debug.Append = true  

    13. log4j.appender.debug.Threshold = DEBUG  

    14. log4j.appender.debug.DatePattern='.'yyyy-MM-dd  

    15. log4j.appender.debug.layout = org.apache.log4j.PatternLayout  

    16. log4j.appender.debug.layout.ConversionPattern = %d %p[%c:%L] - %m%n  

    17.   

    18. ### app ###    

    19. log4j.appender.app = org.apache.log4j.DailyRollingFileAppender  

    20. log4j.appender.app.File = log/app.log  

    21. log4j.appender.app.Append = true  

    22. log4j.appender.app.Threshold = INFO  

    23. log4j.appender.app.DatePattern='.'yyyy-MM-dd  

    24. log4j.appender.app.layout = org.apache.log4j.PatternLayout  

    25. log4j.appender.app.layout.ConversionPattern = %d %p[%c:%L] - %m%n  

    26.   

    27. ### Error ###  

    28. log4j.appender.error = org.apache.log4j.DailyRollingFileAppender  

    29. log4j.appender.error.File = log/error.log  

    30. log4j.appender.error.Append = true  

    31. log4j.appender.error.Threshold = ERROR   

    32. log4j.appender.error.DatePattern='.'yyyy-MM-dd  

    33. log4j.appender.error.layout = org.apache.log4j.PatternLayout  

    34. log4j.appender.error.layout.ConversionPattern =%d %p[%c:%L] - %m%n  

    35. jdbc.driverClassName=com.mysql.jdbc.Driver  

    36. jdbc.url=jdbc:mysql://192.168.1.194:3306/test?useUnicode=true&characterEncoding=UTF-8  

    37. jdbc.username=root  

    38. jdbc.password=root  

    39.   

    40. c3p0.pool.size.max=20  

    41. c3p0.pool.size.min=5  

    42. c3p0.pool.size.ini=3  

    43. c3p0.pool.size.increment=2  

    1.   

    2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  

    3.     xmlns:context="http://www.springframework.org/schema/context"  

    4.     xmlns:mvc="http://www.springframework.org/schema/mvc"  

    5.     xsi:schemaLocation="  

    6.     http://www.springframework.org/schema/beans  

    7.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

    8.     http://www.springframework.org/schema/context  

    9.     http://www.springframework.org/schema/context/spring-context-3.0.xsd  

    10.     http://www.springframework.org/schema/mvc    

    11.     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  

    12.   

    13.       

    14.       

    15.   

    16.       

    17.       

    18.   

    19.       

    20.     

    21.         class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  

    22.           

    23.               

    24.                 text/html;charset=UTF-8  

    25.               

    26.           

    27.       

    28.   

    29.       

    30.     

    31.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  

    32.           

    33.           

    34.           

    35.       

    36.   

    37.       

    38.     

    39.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  

    40.           

    41.           

    42.           

    43.           

    44.           

    45.           

    46.       

    47.   

    48.   

    49.   

    50.   

    51.   

    52.       

    53.        

    1.   

    2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  

    3.     xmlns:context="http://www.springframework.org/schema/context"  

    4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  

    5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   

    6.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   

    7.             http://www.springframework.org/schema/mvc   

    8.             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   

    9.             http://www.springframework.org/schema/context   

    10.             http://www.springframework.org/schema/context/spring-context-3.0.xsd   

    11.             http://www.springframework.org/schema/aop   

    12.             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   

    13.             http://www.springframework.org/schema/tx   

    14.             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">  

    15.   

    16.       

    17.       

    18.       

    19.       

    20.       

    21.       

    22.       

    23.           

    24.           

    25.           

    26.           

    27.           

    28.           

    29.           

    30.           

    31.       

    32.   

    33.   

    34.       

    35.       

    36.           

    37.           

    38.           

    39.       

    40.       

    41.           

    42.           

    43.       

    44.   

    45.       

    46.           

    47.       

    48.       

    49.           

    50.               

    51.               

    52.               

    53.               

    54.               

    55.               

    56.               

    57.               

    58.               

    59.               

    60.               

    61.   

    62.               

    63.               

    64.               

    65.               

    66.               

    67.   

    68.               

    69.           

    70.       

    71.       

    72.           

    73.           

    74.       

    75.               

    76.   

    77.     xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"  

    78.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"  

    79.     version="2.4">  

    80.   

    81.       

    82.       

    83.         contextConfigLocation  

    84.         classpath:spring.xml  

    85.       

    86.       

    87.         org.springframework.web.context.ContextLoaderListener  

    88.       

    89.       

    90.       

    91.         org.springframework.web.util.IntrospectorCleanupListener  

    92.       

    93.   

    94.       

    95.       

    96.         springMVC  

    97.         org.springframework.web.servlet.DispatcherServlet  

    98.           

    99.             contextConfigLocation  

    100.             classpath:spring-mvc.xml  

    101.           

    102.         1  

    103.       

    104.       

    105.         springMVC  

    106.         /  

    107.       

    108.       

    109.       

    110.       

    111.         encodingFilter  

    112.         org.springframework.web.filter.CharacterEncodingFilter  

    113.           

    114.             encoding  

    115.             UTF-8  

    116.           

    117.           

    118.             forceEncoding  

    119.             true  

    120.           

    121.       

    122.       

    123.         encodingFilter  

    124.         /*  

    125.       

    126.       

    127.   

    1.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  

    2.     4.0.0  

    3.     com.yingjun.test  

    4.     TradingState  

    5.     war  

    6.     2.0.1  

    7.   

    8.       

    9.         UTF-8  

    10.         yyyyMMddHHmmss  

    11.         3.2.9.RELEASE  

    12.         3.1.1  

    13.         1.1.1  

    14.       

    15.   

    16.       

    17.           

    18.             org.springframework  

    19.             spring-core  

    20.             ${spring.version}  

    21.           

    22.           

    23.             org.springframework  

    24.             spring-webmvc  

    25.             ${spring.version}  

    26.           

    27.           

    28.             org.springframework  

    29.             spring-test  

    30.             ${spring.version}  


    31. 名称栏目:手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】
      标题路径:http://cdkjz.cn/article/igpidj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220