Dict.CN 在线词典, 英语学习, 在线翻译 ------------- MyGitee 朱秋贵内科诊所 My腾云code

dubbo-case 20200327

c# 开源群 328035181   

 

代码

master.zip

https://gitee.com/sf2016/ssm-case/repository/archive/master.zip

1、common

 1-1 TestService.java

package fv.service;

import fv.pojo.Test;

import java.util.List;

/**
* @author: 
* @Date: 2020/3/4 17:38
*/
public interface TestService {
Test getTestById(int id);
List<Test> getAllTest();
boolean addTest(Test test);
boolean updateTest(Test test);
boolean deleteTest(int id);
}

2、serviceProvider

  2-1 spring-mybatis.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="fv">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
 
<!-- 引入数据库配置文件 -->
<bean id = "propertyConfigurer"
class = "org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name = "location" value="classpath:jdbc.properties" />
</bean>
<!-- 连接池 -->
<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 name="maxActive" value="${jdbc.maxActive}"/>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${jdbc.maxIdle}" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${jdbc.minIdle}" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${jdbc.maxWait}" />
</bean>
 
<!-- 结合Spring和Mybatis -->
<bean id = "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name = "mapperLocations" value="classpath:fv/dao/TestMapper.xml" />
</bean>
 
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name = "basePackage" value="fv.dao" />
<property name = "sqlSessionFactoryBeanName" value = "sqlSessionFactory" />
</bean>
 
<!-- 定义事务 -->
<bean id = "transactionManager"
class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name = "dataSource" ref = "dataSource" />
</bean>
<!-- 使用注解定义事务 -->
<tx:annotation-driven transaction-manager = "transactionManager" />
<import resource="classpath:spring-dubbo-provider2.xml"></import>
</beans>
 
 2-2 spring-dubbo-provider2.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:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
<!--
<dubbo:application dubbo应用
name应用名
ower="yhptest" 拥有者
-->
<dubbo:application name="serviceProvider" owner="yhptest"></dubbo:application>
 
<!--
<dubbo:protocol dubbo协议
name="dubbo" 使用dubbo协议
port="20880" 服务提供都监听接口,同一电脑上面服务提供者的监听端口不能冲突
-->
<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
 
<!--局域网广播注册中心-->
<dubbo:registry address="multicast://239.5.6.3:12346">
<!--<dubbo:registry address="127.0.0.1:2181" protocol="zookeeper">-->
 
</dubbo:registry>
<!--配置式发布-->
<!--spring配置本地bean
<bean id="userService" class="cn.itsource.dubbo.service.impl.UserServiceImpl"></bean>-->
<!--把本地bean注成远程服务
ref 哪个本地bean
interface 以什么接口来暴露为远程
 
<dubbo:service interface="cn.itsource.dubbo.service.IUserService" ref="userService">
</dubbo:service>
-->
<!--注解发布-->
<!--扫描注解包路径,多个包用逗号隔开,不填pacaker表示扫描当前applicationContext中所有的类-->
<dubbo:annotation package="fv.imp"/>
</beans>
 2-3 jdbc.properties
 
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/gomall?useSSL=false
jdbc.username=root
jdbc.password=root
#定义初始连接数
jdbc.initialSize=0
#定义最大连接数
jdbc.maxActive=20
#定义最大空闲
jdbc.maxIdle=20
#定义最小空闲
jdbc.minIdle=1
#定义最长等待时间
jdbc.maxWait=60000
 2-4dubbo.properties
 
 
dubbo.spring.config=classpath:spring-mybatis.xml
 
2-5 TestSeviceImp.java
package fv.imp;/**
* @author: 
* @Date: 2020/3/4 17:45
*/
 
import fv.dao.TestMapper;
import fv.pojo.Test;
import fv.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
/**
* @program: ssmcase
*
* @description:
*
* @author: Zhuqf
*
* @Date: 2020/3/4 17:45
*/
@com.alibaba.dubbo.config.annotation.Service
//@Service
public class TestSeviceImp implements TestService {
@Autowired(required = false)
private TestMapper testMapper=null;
 
 
public Test getTestById(int id) {
return testMapper.selectTestById(id);
}
 
public List<Test> getAllTest() {
return testMapper.getAllTest();
}
 
public boolean addTest(Test test) {
 
boolean flag=false;
try{
testMapper.addTest(test);
flag=true;
}catch (Exception e){
e.printStackTrace();
}
return flag;
}
 
public boolean updateTest(Test test) {
boolean flag=false;
try{
testMapper.updateTest(test);
flag=true;
}catch (Exception e){
e.printStackTrace();
}
return flag;
}
 
public boolean deleteTest(int id) {
boolean flag=false;
try{
testMapper.deleteTest(id);
flag=true;
}catch (Exception e){
e.printStackTrace();
}
return flag;
}
}
2-6 DubboMain.java
package fv;/**
* @author: 
* @Date: 2020/2/26 15:08
*/
 
