一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

2021-03-01 14:31eagle-zhang Java教程

本篇文章主要介紹了Maven構建自己的第一個Java后臺的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了maven構建自己的第一個java后臺的方法,分享給大家,具體如下:

1.知識后顧

關于如何運用maven構建自己的第一個項目,上期我已經詳細的講解過了,上篇鏈接;今天我以springmvc,mybatis框架搭建一個屬于你自己的java后臺。

2.必要準備

①intellij idea,maven環境搭好

②熟悉掌握mybatis,springmvc等框架

③mysql數據庫的創建

3.整體架構布局

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

4.具體步驟

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?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是使用編程的方式來進行測試,而junit4是使用注解的方式來運行junit-->
  <groupid>junit</groupid>
  <artifactid>junit</artifactid>
  <version>4.11</version>
  <scope>test</scope>
 </dependency>
 
 <!--補全項目依賴-->
 <!--1.日志 java日志有:slf4j,log4j,logback,common-logging
  slf4j:是規范/接口
  日志實現: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>
 <!--實現slf4j接口并整合-->
 <dependency>
  <groupid>ch.qos.logback</groupid>
  <artifactid>logback-classic</artifactid>
  <version>1.1.1</version>
 </dependency>
 
 
 <!--1.數據庫相關依賴-->
 <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自身實現的spring整合依賴-->
 <dependency>
  <groupid>org.mybatis</groupid>
  <artifactid>mybatis-spring</artifactid>
  <version>1.2.3</version>
 </dependency>
 
 <!--3.servlet web相關依賴-->
 <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相關依賴-->
 <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相關依賴-->
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-test</artifactid>
  <version>4.1.7.release</version>
 </dependency>
 </dependencies>
</project>

里面涵蓋了spring,mybatis等一系列jar包,這個過程類似android在build.gradle中添加第三方依賴,原理一致。

2.在resourc目錄下建立兩個目錄分別是:mapper,spring

 mapper:mapper是mybatis框架的映射,作用是映射文件在dao層用;這里我創建了一個user.xml映射:

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<!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語句配置
即針對dao接口中的方法編寫我們的sql語句-->
<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

貼代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
<?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 http://www.springframework.org/schema/context/spring-context.xsd">
 <!--配置整合mybatis過程
 1.配置數據庫相關參數-->
 <context:property-placeholder location="classpath:jdbc.properties"/>
 
 <!--2.數據庫連接池-->
 <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"/>
  <!--關閉連接后不自動commit-->
  <property name="autocommitonclose" value="false"/>
  <!--獲取連接超時時間-->
  <property name="checkouttimeout" value="10000"/>
  <!--當獲取連接失敗重試次數-->
  <property name="acquireretryattempts" value="2"/>
 </bean>
 <!--約定大于配置-->
 
 <!--3.配置sqlsessionfactory對象-->
 <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
  <!--往下才是mybatis和spring真正整合的配置-->
  <!--注入數據庫連接池-->
  <property name="datasource" ref="datasource"/>
  <!--配置mybatis全局配置文件:mybatis-config.xml-->
  <property name="configlocation" value="classpath:mybatis-config.xml"/>
  <!--掃描entity包,使用別名,多個用;隔開-->
  <property name="typealiasespackage" value="com.dajiu.bean"/>
  <!--掃描sql配置文件:mapper需要的xml文件-->
  <property name="mapperlocations" value="classpath:mapper/*.xml"/>
 </bean>
 
 <!--4:配置掃描dao接口包,動態實現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>

重視的地方:

連接數據庫:

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

配置全局的mybatis-config以及bean類,mapper下的所有文件

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

配置dao

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

2.spring-service.xml

貼代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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:tx="http://www.springframework.org/schema/tx"
  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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
 <!--掃描service包下所有使用注解的類型-->
 <context:component-scan base-package="com.dajiu.service"/>
 
 <!--配置事務管理器-->
 <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
  <!--注入數據庫連接池-->
  <property name="datasource" ref="datasource"/>
 </bean>
 <bean id="date" class="java.util.date"></bean>
 <!--配置基于注解的聲明式事務
 默認使用注解來管理事務行為-->
 <tx:annotation-driven transaction-manager="transactionmanager"/>
</beans>

重視地方:

配置service

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

 3.spring-web.xml

貼代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
<?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
  http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 <!--配置spring mvc-->
 <!--1,開啟springmvc注解模式
 a.自動注冊defaultannotationhandlermapping,annotationmethodhandleradapter
 b.默認提供一系列的功能:數據綁定,數字和日期的format@numberformat,@datetimeformat
 c:xml,json的默認讀寫支持-->
 <mvc:annotation-driven/>
 
 <!--2.靜態資源默認servlet配置-->
 <!--
  1).加入對靜態資源處理: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相關的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

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

5.邏輯實現(以user為例)

①首先在bean中 定義user類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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層

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

③接著在service中申明接口

?
1
2
3
4
5
6
7
8
9
10
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中具體實現接口邏輯

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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相當于新建一個實例

⑤在controller中實現真正的后臺調用邏輯

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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("")表示訪問的映射路徑,@responsebody表示請求結果以json數據格式打印出來,@controller表示只要訪問了上面的根映射路徑,就直接調用controller;

現在幫大家理理思路:先請求usercontroller---->userservice---->userserviceimpl---->userdao---->user.xml(mapper)---->bean(user)

6.配置tomcat服務器

①點擊右上角的綠色三角形按鈕,點擊edit configuration

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

②點擊+號,選擇tomcat

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

③選擇local

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

④填寫相關配置

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

⑤點擊deployment,點擊+號,選擇artifact

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

接著選擇第一項,一直enter

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

這樣你的整個工程也就完成了,接下來就是訪問了

【IntelliJ IDEA】Maven構建自己的第一個Java后臺的方法

好了,今天就springmvc,mybatis搭建java后臺的講解就告一段落了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/zhangxing52077/article/details/69664662

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本免费观看95视频网站 | 荡女淫春2古装 | 国产成人精品视频一区 | 日韩高清在线免费看 | 桃乃木香奈作品在线 | 俺去俺来也在线www色官网 | 97大香伊在人人线色 | 成人国产精品一区二区不卡 | 亚洲福利视频在线观看 | 精品视频一区二区三区免费 | 欧美成人日韩 | 99视频全部免费 | 无耻之徒第十一季在线观看 | 亚洲成A人片在线观看中文L | 美女的隐私视频免费看软件 | 色久天| 久久免费看少妇高潮A片JA | 青青青久久久 | 人与动人物aaaa | 久久中文字幕免费高清 | 国产精品3p视频 | chinesehdxxx吃奶水 | 欧美日韩久久中文字幕 | 四虎在线视频免费观看视频 | 欧美日韩国产在线一区 | 奇米影视777最新在线 | 亚洲国产成人精品激情 | 午夜理伦片免费 | 日韩风月片 | 久久re视频这里精品一本到99 | 妇女澡堂淋浴性 | 欧洲vodafonewifi日本 | 大吊操| 91在线高清视频 | 免费视频精品一区二区三区 | 男模chinesegayxxxx | 色综合九九 | 国产极品精频在线观看 | xxx黑人又大粗又长 xxxx性欧美极品另类 | 2019午夜福合集高清完整版 | h玉足嫩脚嗯啊白丝 |