提交 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; ...@@ -2,13 +2,20 @@ package org.springside.examples.showcase.entity;
import java.util.List; 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; import com.google.common.collect.Lists;
/** /**
* 项目. * 团队.
* *
* @author calvin * @author calvin
*/ */
@Entity
@Table(name = "SS_TEAM")
public class Team extends IdEntity { public class Team extends IdEntity {
private String name; private String name;
...@@ -23,6 +30,7 @@ public class Team extends IdEntity { ...@@ -23,6 +30,7 @@ public class Team extends IdEntity {
this.name = name; this.name = name;
} }
@OneToOne
public User getMaster() { public User getMaster() {
return master; return master;
} }
...@@ -31,6 +39,7 @@ public class Team extends IdEntity { ...@@ -31,6 +39,7 @@ public class Team extends IdEntity {
this.master = master; this.master = master;
} }
@OneToMany
public List<User> getUserList() { public List<User> getUserList() {
return userList; return userList;
} }
......
...@@ -6,6 +6,7 @@ import javax.persistence.Entity; ...@@ -6,6 +6,7 @@ import javax.persistence.Entity;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.JoinTable; import javax.persistence.JoinTable;
import javax.persistence.ManyToMany; import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy; import javax.persistence.OrderBy;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Transient; import javax.persistence.Transient;
...@@ -36,6 +37,8 @@ public class User extends IdEntity { ...@@ -36,6 +37,8 @@ public class User extends IdEntity {
private String email; private String email;
private String status; private String status;
private Team team;
private List<Role> roleList = Lists.newArrayList(); //有序的关联对象集合 private List<Role> roleList = Lists.newArrayList(); //有序的关联对象集合
@NotBlank @NotBlank
...@@ -113,6 +116,15 @@ public class User extends IdEntity { ...@@ -113,6 +116,15 @@ public class User extends IdEntity {
this.roleList = roleList; this.roleList = roleList;
} }
@ManyToOne
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
}
@Transient @Transient
@JsonIgnore @JsonIgnore
public String getRoleNames() { public String getRoleNames() {
......
...@@ -14,7 +14,7 @@ public interface AccountDao { ...@@ -14,7 +14,7 @@ public interface AccountDao {
public List<User> searchUser(Map<String, Object> parameters); public List<User> searchUser(Map<String, Object> parameters);
public Long saveUser(User user); public void saveUser(User user);
public void deleteUser(Long id); public void deleteUser(Long id);
......
...@@ -103,9 +103,9 @@ public class AccountWebServiceImpl implements AccountWebService { ...@@ -103,9 +103,9 @@ public class AccountWebServiceImpl implements AccountWebService {
User userEntity = BeanMapper.map(user, User.class); User userEntity = BeanMapper.map(user, User.class);
BeanValidators.validateWithException(validator, userEntity); BeanValidators.validateWithException(validator, userEntity);
Long userId = accountDao.saveUser(userEntity); accountDao.saveUser(userEntity);
return new IdResponse(userId); return new IdResponse(userEntity.getId());
} catch (ConstraintViolationException e) { } catch (ConstraintViolationException e) {
String message = StringUtils.join(BeanValidators.extractPropertyAndMessageAsList(e, " "), "\n"); String message = StringUtils.join(BeanValidators.extractPropertyAndMessageAsList(e, " "), "\n");
return handleParameterError(response, e, message); return handleParameterError(response, e, message);
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
<!-- 获取部门详细信息, 输出用resultMap关联嵌套对象 --> <!-- 获取部门详细信息, 输出用resultMap关联嵌套对象 -->
<select id="getTeamWithDetail" parameterType="int" resultMap="teamDetailMap"> <select id="getTeamWithDetail" parameterType="int" resultMap="teamDetailMap">
select d.id as team_id, select t.id as team_id,
t.name as team_name, t.name as team_name,
m.id as master_id, m.id as master_id,
m.name as master_name, m.name as master_name,
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
u.id as user_id, u.id as user_id,
u.name as user_name, u.name as user_name,
u.email as user_email 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 where t.master_id=m.id and t.id=u.team_id
and t.id=#{id} and t.id=#{id}
</select> </select>
......
...@@ -7,33 +7,33 @@ ...@@ -7,33 +7,33 @@
drop table if exists ss_team; drop table if exists ss_team;
create table ss_role ( create table ss_role (
id varchar(16), id bigint generated by default as identity,
name varchar(255) not null unique, name varchar(255) not null unique,
primary key (id) primary key (id)
); );
create table ss_user ( create table ss_user (
id varchar(16), id bigint generated by default as identity,
login_name varchar(255) not null unique, login_name varchar(255) not null unique,
name varchar(64), name varchar(64),
password varchar(255), password varchar(255),
salt varchar(64), salt varchar(64),
email varchar(128), email varchar(128),
status varchar(32), status varchar(32),
team_id varchar(16), team_id bigint,
primary key (id) primary key (id)
); );
create table ss_user_role ( create table ss_user_role (
user_id varchar(16) not null, user_id bigint not null,
role_id varchar(16) not null, role_id bigint not null,
primary key (user_id, role_id) primary key (user_id, role_id)
); );
create table ss_team ( create table ss_team (
id varchar(16), id bigint generated by default as identity,
name varchar(255) not null unique, name varchar(255) not null unique,
master varchar(16), master_id bigint,
primary key (id) primary key (id)
); );
......
...@@ -9,6 +9,8 @@ import org.junit.Test; ...@@ -9,6 +9,8 @@ import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration; 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.examples.showcase.entity.User;
import org.springside.modules.test.spring.SpringTransactionalTestCase; import org.springside.modules.test.spring.SpringTransactionalTestCase;
...@@ -16,14 +18,14 @@ import com.google.common.collect.Maps; ...@@ -16,14 +18,14 @@ import com.google.common.collect.Maps;
@ContextConfiguration(locations = { "/applicationContext.xml" }) @ContextConfiguration(locations = { "/applicationContext.xml" })
@TransactionConfiguration(transactionManager = "defaultTransactionManager") @TransactionConfiguration(transactionManager = "defaultTransactionManager")
public class MyBatisTest extends SpringTransactionalTestCase { public class AccountDaoTest extends SpringTransactionalTestCase {
@Autowired @Autowired
private AccountDao accountMapper; private AccountDao accountDao;
@Test @Test
public void getUser() throws Exception { public void getUser() throws Exception {
User user = accountMapper.getUser(1L); User user = accountDao.getUser(1L);
assertEquals("admin", user.getLoginName()); assertEquals("admin", user.getLoginName());
} }
...@@ -31,8 +33,34 @@ public class MyBatisTest extends SpringTransactionalTestCase { ...@@ -31,8 +33,34 @@ public class MyBatisTest extends SpringTransactionalTestCase {
public void searchUser() throws Exception { public void searchUser() throws Exception {
Map<String, Object> parameter = Maps.newHashMap(); Map<String, Object> parameter = Maps.newHashMap();
parameter.put("name", "Admin"); parameter.put("name", "Admin");
List<User> result = accountMapper.searchUser(parameter); List<User> result = accountDao.searchUser(parameter);
assertEquals(1, result.size()); assertEquals(1, result.size());
assertEquals("admin", result.get(0).getLoginName()); 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 @@ ...@@ -15,5 +15,5 @@
<SS_USER_ROLE USER_ID="4" ROLE_ID="2"/> <SS_USER_ROLE USER_ID="4" ROLE_ID="2"/>
<SS_USER_ROLE USER_ID="5" ROLE_ID="2"/> <SS_USER_ROLE USER_ID="5" ROLE_ID="2"/>
<SS_USER_ROLE USER_ID="6" 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> </dataset>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册