<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.dream</groupId> <artifactId>shop-provider-user</artifactId> <version>0.0.1-SNAPSHOT</version> <name>shop</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR3</spring-cloud.version> <mybatisplus-spring-boot-starter.version>1.0.5</mybatisplus-spring-boot-starter.version> <mybatis-plus.version>2.3</mybatis-plus.version> <fastjson.version>1.2.47</fastjson.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-function-context</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatisplus-spring-boot-starter</artifactId> <version>${mybatisplus-spring-boot-starter.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>${mybatis-plus.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generate</artifactId> <version>${mybatis-plus.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.xmljava
server: # 指定Tomcat端口 port: 8000 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/shop?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=UTC username: root password: root db-name: shop filters: wall,mergeStat mybatis-plus: mapper-locations: classpath:mapping/*.xml typeAliasesPackage: com.dream.shop.model configuration: map-underscore-to-camel-case: true #打印sql log-impl: org.apache.ibatis.logging.stdout.StdOutImpl logging: level: root: INFO # 配置日誌級別
MybatisPlusConfig.javamysql
package com.dream.shop.config; import com.baomidou.mybatisplus.plugins.PaginationInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * MybatisPlus配置 * * @author rain */ @Configuration @MapperScan(basePackages = {"com.dream.shop.dao"}) public class MybatisPlusConfig { /** * mybatis-plus分頁插件 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
UserController.javaweb
package com.dream.shop.controller; import com.dream.shop.dao.UserMapper; import com.dream.shop.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.Optional; /** * @author rain */ @RequestMapping("/users") @RestController public class UserController { @Resource public UserMapper userMapper; @GetMapping("/{id}") public User findById(@PathVariable Long id) { return this.userMapper.selectById(id); } }
UserMapper.xmlspring
<?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.dream.shop.dao.UserMapper"> <!-- 通用查詢映射結果 --> <resultMap id="BaseResultMap" type="com.dream.shop.model.User"> <id column="id" property="id" /> <result column="username" property="username" /> <result column="name" property="name" /> <result column="age" property="age" /> <result column="balance" property="balance" /> </resultMap> <!-- 通用查詢結果列 --> <sql id="Base_Column_List"> id,username, name, age, balance </sql> </mapper>
UserMapper.javasql
package com.dream.shop.dao; import com.baomidou.mybatisplus.mapper.BaseMapper; import com.dream.shop.model.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; /** * <p> * 用戶 Mapper 接口 * </p> * * @author it-park-team * @since 2018-11-29 */ //@Mapper public interface UserMapper extends BaseMapper<User> { }
User.javaapache
package com.dream.shop.model; import com.baomidou.mybatisplus.annotations.TableName; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import javax.persistence.*; import java.math.BigDecimal; /** * @author rain */ @Entity @Data @NoArgsConstructor @AllArgsConstructor @TableName("user") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String username; @Column private String name; @Column private Integer age; @Column private BigDecimal balance; }
啓動
http://localhost:8000/users/1json
{"id":1,"username":"rain","name":"rain","age":18,"balance":2.0}mybatis
MovieApplication.javaapp
package com.dream.shop; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication//(scanBasePackages = {"com.dream.shop"}) public class MovieApplication { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(MovieApplication.class, args); } }
MovieController.javamaven
package com.dream.shop.controller; import com.dream.shop.dao.UserMapper; import com.dream.shop.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; 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 javax.annotation.Resource; import java.util.Optional; /** * @author rain */ @RequestMapping("/movies") @RestController public class MovieController { @Autowired private RestTemplate restTemplate; @GetMapping("/users/{id}") public User findById(@PathVariable Long id) { // 這裏用到了RestTemplate的佔位符能力 User user = this.restTemplate.getForObject("http://localhost:8000/users/{id}", User.class, id); // ...電影微服務的業務... return user; } }
服務啓動
http://localhost:8010/movies/users/1
{"id":1,"username":"rain","name":"rain","age":18,"balance":2.0}
解決方案:
1.使用@Resource
2.增長@Mapper註解
3.增長@MapperScan(basePackages = {"com.dream.shop.dao"})