登陆案例

一、创建Service项目存放共同数据

1.1  创建实体类

 private long id;
  private String loginName;
  private String userName;
  private String password;
  private long userpaymentPass;
  private long sex;
  private String email;
  private String mobile;
  private long type;
  private long userownedcouponsid;
  private String openId;

  public long getId() {
    return id;
  }

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

  public String getLoginName() {
    return loginName;
  }

  public void setLoginName(String loginName) {
    this.loginName = loginName;
  }

  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 long getUserpaymentPass() {
    return userpaymentPass;
  }

  public void setUserpaymentPass(long userpaymentPass) {
    this.userpaymentPass = userpaymentPass;
  }

  public long getSex() {
    return sex;
  }

  public void setSex(long sex) {
    this.sex = sex;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getMobile() {
    return mobile;
  }

  public void setMobile(String mobile) {
    this.mobile = mobile;
  }

  public long getType() {
    return type;
  }

  public void setType(long type) {
    this.type = type;
  }

  public long getUserownedcouponsid() {
    return userownedcouponsid;
  }

  public void setUserownedcouponsid(long userownedcouponsid) {
    this.userownedcouponsid = userownedcouponsid;
  }

  public String getOpenId() {
    return openId;
  }

  public void setOpenId(String openId) {
    this.openId = openId;
  }

1.2 创建Service接口

public interface UserService {
    public void userServicePro();

   EasybuyUser getUserService(String loginName, String password);
}

二、创建消费者Provider项目

2.1 创建Dao层接口

@Repository
public interface User {
    @Select("select * from easybuy_user where loginName=#{loginName} and password=#{password} ")
    EasybuyUser getUser(@Param("loginName") String loginName, @Param("password") String password);
}

2.2 创建ServiecImpl实现类

@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private User user;

    @Override
    public EasybuyUser getUserService(String loginName, String password) {
        return user.getUser(loginName,password);
    }
}

2.3 创建applicationContext.xml文件将项目注入到注册中心

 <context:component-scan base-package="com.dubbo"/>

    <!--加载配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--SqlSessionFactory-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--加载数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--加载Mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <!--扫描Dao层-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dubbo.dao"/>
    </bean>

    <!--声明服务提供方-->
    <dubbo:application name="provider"/>
    <!--注册中心地址-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!--dubbo服务端口-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--服务注册-->
    <dubbo:service interface="com.dubbo.service.DoSomeService" ref="doSomeService"/>

    <dubbo:service interface="com.dubbo.service.UserService" ref="userService"/>

2.4 创建jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/easybuy?useUniCode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123

2.5 创建测试类运行 配置文件

@Test
    public void shouldAnswerWithTrue() throws IOException {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext-provider.xml");

        classPathXmlApplicationContext.start();

        System.out.println("生产者已建立");
        //阻塞
        System.in.read();
    }

三、创建Consumer项目

3.1 创建login.jsp页面

<body class="login">
    <div>
      <a class="hiddenanchor" id="signup"></a>
      <a class="hiddenanchor" id="signin"></a>

      <div class="login_wrapper">
        <div class="animate form login_form">
          <section class="login_content">
            <form action="/getUser" method="post">
              <h1>后台管理系统</h1>
              <div>
                <input type="text" class="form-control" name="loginName" placeholder="请输入用户名" required="" />
              </div>
              <div>
                <input type="password" class="form-control" name="password" placeholder="请输入密码" required="" />
              </div>
              <span>${error }</span>
              <div>
                  <button type="submit" class="btn btn-success">登     录</button>
                  <button type="reset" class="btn btn-default">重 填</button>
              </div>

              <div class="clearfix"></div>

              <div class="separator">
                <div>
                  <p>©2016 All Rights Reserved. </p>
                </div>
              </div>
            </form>
          </section>
        </div>
      </div>
    </div>
  </body>

3.1 创建Controller层

@Controller
public class controller {

    @RequestMapping(value = "/getUser",method = RequestMethod.POST)
    public String getUser(String loginName,String password){
        ApplicationContext ctx=new ClassPathXmlApplicationContext( "applicationContext-consumer.xml");
        UserService userService =(UserService) ctx.getBean("userService");
        EasybuyUser userService1 = userService.getUserService(loginName, password);
        System.out.println("用户"+userService1.getUserName());
        return "index";
    }
}

3.2 创建applicationContext-consumer.xml文件将项目注入到注册中心

<context:component-scan base-package="com.dubbo"/>
    <!--添加MVC支持-->
    <mvc:annotation-driven/>
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--声明服务提供方-->
    <dubbo:application name="consumer"/>
    <!--注册中心地址-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!--服务消费-->
    <dubbo:reference interface="com.dubbo.service.DoSomeService" id="doSomeService"/>

    <!--服务消费-->
    <dubbo:reference interface="com.dubbo.service.UserService" id="userService"/>
05-11 16:14