Complete example of Dubbo + zookeeper + jsp + spring MVC + Spring + mybatis + MySQL + maven

Keywords: MySQL Dubbo Spring Maven Mybatis

The project is divided into three parts, which are divided into three Maven projects (web-based, so the last one created for maven)

1. Interface Definition and Entity Class Definition (api+pojo) - - maven creates a java project and packages it into jar

2.dubbo's service provider: Define the interface implementation. The bottom layer uses mybatis database to configure mapper file and dubbo-provider's configuration file here. There are also files integrated with spring and mybatis - - maven creates java project and packages it into jar

3.dubbo's service consumers: Define the controller class, Jsp page, configure springmvc's configuration file, web.xml file, dubbo-consumer's configuration file - - the web project created by maven, packaged into a war package

4.zookeeper: dubbo's registry (download by yourself, find zkServer.cmd in bin directory and double-click open)

--------------------------------------------------------------------------------------------------------

(1) The ************************************************************************** database (MYSQL)*****************************************

Create database dubbo: create dababase dubbo

Create the user table in the dubbo database with the following fields:

************************************************************************ database (end)*****************************************

(2) Interface design of ***************************************************************** created by maven (packaged into jar)********************************************************************

Eclipse uses maven tools to create java projects with the following structure

The pom.xml configuration for the interface maven project is as follows

