Skip to content

Commit 81c4d95

Browse files
committed
使用SQL关系型数据库-事务处理
1 parent f9f9ff7 commit 81c4d95

34 files changed

+1219
-0
lines changed

spring-boot-demo-15-1/pom.xml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.roncoo.education</groupId>
7+
<artifactId>spring-boot-demo-15-1</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-demo-15-1</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.4.0.RELEASE</version>
18+
<relativePath /> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-devtools</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-freemarker</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.webjars</groupId>
38+
<artifactId>jquery</artifactId>
39+
<version>2.1.4</version>
40+
</dependency>
41+
42+
<!-- 数据库 -->
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-data-jpa</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>mysql</groupId>
49+
<artifactId>mysql-connector-java</artifactId>
50+
<scope>runtime</scope>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-test</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
</dependencies>
59+
60+
<build>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-maven-plugin</artifactId>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
69+
70+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.roncoo.example;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.web.servlet.ServletComponentScan;
6+
7+
@ServletComponentScan
8+
@SpringBootApplication
9+
public class SpringBootDemo151Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(SpringBootDemo151Application.class, args);
13+
}
14+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.roncoo.example.bean;
2+
3+
import java.util.Date;
4+
5+
public class RoncooUser {
6+
private int id;
7+
private String name;
8+
private Date createTime;
9+
10+
public int getId() {
11+
return id;
12+
}
13+
14+
public void setId(int id) {
15+
this.id = id;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
26+
public Date getCreateTime() {
27+
return createTime;
28+
}
29+
30+
public void setCreateTime(Date createTime) {
31+
this.createTime = createTime;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return "RoncooUser [id=" + id + ", name=" + name + ", createTime=" + createTime + "]";
37+
}
38+
39+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.roncoo.example.bean;
2+
3+
import java.util.Date;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
10+
@Entity
11+
public class RoncooUserLog {
12+
13+
@Id
14+
@GeneratedValue
15+
private Integer id;
16+
17+
@Column
18+
private Date createTime;
19+
20+
@Column
21+
private String userName;
22+
23+
@Column
24+
private String userIp;
25+
26+
public Integer getId() {
27+
return id;
28+
}
29+
30+
public void setId(Integer id) {
31+
this.id = id;
32+
}
33+
34+
public Date getCreateTime() {
35+
return createTime;
36+
}
37+
38+
public void setCreateTime(Date createTime) {
39+
this.createTime = createTime;
40+
}
41+
42+
public String getUserName() {
43+
return userName;
44+
}
45+
46+
public void setUserName(String userName) {
47+
this.userName = userName;
48+
}
49+
50+
public String getUserIp() {
51+
return userIp;
52+
}
53+
54+
public void setUserIp(String userIp) {
55+
this.userIp = userIp;
56+
}
57+
58+
@Override
59+
public String toString() {
60+
return "RoncooUserLog [id=" + id + ", createTime=" + createTime + ", userName=" + userName + ", userIp=" + userIp + "]";
61+
}
62+
63+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.roncoo.example.controller;
2+
3+
import java.util.HashMap;
4+
5+
import org.springframework.web.bind.annotation.CrossOrigin;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
import org.springframework.web.bind.annotation.RequestParam;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping("/api")
13+
public class ApiController {
14+
15+
@CrossOrigin(origins = "http://localhost:8080")
16+
@RequestMapping(value = "/get", method = RequestMethod.POST)
17+
public HashMap<String, Object> get(@RequestParam String name) {
18+
HashMap<String, Object> map = new HashMap<String, Object>();
19+
map.put("title", "hello world");
20+
map.put("name", name);
21+
return map;
22+
}
23+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.roncoo.example.controller;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.UUID;
6+
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.stereotype.Controller;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestParam;
12+
import org.springframework.web.bind.annotation.ResponseBody;
13+
import org.springframework.web.multipart.MultipartFile;
14+
15+
@Controller
16+
@RequestMapping(value = "/file")
17+
public class FileController {
18+
19+
private static final Logger logger = LoggerFactory.getLogger(FileController.class);
20+
21+
@RequestMapping(value = "upload")
22+
@ResponseBody
23+
public String upload(@RequestParam("roncooFile") MultipartFile file) {
24+
if (file.isEmpty()) {
25+
return "文件为空";
26+
}
27+
28+
// 获取文件名
29+
String fileName = file.getOriginalFilename();
30+
logger.info("上传的文件名为:" + fileName);
31+
32+
// 获取文件的后缀名
33+
String suffixName = fileName.substring(fileName.lastIndexOf("."));
34+
logger.info("上传的后缀名为:" + suffixName);
35+
36+
// 文件上传路径
37+
String filePath = "d:/roncoo/education/";
38+
39+
// 解决中文问题,liunx下中文路径,图片显示问题
40+
fileName = UUID.randomUUID() + suffixName;
41+
42+
File dest = new File(filePath + fileName);
43+
44+
// 检测是否存在目录
45+
if (!dest.getParentFile().exists()) {
46+
dest.getParentFile().mkdirs();
47+
}
48+
49+
try {
50+
file.transferTo(dest);
51+
return "上传成功";
52+
} catch (IllegalStateException e) {
53+
e.printStackTrace();
54+
} catch (IOException e) {
55+
e.printStackTrace();
56+
}
57+
return "上传失败";
58+
}
59+
60+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.roncoo.example.controller;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.ModelMap;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
9+
@Controller
10+
@RequestMapping("/web")
11+
public class WebController {
12+
13+
private static final Logger logger = LoggerFactory.getLogger(WebController.class);
14+
15+
@RequestMapping("index")
16+
public String index(ModelMap map){
17+
logger.info("这里是controller");
18+
map.put("title", "hello world");
19+
return "index"; // 注意,不要在最前面加上/,linux下面会出错
20+
}
21+
22+
@RequestMapping("error")
23+
public String error(ModelMap map){
24+
throw new RuntimeException("测试异常");
25+
}
26+
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.roncoo.example.dao;
2+
3+
import com.roncoo.example.bean.RoncooUser;
4+
import com.roncoo.example.util.base.Page;
5+
6+
public interface RoncooUserDao {
7+
8+
int insert(RoncooUser roncooUser);
9+
10+
int deleteById(int id);
11+
12+
int updateById(RoncooUser roncooUser);
13+
14+
RoncooUser selectById(int id);
15+
16+
Page<RoncooUser> queryForPage(int i, int j, String string);
17+
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.roncoo.example.dao;
2+
3+
import org.springframework.data.domain.Page;
4+
import org.springframework.data.domain.Pageable;
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.data.jpa.repository.Query;
7+
8+
import com.roncoo.example.bean.RoncooUserLog;
9+
10+
public interface RoncooUserLogDao extends JpaRepository<RoncooUserLog, Integer>{
11+
12+
@Query(value="select u from RoncooUserLog u where u.userName=?1")
13+
RoncooUserLog findByUserName(String string);
14+
15+
RoncooUserLog findByUserNameAndUserIp(String string, String ip);
16+
17+
Page<RoncooUserLog> findByUserName(String string, Pageable pageable);
18+
}

0 commit comments

Comments
 (0)