成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

【Java】基于Maven搭建Spring+SpringMVC+Mybatis框架

edagarli / 1815人閱讀

摘要:關(guān)于的配置,可以參考這篇文章的第一個(gè)小節(jié)配置模板引擎搭什么搭直接用腳手架不行嗎下載就能用下載就能用下載就能用碼云咳咳,開(kāi)個(gè)玩笑,如果本著學(xué)習(xí)態(tài)度的話,那就慢慢啃吧搭建空的項(xiàng)目使用搭建基本的空項(xiàng)目填寫和,,選擇項(xiàng)目的地址,在新的窗口打開(kāi)最

關(guān)于springMVC的配置,可以參考這篇文章的第一個(gè)小節(jié):
【java】intellij idea SpringMVC 配置FreeMarker模板引擎

[TOC]

搭什么搭!直接用腳手架不行嗎?! 下載就能用!!下載就能用??!下載就能用??!

碼云:TestSSM

咳咳,開(kāi)個(gè)玩笑,如果本著學(xué)習(xí)態(tài)度的話,那就慢慢啃吧~

搭建空的Maven項(xiàng)目

使用Intellij idea 搭建基本的maven空項(xiàng)目 new->project

填寫GroupId和ArtifactId,next,選擇項(xiàng)目的地址,next->new window open 在新的窗口打開(kāi)

最后Finish,Maven項(xiàng)目搭建完成,目錄如下:

接下來(lái)我們構(gòu)建項(xiàng)目基本骨架,:
(1)在java下新建com
(2)在com下新建springmvc
(3)在springmvc下新建controller、dao、mapping、pojo、service
(4)在service下新建impl
如下圖:

這里解釋一個(gè)mybatis的工具,叫做mybatis-generator-core-1.3.2,它能夠根據(jù)數(shù)據(jù)庫(kù)中的表生成dao、mapping、pojo三個(gè)類型文件(后面會(huì)在mybatis部分詳細(xì)介紹)

目錄解釋如下:
controller:http(GET和POST等)請(qǐng)求的入口,調(diào)用Service,想客戶端返回?cái)?shù)據(jù)

dao:接口,有mybatis自動(dòng)化工具生成的文件之一,定義了數(shù)據(jù)表映射成的對(duì)象,具有的操作

mapping:具體是sql語(yǔ)句,以xml格式存在,即mybatis的語(yǔ)法文件

pojo:實(shí)體類

service:controller調(diào)用的服務(wù),service調(diào)用dao里面定義的數(shù)據(jù)庫(kù)行為方法,來(lái)執(zhí)行對(duì)應(yīng)mapping里的mybatis,從而完成與數(shù)據(jù)庫(kù)完整的交互。

service-impl:接口,定義了服務(wù)應(yīng)該具有的函數(shù)方法(此處可以理解是提現(xiàn)軟件的設(shè)計(jì)思想,你要是樂(lè)意,直接寫service也可以)

至此:

修改Pom文件

第一步:配置你的maven

setting.xml鏡像配置:(具體可以百度一下其他文章)

     
      alimaven
      aliyun maven
      http://maven.aliyun.com/nexus/content/groups/public/
      central        
    

