后端微服务框架初步完成(还没跑起来)
This commit is contained in:
		@@ -0,0 +1,13 @@
 | 
			
		||||
package com.cxyxiaomo.epp;
 | 
			
		||||
 | 
			
		||||
import org.springframework.boot.SpringApplication;
 | 
			
		||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
 | 
			
		||||
 | 
			
		||||
// 启动类
 | 
			
		||||
 | 
			
		||||
@SpringBootApplication
 | 
			
		||||
public class UserProvider {
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        SpringApplication.run(UserProvider.class, args);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,32 @@
 | 
			
		||||
package com.cxyxiaomo.epp.controller;
 | 
			
		||||
 | 
			
		||||
import com.cxyxiaomo.epp.pojo.User;
 | 
			
		||||
import com.cxyxiaomo.epp.service.UserServiceImpl;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.web.bind.annotation.*;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
// 提供 restful 服务
 | 
			
		||||
 | 
			
		||||
@RestController
 | 
			
		||||
public class UserController {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private UserServiceImpl userService;
 | 
			
		||||
 | 
			
		||||
    @PostMapping("/user/add")
 | 
			
		||||
    public boolean addUser(User user) {
 | 
			
		||||
        return userService.addUser(user);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @GetMapping("/user/get/{id}")
 | 
			
		||||
    public User getUserById(@PathVariable("id") Long id) {
 | 
			
		||||
        return userService.getUserById(id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @GetMapping("/user/list")
 | 
			
		||||
    public List<User> addUser() {
 | 
			
		||||
        return userService.getUserList();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,18 @@
 | 
			
		||||
package com.cxyxiaomo.epp.dao;
 | 
			
		||||
 | 
			
		||||
import com.cxyxiaomo.epp.pojo.User;
 | 
			
		||||
import org.apache.ibatis.annotations.Mapper;
 | 
			
		||||
import org.springframework.stereotype.Repository;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
@Mapper
 | 
			
		||||
@Repository
 | 
			
		||||
public interface UserDao {
 | 
			
		||||
 | 
			
		||||
    public boolean addUser(User user);
 | 
			
		||||
 | 
			
		||||
    public User getUserById(Long id);
 | 
			
		||||
 | 
			
		||||
    public List<User> getUserList();
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,14 @@
 | 
			
		||||
package com.cxyxiaomo.epp.service;
 | 
			
		||||
 | 
			
		||||
import com.cxyxiaomo.epp.pojo.User;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
public interface UserService {
 | 
			
		||||
 | 
			
		||||
    public boolean addUser(User user);
 | 
			
		||||
 | 
			
		||||
    public User getUserById(Long id);
 | 
			
		||||
 | 
			
		||||
    public List<User> getUserList();
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,30 @@
 | 
			
		||||
package com.cxyxiaomo.epp.service;
 | 
			
		||||
 | 
			
		||||
import com.cxyxiaomo.epp.dao.UserDao;
 | 
			
		||||
import com.cxyxiaomo.epp.pojo.User;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
@Service
 | 
			
		||||
public class UserServiceImpl implements UserService {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private UserDao userDao;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean addUser(User user) {
 | 
			
		||||
        return userDao.addUser(user);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public User getUserById(Long id) {
 | 
			
		||||
        return userDao.getUserById(id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<User> getUserList() {
 | 
			
		||||
        return userDao.getUserList();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,21 @@
 | 
			
		||||
server:
 | 
			
		||||
  port: 8001
 | 
			
		||||
 | 
			
		||||
# Mybatis 配置
 | 
			
		||||
mybatis:
 | 
			
		||||
  type-aliases-package: com.cxyxiaomo.epp.pojo
 | 
			
		||||
  config-location: classpath:mybatis/mybatis-config.xml
 | 
			
		||||
  mapper-locations: classpath:mybatis/mapper/*.xml
 | 
			
		||||
  configuration:
 | 
			
		||||
    map-underscore-to-camel-case: on
 | 
			
		||||
 | 
			
		||||
# Spring 配置
 | 
			
		||||
spring:
 | 
			
		||||
  application:
 | 
			
		||||
    name: microservice-provider-user
 | 
			
		||||
  datasource:
 | 
			
		||||
    type: com.alibaba.druid.pool.DruidDataSource
 | 
			
		||||
    driver-class-name: com.mysql.cj.jdbc.Driver
 | 
			
		||||
    url: jdbc:mysql://127.0.0.1:3306/epp?useUnicode=true&characterEncoding=utf8&useSSL=false
 | 
			
		||||
    username: root
 | 
			
		||||
    password: root
 | 
			
		||||
@@ -0,0 +1,20 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8" ?>
 | 
			
		||||
<!DOCTYPE mapper
 | 
			
		||||
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 | 
			
		||||
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 | 
			
		||||
<mapper namespace="com.cxyxiaomo.epp.dao.UserDao">
 | 
			
		||||
 | 
			
		||||
    <insert id="addUser" parameterType="com.cxyxiaomo.epp.pojo.User">
 | 
			
		||||
        INSERT INTO user (username, password)
 | 
			
		||||
        VALUES (#{username}, #{password})
 | 
			
		||||
    </insert>
 | 
			
		||||
 | 
			
		||||
    <select id="getUserById" parameterType="java.lang.Long" resultType="com.cxyxiaomo.epp.pojo.User">
 | 
			
		||||
        SELECT * FROM user
 | 
			
		||||
        WHERE id = #{id}
 | 
			
		||||
    </select>
 | 
			
		||||
 | 
			
		||||
    <select id="getUserList" resultType="com.cxyxiaomo.epp.pojo.User">
 | 
			
		||||
        SELECT * FROM user
 | 
			
		||||
    </select>
 | 
			
		||||
</mapper>
 | 
			
		||||
@@ -0,0 +1,10 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8" ?>
 | 
			
		||||
<!DOCTYPE configuration
 | 
			
		||||
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 | 
			
		||||
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
 | 
			
		||||
<configuration>
 | 
			
		||||
    <settings>
 | 
			
		||||
        <!--开启二级缓存-->
 | 
			
		||||
        <setting name="cacheEnabled" value="true"/>
 | 
			
		||||
    </settings>
 | 
			
		||||
</configuration>
 | 
			
		||||
@@ -0,0 +1,20 @@
 | 
			
		||||
package com.cxyxiaomo.epp;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.assertTrue;
 | 
			
		||||
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Unit test for simple App.
 | 
			
		||||
 */
 | 
			
		||||
public class AppTest
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Rigorous Test :-)
 | 
			
		||||
     */
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldAnswerWithTrue()
 | 
			
		||||
    {
 | 
			
		||||
        assertTrue( true );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user