我是靠谱客的博主 积极蜗牛,最近开发中收集的这篇文章主要介绍idea利用maven实现聚合项目, maven聚合实现登录逻辑,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

什么叫聚合项目
以前我们对项目是按模块划分,这种叫做纵向划分拆分项目,由一个人负责整个功能模块的视图、控制层、服务器层、持久化层以及数据库, 这就会导致我们各个功能模块相对很独立,不利于功能的扩展及交互, 所以有了另外一种拆分的方式,那就是横向拆分,按逻辑层次拆分项目模块,cotrller一层,view一层,dao一层,service一层,各层分别独立的编写。这样最大的好处就是未来可以扩展分布式系统。所以,这绝对是未来的趋势。

那我们就可以利用maven进行横向拆分模块(module), 下面是demo
这里写图片描述
1、新建一个maven project,必须是simple,这个项目是父项目,
这里写图片描述
这里写图片描述
设置名字:artifactid:代表工程名字 icar_parent表示父项目
这里写图片描述
剩下就是全部下一步,点击完成,,出现了项目的目录,其中src是无用的,可以删除,也可以 不删除
这里写图片描述

2、新建一个web项目作为我们的controller模块
注意,此时新建的module项目,不是projet
这里写图片描述
这里写图片描述
该项目取名icar_controller
这里写图片描述
剩下全部是下一步直到项目完成,

新建的项目不会有java和resources目录。所以我们需要手动创建和设置
新建两个Dictory 一个叫java,一个叫resources
这里写图片描述
分别设置为不同的文件形式:
这里写图片描述
这里写图片描述

这是我们先将我们的controller跑通,然后在去编写service和dao
pom.xml导入springmvc的依赖;

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>icar_parent</artifactId>
<groupId>comg.xingxue.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>icar_controller</artifactId>
<packaging>war</packaging>
<name>icar_controller Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet.jsp</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet.jsp.jstl</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>icar_controller</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

配置web.xml

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<!-- spring监听器-用于创建spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring-mvc请求分派器 -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

配置spring-mvc.xml文件, 注意,我们现在需要把该xml放入到resources目录下面,导包的时候该目录会自动打包到跟classes目录下面,idea这里只是方便我们管理,
这里写图片描述

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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 使用注解方式完成映射 -->
<!-- 让扫描spring扫描这个包下所有的类,让标注spring注解的类生效 -->
<context:component-scan base-package="com.xingxue"/>
<mvc:annotation-driven/><!-- 开启注解 -->
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/><!-- 前缀 -->
<property name="suffix" value=".jsp"></property> <!-- 后缀 -->
</bean>
</beans>

编写controller:在java目录下,新建包com.xingxue.controller,在新建一个类UserController

package com.xingxue.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by Administrator on 2018-09-07.
*/
@Controller
@RequestMapping("/user")
public class UserCotrolller {
@RequestMapping("/login.do")
public String login(String account, String pwd) {
System.out.println("========" + account);
return "OK";
}
}

然后项目配置完成,

利用mavne插件打包,成一个war文件,操作如下:
这里写图片描述
打包以后,mavne会自动生成一个target目录,然后将打包后的文件存入改目录,target默认在当前项目下,我们找到工作空间目录,查看
这里写图片描述

发布项目,将改war包拷贝到tomcat的webapps下面,然后启动tomcat
这里写图片描述
这里写图片描述

访问
利用浏览器输入:http://localhost:9998/icar_controller/user/login.do?account=111&pwd=222
得到如下结果
这里写图片描述
这里写图片描述
页面404是正常,因为没有ok.jsp页面,但是服务器输出了====111,证明controller已经正常运行

3、新建一个service的moudle
此项目未来是用于编写service,所以只有普通的java类代码,我们选择quickstart新建项目

这里写图片描述

项目名字取为icar_service
这里写图片描述

service项目如下:
这里写图片描述
打开service的pom.xml文件,然后在项目下面修改艾玛如下
这样,maven打包的时候,就会吧改moudle打包成一个jar文件,注意区别是web项目是打成war包


<artifactId>icar_service</artifactId>
<packaging>pom</packaging>

这里写图片描述

为service引入spring依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>icar_parent</artifactId>
<groupId>comg.xingxue.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>icar_controller</artifactId>
<packaging>war</packaging>
<name>icar_controller Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>comg.xingxue.parent</groupId>
<artifactId>icar_service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet.jsp</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet.jsp.jstl</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>icar_controller</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

新建UserService接口和UserServiceImpl类:并且编写功能

这里写图片描述

UserService代码:

package com.xingxue.service;
/**
* Created by Administrator on 2018-09-07.
*/
public interface UserService {
public String login();
}

UserServiceImpl代码

package com.xingxue.service.com.xingxue.service.impl;
import com.xingxue.service.UserService;
import org.springframework.stereotype.Service;
/**
* Created by Administrator on 2018-09-07.
*/
@Service("userService")
public class UserServiceImpl
implements UserService{
public String login() {
return "ABC";
}
}

