搭建入门demo
搭建Spring+SpringMVC+Dubbo入门demo
准备数据
数据库创建demo表
create table demo (id bigint auto_increment primary key,name varchar(255) null,description text null );
插入数据
INSERT INTO `demo`(`id`, `name`, `description`) VALUES (1, 'demo1', 'demo1'); INSERT INTO `demo`(`id`, `name`, `description`) VALUES (2, 'demo2', 'demo2');
导入依赖
接口项目
①导入lombok依赖
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.4</version></dependency>
提供者项目
①导入Spring-jdbc依赖,用来与数据库建立连接
<dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.2.RELEASE</version></dependency>
②导入commons-dbcp对象池依赖,用来存储Java数据库连接对象
<dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>1.2.2</version></dependency>
③导入mybatis依赖
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.5</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version></dependency>
④导入mysql驱动依赖
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency>
消费者项目
①导入jackson依赖,用来把对象转json
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.9.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.9.0</version></dependency>
项目编辑
接口项目
①创建实体类
package com.agi.dto;import lombok.Data;import java.io.Serializable; import java.util.Objects;@Data public class DemoDTO implements Serializable {private Long id;private String name;private String description; }
② IDemoService接口新增方法用来查询数据库中的数据
package com.agi.api;import com.agi.dto.DemoDTO;import java.util.List;public interface IDemoService {public List<DemoDTO> getDemoList(); }
提供者项目
①创建jdbc.properties,保存数据库相关数据
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm-dubbo?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true jdbc.username=root jdbc.password=123456789 jdbc.initialSize=0 jdbc.maxActive=20 jdbc.maxIdle=20 jdbc.minIdle=1 jdbc.maxWait=60000
②创建provider-mybatis-config.xml,配置mybatis(⭐)
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 自动扫描 --><context:component-scan base-package="com.agi" /><!-- 引入配置文件 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:jdbc.properties" /></bean><!--Mybatis配置--><context:property-placeholder location="classpath:jdbc.properties"/><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><!-- 配置连接池属性 --><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/><!-- 初始化连接大小 --><property name="initialSize" value="${jdbc.initialSize}"></property><!-- 连接池最大数量 --><property name="maxActive" value="${jdbc.maxActive}"></property><!-- 连接池最大空闲 --><property name="maxIdle" value="${jdbc.maxIdle}"></property><!-- 连接池最小空闲 --><property name="minIdle" value="${jdbc.minIdle}"></property><!-- 获取连接最大等待时间 --><property name="maxWait" value="${jdbc.maxWait}"></property></bean><!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自动扫描mapping.xml文件 --><property name="mapperLocations" value="classpath:mapper/*Mapper.xml"></property></bean><!--mapper接口扫描包,动态的实现了dao接口可以注入到spring容器中--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!--注入sqlSessionFactory--><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><!--要扫描的dao包--><property name="basePackage" value="com.agi.mapper"/></bean><!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean> </beans>
③编辑web.xml,把provider-mybatis-config.xml引入进来(⭐)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><!-- Spring配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:provider*.xml</param-value></context-param><!-- Spring监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 防止Spring内存溢出监听器 --><listener><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener> </web-app>
④创建 DemoMapper接口
package com.agi.mapper;import com.agi.dto.DemoDTO;import java.util.List;public interface DemoMapper {public List<DemoDTO> getDemoList(); }
⑤在sources/mapper下创建 DemoMapper.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.agi.mapper.DemoMapper"><select id="getDemoList" resultType="com.agi.dto.DemoDTO">select *from demo</select> </mapper>
⑥ 编辑DemoServiceImpl,添加getDemoList()方法
package com.agi.impl;import com.agi.api.IDemoService; import com.agi.dto.DemoDTO; import com.agi.mapper.DemoMapper; import org.apache.dubbo.config.annotation.Service; import org.springframework.beans.factory.annotation.Autowired;import java.util.List;@Service public class DemoServiceImpl implements IDemoService {@Autowiredprivate DemoMapper demoMapper;@Overridepublic List<DemoDTO> getDemoList() {return demoMapper.getDemoList();} }
消费者项目
①编辑consumer.xml文件,增加对象转json配置
<?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://dubbo.apache.org/schema/dubbo"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://dubbo.apache.org/schema/dubbohttp://dubbo.apache.org/schema/dubbo/dubbo.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 定义服务消费方应用名 --><dubbo:application name="dubbo-consumer"><dubbo:parameter key="qos.port" value="22224"/></dubbo:application><!--注册中心地址--><dubbo:registry address="zookeeper://127.0.0.1:2181"/><!--指明使用的协议--><dubbo:protocol name="dubbo" port="20881"/><!--dubbo包扫描(@Refer)--><dubbo:annotation package="com.agi.controller"/><!--spring包扫描(@RequestMapping,@RestController...)--><context:component-scan base-package="com.agi.controller"/><mvc:annotation-driven><mvc:message-converters><bean class="org.springframework.http.converter.StringHttpMessageConverter"/><bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/></mvc:message-converters></mvc:annotation-driven></beans>
②编辑web.xml文件,增加编码过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><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:consumer.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><!-- 编码过滤器 --><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><async-supported>true</async-supported><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><servlet-mapping><servlet-name>default</servlet-name><url-pattern>*.html</url-pattern></servlet-mapping> </web-app>
③编辑DemoController,增加getDemoList方法
package com.agi.controller;import com.agi.api.IDemoService; import com.agi.dto.DemoDTO; import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.util.List;@RequestMapping("/demo") @RestController public class DemoController {@Referenceprivate IDemoService demoService;@RequestMapping("/getDemoList")public List<DemoDTO> getDemoList() {List<DemoDTO> dtoList=demoService.getDemoList();return dtoList;} }
测试
更新web exploded(⭐)
①File -> Project Structure
启动Zookeeper
启动提供者
启动消费者
浏览器(JSON插件)测试