最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

【IntelliJ IDEA】Maven構(gòu)建自己的第一個(gè)Java后臺(tái)的方法

 更新時(shí)間:2017年12月08日 10:21:34   作者:eagle-zhang  
本篇文章主要介紹了Maven構(gòu)建自己的第一個(gè)Java后臺(tái)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

本文介紹了Maven構(gòu)建自己的第一個(gè)Java后臺(tái)的方法,分享給大家,具體如下:

1.知識(shí)后顧

關(guān)于如何運(yùn)用Maven構(gòu)建自己的第一個(gè)項(xiàng)目,上期我已經(jīng)詳細(xì)的講解過(guò)了,上篇鏈接;今天我以SpringMvc,Mybatis框架搭建一個(gè)屬于你自己的Java后臺(tái)。

2.必要準(zhǔn)備

①I(mǎi)ntelliJ IDEA,Maven環(huán)境搭好

②熟悉掌握MyBatis,SpringMVC等框架

③mysql數(shù)據(jù)庫(kù)的創(chuàng)建

3.整體架構(gòu)布局


4.具體步驟

①在pom.xml中配置工程要使用的jar包

<?xml version="1.0" encoding="UTF-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements. See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership. The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied. See the License for the
 specific language governing permissions and limitations
 under the License.
-->
<!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
<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>
 <packaging>war</packaging>

 <name>yakei</name>
 <groupId>com.yakei</groupId>
 <artifactId>yakei</artifactId>
 <version>1.0-SNAPSHOT</version>

 <dependencies>
 <dependency>
  <!--3.0的junit是使用編程的方式來(lái)進(jìn)行測(cè)試,而junit4是使用注解的方式來(lái)運(yùn)行junit-->
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
 </dependency>

 <!--補(bǔ)全項(xiàng)目依賴-->
 <!--1.日志 java日志有:slf4j,log4j,logback,common-logging
  slf4j:是規(guī)范/接口
  日志實(shí)現(xiàn):log4j,logback,common-logging
  使用:slf4j+logback
 -->
 <dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.12</version>
 </dependency>
 <dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-core</artifactId>
  <version>1.1.1</version>
 </dependency>
 <!--實(shí)現(xiàn)slf4j接口并整合-->
 <dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.1.1</version>
 </dependency>


 <!--1.數(shù)據(jù)庫(kù)相關(guān)依賴-->
 <dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.36</version>
  <scope>runtime</scope>
 </dependency>
 <dependency>
  <groupId>c3p0</groupId>
  <artifactId>c3p0</artifactId>
  <version>0.9.1.1</version>
 </dependency>

 <!--2.dao框架:MyBatis依賴-->
 <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.3.0</version>
 </dependency>
 <!--mybatis自身實(shí)現(xiàn)的spring整合依賴-->
 <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.2.3</version>
 </dependency>

 <!--3.Servlet web相關(guān)依賴-->
 <dependency>
  <groupId>taglibs</groupId>
  <artifactId>standard</artifactId>
  <version>1.1.2</version>
 </dependency>
 <dependency>
  <groupId>jstl</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
 </dependency>
 <dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.5.4</version>
 </dependency>
 <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
 </dependency>

 <!--4:spring依賴-->
 <!--1)spring核心依賴-->
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>4.1.7.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>4.1.7.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.1.7.RELEASE</version>
 </dependency>
 <!--2)spring dao層依賴-->
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>4.1.7.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>4.1.7.RELEASE</version>
 </dependency>
 <!--3)springweb相關(guān)依賴-->
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>4.1.7.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.1.7.RELEASE</version>
 </dependency>
 <!--4)spring test相關(guān)依賴-->
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>4.1.7.RELEASE</version>
 </dependency>
 </dependencies>
</project>

里面涵蓋了Spring,mybatis等一系列jar包,這個(gè)過(guò)程類(lèi)似Android在build.gradle中添加第三方依賴,原理一致。

2.在Resourc目錄下建立兩個(gè)目錄分別是:mapper,spring

 mapper:mapper是mybatis框架的映射,作用是映射文件在dao層用;這里我創(chuàng)建了一個(gè)User.xml映射:


其中紅色部分是要引起重視的,最上面的是映射dao層的路徑,第二個(gè)是返回對(duì)象的類(lèi)型,這里我還是把代碼貼出來(lái):

<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dajiu.dao.UserDao">
<!--目的:為dao接口方法提供sql語(yǔ)句配置
即針對(duì)dao接口中的方法編寫(xiě)我們的sql語(yǔ)句-->
<select id="getAll" resultType="com.dajiu.bean.User">
 select * from user
</select>
<select id="getLogin" resultType="com.dajiu.bean.User">
 select * from user where name = #{name} and password = #{password}
</select>
</mapper>

spring:主要裝載spring的配置文件

1.spring-dao.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:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context  <!--配置整合mybatis過(guò)程
 1.配置數(shù)據(jù)庫(kù)相關(guān)參數(shù)-->
 <context:property-placeholder location="classpath:jdbc.properties"/>

 <!--2.數(shù)據(jù)庫(kù)連接池-->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <!--配置連接池屬性-->
  <property name="driverClass" value="${driver}" />
  <!-- 基本屬性 url、user、password -->
  <property name="jdbcUrl" value="${url}" />
  <property name="user" value="${username}" />
  <property name="password" value="${password}" />
  <!--c3p0私有屬性-->
  <property name="maxPoolSize" value="30"/>
  <property name="minPoolSize" value="10"/>
  <!--關(guān)閉連接后不自動(dòng)commit-->
  <property name="autoCommitOnClose" value="false"/>
  <!--獲取連接超時(shí)時(shí)間-->
  <property name="checkoutTimeout" value="10000"/>
  <!--當(dāng)獲取連接失敗重試次數(shù)-->
  <property name="acquireRetryAttempts" value="2"/>
 </bean>
 <!--約定大于配置-->
 
 <!--3.配置SqlSessionFactory對(duì)象-->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <!--往下才是mybatis和spring真正整合的配置-->
  <!--注入數(shù)據(jù)庫(kù)連接池-->
  <property name="dataSource" ref="dataSource"/>
  <!--配置mybatis全局配置文件:mybatis-config.xml-->
  <property name="configLocation" value="classpath:mybatis-config.xml"/>
  <!--掃描entity包,使用別名,多個(gè)用;隔開(kāi)-->
  <property name="typeAliasesPackage" value="com.dajiu.bean"/>
  <!--掃描sql配置文件:mapper需要的xml文件-->
  <property name="mapperLocations" value="classpath:mapper/*.xml"/>
 </bean>

 <!--4:配置掃描Dao接口包,動(dòng)態(tài)實(shí)現(xiàn)DAO接口,注入到spring容器-->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <!--注入SqlSessionFactory-->
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  <!-- 給出需要掃描的Dao接口-->
  <property name="basePackage" value="com.dajiu.dao"/>
 </bean>
</beans>

重視的地方:

連接數(shù)據(jù)庫(kù):


配置全局的mybatis-config以及bean類(lèi),mapper下的所有文件

配置dao


2.spring-service.xml

貼代碼:

重視地方:

配置service


 3.spring-web.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:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/mvc
   <!--配置spring mvc-->
 <!--1,開(kāi)啟springmvc注解模式
 a.自動(dòng)注冊(cè)DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
 b.默認(rèn)提供一系列的功能:數(shù)據(jù)綁定,數(shù)字和日期的format@NumberFormat,@DateTimeFormat
 c:xml,json的默認(rèn)讀寫(xiě)支持-->
 <mvc:annotation-driven/>

 <!--2.靜態(tài)資源默認(rèn)servlet配置-->
 <!--
  1).加入對(duì)靜態(tài)資源處理:js,gif,png
  2).允許使用 "/" 做整體映射
 -->
 <mvc:default-servlet-handler/>

 <!--3:配置JSP 顯示ViewResolver-->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
  <property name="prefix" value="/WEB-INF/view/"/>
  <property name="suffix" value=".jsp"/>
 </bean>

 <!--4:掃描web相關(guān)的bean-->
 <context:component-scan base-package="com.dajiu.controller"/>
 <mvc:resources mapping="/**/*.html" location="/"/>
 <mvc:resources mapping="/**/*.js" location="/"/>
 <mvc:resources mapping="/**/*.css" location="/"/>
 <mvc:resources mapping="/**/*.png" location="/"/>
 <mvc:resources mapping="/**/*.gif" location="/"/>
