您好,登录后才能下订单哦!
本文介绍了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后台的讲解就告一段落了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。