🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] interface是controller和service层连接 ## 1. 工程结构 ![](https://box.kancloud.cn/cff809222ea79e9e3063e938013ab9c9_359x193.png) > e3-manage:父工程 > e3-manage-dao:子module(普通maven项目) > e3-manage-interface:子module(普通maven项目) > e3-manage-pojo:子module(普通maven项目) > e3-manage-service:子module(web项目,dubbo的资源provider) > e3-manage-service:子module(web项目,dubbo的资源consumer) ## 2. 整合 ### 2.1 pojo(model层) 存放实体类 ![](https://box.kancloud.cn/3ebaa06a6f25d27b35cf0b96713f144f_365x506.png) ### 2.2 dao层 ![](https://box.kancloud.cn/e9281a0344f49425e07df3e20e62baca_368x531.png) #### 2.2.1 mapper接口 TbItemMapper ~~~ package e3mall.mapper; import e3mall.pojo.TbItem; import e3mall.pojo.TbItemExample; import org.apache.ibatis.annotations.Param; import java.util.List; public interface TbItemMapper { int countByExample(TbItemExample example); int deleteByExample(TbItemExample example); int deleteByPrimaryKey(Long id); int insert(TbItem record); int insertSelective(TbItem record); List<TbItem> selectByExample(TbItemExample example); TbItem selectByPrimaryKey(Long id); int updateByExampleSelective(@Param("record") TbItem record, @Param("example") TbItemExample example); int updateByExample(@Param("record") TbItem record, @Param("example") TbItemExample example); int updateByPrimaryKeySelective(TbItem record); int updateByPrimaryKey(TbItem record); } ~~~ #### 2.2.2 xml配置 TbItemMapper.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="e3mall.mapper.TbItemMapper"> <resultMap id="BaseResultMap" type="e3mall.pojo.TbItem"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> <id column="id" jdbcType="BIGINT" property="id" /> <result column="title" jdbcType="VARCHAR" property="title" /> <result column="sell_point" jdbcType="VARCHAR" property="sellPoint" /> <result column="price" jdbcType="BIGINT" property="price" /> <result column="num" jdbcType="INTEGER" property="num" /> <result column="barcode" jdbcType="VARCHAR" property="barcode" /> <result column="image" jdbcType="VARCHAR" property="image" /> <result column="cid" jdbcType="BIGINT" property="cid" /> <result column="status" jdbcType="TINYINT" property="status" /> <result column="created" jdbcType="TIMESTAMP" property="created" /> <result column="updated" jdbcType="TIMESTAMP" property="updated" /> </resultMap> <sql id="Example_Where_Clause"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> <where> <foreach collection="oredCriteria" item="criteria" separator="or"> <if test="criteria.valid"> <trim prefix="(" prefixOverrides="and" suffix=")"> <foreach collection="criteria.criteria" item="criterion"> <choose> <when test="criterion.noValue"> and ${criterion.condition} </when> <when test="criterion.singleValue"> and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue"> and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue"> and ${criterion.condition} <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=","> #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Update_By_Example_Where_Clause"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> <where> <foreach collection="example.oredCriteria" item="criteria" separator="or"> <if test="criteria.valid"> <trim prefix="(" prefixOverrides="and" suffix=")"> <foreach collection="criteria.criteria" item="criterion"> <choose> <when test="criterion.noValue"> and ${criterion.condition} </when> <when test="criterion.singleValue"> and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue"> and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue"> and ${criterion.condition} <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=","> #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Base_Column_List"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> id, title, sell_point, price, num, barcode, image, cid, status, created, updated </sql> <select id="selectByExample" parameterType="e3mall.pojo.TbItemExample" resultMap="BaseResultMap"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> select <if test="distinct"> distinct </if> <include refid="Base_Column_List" /> from tb_item <if test="_parameter != null"> <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null"> order by ${orderByClause} </if> </select> <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> select <include refid="Base_Column_List" /> from tb_item where id = #{id,jdbcType=BIGINT} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Long"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> delete from tb_item where id = #{id,jdbcType=BIGINT} </delete> <delete id="deleteByExample" parameterType="e3mall.pojo.TbItemExample"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> delete from tb_item <if test="_parameter != null"> <include refid="Example_Where_Clause" /> </if> </delete> <insert id="insert" parameterType="e3mall.pojo.TbItem"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> insert into tb_item (id, title, sell_point, price, num, barcode, image, cid, status, created, updated) values (#{id,jdbcType=BIGINT}, #{title,jdbcType=VARCHAR}, #{sellPoint,jdbcType=VARCHAR}, #{price,jdbcType=BIGINT}, #{num,jdbcType=INTEGER}, #{barcode,jdbcType=VARCHAR}, #{image,jdbcType=VARCHAR}, #{cid,jdbcType=BIGINT}, #{status,jdbcType=TINYINT}, #{created,jdbcType=TIMESTAMP}, #{updated,jdbcType=TIMESTAMP}) </insert> <insert id="insertSelective" parameterType="e3mall.pojo.TbItem"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> insert into tb_item <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="title != null"> title, </if> <if test="sellPoint != null"> sell_point, </if> <if test="price != null"> price, </if> <if test="num != null"> num, </if> <if test="barcode != null"> barcode, </if> <if test="image != null"> image, </if> <if test="cid != null"> cid, </if> <if test="status != null"> status, </if> <if test="created != null"> created, </if> <if test="updated != null"> updated, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=BIGINT}, </if> <if test="title != null"> #{title,jdbcType=VARCHAR}, </if> <if test="sellPoint != null"> #{sellPoint,jdbcType=VARCHAR}, </if> <if test="price != null"> #{price,jdbcType=BIGINT}, </if> <if test="num != null"> #{num,jdbcType=INTEGER}, </if> <if test="barcode != null"> #{barcode,jdbcType=VARCHAR}, </if> <if test="image != null"> #{image,jdbcType=VARCHAR}, </if> <if test="cid != null"> #{cid,jdbcType=BIGINT}, </if> <if test="status != null"> #{status,jdbcType=TINYINT}, </if> <if test="created != null"> #{created,jdbcType=TIMESTAMP}, </if> <if test="updated != null"> #{updated,jdbcType=TIMESTAMP}, </if> </trim> </insert> <select id="countByExample" parameterType="e3mall.pojo.TbItemExample" resultType="java.lang.Integer"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> select count(*) from tb_item <if test="_parameter != null"> <include refid="Example_Where_Clause" /> </if> </select> <update id="updateByExampleSelective" parameterType="map"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> update tb_item <set> <if test="record.id != null"> id = #{record.id,jdbcType=BIGINT}, </if> <if test="record.title != null"> title = #{record.title,jdbcType=VARCHAR}, </if> <if test="record.sellPoint != null"> sell_point = #{record.sellPoint,jdbcType=VARCHAR}, </if> <if test="record.price != null"> price = #{record.price,jdbcType=BIGINT}, </if> <if test="record.num != null"> num = #{record.num,jdbcType=INTEGER}, </if> <if test="record.barcode != null"> barcode = #{record.barcode,jdbcType=VARCHAR}, </if> <if test="record.image != null"> image = #{record.image,jdbcType=VARCHAR}, </if> <if test="record.cid != null"> cid = #{record.cid,jdbcType=BIGINT}, </if> <if test="record.status != null"> status = #{record.status,jdbcType=TINYINT}, </if> <if test="record.created != null"> created = #{record.created,jdbcType=TIMESTAMP}, </if> <if test="record.updated != null"> updated = #{record.updated,jdbcType=TIMESTAMP}, </if> </set> <if test="_parameter != null"> <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByExample" parameterType="map"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> update tb_item set id = #{record.id,jdbcType=BIGINT}, title = #{record.title,jdbcType=VARCHAR}, sell_point = #{record.sellPoint,jdbcType=VARCHAR}, price = #{record.price,jdbcType=BIGINT}, num = #{record.num,jdbcType=INTEGER}, barcode = #{record.barcode,jdbcType=VARCHAR}, image = #{record.image,jdbcType=VARCHAR}, cid = #{record.cid,jdbcType=BIGINT}, status = #{record.status,jdbcType=TINYINT}, created = #{record.created,jdbcType=TIMESTAMP}, updated = #{record.updated,jdbcType=TIMESTAMP} <if test="_parameter != null"> <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByPrimaryKeySelective" parameterType="e3mall.pojo.TbItem"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> update tb_item <set> <if test="title != null"> title = #{title,jdbcType=VARCHAR}, </if> <if test="sellPoint != null"> sell_point = #{sellPoint,jdbcType=VARCHAR}, </if> <if test="price != null"> price = #{price,jdbcType=BIGINT}, </if> <if test="num != null"> num = #{num,jdbcType=INTEGER}, </if> <if test="barcode != null"> barcode = #{barcode,jdbcType=VARCHAR}, </if> <if test="image != null"> image = #{image,jdbcType=VARCHAR}, </if> <if test="cid != null"> cid = #{cid,jdbcType=BIGINT}, </if> <if test="status != null"> status = #{status,jdbcType=TINYINT}, </if> <if test="created != null"> created = #{created,jdbcType=TIMESTAMP}, </if> <if test="updated != null"> updated = #{updated,jdbcType=TIMESTAMP}, </if> </set> where id = #{id,jdbcType=BIGINT} </update> <update id="updateByPrimaryKey" parameterType="e3mall.pojo.TbItem"> <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. --> update tb_item set title = #{title,jdbcType=VARCHAR}, sell_point = #{sellPoint,jdbcType=VARCHAR}, price = #{price,jdbcType=BIGINT}, num = #{num,jdbcType=INTEGER}, barcode = #{barcode,jdbcType=VARCHAR}, image = #{image,jdbcType=VARCHAR}, cid = #{cid,jdbcType=BIGINT}, status = #{status,jdbcType=TINYINT}, created = #{created,jdbcType=TIMESTAMP}, updated = #{updated,jdbcType=TIMESTAMP} where id = #{id,jdbcType=BIGINT} </update> </mapper> ~~~ #### 2.2.3 pom 一定要配置编译时把xml加进去 ~~~ <?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>e3-manage</artifactId> <groupId>cn.e3mall</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>e3-manager-dao</artifactId> <dependencies> <dependency> <groupId>cn.e3mall</groupId> <artifactId>e3-manager-pojo</artifactId> <version>1.0</version> </dependency> <!-- Mybatis --> <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> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> </dependencies> <build> <finalName>e3-manager-service</finalName> <resources> <resource> <directory>src/main/java</directory> <filtering>false</filtering> <includes> # 编译打包时把xml文件加入 <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> </resource> </resources> </build> </project> ~~~ ### 2.3 service层 #### 2.3.1 实现接口 ~~~ package e3mall.service; import e3mall.mapper.TbItemMapper; import e3mall.pojo.TbItem; import e3mall.pojo.TbItemExample; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * Created by dailin on 2018/1/30. */ @Service public class ItemServiceImpl implements ItemService{ @Autowired TbItemMapper tbItemMapper; public TbItem getItemById(Long id) { System.out.println("id:" + id); TbItemExample tbItemExample = new TbItemExample(); TbItemExample.Criteria criteria = tbItemExample.createCriteria(); criteria.andIdEqualTo(id); List<TbItem> tbItems = tbItemMapper.selectByExample(tbItemExample); if (tbItems.size()!=0 && tbItems != null){ return tbItems.get(0); } return null; } } ~~~ #### 2.3.2 service层配置 1. 开启注解扫描 2. 指定dubbo应用名 3. 向dubbo注册(zookeeper) 4. 指定service实现类与接口的绑定关系 ~~~ <?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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.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:component-scan base-package="e3mall"/> <!-- 使用dubbo发布服务 --> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="e3-manager" /> <dubbo:registry protocol="zookeeper" address="192.168.56.130:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="e3mall.service.ItemService" ref="itemServiceImpl" timeout="600000"/> </beans> ~~~ ### 2.4 web(controller层) ~~~ package e3mall.controller; import e3mall.pojo.TbItem; import e3mall.service.ItemService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * Created by dailin on 2018/1/31. * 商品管理controller */ @Controller @RequestMapping(value = "/item") public class ItemController { @Autowired ItemService itemService; @RequestMapping(value = "/items/{id}") @ResponseBody public TbItem getItemById(@PathVariable(value = "id") Long itemId){ return itemService.getItemById(itemId); } } ~~~ 配置 ~~~ <?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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:component-scan base-package="e3mall.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> <!-- 配置资源映射 --> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/js/" mapping="/js/**"/> <!-- 引用dubbo服务 --> <dubbo:application name="e3-manager"/> <dubbo:registry protocol="zookeeper" address="192.168.56.130:2181"/> // id="itemService" 怎么写不重要,dubbo会根据接口类型,自动找到service实现类,并注入 <dubbo:reference interface="e3mall.service.ItemService" id="itemService" /> </beans> ~~~