标签:直接 system mysq package err ctc provider imp 不用
数据库名称为Product;
public class Product implements Serializable { private Integer pid; private String productName; private Integer quantity; public Product(Integer pid, String productname, Integer quantity) { this.pid = pid; this.productName = productname; this.quantity = quantity; } public Product() { super(); } public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public Integer getQuantity() { return quantity; } public void setQuantity(Integer quantity) { this.quantity = quantity; } }
公共模块可以达到通用目的,也即需要用到部门实体的话,不用每个工程都定义一份,直接引用本模块即可。
<dependencies>
<!-- mybatis-spring --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency>
<dependency> <groupId>com.wn</groupId> <artifactId>springcloud_api</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
</dependencies>
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql:///invoicingsystem spring.datasource.username=root spring.datasource.password=123 spring.jpa.show-sql=true mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
@Repository("productDao") public interface ProductDao { //查询全部 public List<Product> getAll(); //根据id查询列表 public Product getid(@Param("pid") Integer pid); }
<?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指定Dao接口的完整类名 mybatis会依据这个接口动态创建一个实现类去实现这个接口, 而这个实现类是一个Mapper对象--> <mapper namespace="com.wn.springcloud_provider.dao.ProductDao"> <resultMap id="MapList" type="com.wn.springcloud_api.entity.Product"> <id property="pid" column="pid"></id> <result property="productName" column="productName"></result> <result property="quantity" column="quantity"></result> </resultMap> <!--绑定商品名称下拉框--> <select id="getAll" resultType="com.wn.springcloud_api.entity.Product"> SELECT * FROM product </select> <select id="getid" resultType="com.wn.springcloud_api.entity.Product"> SELECT * FROM product WHERE pid=#{pid} </select> </mapper>
public interface ProductService { //查询全部 public List<Product> getAll(); //根据id查询列表 public Product getid(Integer pid); }
@Service("productServices") public class ProductServiceImpl implements ProductService { @Resource(name = "productDao") private ProductDao dao; @Override public List<Product> getAll() { return dao.getAll(); } @Override public Product getid(Integer pid) { return dao.getid(pid); } }
@Controller @RequestMapping("/product") public class ProductController { @Resource(name = "productServices") private ProductService service; //查询全部 @RequestMapping(value = "/getAll",method = RequestMethod.GET) @ResponseBody public List<Product> getAll() { List<Product> all = service.getAll(); for (Product product:all){ System.out.println(product.getProductName()); } return all; } @RequestMapping(value = "/getid/{pid}",method = RequestMethod.GET) @ResponseBody public Product getid(@PathVariable("pid") Integer pid){ Product getid = service.getid(pid); return getid; } }
@SpringBootApplication @MapperScan("com.wn.springcloud_provider.*") public class SpringcloudProviderApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudProviderApplication.class, args); } }
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.wn</groupId> <artifactId>springcloud_api</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies>
server.port=8081
package com.wn.springcloud_consumer.Bean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ConfigBean { @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
RestTemplate提供了多种便捷访问远程Http服务的方法, 是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集
package com.wn.springcloud_consumer.controller; import com.wn.springcloud_api.entity.Product; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; @RestController public class ProductController { private static final String REST_URL_PREFIX="http://localhost:8080"; @Autowired private RestTemplate restTemplate; //查询全部 @SuppressWarnings("unckecked") @RequestMapping("/controller/product/getAll") public List<Product> getAll(){ System.out.println("--------------------"); return restTemplate.getForObject(REST_URL_PREFIX+"/product/getAll",List.class); } //根据id查询列表 @RequestMapping("/controller/product/getid/{pid}") public Product getid(@PathVariable("pid") Integer pid){ return restTemplate.getForObject(REST_URL_PREFIX+"/product/getid/"+pid,Product.class); } }
@SpringBootApplication public class SpringcloudConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudConsumerApplication.class, args); } }
标签:直接 system mysq package err ctc provider imp 不用
原文地址:https://www.cnblogs.com/wnwn/p/12033217.html