Skip to content

Commit 4597ada

Browse files
committed
add spring-boot-thymeleaf):简单 spring boot thymeleaf 脚手架 - [spring-boot-jpa-thymeleaf-curd](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-jpa-thymeleaf-curd):spring boot + jpa + thymeleaf 增删改查示例
1 parent b254a27 commit 4597ada

40 files changed

+21290
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Spring boot使用的各种示例,以最简单、最实用为标准
2222
- [spring-boot-package-war](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-package-war):spring-boot打包成war包示例
2323
- [spring-boot-shiro](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-shiro):springboot 整合shiro rbac示例
2424
- [spring-boot-thymeleaf](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-thymeleaf):简单 spring boot thymeleaf 脚手架
25+
- [spring-boot-jpa-thymeleaf-curd](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-jpa-thymeleaf-curd):spring boot + jpa + thymeleaf 增删改查示例
2526
- [Favorites-web](https://github.com/cloudfavorites/favorites-web):云收藏(springboot实战开源软件)
2627

2728

README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ Spring Boot Examples, Use the simplest and most useful scene demo.
2323
- [spring-boot-package-war](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-package-war):Spring Boot package war
2424
- [spring-boot-shiro](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-shiro):spring boot shiro rbac demo
2525
- [spring-boot-thymeleaf](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-thymeleaf):simple spring boot thymeleaf demo
26+
spring-boot-thymeleaf):简单 spring boot thymeleaf 脚手架
27+
- [spring-boot-jpa-thymeleaf-curd](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-jpa-thymeleaf-curd):spring boot + jpa + thymeleaf curd demo
2628
- [Favorites-web](https://github.com/cloudfavorites/favorites-web):Open source projects developed using Spring Boot
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>spring-boot-jpa-thymeleaf-curd</artifactId>
5+
<name>spring-boot-jpa-thymeleaf-curd</name>
6+
<description>spring-boot-jpa-thymeleaf-curd</description>
7+
<parent>
8+
<groupId>org.springframework.boot</groupId>
9+
<artifactId>spring-boot-starter-parent</artifactId>
10+
<version>1.5.6.RELEASE</version>
11+
</parent>
12+
13+
<properties>
14+
<java.version>1.8</java.version>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-web</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-data-jpa</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>mysql</groupId>
32+
<artifactId>mysql-connector-java</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-devtools</artifactId>
37+
<optional>true</optional>
38+
</dependency>
39+
</dependencies>
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-maven-plugin</artifactId>
45+
<configuration>
46+
<fork>true</fork>
47+
</configuration>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.neo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.builder.SpringApplicationBuilder;
6+
import org.springframework.boot.web.support.SpringBootServletInitializer;
7+
8+
9+
@SpringBootApplication
10+
public class JpaThymeleafApplication extends SpringBootServletInitializer {
11+
@Override
12+
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
13+
return application.sources(JpaThymeleafApplication.class);
14+
}
15+
16+
public static void main(String[] args) throws Exception {
17+
SpringApplication.run(JpaThymeleafApplication.class, args);
18+
}
19+
}
20+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.neo.entity;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.Id;
7+
8+
@Entity
9+
public class User {
10+
@Id
11+
@GeneratedValue
12+
private long id;
13+
@Column(nullable = false, unique = true)
14+
private String userName;
15+
@Column(nullable = false)
16+
private String password;
17+
@Column(nullable = false)
18+
private int age;
19+
20+
public long getId() {
21+
return id;
22+
}
23+
24+
public User setId(long id) {
25+
this.id = id;
26+
return this;
27+
}
28+
29+
public String getUserName() {
30+
return userName;
31+
}
32+
33+
public User setUserName(String userName) {
34+
this.userName = userName;
35+
return this;
36+
}
37+
38+
public String getPassword() {
39+
return password;
40+
}
41+
42+
public User setPassword(String password) {
43+
this.password = password;
44+
return this;
45+
}
46+
47+
public int getAge() {
48+
return age;
49+
}
50+
51+
public User setAge(int age) {
52+
this.age = age;
53+
return this;
54+
}
55+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.neo.repository;
2+
3+
import com.neo.entity.User;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface UserRepository extends JpaRepository<User, Long> {
7+
8+
User findById(long id);
9+
10+
Long deleteById(Long id);
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.neo.service;
2+
3+
import com.neo.entity.User;
4+
5+
import java.util.List;
6+
7+
public interface UserService {
8+
9+
public List<User> getUserList();
10+
11+
public User findUserById(long id);
12+
13+
public void save(User user);
14+
15+
public void edit(User user);
16+
17+
public void delete(long id);
18+
19+
20+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.neo.service.impl;
2+
3+
import com.neo.entity.User;
4+
import com.neo.repository.UserRepository;
5+
import com.neo.service.UserService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Service;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
@Service
13+
public class UserServiceImpl implements UserService{
14+
15+
@Autowired
16+
private UserRepository userRepository;
17+
18+
@Override
19+
public List<User> getUserList() {
20+
return userRepository.findAll();
21+
}
22+
23+
@Override
24+
public User findUserById(long id) {
25+
return userRepository.findById(id);
26+
}
27+
28+
@Override
29+
public void save(User user) {
30+
userRepository.save(user);
31+
}
32+
33+
@Override
34+
public void edit(User user) {
35+
userRepository.save(user);
36+
}
37+
38+
@Override
39+
public void delete(long id) {
40+
userRepository.delete(id);
41+
}
42+
}
43+
44+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.neo.web;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
8+
@Controller
9+
public class HelloController {
10+
11+
@RequestMapping("/hello")
12+
public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
13+
model.addAttribute("name", name);
14+
return "hello";
15+
}
16+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.neo.web;
2+
3+
import com.neo.entity.User;
4+
import com.neo.service.UserService;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.stereotype.Service;
7+
import org.springframework.ui.Model;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestParam;
10+
11+
import javax.annotation.Resource;
12+
import java.util.List;
13+
14+
@Controller
15+
public class UserController {
16+
17+
@Resource
18+
UserService userService;
19+
20+
21+
@RequestMapping("/")
22+
public String index() {
23+
return "redirect:/list";
24+
}
25+
26+
@RequestMapping("/list")
27+
public String list(Model model) {
28+
List<User> users=userService.getUserList();
29+
model.addAttribute("users", users);
30+
return "user/list";
31+
}
32+
33+
@RequestMapping("/toAdd")
34+
public String toAdd() {
35+
return "user/userAdd";
36+
}
37+
38+
@RequestMapping("/add")
39+
public String add(User user) {
40+
userService.save(user);
41+
return "redirect:/list";
42+
}
43+
44+
45+
@RequestMapping("/toEdit")
46+
public String toEdit(Model model,Long id) {
47+
User user=userService.findUserById(id);
48+
model.addAttribute("user", user);
49+
return "user/userEdit";
50+
}
51+
52+
@RequestMapping("/edit")
53+
public String edit(User user) {
54+
userService.edit(user);
55+
return "redirect:/list";
56+
}
57+
58+
59+
@RequestMapping("/delete")
60+
public String delete(Long id) {
61+
userService.delete(id);
62+
return "redirect:/list";
63+
}
64+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
spring.datasource.url=jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
2+
spring.datasource.username=root
3+
spring.datasource.password=root
4+
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
5+
6+
spring.jpa.properties.hibernate.hbm2ddl.auto=update
7+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
8+
spring.jpa.show-sql= true
9+
10+
spring.thymeleaf.cache=false

0 commit comments

Comments
 (0)