axios+springboot获取数据库信息

1.加入依赖

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository  -->
    </parent>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

2.entity实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Entity
@Table(name = "tbl_user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long uid;
    private String username;
    private String password;
    private String qx;
}

3.Repository层

public interface UserRepository extends JpaRepository<User,Long> {
}

4.service层

public interface UserService {
    User selectOne(Long id);
    List<User> selectAll();
}

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserRepository userRepository;

    @Override
    public User selectOne(Long id) {
        return userRepository.getOne(id);
    }

    @Override
    public List<User> selectAll() {
        return userRepository.findAll();
    }
}

5.controller

@Controller
public class UserController {
    @Resource
    UserService userService;

    @CrossOrigin
    @RequestMapping("/selectOne")
    @ResponseBody
    public String selectOne( Long id){
        User user = userService.selectOne(id);
        return ""+user;
    }

    @CrossOrigin
    @RequestMapping("/findAll")
    @ResponseBody
    public List<User> findAll(){
        List<User> users = userService.selectAll();
        System.out.println(users);
        return users;
    }

    @CrossOrigin
    @RequestMapping("/findString")
    public void findString(String name){
        System.out.println(name);
    }
}

axios方式1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script src="js/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios({
        url:'http://localhost/selectOne',
        params:{
            id:1
        },
    }).then(res=>{
        console.log(res)
    })
</script>
</body>
</html>

并发请求

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios.all([
        axios.get('http://localhost/findAll'),
        axios.get('http://localhost/selectOne?id=1')
    ]).then(
        axios.spread((res1,res2)=>{
            console.log(res1);
            console.log(res2);
        })
    )
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
<!--    axios 全局配置-->
    axios.defaults.baseURI='http://localhost';
    axios.defaults.timeout=5000;
    axios.get('/findAll').then(res=>{console.log(res)});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

<script>
    let newVar=axios.create({
        baseUrl:'http://localhost',
        timeout:5000,
    });//创建axios实例
    newVar({
        url:'findAll'
    }).then(
        res=>{console.log(res)}
    )
</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值