导入依赖
将所有的依赖都导入到父工程当中,供所有的子工程使用;
<dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <!--引入需要的ehcache插件--> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>1.2.3</version> </dependency> <!--mybatis整合ehcache的jar--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.0.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.8</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies>
接口及公共模块
该模块是服务端和消费者端都需要用到的公共模块,包括服务接口以及接口所依赖的实体类,当然其他的公共资源也可以放到这个模块中,公共模块的目录结构如下:
实体类
package com.dubbo.entity; import java.io.Serializable; import java.util.Date; public class Dev_user implements Serializable{ private Integer uid; private String devcode; private String devname; private String devpassword; private String devemail; private String devinfo; private Integer createdby; private Date creationdate; private Integer modifyby; private Date modifydate; public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getDevcode() { return devcode; } public void setDevcode(String devcode) { this.devcode = devcode; } public String getDevname() { return devname; } public void setDevname(String devname) { this.devname = devname; } public String getDevpassword() { return devpassword; } public void setDevpassword(String devpassword) { this.devpassword = devpassword; } public String getDevemail() { return devemail; } public void setDevemail(String devemail) { this.devemail = devemail; } public String getDevinfo() { return devinfo; } public void setDevinfo(String devinfo) { this.devinfo = devinfo; } public Integer getCreatedby() { return createdby; } public void setCreatedby(Integer createdby) { this.createdby = createdby; } public Date getCreationdate() { return creationdate; } public void setCreationdate(Date creationdate) { this.creationdate = creationdate; } public Integer getModifyby() { return modifyby; } public void setModifyby(Integer modifyby) { this.modifyby = modifyby; } public Date getModifydate() { return modifydate; } public void setModifydate(Date modifydate) { this.modifydate = modifydate; } public Dev_user(Integer uid, String devcode, String devname, String devpassword, String devemail, String devinfo, Integer createdby, Date creationdate, Integer modifyby, Date modifydate) { this.uid = uid; this.devcode = devcode; this.devname = devname; this.devpassword = devpassword; this.devemail = devemail; this.devinfo = devinfo; this.createdby = createdby; this.creationdate = creationdate; this.modifyby = modifyby; this.modifydate = modifydate; } public Dev_user(){ } }
service接口
public interface UserService { //用戶登陸 public Dev_user login(String devCode, String devPassword); }
生产者
导入依赖
<dependencies>
<!--公共模块导入的依赖--> <dependency> <groupId>com.dubbo</groupId> <artifactId>dubbo_service01_02</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies>
dao接口层
@Repository("userDao") public interface UserDao { //用戶登陸 public Dev_user login(@Param("devCode") String devCode, @Param("devPassword") String devPassword); }
dao.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="com.dubbo02.dao.UserDao"> <!--登录--> <select id="login" resultType="com.dubbo.entity.Dev_user"> SELECT * FROM dev_user WHERE devcode=#{devCode} AND devpassword=#{devPassword} </select> </mapper>
serviceImpl实现层
在pom.xml文件中带入了接口及公共模块的依赖,所以这里就不需要编写service接口层和实现类
@Service("userService") public class UserServiceImpl implements UserService { //植入dao层对象 @Resource(name = "userDao") private UserDao dao; @Override public Dev_user login(String devCode, String devPassword) { return dao.login(devCode,devPassword); } }
jdbc.properties文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///appinfodb jdbc.username=root jdbc.password=123
applicationCotext.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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<!--扫描注解--> <context:component-scan base-package="com.dubbo02"/>
<!--添加MVC支持--> <mvc:annotation-driven/> <!--加载配置文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--SqlSessionFactory--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!--加载数据源--> <property name="dataSource" ref="dataSource"/> <!--加载Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--扫描Dao层--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dubbo02.dao"/> </bean> <!--开启事务注解支持--> <tx:annotation-driven/> <!--事务管理器--> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--服务器的应用名称--> <dubbo:application name="dubbo02_zook"/> <!--需要扫描的服务所在的包--> <dubbo:annotation package="com.dubbo02.service"/> <!--指定的注册中心--> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!--用户中心暴露的端口服务--> <dubbo:protocol name="dubbo" prompt="20882"/> <!--声明需要暴露的服务接口--> <dubbo:service interface="com.dubbo.service.UserService" ref="userService" /> </beans>
mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!--根节点--> <configuration> <settings> <!--log4j配置引入--> <setting name="logImpl" value="LOG4J"/> <!--全局映射--> <setting name="autoMappingBehavior" value="FULL"/> </settings> <!--mappers 代表小配置集合--> <mappers> <!--<mapper resource="org/mybatis/example/BlogMapper.xml"/>--> <package name="com.dubbo02.dao"/> </mappers> </configuration>
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> <display-name>Archetype Created Web Application</display-name> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!--初始化参数--> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <!--强制使用UTF-8编码--> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> <!--中央调度器--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationCotext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
配置Tomcat
启动生产者即可;
消费者
导入依赖
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>com.dubbo</groupId> <artifactId>dubbo_service01_02</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies>
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> <display-name>Archetype Created Web Application</display-name> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!--初始化参数--> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <!--强制使用UTF-8编码--> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> <!--中央调度器--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 设置欢迎页面 --> <welcome-file-list> <welcome-file>jsp/devlogin.jsp</welcome-file> </welcome-file-list> </web-app>
applicationContext.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:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--名称--> <dubbo:application name="dubbo01"/> <!--需要注解--> <dubbo:annotation package="com.dubbo01.controller"/> <!--指定的注册中心--> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!--service--> <dubbo:reference interface="com.dubbo.service.UserService" id="userService"/> <!--扫描注解--> <context:component-scan base-package="com.dubbo01"/> <!--添加MVC支持--> <mvc:annotation-driven/> <!--开启事务注解支持--> <tx:annotation-driven/> <!--释放静态资源--> <mvc:default-servlet-handler/> <!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
UserController控制层
@Controller @RequestMapping("/user") public class UserController { @Resource private UserService userService; @RequestMapping("/login") public ModelAndView login(Dev_user user, ModelAndView mv, HttpServletRequest request){ System.out.println("dfdfdfdf"); Dev_user login = userService.login(user.getDevcode(),user.getDevpassword()); System.out.println(login); if (login!=null){ request.getSession().setAttribute("login",login); System.out.println("登录成功"); mv.setViewName("main"); }else{ System.out.println("失败"); mv.setViewName("devlogin"); } return mv; } }
devlogin.jsp登录页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <!-- Meta, title, CSS, favicons, etc. --> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>APP开发者平台</title> <!-- Bootstrap --> <link href="${pageContext.request.contextPath }/statics/css/bootstrap.min.css" rel="stylesheet"> <!-- Font Awesome --> <link href="${pageContext.request.contextPath }/statics/css/font-awesome.min.css" rel="stylesheet"> <!-- NProgress --> <link href="${pageContext.request.contextPath }/statics/css/nprogress.css" rel="stylesheet"> <!-- Animate.css --> <%--<link href="https://colorlib.com/polygon/gentelella/css/animate.min.css" rel="stylesheet">--%> <!-- Custom Theme Style --> <link href="${pageContext.request.contextPath }/statics/css/custom.min.css" rel="stylesheet"> </head> <body class="login"> <div> <a class="hiddenanchor" id="signup"></a> <a class="hiddenanchor" id="signin"></a> <div class="login_wrapper"> <div class="animate form login_form"> <section class="login_content"> <form action="${pageContext.request.contextPath }/user/login" method="post"> <h1>APP开发者平台</h1> <div> <input type="text" class="form-control" id="username" name="devcode" placeholder="请输入用户名" required="" /> </div> <div> <input type="password" class="form-control" id="password" name="devpassword" placeholder="请输入密码" required="" /> </div> <span>${error }</span> <div> <button type="submit" id="submit" onclick="check()" class="btn btn-success">登 录</button> <button type="reset" class="btn btn-default">重 填</button> </div> <div class="clearfix"></div> <div class="separator"> <div> <p>©2016 All Rights Reserved. </p> </div> </div> </form> </section> </div> </div> </div> </body> </html>
main.jsp首页
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <div class="page-title"> <div class="title_left"> <h3> 欢迎你:${login.devname }<strong> | 角色:开发者账户</strong> </h3> </div> </div> <div class="clearfix"></div>
配置Tomcat启动
测试结果