提交 5fdefc6e 编写于 作者: C Calvin

#57 将MyBatis重新加入showcase,...

#57 将MyBatis重新加入showcase, 而且充分使用了MyBatis-Spring,配置与原来比更漂亮,省掉了DAO的实现和Confiugration.xml里的alias定义, Dao的命名也不用叫自己不喜欢的XXXMapper.
上级 82c2cf5e
......@@ -2,13 +2,20 @@ package org.springside.examples.showcase.entity;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.google.common.collect.Lists;
/**
* 项目.
* 团队.
*
* @author calvin
*/
@Entity
@Table(name = "SS_TEAM")
public class Team extends IdEntity {
private String name;
......@@ -23,6 +30,7 @@ public class Team extends IdEntity {
this.name = name;
}
@OneToOne
public User getMaster() {
return master;
}
......@@ -31,6 +39,7 @@ public class Team extends IdEntity {
this.master = master;
}
@OneToMany
public List<User> getUserList() {
return userList;
}
......
......@@ -6,6 +6,7 @@ import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
......@@ -36,6 +37,8 @@ public class User extends IdEntity {
private String email;
private String status;
private Team team;
private List<Role> roleList = Lists.newArrayList(); //有序的关联对象集合
@NotBlank
......@@ -113,6 +116,15 @@ public class User extends IdEntity {
this.roleList = roleList;
}
@ManyToOne
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
}
@Transient
@JsonIgnore
public String getRoleNames() {
......
......@@ -14,7 +14,7 @@ public interface AccountDao {
public List<User> searchUser(Map<String, Object> parameters);
public Long saveUser(User user);
public void saveUser(User user);
public void deleteUser(Long id);
......
......@@ -103,9 +103,9 @@ public class AccountWebServiceImpl implements AccountWebService {
User userEntity = BeanMapper.map(user, User.class);
BeanValidators.validateWithException(validator, userEntity);
Long userId = accountDao.saveUser(userEntity);
accountDao.saveUser(userEntity);
return new IdResponse(userId);
return new IdResponse(userEntity.getId());
} catch (ConstraintViolationException e) {
String message = StringUtils.join(BeanValidators.extractPropertyAndMessageAsList(e, " "), "\n");
return handleParameterError(response, e, message);
......
......@@ -20,7 +20,7 @@
<!-- 获取部门详细信息, 输出用resultMap关联嵌套对象 -->
<select id="getTeamWithDetail" parameterType="int" resultMap="teamDetailMap">
select d.id as team_id,
select t.id as team_id,
t.name as team_name,
m.id as master_id,
m.name as master_name,
......@@ -28,7 +28,7 @@
u.id as user_id,
u.name as user_name,
u.email as user_email
from acct_team t, ss_user m, ss_user u
from ss_team t, ss_user m, ss_user u
where t.master_id=m.id and t.id=u.team_id
and t.id=#{id}
</select>
......
......@@ -7,33 +7,33 @@
drop table if exists ss_team;
create table ss_role (
id varchar(16),
id bigint generated by default as identity,
name varchar(255) not null unique,
primary key (id)
);
create table ss_user (
id varchar(16),
id bigint generated by default as identity,
login_name varchar(255) not null unique,
name varchar(64),
password varchar(255),
salt varchar(64),
email varchar(128),
status varchar(32),
team_id varchar(16),
team_id bigint,
primary key (id)
);
create table ss_user_role (
user_id varchar(16) not null,
role_id varchar(16) not null,
user_id bigint not null,
role_id bigint not null,
primary key (user_id, role_id)
);
create table ss_team (
id varchar(16),
id bigint generated by default as identity,
name varchar(255) not null unique,
master varchar(16),
master_id bigint,
primary key (id)
);
......
......@@ -9,6 +9,8 @@ import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springside.examples.showcase.data.UserData;
import org.springside.examples.showcase.entity.Team;
import org.springside.examples.showcase.entity.User;
import org.springside.modules.test.spring.SpringTransactionalTestCase;
......@@ -16,14 +18,14 @@ import com.google.common.collect.Maps;
@ContextConfiguration(locations = { "/applicationContext.xml" })
@TransactionConfiguration(transactionManager = "defaultTransactionManager")
public class MyBatisTest extends SpringTransactionalTestCase {
public class AccountDaoTest extends SpringTransactionalTestCase {
@Autowired
private AccountDao accountMapper;
private AccountDao accountDao;
@Test
public void getUser() throws Exception {
User user = accountMapper.getUser(1L);
User user = accountDao.getUser(1L);
assertEquals("admin", user.getLoginName());
}
......@@ -31,8 +33,34 @@ public class MyBatisTest extends SpringTransactionalTestCase {
public void searchUser() throws Exception {
Map<String, Object> parameter = Maps.newHashMap();
parameter.put("name", "Admin");
List<User> result = accountMapper.searchUser(parameter);
List<User> result = accountDao.searchUser(parameter);
assertEquals(1, result.size());
assertEquals("admin", result.get(0).getLoginName());
}
@Test
public void getTeamWithDetail() throws Exception {
Team team = accountDao.getTeamWithDetail(1L);
assertEquals("Dolphin", team.getName());
assertEquals("Admin", team.getMaster().getName());
}
@Test
public void createAndDeleteUser() throws Exception {
//create
int count = countRowsInTable("ss_user");
User user = UserData.randomUser();
accountDao.saveUser(user);
Long id = user.getId();
assertEquals(count + 1, countRowsInTable("ss_user"));
User result = accountDao.getUser(id);
assertEquals(user.getLoginName(), result.getLoginName());
//delete
accountDao.deleteUser(id);
assertEquals(count, countRowsInTable("ss_user"));
assertNull(accountDao.getUser(id));
}
}
......@@ -15,5 +15,5 @@
<SS_USER_ROLE USER_ID="4" ROLE_ID="2"/>
<SS_USER_ROLE USER_ID="5" ROLE_ID="2"/>
<SS_USER_ROLE USER_ID="6" ROLE_ID="2"/>
<SS_TEAM ID="1" NAME="Dolphin" MASTER="1"/>
<SS_TEAM ID="1" NAME="Dolphin" MASTER_ID="1"/>
</dataset>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册