MySQL数据库
建立cat表并存储数据
建立后端
1 点击create new project新建一个项目。
2 点击spring initializr建立springboot项目,点击next
3 填写group和artifact,也可以不修改直接点next
4 点击web,选择web,点击next
5 填写projectName和project location,点击finish。
6 建立的项目目录如图所示
7 配置pom.xml
<?xml<?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 http://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.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JSTL (JSP standard Tag Library) JSP 标准标签库 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!-- <scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--连接数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--支持通过jdbc连接数据库-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
8 配置application.properties(与MySQL数据库相连)
mybatis.mapperLocations=classpath:mapper/*.xml
#端口号server.port=8080
#session失效时间server.session-timeout=30
spring.datasource.url=jdbc:mysql://localhost:3306/mysql?characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
9 在src中创建包controller、entity、mapper、service、service.impl。在resources中创建包mapper。
userController
package com.example.demo.Controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@ResponseBody
@RequestMapping("/findAll")
public List<User> findAll( ) {
List<User> list = userService.findAll();
return list;
}
@ResponseBody
@RequestMapping("/count")
public int count(){
int count=userService.count();
System.out.println(count);
return count;
}
}
user:
package com.example.demo.entity;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public String getName() {
return this.name;
}
public Integer getAge() {
return age;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public User(){
}
public User(int id){
this.id=id;
}
public User(int age, String name){
this.age=age;
this.name=name;
}
@Override
public String toString() {
return super.toString();
}
}
userMapper:
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
List<User> findAll();
int count();
}
userservice
package com.example.demo.service;
import com.example.demo.entity.User;
import java.util.List;
public interface UserService {
List<User> findAll();
int count();
}
userserviceImpl
package com.example.demo.service.Impl;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("UserService")
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
List<User> list = userMapper.findAll();
return list; }
@Override
public int count() {
int count=userMapper.count();
return count;
}
}
userMapper.xml
<?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.example.demo.mapper.UserMapper">
<select id="findAll" resultType="com.example.demo.entity.User">
SELECT * FROM cat
</select>
<select id="count" resultType="java.lang.Integer">
select count(*) from cat
</select>
</mapper>
建立前端react:
1 在根目录下建立react项目web
2 建立好的目录如下图所示
在app.js中用ajax取值。
import React, {Component} from 'react';
import './App.css';
import $ from 'jquery';
class App extends Component{
constructor(props)
{
super(props);
this.state = {
}
}
componentDidMount()
{
//先执行Ajax数据请求,全局的get方法
var source="/user/findAll";
this.serverRequest = $.get(source, function (result) {
alert(result);
this.setState({
result
});
}.bind(this));
}
//组件的render方法
render()
{
let {result=[]}=this.state;
return (
<div>
<div>
<h1>111111111111</h1>
</div>
<div>
<table className='table'>
<thead>
<tr>
<th>id</th>
<th>姓名</th>
</tr>
</thead>
<tbody>
{result.map(({id, name}) =>
<tr>
<td>{id}</td>
<td>{name}</td>
</tr>
)}
</tbody>
</table>
</div>
</div>
);
}
}
export default App;
要使用jquery需要先添加,在引用
import $ from'jquery';
要使用后端的数据,需要在package.json中添加
"proxy": "http://localhost:8080"
设置npm
启动前端选start,启动后端选demoApplication,
同时启动后,输入http://localhost:3000/#
显示页面
userMapper显示红线时,需要早fileàsettings中找inspections将autowiring for bean class 中的√取消掉。