Skip to content

Commit

Permalink
update user insert and insert address
Browse files Browse the repository at this point in the history
  • Loading branch information
leelance committed Jul 16, 2014
1 parent ce9aaf4 commit 840f02d
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ public String getProvince() {
public void setProvince(String province) {
this.province = province;
}
public UserEntity getUser() {
return user;
}
public void setUser(UserEntity user) {
this.user = user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public abstract class BaseEntity {
//0:无效 1:有效
protected int status;
//创建信息
protected String createById;
protected long createById;
protected Date createDate;
protected String updateById;
protected long updateById;
protected Date updateDate;

public long getId() {
Expand All @@ -31,10 +31,10 @@ public long getId() {
public void setId(long id) {
this.id = id;
}
public String getCreateById() {
public long getCreateById() {
return createById;
}
public void setCreateById(String createById) {
public void setCreateById(long createById) {
this.createById = createById;
}
public Date getCreateDate() {
Expand All @@ -43,10 +43,10 @@ public Date getCreateDate() {
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public String getUpdateById() {
public long getUpdateById() {
return updateById;
}
public void setUpdateById(String updateById) {
public void setUpdateById(long updateById) {
this.updateById = updateById;
}
public Date getUpdateDate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.LinkedList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
Expand All @@ -25,7 +26,7 @@ public class UserEntity extends BaseEntity {
private String password;

//地址信息
@OneToMany(mappedBy="user")
@OneToMany(mappedBy="user",cascade=CascadeType.ALL)
private List<AddressEntity> addresses = new LinkedList<AddressEntity>();

@OneToMany(mappedBy="blongUser")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ public interface UserService {
* @return
*/
List<UserEntity> findAll();

/**
* save user
* @param user
*/
void save(UserEntity user) ;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lance.entity.AddressEntity;
import com.lance.entity.UserEntity;
import com.lance.repository.UserRepository;

Expand All @@ -20,4 +21,15 @@ public class UserServiceImpl implements UserService{
public List<UserEntity> findAll(){
return userRepository.findAll();
}

/**
* save user
* @param user
*/
public void save(UserEntity user) {
for(AddressEntity address: user.getAddresses()) {
address.setUser(user);
}
userRepository.save(user);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.lance.web;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lance.entity.UserEntity;
import com.lance.service.UserService;
import com.lance.utils.CurrentUserUtils;

@Controller
@RequestMapping("/user/")
Expand All @@ -23,4 +27,18 @@ public class UserController {
public String findAll(){
return JSON.toJSONString(userService.findAll());
}

/**
* save user object
* @return
*/
@RequestMapping("post")
public String post(UserEntity user){
UserEntity curUser = CurrentUserUtils.getInstance().getUser();
user.setCreateById(curUser.getId());
user.setCreateDate(new Date());

userService.save(user);
return "redirect:/user/home/adduser";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
.user-title{
line-height: 40px;
font-weight: bold;
}
.user-addr-row{
padding: 5px;
}
.addr-group-border{
border-top: 1px solid #ccc;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ var AddUser = {
$('#home-left li').removeClass("active").eq(3).prop('class','active');

this.queryUser();

this.saveUser();

this.handlerAddress();
},

/**
Expand All @@ -12,6 +16,41 @@ var AddUser = {
$.getJSON("/user/list", function(json){
$("#user-tmpl").tmpl(json).appendTo("#query-user");
});

//处理name下显示/隐藏地址信息
$("#query-user").on('click', '.user-name-hide', function(){
$(this).next().toggle();
});
},

/**
* save user
*/
saveUser: function() {
$("#saveBtn").on('click', function(){
$("#user-form").submit();
});
},

/**
* handler address
* add address/ delete address
*/
handlerAddress: function(){
$(".address-btn").on('click', 'a',function(){
var len = $('#user-form .addr-group-border').length;
var data = {
cityName: 'addresses['+len+'].city',
proName: 'addresses['+len+'].province',
addName: 'addresses['+len+'].address'
}
$('#user-addr-tmpl').tmpl(data).appendTo("#user-form");
});

$("#user-form").on('click','.address-del-btn a',function(){
console.log($(this).parents('.addr-group-border'));
$(this).parents('.addr-group-border').remove();
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
<link rel="stylesheet" href="/css/customer/home.css">
<link rel="stylesheet" href="/css/customer/user.css">

<!-- JS file -->
<script src="http://code.jquery.com/jquery.js"></script>

</head>
<body>
<!-- header -->
Expand Down Expand Up @@ -67,7 +64,7 @@
<h4 class="modal-title" id="myLargeModalLabel">Add User</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form">
<form class="form-horizontal" role="form" action="/user/post" method="post" id="user-form">
<div class="form-group">
<span class="col-sm-1"></span>
<label for="inputName3" class="col-sm-2 control-label">Name</label>
Expand Down Expand Up @@ -100,12 +97,39 @@
<div class="col-sm-6">
<input type="tel" class="form-control" id="inputPhone3" placeholder="Phone" name="tel">
</div>
</div>
<!-- Address -->
<div class="form-group addr-group-border">
<div class="row user-addr-row">
<span class="col-sm-1"></span>
<label class="col-sm-2 control-label">City</label>
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="City" name="addresses[0].city">
</div>
<div class="col-sm-1 address-btn">
<a href="#" class="btn btn-primary" id="add-address">add</a>
</div>
</div>
<div class="row user-addr-row">
<span class="col-sm-1"></span>
<label class="col-sm-2 control-label">Province</label>
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="Province" name="addresses[0].province">
</div>
</div>
<div class="row user-addr-row">
<span class="col-sm-1"></span>
<label class="col-sm-2 control-label">Address</label>
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="Address" name="addresses[0].address">
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-primary" id="saveBtn">Save changes</button>
</div>
</div>
</div>
Expand All @@ -115,7 +139,20 @@
<script id="user-tmpl" type="text/x-jquery-tmpl">
<tr>
<td><input type="checkbox"></td>
<td>\${name}</td>
<td>
<a href="#" class="user-name-hide">\${name}</a>
<div style="display: none;">
<table class="table table-bordered table-striped">
{{each addresses}}
<tr>
<td>\${city}</td>
<td>\${province}</td>
<td>\${address}</td>
</tr>
{{/each}}
</table>
</div>
</td>
<td>\${email}</td>
<td>
{{if sex == 1}}
Expand All @@ -125,11 +162,41 @@
{{/if}}
</td>
<td>\${tel}</td>
<td><a href="#">Del<input type="hidden" value="\${id}"></a></td>
<td><a href="/user/del/\${id}" class="user-del">Del</a></td>
</tr>
</script>
<!-- 地址模板 -->
<script id="user-addr-tmpl" type="text/x-jquery-tmpl">
<div class="form-group addr-group-border">
<div class="row user-addr-row">
<span class="col-sm-1"></span>
<label class="col-sm-2 control-label">City</label>
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="City" name="\${cityName}">
</div>
<div class="col-sm-1 address-del-btn">
<a href="#" class="btn btn-primary">del</a>
</div>
</div>
<div class="row user-addr-row">
<span class="col-sm-1"></span>
<label class="col-sm-2 control-label">Province</label>
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="Province" name="\${proName}">
</div>
</div>
<div class="row user-addr-row">
<span class="col-sm-1"></span>
<label class="col-sm-2 control-label">Address</label>
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="Address" name="\${addName}">
</div>
</div>
</div>
</script>

<!-- JS file -->
<script src="http://code.jquery.com/jquery.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/plugins/jquery.tmpl.min.js"></script>
<script src="/js/customer/AddUser.js"></script>
Expand Down

0 comments on commit 840f02d

Please sign in to comment.