import com.alibaba.dubbo.container.Main;
 
import java.io.IOException;
 
/**
* @program: dubbodemo2
*
* @description:
*
* @author: 
*
* @Date: 2020/2/26 15:08
*/
public class DubboMain {
 
public static void main(String[] args) throws IOException{
/* ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring-dubbo-provider2.xml");
context.start();
System.out.println("服务注册成功!");
System.in.read();//一直读*/
Main.main(args);
System.out.println("服务注册成功!");
}
 
}
2-7 MainTest.java
package fv;
 
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import java.io.IOException;
 
public class MainTest {
public static void main(String[] args) throws IOException {
//System.out.println(StringUtils.trim("SDFSFSF 1231231312DSFSDFS"));
//创建ApplicationContext 实际上就是创建初始化了spring
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring-dubbo-provider2.xml");
//查看spring里面所的的bean
String[] beanDefinitionNames=context.getBeanDefinitionNames();
for(String string:beanDefinitionNames)
{
System.out.println(string);
}
System.out.println("服务注册成功!");
System.in.read();//一直读
//为了能够正常接收请求,需要阻载线程,不让他结束
}
}
 
 
 3-1readme.txt
http://localhost:8099/api/test/1
http://localhost:8099/api/test/all
http://localhost:8099/api/test/add?test1=62&test2=test62
http://localhost:8099/api/test/update?test1=6262&test2=test6262&id=62
http://localhost:8099/api/test/delete/66

http://localhost:8099/api/test/list

3-2 TestContorller.java 

package com.dubbo.ssmcaseconsumeboot;

/**
* @author: 
* @Date: 2020/3/9 17:47
*/

import com.alibaba.dubbo.config.annotation.Reference;
import fv.pojo.Test;
import fv.service.TestService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* @program: ssmcaseconsumeboot
*
* @description:
*
* @author:
*
* @Date: 2020/3/9 17:47
*/
//@RestController// rest接口(json格式)是解析不到html
@Controller
@RequestMapping(value = "/api")
public class TestContorller {
@Resource
@Reference
private TestService testService;

 

@GetMapping("/test/{id}")
@ResponseBody
public Test getTestById(@PathVariable int id)
{
return testService.getTestById(id);
}

@GetMapping(value = "/test/all")
@ResponseBody
public List<Test> getAllTest()
{
return testService.getAllTest();
}

@RequestMapping(value = "/test/add")
public boolean addTest(Test test)
{
return testService.addTest(test);
}

@RequestMapping(value = "/test/update")
//@RequestMapping(value = "/test")
public boolean updateTest(Test test)
{
return testService.updateTest(test);
}

//@RequestMapping(value = "/api/test",method = RequestMethod.DELETE)
@RequestMapping(value = "/test/delete/{id}")
public boolean deleteTest(@PathVariable("id") int id)
{
return testService.deleteTest(id);
}

/* @GetMapping(value = "/test/list")
public String getAllTest(Model model)
{
List<Test> testList=testService.getAllTest();
//model.addAttribute("tests",testList);
return "api/test/list12";
}*/

//@GetMapping(value = "/test/list")
/* public String list(Model model)
{
List<Test> testList=testService.getAllTest();
model.addAttribute("tests",testList);
return "/api/test/list";
}*/

@GetMapping(value = "/test/list")
public String getTestList(Model model)
{
List<Test> testList=testService.getAllTest();
model.addAttribute("tests",testList);
return "testList";
}
}

  3-3 SsmcaseconsumebootApplication .java

package com.dubbo.ssmcaseconsumeboot;

import com.alibaba.dubbo.config.annotation.Reference;
import fv.service.TestService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

@ImportResource(locations = {"classpath:spring-dubbo-consumer2.xml"})
@SpringBootApplication
public class SsmcaseconsumebootApplication {
public static void main(String[] args) {
//SpringApplication.run(SsmcaseconsumebootApplication.class, args);
ApplicationContext applicationContext=SpringApplication.run(SsmcaseconsumebootApplication.class,args);
TestService testService=applicationContext.getBean(TestService.class);
System.out.println(testService.getTestById(1));
System.out.println(testService.getAllTest());
fv.pojo.Test test=new fv.pojo.Test();
test.setTest1(3);
test.setTest2("test3");
System.out.println(testService.addTest(test));
fv.pojo.Test tes2=new fv.pojo.Test();
tes2.setTest1(3);
tes2.setTest2("test3");
tes2.setId(3);
System.out.println(testService.updateTest(tes2));
System.out.println(testService.deleteTest(14));
}

}

 
posted @ 2020-03-27 10:44  cn2024  阅读(149)  评论(0编辑  收藏  举报