</beans>

重視地方:

配置controller


5.邏輯實(shí)現(xiàn)(以u(píng)ser為例)

①首先在bean中 定義user類(lèi)

package com.dajiu.bean;
/**
 * Created by zhangxing on 2017/4/7.
 */
public class User {
 private int id;
 private String name;
 private String password;
 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }
}

②然后再dao中定義UserDao接口

package com.dajiu.dao;
import com.dajiu.bean.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
 * Created by zhangxing on 2017/4/7.
 */
@Repository
public interface UserDao {
 List<User> getAll();
 User getLogin(@Param("name") String name, @Param("password") String password);
}

在user.xml中映射dao層


③接著在service中申明接口

package com.dajiu.service;
import com.dajiu.bean.User;
import java.util.List;
/**
 * Created by zhangxing on 2017/4/7.
 */
public interface UserService {
 List<User> getAll();
 User getLogin(String name,String password);
}

④再在service.impl中具體實(shí)現(xiàn)接口邏輯

package com.dajiu.service.impl;
import com.dajiu.bean.User;
import com.dajiu.dao.UserDao;
import com.dajiu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * Created by zhangxing on 2017/4/7.
 */
@Service("UserService")
public class UserServiceImpl implements UserService {
 @Autowired
 UserDao userDao;
 public List<User> getAll() {
  return userDao.getAll();
 }

 public User getLogin(String name, String password) {
  return userDao.getLogin(name,password);
 }
}

這里的@Autowired相當(dāng)于新建一個(gè)實(shí)例

⑤在controller中實(shí)現(xiàn)真正的后臺(tái)調(diào)用邏輯

package com.dajiu.controller;
import com.dajiu.bean.User;
import com.dajiu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by zhangxing on 2017/4/7.
 */
@Controller
@RequestMapping("/blog")
public class UserController {
 @Autowired
 UserService userService;
 @RequestMapping("/getUser")
 @ResponseBody
 public Map<String,Object> getUser(){
  Map map = new HashMap();
  List<User> list = userService.getAll();
  map.put("user",list);
  map.put("status",1);
  map.put("success",true);
  return map;
 }

 @RequestMapping("getLogin")
 @ResponseBody
 public Map<String,Object> getLogin(String name,String password){
  Map map = new HashMap();
  User user = userService.getLogin(name,password);
  map.put("user",user);
  map.put("isLogin",true);
  map.put("status",1);
  return map;
 }
}

這里的@RequestMapping("")表示訪問(wèn)的映射路徑,@ResponseBody表示請(qǐng)求結(jié)果以json數(shù)據(jù)格式打印出來(lái),@Controller表示只要訪問(wèn)了上面的根映射路徑,就直接調(diào)用controller;

現(xiàn)在幫大家理理思路:先請(qǐng)求UserController---->UserService---->UserServiceImpl---->UserDao---->user.xml(mapper)---->bean(user)

6.配置Tomcat服務(wù)器

①點(diǎn)擊右上角的綠色三角形按鈕,點(diǎn)擊Edit Configuration


②點(diǎn)擊+號(hào),選擇Tomcat


③選擇local


④填寫(xiě)相關(guān)配置


⑤點(diǎn)擊Deployment,點(diǎn)擊+號(hào),選擇Artifact


接著選擇第一項(xiàng),一直enter


這樣你的整個(gè)工程也就完成了,接下來(lái)就是訪問(wèn)了


