Dubbox+SSM分散式框架搭建與測試
Zookeeper的安裝這裡就不寫了,可以百度一下(文末會附上工程原始碼)
目錄
一 工程結構
工程說明:
demo-parent 為聚合工程
demo-dao 為資料庫訪問層
demo-pojo 為實體類
demo-xxx-interface 為xxx服務層的介面
demo-xxx-service 為xxx服務層的實現
demo-xxx-web 為web工程
後面可以根據自己的業務需求新增一些服務層和web層,比如有訂單模組,那就可以新增
demo-order-interface,demo-order-service,demo-order-web 三個模組
二 框架搭建
2.1 父工程
建立 Maven 工程 demo-parent ,groupId 為 cn.demo ,artifactId 為demo-parent , 在 pom.xml 引入下面的依賴,下面的模組都繼承自父工程
<!-- 集中定義依賴版本號 --> <properties> <junit.version>4.12</junit.version> <spring.version>4.2.4.RELEASE</spring.version> <pagehelper.version>4.0.0</pagehelper.version> <servlet-api.version>2.5</servlet-api.version> <dubbo.version>2.8.4</dubbo.version> <zookeeper.version>3.4.7</zookeeper.version> <zkclient.version>0.1</zkclient.version> <mybatis.version>3.2.8</mybatis.version> <mybatis.spring.version>1.2.2</mybatis.spring.version> <mybatis.paginator.version>1.2.15</mybatis.paginator.version> <mysql.version>5.1.32</mysql.version> <druid.version>1.0.9</druid.version> </properties> <dependencyManagement> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</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-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</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> <!-- dubbo相關 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <!--zookeeper--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>${zkclient.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.11.0.GA</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <dependency> <groupId>com.github.miemiedev</groupId> <artifactId>mybatis-paginator</artifactId> <version>${mybatis.paginator.version}</version> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- 連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!-- java編譯外掛 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
2.2 實體類模組
在demo-parent模組下建立實體類模組demo-pojo,並在java資料夾下建立包,包名為com.demo.pojo,包下的TbUser和TbUserExample會在2.7節用逆向工程講解如何生成
2.3 資料訪問模組
這裡有幾個檔案要配置下1.pom.xml 2 db.properties 3 SqlMapConfig.xml 4 applicationContext-dao.xml
1 pom.xml
<dependencies> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency> <dependency> <groupId>com.github.miemiedev</groupId> <artifactId>mybatis-paginator</artifactId> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <!--引入實體類--> <dependency> <groupId>cn.demo</groupId> <artifactId>demo-pojo</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
2 db.properties
這裡配置了一些資料庫連線地址,埠號,以及指定的資料庫,還有資料連線的賬號和密碼
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123
3 SqlMapConfig.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>
<plugins>
<!-- com.github.pagehelper 為 PageHelper 類所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 設定資料庫型別 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六種資料庫-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
4 applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 資料庫連線池 -->
<!-- 載入配置檔案 -->
<context:property-placeholder location="classpath*:properties/*.properties"/>
<!-- 資料庫連線池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}"/><!--資料庫連線地址,埠號,資料庫名-->
<property name="username" value="${jdbc.username}"/><!--資料庫使用者名稱-->
<property name="password" value="${jdbc.password}"/><!--資料庫密碼-->
<property name="driverClassName" value="${jdbc.driver}"/><!--資料庫驅動-->
<property name="maxActive" value="10"/>
<property name="minIdle" value="5"/>
</bean>
<!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 資料庫連線池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 載入mybatis的全域性配置檔案,可以自定義配置其他的檔案 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
</bean>
<!--mapper檔案掃描的配置-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.demo.mapper"/>
</bean>
</beans>
2.4 介面模組
pom.xml檔案引入下面依賴
<dependencies>
<dependency>
<groupId>cn.demo</groupId>
<artifactId>demo-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
2.5 服務模組
需要修改的檔案1 pom.xml 2 web.xml 3 applicationContext-service.xml 4 applicationContext-tx.xml
1 pom.xml引入依賴
<packaging>war</packaging>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- dubbo相關 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!--動態類庫,與jdk自帶的反射功能類似,但比反射功能更強大-->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</dependency>
<!--zookepper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<!--user介面-->
<dependency>
<groupId>cn.demo</groupId>
<artifactId>demo-user-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--dao層-->
<dependency>
<groupId>cn.demo</groupId>
<artifactId>demo-dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 指定埠 -->
<port>9001</port>
<!-- 請求路徑 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
2 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- 載入spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--這裡的*表示:會將本地的以及依賴進來dao層配置檔案一起載入到spring容器-->
<param-value>classpath*:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3 applicationContext-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: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://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--指定服務的埠號-->
<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
<!--註冊的名稱-->
<dubbo:application name="demo-user-service"/>
<!--zookepper的註冊地址和埠-->
<dubbo:registry address="zookeeper://192.168.25.128:2181"/>
<!--配置註解掃描包-->
<dubbo:annotation package="com.demo.user.service.impl"/>
</beans>
4 applicationContext-tx.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: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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入資料來源-->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 開啟事務控制的註解支援,開啟這個,只需要在類上加上註解,下面每個方法就會收到事務管理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
2.6 控制層模組
此步驟需要修改的檔案 1 pom.xml 2 web.xml 3 springmvc.xml
1 pom.xml
<packaging>war</packaging>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>cn.demo</groupId>
<artifactId>demo-user-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- dubbo相關 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!--動態類庫,與jdk自帶的反射功能類似,但比反射功能更強大-->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</dependency>
<!--zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 指定埠 -->
<port>9101</port>
<!-- 請求路徑 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
2 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- 解決post亂碼 -->
<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>
<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>
<!-- 指定載入的配置檔案 ,通過引數contextConfigLocation載入-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3 springmvc.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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!--配置訊息轉換器-->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json"/>
<property name="features">
<array>
<value>WriteMapNullValue</value>
<value>WriteDateUseDateFormat</value>
</array>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 引用dubbo 服務 -->
<dubbo:application name="demo-user-web"/>
<!--zookeeper的註冊地址和埠號-->
<dubbo:registry address="zookeeper://192.168.25.128:2181"/>
<!--註解掃描-->
<dubbo:annotation package="com.demo.user.controller"/>
</beans>
2.7 逆向工程
開啟逆向工程,修改generatorConfig.xml配置檔案,修改下圖中,標紅色框框的資訊(逆向工程最後會附上下載地址)
執行GeneratorSqlmap下的main函式,會的到 下圖
將實體類拷貝到工程的pojo下,將mapper介面和xml檔案拷貝到dao層下,到這裡環境就配置好了,後面開始寫後端程式碼和前端程式碼了 ,注意點:實體類都要實現序列化,因為是遠端呼叫
三 測試
3.1 資料庫表
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_name` varchar(100) DEFAULT NULL COMMENT '使用者名稱',
`password` varchar(100) DEFAULT NULL COMMENT '密碼',
`name` varchar(100) DEFAULT NULL COMMENT '姓名',
`age` int(10) DEFAULT NULL COMMENT '年齡',
`sex` tinyint(1) DEFAULT NULL COMMENT '性別,1男性,2女性',
`birthday` date DEFAULT NULL COMMENT '出生日期',
`created` datetime DEFAULT NULL COMMENT '建立時間',
`updated` datetime DEFAULT NULL COMMENT '更新時間',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`user_name`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES ('1', 'zhangsan', '123456', '張三', '30', '1', '1984-08-08', '2014-09-19 16:56:04', '2014-09-21 11:24:59');
INSERT INTO `tb_user` VALUES ('2', 'lisi', '123456', '李四', '21', '2', '1991-01-01', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('3', 'wangwu', '123456', '王五', '22', '2', '1989-01-01', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('4', 'zhangwei', '123456', '張偉', '20', '1', '1988-09-01', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('5', 'lina', '123456', '李娜', '28', '1', '1985-01-01', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('6', 'lilei', '123456', '李磊', '23', '1', '1988-08-08', '2014-09-20 11:41:15', '2014-09-20 11:41:15');
INSERT INTO `tb_user` VALUES ('8', 'xiaofeng', '123456', '蕭峰', '30', '1', '2018-06-25', '2018-06-25 18:42:25', '2018-06-25 18:42:25');
INSERT INTO `tb_user` VALUES ('12', 'zhendeshuai', '565656', '吳彥祖', '26', '1', '2018-06-26', '2018-06-26 14:55:59', '2018-06-26 15:12:12');
INSERT INTO `tb_user` VALUES ('14', 'jiumozhi', '123456', '鳩摩智', '30', '1', '2018-06-26', '2018-06-26 23:53:15', '2018-06-26 23:53:15');
INSERT INTO `tb_user` VALUES ('21', 'zhangsan1234', '12345', '傑克', '20', '1', '2000-01-01', '2018-07-07 21:50:26', '2018-07-07 21:50:26');
INSERT INTO `tb_user` VALUES ('22', 'zhangsanqwe1', '123455', '張三', '30', '1', '1999-02-25', '2018-07-07 21:51:20', '2018-07-07 21:51:20');
3.2 後端程式碼
後端主要編寫1 UserService(介面層) 2 UserServiceImpl(實現層) 3 UserController層
1 UserService(介面層)
package com.demo.user.service;
import com.demo.pojo.TbUser;
import java.util.List;
public interface UserService {
/**
* 查詢所有使用者
* @return
*/
public List<TbUser> findUserList();
/**
* 根據使用者名稱查詢使用者
* @param username
* @return
*/
public TbUser findUser(String username);
/**
* 通過年齡段查詢使用者
* @param start
* @param end
* @return
*/
public List<TbUser> findUsersByAge(Integer start,Integer end);
}
2 UserServiceImpl(實現層)
這裡實現類上面一定不能忘了加上@Service註解,而且是dubbo下的@Service
package com.demo.user.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.demo.mapper.TbUserMapper;
import com.demo.pojo.TbUser;
import com.demo.pojo.TbUserExample;
import com.demo.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private TbUserMapper userMapper;
@Override
public List<TbUser> findUserList() {
//傳null就是查詢所有
List<TbUser> userList = userMapper.selectByExample(null);
return userList;
}
@Override
public TbUser findUser(String username) {
TbUserExample example = new TbUserExample();
TbUserExample.Criteria criteria = example.createCriteria();
criteria.andUserNameEqualTo(username);
List<TbUser> userList = userMapper.selectByExample(example);
//根據使用者名稱查詢出來肯定只有一條記錄
return userList.get(0);
}
@Override
public List<TbUser> findUsersByAge(Integer start, Integer end) {
TbUserExample example = new TbUserExample();
TbUserExample.Criteria criteria = example.createCriteria();
//新增年齡查詢條件
criteria.andAgeBetween(start,end);
List<TbUser> userList = userMapper.selectByExample(example);
return userList;
}
}
3 UserController層
這裡需要注意:注入UserService時,用dubbox的@Reference註解
package com.demo.user.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.demo.pojo.TbUser;
import com.demo.user.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Reference
private UserService userService;
@RequestMapping("/findList")
public List<TbUser> findList() {
return userService.findUserList();
}
@RequestMapping("/findUser")
public TbUser findUser(String username){
return userService.findUser(username);
}
@RequestMapping("/findUsersByAge")
public List<TbUser> findUsersByAge(Integer start, Integer end){
return userService.findUsersByAge(start,end);
}
}
3.3 前端程式碼
在webapp下新增上面紅色框框的檔案,有1 angular.min.js(前端框架)2 base.js 3 userService.js 4 userController.js 5 user.html
base.js
var app=angular.module("demo",[]);//構建模組
userService.js
app.service("userService", function ($http) {//構建前端服務層
this.findList = function () {
return $http.get('user/findList.do');
}
this.findUser = function (username) {
return $http.get('user/findUser.do?username=' + username);
}
this.findUsersByAge = function (start, end) {
return $http.get('user/findUsersByAge.do?start=' + start + '&end=' + end);
}
})
userController.js
app.controller("userController",function ($scope,userService) {
//查詢所有使用者
$scope.findList=function () {
userService.findList().success(function (response) {
$scope.list=response;
})
}
//根據使用者名稱查詢使用者資訊
$scope.findUser=function (username) {
userService.findUser(username).success(function (response) {
$scope.entity=response;
})
}
//根據年齡段查詢使用者
$scope.findUsersByAge=function (start,end) {
userService.findUsersByAge(start,end).success(function (response) {
$scope.users=response;
})
}
})
user.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>使用者查詢</title>
<!--引入angularjs-->
<script src="js/angularjs/angular.min.js"></script>
<script src="js/base.js"></script>
<script src="js/service/userService.js"></script>
<script src="js/controller/userController.js"></script>
</head>
<body ng-app="demo" ng-controller="userController">
<!--查詢所有使用者資訊-->
<input type="button" ng-click="findList()" value="查詢所有使用者資訊">
<table >
<tr>
<th>編號</th>
<th>使用者名稱</th>
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
<th>出生日期</th>
<th>建立時間</th>
<th>更新時間</th>
</tr>
<!--遍歷集合-->
<tr ng-repeat="entity in list">
<td>{{entity.id}}</td>
<td>{{entity.userName}}</td>
<td>{{entity.password}}</td>
<td>{{entity.age}}</td>
<td>{{entity.sex}}</td>
<td>{{entity.birthday}}</td>
<td>{{entity.created}}</td>
<td>{{entity.updated}}</td>
</tr>
</table>
<hr>
<!--根據使用者名稱查詢使用者資訊-->
請輸入使用者名稱:<input ng-model="username">
<input type="button" ng-click="findUser(username)" value="查詢">
<table >
<tr>
<th>編號</th>
<th>使用者名稱</th>
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
<th>出生日期</th>
<th>建立時間</th>
<th>更新時間</th>
</tr>
<tr>
<td>{{entity.id}}</td>
<td>{{entity.userName}}</td>
<td>{{entity.password}}</td>
<td>{{entity.age}}</td>
<td>{{entity.sex}}</td>
<td>{{entity.birthday}}</td>
<td>{{entity.created}}</td>
<td>{{entity.updated}}</td>
</tr>
</table>
<hr>
<!--根據年齡段查詢使用者資訊-->
年齡:從<input ng-model="start">到<input ng-model="end">
<input type="button" ng-click="findUsersByAge(start,end)" value="查詢">
<table >
<tr>
<th>編號</th>
<th>使用者名稱</th>
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
<th>出生日期</th>
<th>建立時間</th>
<th>更新時間</th>
</tr>
<!--遍歷集合-->
<tr ng-repeat="entity in users">
<td>{{entity.id}}</td>
<td>{{entity.userName}}</td>
<td>{{entity.password}}</td>
<td>{{entity.age}}</td>
<td>{{entity.sex}}</td>
<td>{{entity.birthday}}</td>
<td>{{entity.created}}</td>
<td>{{entity.updated}}</td>
</tr>
</table>
</body>
</html>
3.4 結果展示
四 常見錯誤
4.1 註冊中心找不到服務
java.lang.IllegalStateException: Failed to check the status of the service
com.demo.user.service.UserService. No provider available for the service
com.demo.user.service.UserService from the url
zookeeper://192.168.25.129:2181/com.alibaba.dubbo.registry.RegistryService
如果出現上面的錯誤的話,檢查一下你的服務層程式碼是否添加了@service 註解,並且該註解的包一定是 com.alibaba.dubbo.config.annotation 包,不是org.springframework.stereotype.Service,這個地方極容易出錯
4.2 無法連線到註冊中心
org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server within
timeout: 5000 org.I0Itec.zkclient.ZkClient.connect(ZkClient.java:876)
如果出現上面這個異常,檢查下 註冊地址和埠號有沒有寫錯,以及看下zookeeper是否啟動
五 原始碼下載
原始碼:
連結:https://pan.baidu.com/s/1pRMAaQqgr3ezHiK1yUXjow 密碼:fmrr
逆向工程:
連結:https://pan.baidu.com/s/1sudm5xINAHKqVNzI7nwBgA 密碼:ivpx