<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="testTables" targetRuntime="MyBatis3">
<!-- 配置生成pojo的序列化的插件,mybatis支持不少插件,這些插件都在 org.mybatis.generator.plugins包下 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<!--配置生成pojo的toString()方法的插件,mybatis支持不少插件,這些插件都在 org.mybatis.generator.plugins包下 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<commentGenerator>
<!-- 是否去除自動生成的註釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--數據庫鏈接的信息:驅動類、鏈接地址、用戶名、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/taotao" userId="root" password="root">
</jdbcConnection>
<!-- 默認false時,把JDBC DECIMAL 和 NUMERIC 類型解析爲 Integer
當爲 true時,把JDBC DECIMAL 和 NUMERIC 類型解析爲java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO類的位置 -->
<javaModelGenerator targetPackage="com.taotao.pojo" targetProject=".\src">
<!-- enableSubPackages:是否讓schema做爲包的後綴 -->
<property name="enableSubPackages" value="false" />
<!-- 從數據庫返回的值被清理先後的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.taotao.mapper" targetProject=".\src">
<!-- enableSubPackages:是否讓schema做爲包的後綴 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.taotao.mapper" targetProject=".\src">
<!-- enableSubPackages:是否讓schema做爲包的後綴 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定數據庫表 -->
<table schema="" tableName="tb_content"></table>
<table schema="" tableName="tb_content_category"></table>
<table schema="" tableName="tb_item"></table>
<table schema="" tableName="tb_item_cat"></table>
<table schema="" tableName="tb_item_desc"></table>
<table schema="" tableName="tb_item_param"></table>
<table schema="" tableName="tb_item_param_item"></table>
<table schema="" tableName="tb_order"></table>
<table schema="" tableName="tb_order_item"></table>
<table schema="" tableName="tb_order_shipping"></table>
<table schema="" tableName="tb_user"></table>
</context>
</generatorConfiguration>
使用方法:將Java工程generatorSqlmapCustom導入到eclipse中,運行/generatorSqlmapCustom/src/下的GeneratorSqlmap.java文件生成咱們所需的代碼。php
因爲淘淘商城是基於soa的架構,表現層和服務層是不一樣的工程。因此要實現商品列表查詢須要兩個系統之間進行通訊。
如何實現遠程通訊?css
效率不高
,它是基於soap協議(html+xml
)。項目中不推薦使用。優勢是:跨語言、跨平臺。適用於兩個公司之間。http+json
。不少項目中應用。若是服務愈來愈多,服務與服務之間的調用關係複雜,調用服務的URL管理複雜,何時添加機器難以肯定。 須要治療服務。適用於中小型企業。RPC協議
進行遠程調用,直接使用socket通訊
。傳輸效率高
,而且能夠統計出系統之間的調用關係、調用次數,管理服務。適用於大型企業。缺點:不能跨語言。只能java語言。 最新中文網址:https://dubbo.incubator.apache.org/zh-cn/index.html
DUBBO是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,是阿里巴巴SOA服務化治理方案的核心框架,天天爲2,000+個服務提供3,000,000,000+次訪問量支持,並被普遍應用於阿里巴巴集團的各成員站點。
隨着互聯網的發展,網站應用的規模不斷擴大,常規的垂直應用架構
已沒法應對,分佈式服務架構
以及流動計算架構
勢在必行,亟需一個治理系統
確保架構有條不紊的演進。html
數據訪問框架(ORM)
是關鍵。Web框架(MVC)
是關鍵。分佈式服務框架(RPC)
是關鍵。資源調度和治理中心(SOA)
是關鍵。資源調度和治理中心的管理工具
。統計
和管理服務
直接的調用狀況(包括服務被誰調用了,調用的次數是如何,以及服務的使用情況)。 <bean id="xxxService" class="com.xxx.XxxServiceImpl" />
<bean id="xxxAction" class="com.xxx.XxxAction">
<property name="xxxService" ref="xxxService" />
</bean>
,在服務消費方增長引用服務配置
。服務層發佈服務:前端
<!-- 和本地服務同樣實現遠程服務 -->
<bean id="xxxService" class="com.xxx.XxxServiceImpl" />
<!-- 增長暴露遠程服務配置 -->
<dubbo:service interface="com.xxx.XxxService" ref="xxxService" />
表現層調用服務:java
<!-- 增長引用遠程服務配置 -->
<dubbo:reference id="xxxService" interface="com.xxx.XxxService" />
<!-- 和本地服務同樣使用遠程服務 -->
<bean id="xxxAction" class="com.xxx.XxxAction">
<property name="xxxService" ref="xxxService" />
</bean>
Zookeeper(集羣管理工具:動物管理員)適用於大數據裏面的生態系統。好比:大數據中的hadoop(大象)、Hive(蜂巢)等等動物須要動物管理員來管理哈!
Dubbo中使用Zookeeper是做爲註冊中心使用的,註冊中心負責服務地址的註冊與查找,至關於目錄服務
,服務提供者和消費者只在啓動時與註冊中心交互,註冊中心不轉發請求,壓力較小。使用dubbo-2.3.3以上版本,建議使用Zookeeper註冊中心。
Zookeeper是Apacahe Hadoop的子項目,是一個樹型的目錄服務
,支持變動推送,適合做爲Dubbo服務的註冊中心,工業強度較高(穩定性好),可用於生產環境,並推薦使用。mysql
mv zoo_sample.cfg zoo.cfg
mkdir data
)服務層:taotao-manager-service/pom.xmllinux
<!-- 配置對dubbo的依賴 -->
<!-- dubbo相關 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<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>
注意:當咱們配置對dubbo的依賴後,要查看dubbo的依賴關係,排除掉dubbo依賴的低版本的jar,好比:spring-2.5.6.SEC03.jar和netty-3.2.5.Final.jar(這一樣也是編程的好習慣)!
排除方法:
方法一:在taotao-manager-service/pom.xml中手動編寫代碼,因此最終pom.xml中要添加的內容以下:git
<!-- 配置對dubbo的依賴 -->
<!-- dubbo相關 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<!-- 排除對低版本jar包的依賴 -->
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>netty</artifactId>
<groupId>org.jboss.netty</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
方法二:使用圖形化界面,以下圖所示:github
<!-- 配置對dubbo的依賴 -->
<!-- dubbo相關 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<!-- 排除對低版本jar包的依賴 -->
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>netty</artifactId>
<groupId>org.jboss.netty</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
發佈dubbo服務
。引用dubbo服務
。SqlMapConfig.xml(因爲和spring進行整合了,裏面空空如也,可是該文件必需要有!)web
<?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>
</configuration>
建立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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.2.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.taotao.mapper" />
</bean>
</beans>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
備註:
Druid(德魯伊)是目前最好的數據庫鏈接池,在功能、性能、擴展性方面,都超過其餘數據庫鏈接池,包括DBCP、C3P0、BoneCP、Proxool、JBoss DataSource。
Druid已經在阿里巴巴部署了超過600個應用,通過多年多生產環境大規模部署的嚴苛考驗。
applicationContext-service.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 配置包掃描器,掃描全部須要帶@Service註解的類 -->
<context:component-scan base-package="com.taotao.service" />
<!-- 使用dubbo發佈服務:須要先引入dubbo的約束 -->
<!-- 提供方應用信息,用於計算依賴關係 -->
<dubbo:application name="taotao-manager" />
<dubbo:registry protocol="zookeeper" address="192.168.5.130:2181" />
<!-- 用dubbo協議在20880端口暴露服務 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 聲明須要暴露的服務接口 -->
<dubbo:service interface="com.taotao.service.ItemService" ref="itemServiceImpl" />
</beans>
建立applicationContext-transaction.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 配置事務管理器 :包括:通知、切面-->
<!-- 事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 數據源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 傳播行爲 -->
<!-- 增刪改操做須要配置事務 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<!-- 查詢操做不須要配置事務 -->
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.taotao.service.*.*(..))" />
</aop:config>
</beans>
因爲taotao-manager-service是一個聚合工程中的web工程,因此咱們須要配置下該工程的web.xml
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"
id="WebApp_ID" version="2.5">
<display-name>taotao-manage-servicer</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 初始化spring容器:也即加載spring容器,taotao-manager-service是web工程,須要在web容器中運行 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.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-4.2.xsd">
<!-- 配置包掃描器,掃描全部須要帶@Controller註解的類 -->
<context:component-scan base-package="com.taotao.controller" />
<!-- 配置註解驅動 -->
<mvc:annotation-driven />
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 引用dubbo服務 :須要先引入dubbo的約束-->
<dubbo:application name="taotao-manager-web"/>
<dubbo:registry protocol="zookeeper" address="192.168.5.130:2181"/>
<dubbo:reference interface="com.taotao.service.ItemService" id="itemService" />
</beans>
注意:若是沒有添加interface的依賴,還須要在表現層工程的pom文件中添加taotao-manager-interface工程的依賴。
添加的內容以下:
<!-- 配置對interface的依賴:表現層調用服務要經過該接口 -->
<dependency>
<groupId>com.taotao</groupId>
<artifactId>taotao-manager-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
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"
id="WebApp_ID" version="2.5">
<display-name>taotao-manager-web</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置解決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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<!-- /表示攔截全部請求,但不攔截jsp,/*表示攔截全部請求 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置springmvc的前端控制器 -->
<servlet>
<servlet-name>taotao-manager-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必須的, 若是不配置contextConfigLocation,
springmvc的配置文件默認在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup><!-- 1表示tomcat啓動以後就會加載servlet前端控制器,不然表示第一次請求的時候才加載 -->>
</servlet>
<servlet-mapping>
<servlet-name>taotao-manager-web</servlet-name>
<!-- /表示攔截全部請求,但不攔截jsp,/*表示攔截全部請求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
注意:其中配置項<load-on-startup>1</load-on-startup>
表示tomcat啓動以後就會加載這個servlet前端控制器,若是不配置的話,就表示第一次請求的時候才加載這個servlet前端控制器。
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
/**
* 根據商品id查詢商品信息,並將商品信息使用json數據格式返回。
* @param itemId
* @return
*/
@RequestMapping("/item/{itemId}")
@ResponseBody // 在後臺,把JavaBean強制轉換成json格式數據返回給前臺頁面。
public TbItem getItemById(@PathVariable Long itemId) {
TbItem item = itemService.getItemById(itemId);
return item;
}
}
注意:使用註解@ResponseBody可能會出現訪問406錯誤,90%的狀況下,是因爲沒有把jackson註解的jar包沒有引入。
@ResponseBody的做用:在後臺,把JavaBean強制轉換成json格式數據返回給前臺頁面。沒有該jar,就不能把對象轉成一個json格式的數據(json串)。服務器會出現406錯誤。
本工程taotao-manager-web中有jackson的jar依賴,由於工程taotao-manager-web依賴工程taotao-common了,而在工程taotao-common依賴了jackson的jar包。
[root@itheima ~]# cd /etc/firewalld/zones/
[root@itheima zones]# firewall-cmd --permanent --add-port=2181/tcp
[root@itheima zones]# service firewalld restart
Redirecting to /bin/systemctl restart firewalld.service
[root@itheima zones]#
<build>
<resources>
<!-- 若是不添加此節點,mybatis的mapper.xml文件都會被漏掉 -->
<!-- 注意:配置了此方式,原來的默認的資源拷貝行爲將無效了 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<!-- 若是不添加此節點,src/main/resources目錄下的配置文件將被忽略 -->
<resource>
<directory>src/main/resource</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
taotao-manager-dao咱們在表現層也會用到,因此改好後須要選擇taotao-manager-dao工程進行安裝,選中工程右鍵 Run as --> Maven install
注意:因爲taotao-manager是一個聚合工程,taotao-manager-dao是該聚合工程中的模塊工程,因此taotao-manager-dao中有修改的話,taotao-manager-dao也不須要安裝,因此咱們直接重啓工程taotao-manager便可,命令:clean tomcat7:run
再次測試訪問:localhost:8081/item/830972,出現了新的錯誤,以下:
<!-- 配置pojo的序列化 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
修改完成後,因爲咱們在表現層也會用到taotao-manager-pojo,因此咱們須要從新安裝下,選中工程右鍵 Run as --> Maven install,安裝完成後而後咱們重啓服務端工程和表現層工程。整合成功!
瀏覽器效果以下圖:
瀏覽器美化後的json格式數據:
tar -zxvf apache-tomcat-7.0.47.tar.gz
rm -rf apache-tomcat-7.0.47.tar.gz
[root@itheima tomcat]# ll
總用量 26296
drwxr-xr-x. 9 root root 160 11月 10 15:04 apache-tomcat-7.0.47
-rw-r--r--. 1 root root 26924417 9月 26 22:11 dubbo-admin-2.5.4.war
[root@itheima tomcat]# cp dubbo-admin-2.5.4.war apache-tomcat-7.0.47/webapps/
[root@itheima tomcat]# rm -rf dubbo-admin-2.5.4.war
[root@itheima tomcat]# ll apache-tomcat-7.0.47/webapps/
總用量 26304
drwxr-xr-x. 13 root root 4096 11月 10 15:04 docs
drwxr-xr-x. 8 root root 149 11月 10 15:18 dubbo-admin-2.5.4
-rw-r--r--. 1 root root 26924417 11月 10 15:18 dubbo-admin-2.5.4.war
drwxr-xr-x. 7 root root 111 11月 10 15:04 examples
drwxr-xr-x. 5 root root 87 11月 10 15:04 host-manager
drwxr-xr-x. 5 root root 103 11月 10 15:04 manager
drwxr-xr-x. 3 root root 4096 11月 10 15:04 ROOT
[root@itheima tomcat]#
[root@itheima tomcat]# cd apache-tomcat-7.0.47/bin/
[root@itheima bin]# ./startup.sh --啓動tomcat
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-7.0.47
Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-7.0.47
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-7.0.47/temp
Using JRE_HOME: /usr/local/java/jdk1.7.0_80/jre
Using CLASSPATH: /usr/local/tomcat/apache-tomcat-7.0.47/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-7.0.47/bin/tomcat-juli.jar
[root@itheima bin]# ps -ef|grep tomcat --查看Tomcat的日誌
root 4741 1 18 15:07 pts/1 00:00:03 /usr/local/java/jdk1.7.0_80/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/apache-tomcat-7.0.47/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat/apache-tomcat-7.0.47/endorsed -classpath /usr/local/tomcat/apache-tomcat-7.0.47/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-7.0.47/bintomcat-juli.jar -Dcatalina.base=/usr/local/tomcat/apache-tomcat-7.0.47 -Dcatalina.home=/usr/local/tomcat/apache-tomcat-7.0.47 -Djava.io.tmpdir=/usr/local/tomcat/apache-tomcat-7.0.47/temp org.apache.catalina.startup.Bootstrap start
root 4761 3081 0 15:07 pts/1 00:00:00 grep --color=auto tomcat
[root@itheima bin]#
http://192.168.5.130:8080/dubbo-admin-2.5.4/
,瀏覽器彈出訪問鏈接超時[root@itheima ~]# cd /etc/firewalld/zones/
[root@itheima zones]# firewall-cmd --permanent --add-port=8080/tcp
[root@itheima zones]# service firewalld restart
Redirecting to /bin/systemctl restart firewalld.service
[root@itheima zones]#
tail -f logs/catalina.out
dubbo-admin不能在jdk1.8的環境下運行
,mmp!因而我卸載了jdk1.8,從新安裝了jdk1.7。問題解決了!!!彈出了我想要的界面:靜態頁面位置:如圖:
「/」表示攔截全部的url請求
,包括靜態資源例如css、js等。因此須要在springmvc.xml中添加資源映射標籤:
<!-- 配置資源映射標籤 -->
<mvc:resources location="/WEB-INF/js/" mapping="/js/**" />
<mvc:resources location="/WEB-INF/css/" mapping="/css/**" />
/**
* 頁面展現Controller
* @author chenmingjun
* @date 2018年11月9日下午7:59:33
* @version 1.0
*/
@Controller
public class PageController {
/**
* 展現後臺首頁
* @return
*/
@RequestMapping("/")
public String showIndex() {
return "index";
}
/**
* 展現後臺菜單各頁面
* @param page
* @return
*/
@RequestMapping("/{page}")
public String showPage(@PathVariable String page) {
return page;
}
注意:要先刪掉webapp下測試用的的index.jsp。
建立商品列表查詢時的返回的數據類EasyUIDataGridResult(包裝類),將該類放入taotao-common中,由於服務層和表現層都要用它!
package com.taotao.common.pojo;
import java.io.Serializable;
import java.util.List;
/**
* 列表查詢時的返回的數據類
* @author chenmingjun
* @date 2018年11月11日上午11:54:31
* @version 1.0
*/
public class EasyUIDataGridResult implements Serializable{
private Long total;
private List<?> rows;
private static final long serialVersionUID = 1L;
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<?> getRows() {
return rows;
}
public void setRows(List<?> rows) {
this.rows = rows;
}
@Override
public String toString() {
return "EasyUIDataGridResult [total=" + total + ", rows=" + rows + "]";
}
}
MariaDB
、SQLite、Hsqldb、PostgreSQL六種數據庫分頁。不支持SQLServer數據庫(微軟)。Maven中央倉庫最近更新的Artifact:http://maven.outofmemory.cn/
第一步:把PageHelper依賴的jar包添加到工程中。官方提供的代碼對逆向工程支持的很差,使用參考資料中的pagehelper-fix。
<!-- 添加對分頁插件pagehelper的依賴 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency>
第三步:在Mybatis的全局配置文件中配置對攔截器插件的依賴。
配置文件位置:/taotao-manager-service/src/main/resources/mybatis/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>
<!-- 配置攔截器插件:做用是在執行sql語句以前,將sql語句攔截下來,在sql語句中拼接上limit -->
<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>
第四步:在代碼中使用,參考「分頁測試」的代碼。
在/taotao-manager-service/src/test/java/com/taotao/pagehelper/中新建PagehelperTest.java文件,文件內容以下:
@Test
public void pagehelperTest() throws Exception {
// 一、先在Mybatis的配置文件中配置攔截器插件:做用是在執行sql語句以前,將sql語句攔截下來,在sql語句中拼接上limit
// 二、在執行查詢以前設置分頁條件,使用Pagehelper的靜態方法
PageHelper.startPage(1, 10);
// 三、執行查詢,使用mapper,須要初始化一個spring容器,從spring容器中的獲得這個mapper的代理對象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
// 建立TbItemExample對象
TbItemExample example = new TbItemExample();
// 調用createCriteria()方法,建立條件查詢對象,本測試沒有條件,能夠不用調用,表示查詢全部
// Criteria criteria = example.createCriteria();
List<TbItem> list = itemMapper.selectByExample(example); // 本測試沒有條件,表示查詢全部
// 四、取出分頁信息,分頁後,實際返回的結果list類型是Page<E>,若是想取出分頁信息
// 方式一:須要強制轉換爲Page<E>
// Page<TbItem> listIteam = (Page<TbItem>)list;
// 方式二:咱們使用PageInfo對象對查詢出來的結果進行包裝,推薦使用第二種
PageInfo<TbItem> pageInfo = new PageInfo<>(list);
// PageInfo中包含了很是全面的分頁屬性,下面演示幾個
System.out.println("總記錄數:" + pageInfo.getTotal());
System.out.println("總頁數:" + pageInfo.getPages());
System.out.println("當前頁數:" + pageInfo.getPageNum());
System.out.println("每頁記錄數:" + pageInfo.getPageSize());
}
參數:Integer page、Integer rows
業務邏輯:查詢全部商品列表,要進行分頁處理。
返回值:EasyUIDataGridResult
/**
* 分頁查詢後臺商品列表信息
* @param page
* @param rows
* @return
*/
EasyUIDataGridResult getItemList(Integer page, Integer rows);
@Override
public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
// 設置分頁信息,使用PageHelper
if (page == null) {
page = 1;
}
if (rows == null) {
rows = 30;
}
PageHelper.startPage(page, rows);
// 執行查詢,不須要設置查詢條件,即查詢全部
TbItemExample example = new TbItemExample();
List<TbItem> list = itemMapper.selectByExample(example);
// 取分頁信息
PageInfo<TbItem> pageInfo = new PageInfo<>(list);
// 建立返回結果對象
EasyUIDataGridResult result = new EasyUIDataGridResult();
// 給返回的結果對象設置值(即封裝數據)
result.setTotal(pageInfo.getTotal());
result.setRows(list);
return result;
}
在taotao-manager-service中的applicationContext-service.xml中發佈服務。
注意address的值:使用本身的zookeeper所在的系統的ip地址和端口。
一、初始化表格請求的url:/item/list
二、Datagrid默認請求參數:
1)page:當前的頁碼,從1開始。
2)rows:每頁顯示的記錄數。
三、服務器響應的數據:json格式的數據。EasyUIDataGridResult
/**
* 分頁查詢後臺商品列表信息,並將商品列表信息使用json數據格式返回。
* @param page
* @param rows
* @return
*/
@RequestMapping("/item/list")
@ResponseBody // 在後臺,把JavaBean強制轉換成json格式數據返回給前臺頁面。
public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
EasyUIDataGridResult result = itemService.getItemList(page, rows);
return result;
}
在taotao-manager-web工程中的springmvc.xml中引入服務。
注意address的值:使用本身的zookeeper所在的系統的ip地址和端口。
1)須要先啓動zookeeper,再啓動服務層,再啓動表現層。
若是先啓動表現層,後啓動服務層,會報錯,可是不影響使用。
2)爲了更方便的進行測試,表現層工程和服務層工程屬於不一樣的工程,要debug的時候須要設置源碼,以下:
Debug設置源代碼,涉及到工程都要添加,爲了方便
,能夠添加全部的工程。
以工程taotao-manager爲例:
步驟一:
1)表現層警告:
Page<E>
,在表現層沒有這個類,可是咱們有這個類的父類ArrayList,由於該類Page繼承ArrayList,以下圖,因此說這個警告咱們能夠不用管!若想解決這個警告,咱們能夠在web層(表現層)加入對分頁pagehelper的jar包的依賴(該包中有該類)。
2)表現層異常: