【IntelliJ IDEA】Maven构建自己的第一个Java后台的方法
本文介绍了maven构建自己的第一个java后台的方法,分享给大家,具体如下:
1.知识后顾
关于如何运用maven构建自己的第一个项目,上期我已经详细的讲解过了,上篇链接;今天我以springmvc,mybatis框架搭建一个属于你自己的java后台。
2.必要准备
①intellij idea,maven环境搭好
②熟悉掌握mybatis,springmvc等框架
③mysql数据库的创建
3.整体架构布局
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是使用编程的方式来进行测试,而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映射:
其中红色部分是要引起重视的,最上面的是映射dao层的路径,第二个是返回对象的类型,这里我还是把代码贴出来:
<!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
贴代码:
<?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>
重视的地方:
连接数据库:
配置全局的mybatis-config以及bean类,mapper下的所有文件
配置dao
2.spring-service.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: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
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 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
5.逻辑实现(以user为例)
①首先在bean中 定义user类
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中具体实现接口逻辑
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中实现真正的后台调用逻辑
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
②点击+号,选择tomcat
③选择local
④填写相关配置
⑤点击deployment,点击+号,选择artifact
接着选择第一项,一直enter
这样你的整个工程也就完成了,接下来就是访问了
好了,今天就springmvc,mybatis搭建java后台的讲解就告一段落了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。