application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test? serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
server.port=9091
Copy the code
pom.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: / / http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd ">
The < modelVersion > 4.0.0 < / modelVersion >
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
< version > 2.1.4. RELEASE < / version >
<relativePath/> <! -- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringDemo</artifactId>
< version > 0.0.1 - the SNAPSHOT < / version >
<name>SpringDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
< Java version > 1.8 < / Java. Version >
</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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
The < version > 2.1.1 < / version >
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</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-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Copy the code
SuddleData.java
package com.example.demo.entity;
import java.util.Date;
import javax.persistence.*;
@Entity
public class SuddleData {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
public String name;
public String sex;
public int getId(a) {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String getName(a) {
return name;
}
public void setSex(String sex) {
this.sex = sex;
}
public String name(a) {
returnsex; }}Copy the code
SuddleService.java
package com.example.demo.services;
import java.util.List;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;
import com.example.demo.dao.SuddleDao;
import com.example.demo.entity.SuddleData;
@Service
public class SuddleService {
@Resource
private SuddleDao sdao;
@Transactional
public boolean ExistsById(Integer id) {
return sdao.existsById(id);
}
@Transactional
public void save(SuddleData model) {
sdao.save(model);
}
// @Transactional
// public void update(SuddleDao model)
// {
// sdao.update(model);
/ /}
public List<SuddleData> list(a) {
return sdao.findAll();
}
@Transactional
public void delete(SuddleData model) { sdao.delete(model); }}Copy the code
SuddleDao.java
package com.example.demo.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.entity.SuddleData;
@Repository
public interface SuddleDao extends JpaRepository<SuddleData.Integer>{
// void update(SuddleDao model);
// If jPA does not satisfy the query then write your own SQL query
// @Query(value = "SELECT u FROM User u where u.userName = ? 1 and u.email = ? 2)"
// List
queryUserNameAndEmail(String name, String email);
}
Copy the code
SuddleController.java
package com.example.demo.controller;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.entity.SuddleData;
import com.example.demo.services.SuddleService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@RestController
public class SuddleController {
@Autowired
private SuddleService sService;
@RequestMapping(value = "/getsuddleId", method = RequestMethod.POST)
public String getByName(@RequestParam Integer id) {
int Id;
boolean b = sService.ExistsById(id);
if (b) {
return "The id is exist.";
}
return "user id is not exist.";
}
@RequestMapping(value = "/savesuddle", method = RequestMethod.POST)
public String save(@RequestParam String username, @RequestParam String sex) {
try {
SuddleData model = new SuddleData();
model.setName(username);
model.setSex(sex);
sService.save(model);
} catch (Exception e) {
// TODO: handle exception
throw e;
} finally {
return "save success."; }}}Copy the code