Skip to content

Commit

Permalink
上传整合jpa项目
Browse files Browse the repository at this point in the history
  • Loading branch information
codingxin committed Oct 26, 2018
1 parent 821036f commit 75a11a1
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 40 deletions.
9 changes: 5 additions & 4 deletions fanke-start/fanke-start/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ buildscript {
}

}
bootRun {
//开启页面热加载功能
addResources = true
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
Expand All @@ -42,7 +39,11 @@ dependencies {
// https://mvnrepository.com/artifact/nz.net.ultraq.thymeleaf/thymeleaf-layout-dialect
compile group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect', version: '2.3.0'

// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.13'

compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.0.6.RELEASE'
runtime('com.h2database:h2:1.4.197')

implementation('org.springframework.boot:spring-boot-starter-web')
testImplementation('org.springframework.boot:spring-boot-starter-test')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ public String hello() {
@GetMapping
public ModelAndView list(Model model) {
System.out.println("123");
model.addAttribute("userList", userRepository.listUser());
model.addAttribute("userList", userRepository.findAll());
model.addAttribute("title", "用户管理");
return new ModelAndView("users/list", "userModel", model);
}


/**
* 根据id来查询用户
*
Expand All @@ -56,7 +55,7 @@ public ModelAndView list(Model model) {
*/
@GetMapping("{id}") //默认 id为字符串String类型
public ModelAndView finduserByid(@PathVariable("id") Long id, Model model) {
User user = userRepository.getUserById(id);
User user = userRepository.findById(id).get();
model.addAttribute("user", user);
model.addAttribute("title", "查看用户");
return new ModelAndView("users/view", "userModel", model);
Expand All @@ -70,16 +69,51 @@ public ModelAndView finduserByid(@PathVariable("id") Long id, Model model) {
*/
@GetMapping("/form")
public ModelAndView createForm(Model model) {
model.addAttribute("user", new User());
model.addAttribute("user", new User(null, null, null));
model.addAttribute("title", "创建用户");
return new ModelAndView("users/form", "userModel", model);
}

/**
* 修改页面
*
* @param model
* @return
*/
@GetMapping("/modify/{id}")
public ModelAndView modifyusers(@PathVariable("id") Long id, Model model) {
User user = userRepository.findById(id).get();
model.addAttribute("user", user);
model.addAttribute("title", "修改用户");
return new ModelAndView("users/form", "userModel", model);
}


/**
* 表单提交保存数据
*
* @param user
* @param model
* @return
*/
@PostMapping
public ModelAndView saveOrUpdateUser(User user, Model model) {
user = userRepository.saveOrUpdateUser(user); //新建的用户,给他赋值id
user = userRepository.save(user); //最初方法新建的用户,给他赋值id
return new ModelAndView("users/form", "userModel", model);
}

/**
* 删除用户
*
* @param id
* @return
*/
@GetMapping("/delete/{id}")
public ModelAndView delete(@PathVariable("id") Long id) {
//userRepository.deleteUser(id);
//Modelandview 设置重定向
return new ModelAndView("redirect:/users");
}


}
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package com.zhang.springboot.blog.fankestart.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
* @author created by Zhangdazhuang
* @version v.0.1
* @date 2018/10/23
* @备注 用户实体
**/
@Entity
public class User {
Long id; //实体唯一标识
String name;
String email;
public User(){
@Id //主键
@GeneratedValue(strategy = GenerationType.IDENTITY)//生成策略
Long id; //实体唯一标识
String name;
String email;

protected User() {
//设为protected ,防止直接使用
}

public User(Long id, String name, String email) {
this.id = id;
this.name = name;
Expand Down Expand Up @@ -42,4 +52,9 @@ public String getEmail() {
public void setEmail(String email) {
this.email = email;
}

@Override
public String toString() {
return String.format("User[id=%d,name='%s',email='%s']",id,name,email);
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
package com.zhang.springboot.blog.fankestart.repository;

import com.zhang.springboot.blog.fankestart.entity.User;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
* @author created by Zhangdazhuang
* @version v.0.1
* @date 2018/10/23
* @备注 测试
* @备注 继承CrudRepository,有各种实现
**/
public interface UserRepository {
public interface UserRepository extends CrudRepository<User, Long> {
/**
* 创建或者修改用户
*
* @param user
* @return
*/
public User saveOrUpdateUser(User user);
/* public User saveOrUpdateUser(User user);
/**
*//**
* 删除用户
*
* @param id
*/
*//*
public void deleteUser(Long id);
/**
*//**
* 根据id穿用户
*
* @param id
* @return
*/
*//*
public User getUserById(Long id);
/**
*//**
* 获取用户列表
*
* @return
*/
*//*
public List<User> listUser();

*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* @备注 实现 User各种方法
**/
//@Repository对应数据访问层Bean ,例如:
@Repository
public class UserRepositoryImpl implements UserRepository {
//@Repository
public class UserRepositoryImpl {
//唯一标示id,用于计数。每次递增
private static AtomicLong counter=new AtomicLong();
//ConcurrentHashMap它是HashMap的一个线程安全的、支持高效并发的版本
Expand All @@ -27,7 +27,7 @@ public class UserRepositoryImpl implements UserRepository {
*/
private final ConcurrentMap<Long, User> userMap = new ConcurrentHashMap<>();

@Override

public User saveOrUpdateUser(User user) {
Long id=user.getId();
if(id==null) {
Expand All @@ -39,17 +39,17 @@ public User saveOrUpdateUser(User user) {
return user;
}

@Override

public void deleteUser(Long id) {
this.userMap.remove(id);
}

@Override

public User getUserById(Long id) {
return userMap.get(id);
}

@Override

public List<User> listUser() {
//new ArrayList<类型>(包装的数据)
return new ArrayList<User>(this.userMap.values());
Expand Down
17 changes: 15 additions & 2 deletions fanke-start/fanke-start/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@
########################################################
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
# ;charset=<encoding> is added
# set to false for hot refresh
spring.thymeleaf.cache=false
spring.h2.console.enabled=true

spring.datasource.url = jdbc:mysql://localhost:3306/db_testboot?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

spring.jpa.show-sql = true
#×Ô¶¯É¾³ý±í
spring.jpa.hibernate.ddl-auto=create-drop
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
</head>
<body>
<div th:replace="~{fragments/header :: header}"></div>
<h3 th:text="${userModel.title}">静态显示文本被覆盖</h3>
<!--<h3 th:text="${userModel.title}"></h3>-->
<!--<form action="/users" ></form> 都可以-->
<form action="/users" th:action="@{/users}" method="post">
<input type="hidden" name="id">
<!--th:object拿到user变量-->
<form action="/users" th:action="@{/users}" method="post" th:object="${userModel.user}">
<!-- <input type="hidden" name="id" th:value="${userModel.user.id}">-->
<!--两种都可以找到id,下面采用选择表达式-->
<input type="hidden" name="id" th:value="*{id}">
名称:<br>
<input type="text" name="name">
<input type="text" name="name" th:value="*{name}"><br>
邮箱:<br>
<input type="text" name="email">
<input type="text" name="email" th:value="*{email}">
<input type="submit" value="提交">
</form>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
</head>
<body>

<!--引入头部信息-->
<!--引入头部片段-->
<div th:replace="fragments/header :: header"></div>
<h3 th:text="${userModel.title}"></h3>
<div>
<a href="/users/form.html" th:href="@{/users/form}">创建用户</a>
</div>



<table border="1">
<thead>
<tr>
Expand All @@ -31,7 +30,8 @@ <h3 th:text="${userModel.title}"></h3>
</tr>
<tr th:each="user : ${userModel.userList}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<!--<td th:text="${user.name}"></td>-->
<td><a th:href="@{'/users/'+${user.id}}" th:text="${user.name}"></a></td>
<td th:text="${user.email}"></td>
</tr>
</tbody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@
<title>Thymeleaf in action</title>
</head>
<body>
<p>21231</p>
<p>21231</p>
<div th:replace="~{fragments/header :: header}"></div>
<h3 th:text="${userModel.title}">静态显示文本被覆盖</h3>
<div>
<p><strong>ID:</strong><span th:text="${userModel.user.id}"></span></p>
<p><strong>Name:</strong><span th:text="${userModel.user.name}"></span></p>
<p><strong>Email:</strong><span th:text="${userModel.user.email}"></span></p>
</div>



<div>
<a th:href="@{'/users/delete/'+${userModel.user.id}}">删除</a>
<a th:href="@{'/users/modify/'+${userModel.user.id}}">修改</a>
</div>



<div th:replace="~{fragments/footer :: footer}"></div>


</body>
</html>

0 comments on commit 75a11a1

Please sign in to comment.