#57 将MyBatis重新加入showcase, 而且充分使用了MyBatis-Spring,配置与原来比更漂亮,省掉了DAO的实现和Confiugration.xml里的alias定义.
This commit is contained in:
-37
@@ -1,37 +0,0 @@
|
||||
package org.springside.examples.showcase.repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springside.examples.showcase.entity.Team;
|
||||
import org.springside.examples.showcase.entity.User;
|
||||
|
||||
@Component
|
||||
public class AccountMybatisDaoImpl implements AccountMybatisDao {
|
||||
|
||||
@Override
|
||||
public Team getTeamDetail(Long id) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> searchUser(Map<String, Object> parameters) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long saveUser(User user) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteUser(Long id) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.springside.examples.showcase.repository;
|
||||
package org.springside.examples.showcase.repository.jpa;
|
||||
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springside.examples.showcase.entity.User;
|
||||
+5
-3
@@ -1,4 +1,4 @@
|
||||
package org.springside.examples.showcase.repository;
|
||||
package org.springside.examples.showcase.repository.mybatis;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -6,9 +6,11 @@ import java.util.Map;
|
||||
import org.springside.examples.showcase.entity.Team;
|
||||
import org.springside.examples.showcase.entity.User;
|
||||
|
||||
public interface AccountMybatisDao {
|
||||
public interface AccountDao {
|
||||
|
||||
public Team getTeamDetail(Long id);
|
||||
public Team getTeamWithDetail(Long id);
|
||||
|
||||
public User getUser(Long id);
|
||||
|
||||
public List<User> searchUser(Map<String, Object> parameters);
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ import org.springside.examples.showcase.entity.User;
|
||||
import org.springside.examples.showcase.modules.cache.memcached.MemcachedObjectType;
|
||||
import org.springside.examples.showcase.modules.jms.simple.NotifyMessageProducer;
|
||||
import org.springside.examples.showcase.modules.jmx.ApplicationStatistics;
|
||||
import org.springside.examples.showcase.repository.UserDao;
|
||||
import org.springside.examples.showcase.repository.jpa.UserDao;
|
||||
import org.springside.examples.showcase.service.ShiroDbRealm.HashPassword;
|
||||
import org.springside.modules.cache.memcached.SpyMemcachedClient;
|
||||
import org.springside.modules.mapper.JsonMapper;
|
||||
|
||||
+3
-3
@@ -15,7 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springside.examples.showcase.entity.Team;
|
||||
import org.springside.examples.showcase.entity.User;
|
||||
import org.springside.examples.showcase.repository.AccountMybatisDao;
|
||||
import org.springside.examples.showcase.repository.mybatis.AccountDao;
|
||||
import org.springside.examples.showcase.webservice.soap.response.GetTeamDetailResponse;
|
||||
import org.springside.examples.showcase.webservice.soap.response.SearchUserResponse;
|
||||
import org.springside.examples.showcase.webservice.soap.response.base.IdResponse;
|
||||
@@ -40,7 +40,7 @@ public class AccountWebServiceImpl implements AccountWebService {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(AccountWebServiceImpl.class);
|
||||
@Autowired
|
||||
private AccountMybatisDao accountDao;
|
||||
private AccountDao accountDao;
|
||||
@Autowired
|
||||
private Validator validator;
|
||||
|
||||
@@ -54,7 +54,7 @@ public class AccountWebServiceImpl implements AccountWebService {
|
||||
|
||||
Validate.notNull(id, "id参数为空");
|
||||
|
||||
Team team = accountDao.getTeamDetail(id);
|
||||
Team team = accountDao.getTeamWithDetail(id);
|
||||
|
||||
Validate.notNull(team, "项目不存在(id:" + id + ")");
|
||||
|
||||
|
||||
@@ -42,6 +42,19 @@
|
||||
<!-- Spring Data Jpa配置 -->
|
||||
<jpa:repositories base-package="org.springside.examples.showcase" transaction-manager-ref="defaultTransactionManager" entity-manager-factory-ref="entityManagerFactory"/>
|
||||
|
||||
<!-- MyBatis配置 -->
|
||||
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<!-- 自动扫描目录,省掉Configuration.xml里的手工配置, 用','分隔多个package -->
|
||||
<property name="typeAliasesPackage" value="org.springside.examples.showcase.entity" />
|
||||
<!-- 没有把mapper文件放到与dao相同的深目录 -->
|
||||
<property name="mapperLocations" value="classpath:/mybatis/*Mapper.xml" />
|
||||
</bean>
|
||||
<!-- 扫描MyBatis Dao-->
|
||||
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
|
||||
<property name="basePackage" value="org.springside.examples.showcase.repository.mybatis" />
|
||||
</bean>
|
||||
|
||||
<!-- 事务管理器配置, Jpa单数据源事务 -->
|
||||
<bean id="defaultTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
|
||||
<property name="entityManagerFactory" ref="entityManagerFactory"/>
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
|
||||
<mapper namespace="org.springside.examples.showcase.repository.mybatis.AccountDao">
|
||||
<resultMap id="teamDetailMap" type="Team">
|
||||
<id property="id" column="team_id" />
|
||||
<result property="name" column="team_name" />
|
||||
<association property="master" column="master_id" javaType="User">
|
||||
<id property="id" column="master_id" />
|
||||
<result property="name" column="master_name" />
|
||||
<result property="email" column="master_email" />
|
||||
</association>
|
||||
<collection property="userList" javaType="User">
|
||||
<id property="id" column="user_id" />
|
||||
<result property="name" column="user_name" />
|
||||
<result property="email" column="user_email" />
|
||||
</collection>
|
||||
</resultMap>
|
||||
|
||||
<!-- 获取部门详细信息, 输出用resultMap关联嵌套对象 -->
|
||||
<select id="getTeamWithDetail" parameterType="int" resultMap="teamDetailMap">
|
||||
select d.id as team_id,
|
||||
t.name as team_name,
|
||||
m.id as master_id,
|
||||
m.name as master_name,
|
||||
m.email as master_email,
|
||||
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
|
||||
where t.master_id=m.id and t.id=u.team_id
|
||||
and t.id=#{id}
|
||||
</select>
|
||||
|
||||
<!--
|
||||
获取用户: 输出直接映射到对象, login_name列要"as loginName"以方便映射 team_id as "team.id"创建team对象并赋值
|
||||
-->
|
||||
<select id="getUser" parameterType="int" resultType="User">
|
||||
select id, name, email,
|
||||
login_name as loginName,
|
||||
team_id as "team.id"
|
||||
from ss_user
|
||||
where id=#{id}
|
||||
</select>
|
||||
|
||||
<!-- 查询用户,演示: 1.输入用map传入多个参数 2.<where>语句, 智能添加where和and关键字 3.输出直接映射对象 -->
|
||||
<select id="searchUser" parameterType="map" resultType="User">
|
||||
select id, name, email,
|
||||
login_name as loginName,
|
||||
team_id as "team.id"
|
||||
from ss_user
|
||||
<where>
|
||||
<if test="loginName != null">
|
||||
login_name=#{loginName}
|
||||
</if>
|
||||
<if test="name != null">
|
||||
and name=#{name}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<!-- 插入用户: 1.由数据库生成id并赋值到user对象 2.输入用对象, 嵌套属性表达式#{team.id} -->
|
||||
<insert id="saveUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
|
||||
insert into ss_user (
|
||||
login_name, name, password, email, team_id)
|
||||
values (
|
||||
#{loginName}, #{name}, #{password}, #{email},
|
||||
#{team.id})
|
||||
</insert>
|
||||
|
||||
<!-- 删除用户 -->
|
||||
<delete id="deleteUser" parameterType="int">
|
||||
delete from ss_user where id=#{id}
|
||||
</delete>
|
||||
</mapper>
|
||||
@@ -3,6 +3,8 @@
|
||||
drop table if exists ss_role;
|
||||
|
||||
drop table if exists ss_user;
|
||||
|
||||
drop table if exists ss_team;
|
||||
|
||||
create table ss_role (
|
||||
id varchar(16),
|
||||
@@ -18,6 +20,7 @@
|
||||
salt varchar(64),
|
||||
email varchar(128),
|
||||
status varchar(32),
|
||||
team_id varchar(16),
|
||||
primary key (id)
|
||||
);
|
||||
|
||||
@@ -27,13 +30,11 @@
|
||||
primary key (user_id, role_id)
|
||||
);
|
||||
|
||||
create table ss_team (
|
||||
id varchar(16),
|
||||
name varchar(255) not null unique,
|
||||
master varchar(16),
|
||||
primary key (id)
|
||||
);
|
||||
|
||||
alter table ss_user_role
|
||||
add constraint FK1306854BF125651E
|
||||
foreign key (user_id)
|
||||
references ss_user;
|
||||
|
||||
alter table ss_user_role
|
||||
add constraint FK1306854B4BFAA13E
|
||||
foreign key (role_id)
|
||||
references ss_role;
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.springside.examples.showcase.repository;
|
||||
package org.springside.examples.showcase.repository.jpa;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package org.springside.examples.showcase.repository.mybatis;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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.entity.User;
|
||||
import org.springside.modules.test.spring.SpringTransactionalTestCase;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
@ContextConfiguration(locations = { "/applicationContext.xml" })
|
||||
@TransactionConfiguration(transactionManager = "defaultTransactionManager")
|
||||
public class MyBatisTest extends SpringTransactionalTestCase {
|
||||
|
||||
@Autowired
|
||||
private AccountDao accountMapper;
|
||||
|
||||
@Test
|
||||
public void getUser() throws Exception {
|
||||
User user = accountMapper.getUser(1L);
|
||||
assertEquals("admin", user.getLoginName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void searchUser() throws Exception {
|
||||
Map<String, Object> parameter = Maps.newHashMap();
|
||||
parameter.put("name", "Admin");
|
||||
List<User> result = accountMapper.searchUser(parameter);
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("admin", result.get(0).getLoginName());
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -8,7 +8,7 @@ import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springside.examples.showcase.entity.User;
|
||||
import org.springside.examples.showcase.repository.UserDao;
|
||||
import org.springside.examples.showcase.repository.jpa.UserDao;
|
||||
import org.springside.examples.showcase.service.AccountService;
|
||||
import org.springside.examples.showcase.service.ServiceException;
|
||||
import org.springside.modules.test.security.shiro.ShiroTestUtils;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
<?xml version='1.0' encoding='UTF-8'?>
|
||||
<dataset>
|
||||
<SS_USER ID="1" EMAIL="admin@springside.org.cn" LOGIN_NAME="admin" NAME="Admin" PASSWORD="691b14d79bf0fa2215f155235df5e670b64394cc" SALT="7efbd59d9741d34f" STATUS="enabled"/>
|
||||
<SS_USER ID="2" EMAIL="user@springside.org.cn" LOGIN_NAME="user" NAME="Calvin" PASSWORD="2488aa0c31c624687bd9928e0a5d29e7d1ed520b" SALT="6d65d24122c30500" STATUS="enabled"/>
|
||||
<SS_USER ID="3" EMAIL="jack@springside.org.cn" LOGIN_NAME="user2" NAME="Jack" PASSWORD="2488aa0c31c624687bd9928e0a5d29e7d1ed520b" SALT="6d65d24122c30500" STATUS="enabled"/>
|
||||
<SS_USER ID="4" EMAIL="kate@springside.org.cn" LOGIN_NAME="user3" NAME="Kate" PASSWORD="2488aa0c31c624687bd9928e0a5d29e7d1ed520b" SALT="6d65d24122c30500" STATUS="enabled"/>
|
||||
<SS_USER ID="5" EMAIL="sawyer@springside.org.cn" LOGIN_NAME="user4" NAME="Sawyer" PASSWORD="2488aa0c31c624687bd9928e0a5d29e7d1ed520b" SALT="6d65d24122c30500" STATUS="enabled"/>
|
||||
<SS_USER ID="6" EMAIL="ben@springside.org.cn" LOGIN_NAME="user5" NAME="Ben" PASSWORD="2488aa0c31c624687bd9928e0a5d29e7d1ed520b" SALT="6d65d24122c30500" STATUS="enabled"/>
|
||||
<SS_USER ID="1" EMAIL="admin@springside.org.cn" LOGIN_NAME="admin" NAME="Admin" PASSWORD="691b14d79bf0fa2215f155235df5e670b64394cc" SALT="7efbd59d9741d34f" STATUS="enabled" TEAM_ID="1"/>
|
||||
<SS_USER ID="2" EMAIL="user@springside.org.cn" LOGIN_NAME="user" NAME="Calvin" PASSWORD="2488aa0c31c624687bd9928e0a5d29e7d1ed520b" SALT="6d65d24122c30500" STATUS="enabled" TEAM_ID="1"/>
|
||||
<SS_USER ID="3" EMAIL="jack@springside.org.cn" LOGIN_NAME="user2" NAME="Jack" PASSWORD="2488aa0c31c624687bd9928e0a5d29e7d1ed520b" SALT="6d65d24122c30500" STATUS="enabled" TEAM_ID="1"/>
|
||||
<SS_USER ID="4" EMAIL="kate@springside.org.cn" LOGIN_NAME="user3" NAME="Kate" PASSWORD="2488aa0c31c624687bd9928e0a5d29e7d1ed520b" SALT="6d65d24122c30500" STATUS="enabled" TEAM_ID="1"/>
|
||||
<SS_USER ID="5" EMAIL="sawyer@springside.org.cn" LOGIN_NAME="user4" NAME="Sawyer" PASSWORD="2488aa0c31c624687bd9928e0a5d29e7d1ed520b" SALT="6d65d24122c30500" STATUS="enabled" TEAM_ID="1"/>
|
||||
<SS_USER ID="6" EMAIL="ben@springside.org.cn" LOGIN_NAME="user5" NAME="Ben" PASSWORD="2488aa0c31c624687bd9928e0a5d29e7d1ed520b" SALT="6d65d24122c30500" STATUS="enabled" TEAM_ID="1"/>
|
||||
<SS_ROLE ID="1" NAME="Admin"/>
|
||||
<SS_ROLE ID="2" NAME="User"/>
|
||||
<SS_USER_ROLE USER_ID="1" ROLE_ID="1"/>
|
||||
@@ -15,4 +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"/>
|
||||
</dataset>
|
||||
|
||||
Reference in New Issue
Block a user