好了,今天就springMvc,mybatis搭建Java后臺(tái)的講解就告一段落了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中保證線程順序執(zhí)行的操作代碼

    Java中保證線程順序執(zhí)行的操作代碼

    本文給大家分享一篇教程關(guān)于java線程順序執(zhí)行問(wèn)題,如何保證線程的順序執(zhí)行呢?今天通過(guò)實(shí)例代碼給大家詳細(xì)講解下,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • 搜索一文入門(mén)ElasticSearch(節(jié)點(diǎn) 分片 CRUD 倒排索引 分詞)

    搜索一文入門(mén)ElasticSearch(節(jié)點(diǎn) 分片 CRUD 倒排索引 分詞)

    這篇文章主要為大家介紹了搜索一文入門(mén)ElasticSearch(節(jié)點(diǎn) 分片 CRUD 倒排索引 分詞)的基礎(chǔ)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Java設(shè)計(jì)模式之組合模式(Composite模式)介紹

    Java設(shè)計(jì)模式之組合模式(Composite模式)介紹

    這篇文章主要介紹了Java設(shè)計(jì)模式之組合模式(Composite模式)介紹,Composite定義:將對(duì)象以樹(shù)形結(jié)構(gòu)組織起來(lái),以達(dá)成“部分-整體” 的層次結(jié)構(gòu),使得客戶端對(duì)單個(gè)對(duì)象和組合對(duì)象的使用具有一致性,需要的朋友可以參考下
    2015-03-03
  • Shiro 控制并發(fā)登錄人數(shù)限制及登錄踢出的實(shí)現(xiàn)代碼

    Shiro 控制并發(fā)登錄人數(shù)限制及登錄踢出的實(shí)現(xiàn)代碼

    本文通過(guò)shiro實(shí)現(xiàn)一個(gè)賬號(hào)只能同時(shí)一個(gè)人使用,本文重點(diǎn)給大家分享Shiro 控制并發(fā)登錄人數(shù)限制及登錄踢出的實(shí)現(xiàn)代碼,需要的朋友參考下吧
    2017-09-09
  • 深入學(xué)習(xí)java中的Groovy 和 Scala 類(lèi)

    深入學(xué)習(xí)java中的Groovy 和 Scala 類(lèi)

    本文將探討三種下一代 JVM 語(yǔ)言:Groovy、Scala 和 Clojure,比較并對(duì)比新的功能和范例,讓 Java 開(kāi)發(fā)人員對(duì)自己近期的未來(lái)發(fā)展有大體的認(rèn)識(shí)。,需要的朋友可以參考下
    2019-06-06
  • SpringCloud Gateway實(shí)現(xiàn)限流功能詳解

    SpringCloud Gateway實(shí)現(xiàn)限流功能詳解

    SpringCloud Gateway 是 Spring Cloud 的一個(gè)全新項(xiàng)目,它旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單有效的統(tǒng)一的 API 路由管理方式。這篇文章主要介紹了SpringCloud Gateway實(shí)現(xiàn)限流,需要的朋友可以參考下
    2022-11-11
  • java9新特性Reactive?Stream響應(yīng)式編程?API

    java9新特性Reactive?Stream響應(yīng)式編程?API

    這篇文章主要為大家介紹了java9新特性響應(yīng)式編程API的特點(diǎn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • Java實(shí)現(xiàn)控制小數(shù)精度的方法

    Java實(shí)現(xiàn)控制小數(shù)精度的方法

    這篇文章主要介紹了Java實(shí)現(xiàn)控制小數(shù)精度的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java簡(jiǎn)單實(shí)現(xiàn)定時(shí)器

    Java簡(jiǎn)單實(shí)現(xiàn)定時(shí)器

    這篇文章主要為大家詳細(xì)介紹了Java簡(jiǎn)單實(shí)現(xiàn)定時(shí)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • JAVA中的FileWriter流解析

    JAVA中的FileWriter流解析

    這篇文章主要介紹了JAVA中的FileWriter流解析,FileWriter類(lèi)提供了多種寫(xiě)入字符的方法,包括寫(xiě)入單個(gè)字符、寫(xiě)入字符數(shù)組和寫(xiě)入字符串等,它還提供了一些其他的方法,如刷新緩沖區(qū)、關(guān)閉文件等,需要的朋友可以參考下
    2023-10-10

最新評(píng)論

新竹市| 即墨市| 攀枝花市| 宝山区| 尖扎县| 辉南县| 临夏市| 什邡市| 漳州市| 鸡泽县| 淮阳县| 会东县| 杭锦后旗| 当阳市| 曲靖市| 宜丰县| 武乡县| 新安县| 临泽县| 柳河县| 海门市| 成武县| 灵武市| 安吉县| 潜江市| 星子县| 普陀区| 泾川县| 山丹县| 曲麻莱县| 永平县| 巴塘县| 石泉县| 吴堡县| 白玉县| 龙泉市| 哈巴河县| 台东县| 邹平县| 宁河县| 祁阳县|