最后一步,在icar_controller里面依赖icar_service
这里写图片描述


<dependency>
<groupId>comg.xingxue.parent</groupId>
<artifactId>icar_service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

现在UserController就可以访问UserService接口了

这里写图片描述

package com.xingxue.controller;
import com.xingxue.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by Administrator on 2018-09-07.
*/
@Controller
@RequestMapping("/user")
public class UserCotrolller {
@Autowired
UserService userService;
@RequestMapping("/login.do")
public String login(String account, String pwd) {
System.out.println("========" + account);
String info = this.userService.login();
System.out.println("+++++++++"+info);
return "OK";
}
}

在重新打包发布项目,并且重复访问该接口,发现service被正常调用
这里写图片描述

controller和service聚合已经完成!

此时我们到父工程去看pom.xml文件,发现里面多了一个配置项:
这里写图片描述
这个就表示,controller和service被聚合在一个项目了

3、新建dao层的module
和service新建方式基本一样
这里写图片描述
这里写图片描述

修改dao的打包方式:
这里写图片描述

新建好了以后会发现,父工程同样聚合了dao的module
这里写图片描述

在service中先依赖改dao的module
这里写图片描述

编写dao的逻辑:
导入mybatis的依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>icar_parent</artifactId>
<groupId>comg.xingxue.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>icar_dao</artifactId>
<packaging>pom</packaging>
<name>icar_dao</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- mybatis相关包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!-- mybatis/spring连接包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 导入alibaba的jar包,现在基本都用这个管理,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

注意:此处导入的依赖在controller工程上也需要重复导入,同时在controller里面新建spring-core.xml 主要配置mybatis、事务、数据库连接池:
这里写图片描述

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
<!-- spring开启注解 @ -->
<context:component-scan base-package="com.xingxue">
</context:component-scan>
<!-- 阿里巴巴druid配置 -->
<bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/xingxue" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 整合mybatis -->
<!-- 完美整合spring和MyBatis,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource1"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath*:mapper/*.xml"></property>
</bean>
<!-- 配置扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描me.gacl.dao这个包以及它的子包下的所有映射接口类 -->
<property name="basePackage" value="comg.xingxue.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 配置Spring的事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource1" />
</bean>
<!-- 注解方式配置事物 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
<!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 如果已经又事务就沿用当前事务,如果没有事务就开启一个新事务来控制 -->
<tx:method name="deleteByPrimaryKey" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" />
<tx:method name="xiugai*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.xingxue.service.impl.*.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>
<!-- 配置druid监控spring jdbc -->
<bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
</bean>
<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
<property name="patterns">
<list>
<value>com.xingxue.service.*</value>
</list>
</property>
</bean>
</beans>

到此,配置全部完成,我们编写dao里面的实体类和xml文件
UserDao接口如下:

package comg.xingxue.mapper;
import java.util.HashMap;
/**
* Created by Administrator on 2018-09-07.
*/
public interface UserDao {
public HashMap<String, Object>
login(HashMap<String, Object> param);
}

userMapper.xml如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="comg.xingxue.mapper.UserDao" >
<select id="login" resultType="java.util.HashMap" parameterType="java.util.HashMap">
select * from employee where account = #{account} and pwd =#{pwd}
</select>
</mapper>

userService代码如下:

package com.xingxue.service.impl;
import com.xingxue.service.UserService;
import comg.xingxue.mapper.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
/**
* Created by Administrator on 2018-09-07.
*/
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
UserDao userDao;
public HashMap<String, Object> login(HashMap<String, Object> param) {
return this.userDao.login(param);
}
}

userController代码如下:

package com.xingxue.controller;
import com.xingxue.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.HashMap;
/**
* Created by Administrator on 2018-09-07.
*/
@Controller
@RequestMapping("/user")
public class UserCotrolller {
@Autowired
UserService userService;
@RequestMapping("/login.do")
public String login(String account, String pwd) {
HashMap<String, Object> param = new HashMap<String, Object>();
param.put("account" , account);
param.put("pwd", pwd);
HashMap<String, Object> loginUser = this.userService.login(param);
System.out.println(loginUser);
return "OK";
}
}

目录结构如下:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

然后打包运行:
这里写图片描述
这里写图片描述

此处打包注意一下:有可能maven需要一个插件,否则无法打包我们的mybatis的xml文件,建议手动加上该插件之后在打包

 <plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-xmls</id>
<phase>process-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

导包后xml文件应该存放在我们的dao的jar包下,
这里写图片描述

访问后,正常输出信息,一个聚合实现的登录功能及完成
这里写图片描述

最后

以上就是积极蜗牛为你收集整理的idea利用maven实现聚合项目, maven聚合实现登录逻辑的全部内容,希望文章能够帮你解决idea利用maven实现聚合项目, maven聚合实现登录逻辑所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(29)

评论列表共有 0 条评论

立即
投稿
返回
顶部