第二步:在pom.xml文件中添加如下內(nèi)容:


  4.0.0
  hugo
  example
  war
  1.0-SNAPSHOT
  hugo Maven Webapp
  http://maven.apache.org

  
    UTF-8
    
    4.1.4.RELEASE
    
    3.2.8
    
    1.7.7
    1.2.17
    
    2.5.0
  
  
  

    
      junit
      junit
      3.8.1
      test
    
    
    
      org.mybatis
      mybatis
      ${mybatis.version}
    
    
    
      org.mybatis
      mybatis-spring
      1.2.2
    

    
    
      org.springframework
      spring-test
      ${spring.version}
      test
    

    
    
      org.springframework
      spring-webmvc
      ${spring.version}
    

    
    
      org.springframework
      spring-core
      ${spring.version}
    

    
      org.springframework
      spring-beans
      ${spring.version}
    

    
      org.springframework
      spring-context
      ${spring.version}
    

    
      org.springframework
      spring-context-support
      ${spring.version}
    

    
      org.springframework
      spring-tx
      ${spring.version}
    

    
      org.springframework
      spring-web
      ${spring.version}
    

    
      org.springframework
      spring-jdbc
      ${spring.version}
    

    
      javax.servlet
      javax.servlet-api
      3.0.1
      provided
    

    
      javax.servlet.jsp
      jsp-api
      2.2
      provided
    

    
    
      mysql
      mysql-connector-java
      5.1.30
    

    
    
      commons-dbcp
      commons-dbcp
      1.2.2
    

    
    
      jstl
      jstl
      1.2
    
    
    
    
      log4j
      log4j
      ${log4j.version}
    


    
    
      com.alibaba
      fastjson
      1.1.41
    


    
      org.slf4j
      slf4j-api
      ${slf4j.version}
    

    
      org.slf4j
      slf4j-log4j12
      ${slf4j.version}
    
    
    
    
      org.codehaus.jackson
      jackson-mapper-asl
      1.9.13
    

    
      com.fasterxml.jackson.core
      jackson-databind
      2.1.0
    
    
      com.fasterxml.jackson.core
      jackson-annotations
      2.1.0
    

    
    
      commons-fileupload
      commons-fileupload
      1.3.1
    
    
      commons-io
      commons-io
      2.4
    
    
      commons-codec
      commons-codec
      1.9
    

    
      junit
      junit
      4.11
      
      test
    

    
    
      org.springframework
      spring-test
      4.1.7.RELEASE
    
    
      junit
      junit
      RELEASE
    

    
      org.jsoup
      jsoup
      1.8.1
    

    
      net.sourceforge.htmlunit
      htmlunit
      2.15
    


    
      org.seleniumhq.selenium
      selenium-java
      2.33.0
    

    
      us.codecraft
      webmagic-core
      0.6.1
    
    
      us.codecraft
      webmagic-extension
      0.6.1
    


  
  
    hugo
  
    
      src/main/java
      
        **/*.xml
      
      true
    
  
  

版本可自行更換,有興趣的同學(xué)可以看看具體配置。maven的更新方式可以選擇enable-auto-update(大致意思是這個(gè),當(dāng)pom文件改動(dòng)時(shí),自動(dòng)更新)

編寫SSM配置文件

第一部分:與數(shù)據(jù)庫(kù)相關(guān)的jdbc.properties

在resources下新建個(gè)配置文件jdbc.properties,內(nèi)容如下:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/project?useUnicode=true&characterEncoding=utf-8
username=root
password=123456
#定義初始連接數(shù)
initialSize=0
#定義最大連接數(shù)
maxActive=20
#定義最大空閑
maxIdle=20
#定義最小空閑
minIdle=1
#定義最長(zhǎng)等待時(shí)間
maxWait=60000

請(qǐng)確保你的mysql服務(wù)是開(kāi)啟的,且存在數(shù)據(jù)庫(kù)project

第二部分:spring-mvc
在resources下新建個(gè)配置文件spring-mvc.xml,內(nèi)容如下:



    
    

    
    
        
            
                text/html;charset=UTF-8
            
        
    
    
    
        
            
                    
            
        
    
    
    
        
        
        
    
    
    
      
        
          
        
          
        
          
     

請(qǐng)務(wù)必仔細(xì)看里面的配置內(nèi)容,尤其涉及到路徑的地方

第三部分:mybatis-config.xml和spring-mybatis.xml

mybatis-config.xml配置:




    
    
        
        
    

此配置的作用是可以讓mybatis在執(zhí)行的時(shí)候打印日志,mybatis還有其他很多配置選項(xiàng),有需求的小伙伴再仔細(xì)查看更詳細(xì)的博文。關(guān)于mybatis的配置。

spring-mybatis配置:



    
    
    
    
        
    

    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
    

    
    
        
        
    

    
    
        
    

請(qǐng)務(wù)必仔細(xì)看里面的配置內(nèi)容,尤其涉及到路徑的地方很多地方出現(xiàn)錯(cuò)誤,都是因?yàn)閟pring或mybatis自動(dòng)掃描的路徑錯(cuò)誤,導(dǎo)致mapping無(wú)法被識(shí)別。

第四部分:log4j.xml和log4j.properties
log4j.xml
日志模塊的配置:




    
    
    
    
        
            
        
    

    
    
        
        
    

    
        
        
    
    
        
        
    

    
        
        
    

    
        
        
    

    
        
        
    

    
    
        
        
    

    
    
        
        
    
    
    
    
    
        
        
    

log4j.properties

#定義LOG輸出級(jí)別
log4j.rootLogger=INFO,Console,File
#定義日志輸出目的地為控制臺(tái)
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以靈活地指定日志輸出格式,下面一行是指定具體的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

#文件大小到達(dá)指定尺寸的時(shí)候產(chǎn)生一個(gè)新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定輸出目錄
log4j.appender.File.File = logs/ssm.log
#定義文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 輸出所以日志,如果換成DEBUG表示輸出DEBUG以上級(jí)別日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH:mm:ss}][%c]%m%n

以上只是基本配置,如果有其他需求,自行查閱log4j的相關(guān)資料。

至此:

配置項(xiàng)目工程

第一步:將配置文件添加入工程

至此如下:

第二步:配置webapp
在src下創(chuàng)建目錄webapp

web.xml文件




  Archetype Created Web Application
  
  
    contextConfigLocation
    classpath:spring-mybatis.xml
  

  
  
    log4jRefreshInterval
    60000
  
  
    org.springframework.web.util.Log4jConfigListener
  


  
  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    

  

  
    encodingFilter
    /*
  

  
  
    org.springframework.web.context.ContextLoaderListener
  
  
  
    org.springframework.web.util.IntrospectorCleanupListener
  


  
  
    mvc-dispatcher
    org.springframework.web.servlet.DispatcherServlet
    
    
    
      contextConfigLocation
      classpath:spring-mvc.xml
    
    1
    true
  


  
  
    mvc-dispatcher
    
    /
  
  

  
  
    15
  

接下來(lái)我們把webapp添加入項(xiàng)目工程

發(fā)現(xiàn)里面的web.xml地址路徑錯(cuò)誤,這里我們自行修改

第三步:添加artifacts

至此如下:

第三步:配置tomcat

至此:

第四步:編寫小測(cè)試

在controller中新建main.
點(diǎn)擊運(yùn)行,若無(wú)報(bào)錯(cuò),則基本配置成功。

package com.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class main {
    @RequestMapping("/")
    @ResponseBody
    public String index(){
        return "hello world";
    }
}

編寫mybatis的測(cè)試用例

模擬的用戶登錄的行為,進(jìn)行測(cè)試,這里我們?cè)跀?shù)據(jù)庫(kù)里新建一張表

非常簡(jiǎn)單的表結(jié)構(gòu)

然后我們使用mybatis的自動(dòng)生成工具mybatis-generator-core,生成pojo、mapping、dao

這里請(qǐng)注意包名和數(shù)據(jù)庫(kù)的名稱

  
  
  
      
      
          
              
              
          
          
          
          
              
          
          
          
              
              
          
          
          
              
          
          
          
              
          
        

然后在mybatis-generator目錄的lib目錄上執(zhí)行

Java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

接著在src目錄下就能看見(jiàn)生成的三個(gè)文件夾

把它們拷貝到我們的工程目錄里,在寫一個(gè)簡(jiǎn)單的service

package com.springmvc.service;

import com.springmvc.dao.AdminMapper;
import com.springmvc.pojo.Admin;

import javax.annotation.Resource;

public class AdminService {
    @Resource
    private AdminMapper adminMapper;


    public Admin getAdmin(int id){
        Admin a=this.adminMapper.selectByPrimaryKey(id);
        if(a!=null){
            return a;
        }
        return null;
    }
    
    public int insertAdmin(Admin admin){
        return this.adminMapper.insert(admin);
    }
}

至此:

接下來(lái)我們?cè)诰帉懸幌耤ontroller,在main中添加如下代碼:

   @RequestMapping(value = "/admin",method = RequestMethod.POST)
    @ResponseBody
    public int insertUser(Admin admin){
       return adminService.insertAdmin(admin);

    }
    @RequestMapping(value = "/admin/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Admin getUser(@PathVariable  Integer id){
        return adminService.getAdmin(id);

    }

最后我們使用postMan來(lái)測(cè)試:

注冊(cè)admin:

獲取admin:

從控制臺(tái)里,我們可以看到打印出來(lái)的mysql。

以上。

總結(jié)

至此,整個(gè)項(xiàng)目的基本框架已經(jīng)搭建完成了,我們使用空的maven項(xiàng)目搭建起了ssm,實(shí)現(xiàn)了項(xiàng)目的基本架構(gòu),最后如果有不明白的地方可以下載源碼查看。

歡迎大家留言交流~

碼云:TestSSM

【參考】
SSM框架——詳細(xì)整合教程(Spring+SpringMVC+MyBatis)

使用maven創(chuàng)建web項(xiàng)目

intellij idea下使用Maven搭建SpringMVC

IDEA搭建SpringMVC并用maven配置的小例子(非常詳細(xì))

關(guān)于Java Servlet的中文亂碼

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/71048.html

相關(guān)文章

  • Java后端

    摘要:,面向切面編程,中最主要的是用于事務(wù)方面的使用。目標(biāo)達(dá)成后還會(huì)有去構(gòu)建微服務(wù),希望大家多多支持。原文地址手把手教程優(yōu)雅的應(yīng)用四手把手實(shí)現(xiàn)后端搭建第四期 SpringMVC 干貨系列:從零搭建 SpringMVC+mybatis(四):Spring 兩大核心之 AOP 學(xué)習(xí) | 掘金技術(shù)征文 原本地址:SpringMVC 干貨系列:從零搭建 SpringMVC+mybatis(四):Sp...

    joyvw 評(píng)論0 收藏0
  • springmvc簡(jiǎn)介和快速搭建

    摘要:簡(jiǎn)介和眾多其他框架一樣,它基于的設(shè)計(jì)理念,此外,它采用可松散耦合可插拔組件結(jié)構(gòu),比其他框架更具擴(kuò)展性和靈活性。框架圍繞核心展開(kāi),是框架的總導(dǎo)演,總策劃,它負(fù)責(zé)截獲請(qǐng)求并將其分派給相應(yīng)的處理器處理。 springmvc簡(jiǎn)介 springmvc和眾多其他web框架一樣,它基于MVC的設(shè)計(jì)理念,此外,它采用可松散耦合可插拔組件結(jié)構(gòu),比其他MVC框架更具擴(kuò)展性和靈活性。 springmvc通過(guò)...

    Sike 評(píng)論0 收藏0
  • SpringMVC入門筆記

    摘要:入門筆記簡(jiǎn)介是一種基于的實(shí)現(xiàn)了設(shè)計(jì)模式的請(qǐng)求驅(qū)動(dòng)類型的輕量級(jí)框架,是系開(kāi)源項(xiàng)目中的一個(gè),和配合使用。配置在中需要添加使用的和映射規(guī)則。入門較快,而掌握起來(lái)相對(duì)較難。 SpringMVC入門筆記 1. 簡(jiǎn)介 Spring MVC是一種基于Java的實(shí)現(xiàn)了Web MVC設(shè)計(jì)模式的請(qǐng)求驅(qū)動(dòng)類型的輕量級(jí)Web框架 ,是Spring系開(kāi)源項(xiàng)目中的一個(gè),和IoC配合使用。通過(guò)策略接口,Spring...

    zhaochunqi 評(píng)論0 收藏0
  • Spring+SpringMVC+Maven+Mybatis+MySQL+Jetty項(xiàng)目搭建(1)

    摘要:接口聲明并實(shí)現(xiàn)接口聲明一個(gè)接口新建一個(gè)類,并實(shí)現(xiàn)接口單元測(cè)試單元測(cè)試是為了驗(yàn)證第步中接口的方法。中新增類使用實(shí)現(xiàn)單元測(cè)試指定注入的配置文件使用標(biāo)準(zhǔn)的注釋來(lái)告訴使用在中新增類文件運(yùn)行單元測(cè)試右鍵運(yùn)行結(jié)果到此,我們已經(jīng)搭建了一個(gè)基于的項(xiàng)目環(huán)境。 本文詳細(xì)講述如何搭建一個(gè)Spring+SpringMVC+Maven+Mybatis+MySQL項(xiàng)目環(huán)境。eclipse、maven 及 mysq...

    KoreyLee 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<