<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">
  <modelVersion>4.0.0</modelVersion>

    <groupId>com.dfx.dubbo</groupId>
    <artifactId>demo_interface</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>demo_interface</name>
    <url>http://maven.apache.org</url>
  
    <build>
        <finalName>demo_interface</finalName>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${compiler.source}</source>
                    <target>${compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <!-- Source package
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            -->
        </plugins>
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <compiler.source>1.7</compiler.source>
        <compiler.target>1.7</compiler.target>
        <junit.version>4.12</junit.version>
          <!-- spring version number -->
        <spring.version>4.0.2.RELEASE</spring.version>
        <!-- mybatis version number -->
        <mybatis.version>3.2.6</mybatis.version>
        <!-- log4j Log File Management Package Version -->
        <slf4j.version>1.7.7</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        
         <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
            <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.12</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.12</version>
        </dependency>    
         <dependency>  
                <groupId>org.javassist</groupId>  
                <artifactId>javassist</artifactId>  
                <version>3.18.1-GA</version>  
           </dependency> 
        <dependency>
              <groupId>org.jboss.netty</groupId>
              <artifactId>netty</artifactId>
              <version>3.2.5.Final</version>
        </dependency>
        <dependency>
              <groupId>org.apache.zookeeper</groupId>
              <artifactId>zookeeper</artifactId>
              <version>3.4.6</version>
        </dependency>    
        <!-- mybatis Core package -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>  
            </dependency>
            <!-- Log File Management Package -->
        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
            <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- log end -->      
    </dependencies>        
</project>

 

The interface class UserIService.java is as follows

package com.dfx.dubbo.demo_interface.api;

import com.dfx.dubbo.demo_interface.pojo.User;

/**
 * Business Method Interface Design
 * @author Administrator
 *
 */
public interface UserIService {

    public void add(User user);
    
    public void edit(User user);
    
    public User findById(int id);
    
    public void delete(int id);
}

 

The entity class User.java is as follows

package com.dfx.dubbo.demo_interface.pojo;

import java.io.Serializable;

/**
 * Entity class
 * @author Administrator
 *
 */
public class User implements Serializable{
    
    private int id;
    private String username;
    private String password;
    private int age;
    
    public User(){}

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
    
}

 

************************************interface (end) *************************************************

(3) Provider (java project created by maven, packaged into jar)********************************************************************************************************

The project directory structure of the service provider is as follows

The pom.xml of the service provider is as follows

<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">
  <modelVersion>4.0.0</modelVersion>

    <groupId>com.dfx.dubbo</groupId>
    <artifactId>demo_provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>demo_provider</name>
    <url>http://maven.apache.org</url>
  
    <build>
        <finalName>demo_provider</finalName>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${compiler.source}</source>
                    <target>${compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <!-- Source package
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            -->
        </plugins>
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <compiler.source>1.7</compiler.source>
        <compiler.target>1.7</compiler.target>
        <junit.version>4.12</junit.version>
          <!-- spring version number -->
        <spring.version>4.0.2.RELEASE</spring.version>
        <!-- mybatis version number -->
        <mybatis.version>3.2.6</mybatis.version>
        <!-- log4j Log File Management Package Version -->
        <slf4j.version>1.7.7</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        
        <!-- Introducing Interface Dependency -->
        <dependency>
            <groupId>com.dfx.dubbo</groupId>
            <artifactId>demo_interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
          <!-- mybatis/spring package -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- Import java ee jar package -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <!-- Import Mysql Database Links jar package -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <!-- Import dbcp Of jar Packet, used in the uuuuuuuuuuu applicationContext.xml Configure database in -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- JSTL Tag class -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    
        <!-- Format objects to facilitate log output -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.41</version>
        </dependency>

        <!-- Reflect JSON -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <!-- Upload component packages -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>    
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.4.10</version>
             <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.4</version>
    </dependency>
    </dependencies>        
</project>

The persistence interface UserMapper.java is designed as follows

 1 package com.dfx.dubbo.demo_provider.mapper;
 2 
 3 import org.mybatis.spring.annotation.MapperScan;
 4 
 5 import com.dfx.dubbo.demo_interface.pojo.User;
 6 
 7 @MapperScan
 8 public interface UserMapper {
 9 
10     public void add(User user);
11     
12     public void edit(User user);
13     
14     public User findById(int id);
15     
16     public void delete(int id);
17 }

The Mapper configuration file UserMapper.xml for the persistence layer interface is as follows

<?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.dfx.dubbo.demo_provider.mapper.UserMapper">

    <!-- Result Set Definition
    <resultMap id="userMap" type="com.dfx.dubbo.demo_provider.mapper.UserMapper">
        <id column="id" property="id" />
        <result column="username" property="username" />
        <result column="password" property="password" />
        <result column="age" property="age" />
    </resultMap>
     -->

    <!-- New operation -->
    <insert id="add" parameterType="com.dfx.dubbo.demo_interface.pojo.User" >
        insert into user(username,password,age) values(#{username},#{password},#{age})
    </insert>

    <!-- Modify operation -->
    <update id="edit" parameterType="com.dfx.dubbo.demo_interface.pojo.User">
        update user set username=#{username},password=#{password},age=#{age} where id=#{id}
    </update>
    
    <!-- Delete operation -->
    <delete id="delete" parameterType="int">
        delete  from user where id=#{0}
    </delete>
    
    <!-- Query operation -->
    <select id="findById" parameterType="int" resultType="com.dfx.dubbo.demo_interface.pojo.User">
        select id,username,password,age from user where id=#{0}
    </select>
    
    </mapper>

The business implementation class UserService.java is as follows

package com.dfx.dubbo.demo_provider.service.impl;

import javax.annotation.Resource;

import com.dfx.dubbo.demo_interface.api.UserIService;
import com.dfx.dubbo.demo_interface.pojo.User;
import com.dfx.dubbo.demo_provider.mapper.UserMapper;

/**
 * Business Layer Interface Method Implementation Class
 * @author Administrator
 *
 */
public class UserService implements UserIService {

    @Resource
    private UserMapper userDao;
    
    @Override
    public void add(User user) {
        userDao.add(user);
    }

    @Override
    public void edit(User user) {
        userDao.edit(user);
    }

    @Override
    public User findById(int id) {
        return userDao.findById(id);
    }

    @Override
    public void delete(int id) {
        userDao.delete(id);
    }

}

The main class Provider.java for the service provider to open the service is as follows

package com.dfx.dubbo.demo_provider.main;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * Open the service main class
 * 
 * */
public class Provider {
    static int count=0;
    public static void main(String[] args) throws IOException{
        count++;
        System.out.println("123 At first, count="+count+",Implemented--"+count+"second");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContex_provider.xml"});
        context.start();
        System.in.read();
        
    }
}

Mybatis configuration file 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>
        <! - This configuration enables global mappers to enable or disable caching - >
        <setting name="cacheEnabled" value="true" />
        <! - Enable or disable lazy loading globally. When disabled, all associated objects are loaded immediately - >.
        <setting name="lazyLoadingEnabled" value="true" />
        <! - Allows or disallows multiple result sets to be returned from a single statement (requiring appropriate drivers)-->
        <setting name="multipleResultSetsEnabled" value="true" />
        <! -- Use column labels instead of column names. Different drivers behave differently in this convenience. Reference to Driver Documentation or Full Testing to Determine the Driver to Use
        <setting name="useColumnLabel" value="true" />
        <! -- Allows JDBC to support generated keys. A suitable driver is needed. If set to true, this setting forces the generated keys to be used, which are still valid even though some drivers refuse to be compatible (e.g. Derby)-->
        <setting name="useGeneratedKeys" value="false" />
        <! -- Configure the defau lt executor. There is nothing special about the SIMPLE executor. REUSE executor reuses preprocessed statements. BATCH Executor Reuse Statements and Batch Updates - >
        <setting name="defaultExecutorType" value="SIMPLE" />
        <! - Sets the timeout time, which determines the time the driver waits for a database response - >.
        <setting name="defaultStatementTimeout" value="100" />
        <setting name="safeRowBoundsEnabled" value="false" />
        <setting name="mapUnderscoreToCamelCase" value="false" />
        <setting name="localCacheScope" value="SESSION" />
        <setting name="jdbcTypeForNull" value="OTHER" />
        <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />
    </settings>
</configuration>

dubbo service provider configuration file provider.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:dubbo="http://code.alibabatech.com/schema/dubbo"
    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://code.alibabatech.com/schema/dubbo
         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
         
    <!-- Specific implementation bean -->
    <bean id="userService" class="com.dfx.dubbo.demo_provider.service.impl.UserService"></bean>
    <dubbo:application name="user-provider"  />
     <!-- Use zookeeper Radio Registry Exposure Service Address -->
    <dubbo:registry address="localhost:2181" protocol="zookeeper"/>
    <!-- use dubbo Protocol Exposure Service at Port 20880 -->
    <dubbo:protocol name="dubbo" port="20880" id="mydubbo"/>

    <dubbo:service interface="com.dfx.dubbo.demo_interface.api.UserIService" ref="userService"></dubbo:service>

</beans>

spring and mybatis integrated configuration file applicationContex_provider.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:aop="http://www.springframework.org/schema/aop"
    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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- Automatic scanning -->
    <context:component-scan base-package="com.dfx.dubbo.demo_provider"></context:component-scan>
   
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/dubbo" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <!-- spring and MyBatis Perfect integration, no need mybatis Configuration mapping file -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- Automatic scanning mapping.xml file -->
        <property name="mapperLocations" value="classpath:com/dfx/dubbo/demo_provider/mapper/UserMapper.xml"/>
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>


    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
       <property name="sqlSessionFactory" ref="sqlSessionFactory"/>  
       <property name="basePackage" value="com.dfx.dubbo.demo_provider.mapper; "/>  
     </bean>  


    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="close" scope="prototype">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

    <!-- (transaction management)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <import resource="classpath:provider.xml"/>
</beans>

Here you can download zookeeper, open the folder, go to the bin directory, double-click zkServer.cmd to start (keep it in the boot state, you can put it aside)

First, package the interface project demo-interface into a jar package (packaging: Eclipse tool right-click the project maven install)

Then package the service provider project demo_provider as a jar package (packaging: Eclipse tool right-click the project maven install)

Find Provider.java for the demo_provider project and execute it in Java Application as follows

See here, it shows that the previous configuration is all right.

************************************privoder (end) *************************************************

(4)***************************************** dubbo Service Consumer (web project created by maven, packaged into jwar)************************************

The demo_consumer project architecture is as follows

 

Consumer's pom.xml

<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>
    <groupId>com.dfx.dubbo</groupId>
    <artifactId>demo_consumer</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo_consumer Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <build>
        <finalName>demo_consumer</finalName>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>${compiler.source}</source>
                    <target>${compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <compilerArguments>
                        <extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <!-- Source package
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            -->
        </plugins>
    </build>
    
        <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <compiler.source>1.7</compiler.source>
        <compiler.target>1.7</compiler.target>

        <!-- servlet/jsp/EL (2.4/2.0/?)(2.5/2.1/2.1),(3.0/2.2/2.2),(3.1/2.3/3.0) -->
        <servlet.version>3.1.0</servlet.version>
        <jsp.version>2.3.1</jsp.version>
        <jstl.version>1.2</jstl.version>
        <junit.version>4.12</junit.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>${jsp.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        
        <!-- Introducing Interface Dependency -->
        <dependency>
            <groupId>com.dfx.dubbo</groupId>
            <artifactId>demo_interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.dfx.dubbo</groupId>
            <artifactId>demo_provider</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
    </dependencies>
    
</project>

Controller class UserController.java

package com.dfx.dubbo.demo_consumer.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dfx.dubbo.demo_interface.api.UserIService;
import com.dfx.dubbo.demo_interface.pojo.User;

/**
 * Front-end control class
 * @author Administrator
 *
 */
@Controller
public class UserController {
    
    @Resource
    private UserIService userService;
    
    @RequestMapping("/add")
    public String add(User user){
        userService.add(user);
        return "main";
    }
    
    @RequestMapping("/delete")
    public String delete(int id){
        userService.delete(id);
        return "main";
    }
    
    @RequestMapping("/edit")
    public String edit(User user){
        userService.edit(user);
        return "main";
    }
    
    @RequestMapping("/find")
    public String find(int id,HttpServletRequest request){
        User user = userService.findById(id);
        System.out.println(user+"--"+user.getPassword()+user.getUsername()+user.getAge());
        request.setAttribute("  user", user);
        return "main";
    }
    
}

Configuration file dubbo consumer configuration. 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:dubbo="http://code.alibabatech.com/schema/dubbo"
    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://code.alibabatech.com/schema/dubbo
         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
    <dubbo:application name="user-consumer"  />
 
    <dubbo:registry address="zookeeper://localhost:2181" protocol="zookeeper"/>

    <dubbo:reference interface="com.dfx.dubbo.demo_interface.api.UserIService" id="userService" check="false" timeout="10000"/>
    
</beans>

Configuration application Context_consumer.xml for the configuration file Springmvc

<?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:p="http://www.springframework.org/schema/p"
    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">

    <!-- Scanning the package automatically SpringMVC Think the package is used.@controller The annotated class is the controller -->
    <context:component-scan base-package="com.dfx.dubbo.demo_consumer" />
    
    <!-- Define the prefix and suffix of the jumped file, view mode configuration-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- The configuration here is automatic to the back. action Method return The string with prefix and suffix becomes available url address -->
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <import resource="classpath:consumer.xml" />
</beans>

Configuration file web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="demo_consumer" version="3.1">
    
    <display-name>demo_consumer</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:applicationContext_consumer.xml</param-value>  
    </context-param>  
    <!-- Coding filter -->  
    <filter>  
        <filter-name>encodingFilter</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>  
    </filter>  
    <filter-mapping>  
        <filter-name>encodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
    <!-- Spring Monitor -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    <!-- Prevent Spring Memory overflow listener -->  
    <listener>  
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
    </listener>  
  
    <!-- Spring MVC servlet -->  
    <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_consumer.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>SpringMVC</servlet-name>  
        <!-- Here you can configure it to*.do,Corresponding struts Suffix Habits -->  
        <url-pattern>*.do</url-pattern>  
    </servlet-mapping> 
</web-app>

jsp page index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>

</head>
<body>
    <h2>Hello World! index</h2>
    <a href="add.do?username='admin'&password='123'&age=22">Add to</a><br>
    <a href="edit.do?id=15&username='user15'&password='123'&age=20">modify</a><br>
    <a href="delete.do?id=2">delete</a><br>
    <a href="find.do?id=15">query</a><br>
</body>
</html>

JSP page main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<result page><hr>

<c:if test="${user != null }" >
    id:${user.id }<br>
    username:${user.username }<br>
    password:${user.password }<br>
    age:${user.age }<br>
</c:if>
</body>
</html>

Configuration is over, and then deploy the project to Tomcat

First package the project as war, right-click the project run as - > Maven install with Eclipse development tool

Running Project: Right-click Project --> Run on Server (premise of successful execution: zkServer.cmd is open, Provider.java is running, demo_interface,demo_provider are both packaged as jar using maven command)

Results of running the project

Click on the results of the hyperlink query (the rest of the additions, modifications, deletions are the same)

Over (The whole play is over)

************************************consumer (end) *************************************************</p

Posted by mydimension on Sun, 16 Dec 2018 03:03:04 -0800