#00061 Remove mybatis, use JDBC implement replace

0.4-beta
Li Shengzhao 2015-11-16 17:52:51 +08:00
parent 8a8f412dc8
commit bd1eca66cb
24 changed files with 470 additions and 287 deletions

14
pom.xml
View File

@ -20,7 +20,6 @@
<fasterxml.jackson.version>2.5.4</fasterxml.jackson.version>
<aspectj.version>1.8.6</aspectj.version>
<mybatis.version>3.2.1</mybatis.version>
<!--jdbc execute sql config-->
<jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>
@ -198,7 +197,7 @@
<version>${aspectj.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
@ -328,17 +327,6 @@
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>

View File

@ -11,6 +11,8 @@ import java.util.Date;
public class OauthClientDetails implements Serializable {
private static final long serialVersionUID = -6947822646185526939L;
private Date createTime = DateUtils.now();
private boolean archived = false;
@ -64,9 +66,22 @@ public class OauthClientDetails implements Serializable {
*/
private boolean trusted = false;
/**
* Value is 'true' or 'false', default 'false'
*/
private String autoApprove;
public OauthClientDetails() {
}
public String autoApprove() {
return autoApprove;
}
public OauthClientDetails autoApprove(String autoApprove) {
this.autoApprove = autoApprove;
return this;
}
public boolean trusted() {
return trusted;
@ -76,6 +91,11 @@ public class OauthClientDetails implements Serializable {
return createTime;
}
public OauthClientDetails createTime(Date createTime) {
this.createTime = createTime;
return this;
}
public boolean archived() {
return archived;
}
@ -196,4 +216,9 @@ public class OauthClientDetails implements Serializable {
this.additionalInformation = additionalInformation;
return this;
}
public OauthClientDetails archived(boolean archived) {
this.archived = archived;
return this;
}
}

View File

@ -1,7 +1,6 @@
package cc.wdcy.domain.oauth;
import cc.wdcy.domain.shared.Repository;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@ -14,7 +13,7 @@ public interface OauthRepository extends Repository {
List<OauthClientDetails> findAllOauthClientDetails();
void updateOauthClientDetailsArchive(@Param("clientId") String clientId, @Param("archive") boolean archive);
void updateOauthClientDetailsArchive(String clientId, boolean archive);
void saveOauthClientDetails(OauthClientDetails clientDetails);
}

View File

@ -15,6 +15,8 @@ import java.util.List;
*/
public class WdcyUserDetails implements UserDetails {
private static final long serialVersionUID = 3957586021470480642L;
protected static final String ROLE_PREFIX = "ROLE_";
protected static final GrantedAuthority DEFAULT_USER_ROLE = new SimpleGrantedAuthority(ROLE_PREFIX + Privilege.USER.name());

View File

@ -2,6 +2,7 @@ package cc.wdcy.domain.user;
import cc.wdcy.domain.AbstractDomain;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -12,6 +13,9 @@ import java.util.List;
public class User extends AbstractDomain {
private static final long serialVersionUID = -2921689304753120556L;
private String username;
private String password;
@ -92,7 +96,18 @@ public class User extends AbstractDomain {
return lastLoginTime;
}
public void lastLoginTime(Date lastLoginTime) {
public User lastLoginTime(Date lastLoginTime) {
this.lastLoginTime = lastLoginTime;
return this;
}
public User createTime(Date createTime) {
this.createTime = createTime;
return this;
}
public User password(String password) {
this.password = password;
return this;
}
}

View File

@ -0,0 +1,66 @@
/*
* Copyright (c) 2015 MONKEYK Information Technology Co. Ltd
* www.monkeyk.com
* All rights reserved.
*
* This software is the confidential and proprietary information of
* MONKEYK Information Technology Co. Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with MONKEYK Information Technology Co. Ltd.
*/
package cc.wdcy.infrastructure.jdbc;
import cc.wdcy.domain.oauth.OauthClientDetails;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 2015/11/16
*
* @author Shengzhao Li
*/
public class OauthClientDetailsRowMapper implements RowMapper<OauthClientDetails> {
public OauthClientDetailsRowMapper() {
}
@Override
public OauthClientDetails mapRow(ResultSet rs, int i) throws SQLException {
OauthClientDetails clientDetails = new OauthClientDetails();
clientDetails.clientId(rs.getString("client_id"));
clientDetails.resourceIds(rs.getString("resource_ids"));
clientDetails.clientSecret(rs.getString("client_secret"));
clientDetails.scope(rs.getString("scope"));
clientDetails.authorizedGrantTypes(rs.getString("authorized_grant_types"));
clientDetails.webServerRedirectUri(rs.getString("web_server_redirect_uri"));
clientDetails.authorities(rs.getString("authorities"));
clientDetails.accessTokenValidity(getInteger(rs, "access_token_validity"));
clientDetails.refreshTokenValidity(getInteger(rs, "refresh_token_validity"));
clientDetails.additionalInformation(rs.getString("additional_information"));
clientDetails.createTime(rs.getTimestamp("create_time"));
clientDetails.archived(rs.getBoolean("archived"));
clientDetails.trusted(rs.getBoolean("trusted"));
clientDetails.autoApprove(rs.getString("autoapprove"));
return clientDetails;
}
private Integer getInteger(ResultSet rs, String columnName) throws SQLException {
final Object object = rs.getObject(columnName);
if (object != null) {
return (Integer) object;
}
return null;
}
}

View File

@ -0,0 +1,92 @@
/*
* Copyright (c) 2015 MONKEYK Information Technology Co. Ltd
* www.monkeyk.com
* All rights reserved.
*
* This software is the confidential and proprietary information of
* MONKEYK Information Technology Co. Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with MONKEYK Information Technology Co. Ltd.
*/
package cc.wdcy.infrastructure.jdbc;
import cc.wdcy.domain.oauth.OauthClientDetails;
import cc.wdcy.domain.oauth.OauthRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.stereotype.Repository;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
/**
* 2015/11/16
*
* @author Shengzhao Li
*/
@Repository("oauthRepositoryJdbc")
public class OauthRepositoryJdbc implements OauthRepository {
private static OauthClientDetailsRowMapper oauthClientDetailsRowMapper = new OauthClientDetailsRowMapper();
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public OauthClientDetails findOauthClientDetails(String clientId) {
final String sql = " select * from oauth_client_details where client_id = ? ";
final List<OauthClientDetails> list = this.jdbcTemplate.query(sql, new Object[]{clientId}, oauthClientDetailsRowMapper);
return list.isEmpty() ? null : list.get(0);
}
@Override
public List<OauthClientDetails> findAllOauthClientDetails() {
final String sql = " select * from oauth_client_details order by create_time desc ";
return this.jdbcTemplate.query(sql, oauthClientDetailsRowMapper);
}
@Override
public void updateOauthClientDetailsArchive(String clientId, boolean archive) {
final String sql = " update oauth_client_details set archived = ? where client_id = ? ";
this.jdbcTemplate.update(sql, archive, clientId);
}
@Override
public void saveOauthClientDetails(final OauthClientDetails clientDetails) {
final String sql = " insert into oauth_client_details(client_id,resource_ids,client_secret,scope,authorized_grant_types,web_server_redirect_uri," +
" authorities,access_token_validity,refresh_token_validity,additional_information,trusted,autoapprove) values (?,?,?,?,?,?,?,?,?,?,?,?)";
this.jdbcTemplate.update(sql, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, clientDetails.clientId());
ps.setString(2, clientDetails.resourceIds());
ps.setString(3, clientDetails.clientSecret());
ps.setString(4, clientDetails.scope());
ps.setString(5, clientDetails.authorizedGrantTypes());
ps.setString(6, clientDetails.webServerRedirectUri());
ps.setString(7, clientDetails.authorities());
ps.setObject(8, clientDetails.accessTokenValidity());
ps.setObject(9, clientDetails.refreshTokenValidity());
ps.setString(10, clientDetails.additionalInformation());
ps.setBoolean(11, clientDetails.trusted());
ps.setString(12, clientDetails.autoApprove());
}
});
}
}

View File

@ -0,0 +1,121 @@
/*
* Copyright (c) 2015 MONKEYK Information Technology Co. Ltd
* www.monkeyk.com
* All rights reserved.
*
* This software is the confidential and proprietary information of
* MONKEYK Information Technology Co. Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with MONKEYK Information Technology Co. Ltd.
*/
package cc.wdcy.infrastructure.jdbc;
import cc.wdcy.domain.user.Privilege;
import cc.wdcy.domain.user.User;
import cc.wdcy.domain.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.stereotype.Repository;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* 2015/11/16
*
* @author Shengzhao Li
*/
@Repository("userRepositoryJdbc")
public class UserRepositoryJdbc implements UserRepository {
private static UserRowMapper userRowMapper = new UserRowMapper();
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public User findByGuid(String guid) {
final String sql = " select * from user_ where guid = ? ";
final List<User> list = this.jdbcTemplate.query(sql, new Object[]{guid}, userRowMapper);
User user = null;
if (!list.isEmpty()) {
user = list.get(0);
user.privileges().addAll(findPrivileges(user.id()));
}
return user;
}
private Collection<Privilege> findPrivileges(int userId) {
final String sql = " select privilege from user_privilege where user_id = ? ";
final List<String> strings = this.jdbcTemplate.queryForList(sql, new Object[]{userId}, String.class);
List<Privilege> privileges = new ArrayList<>(strings.size());
for (String string : strings) {
privileges.add(Privilege.valueOf(string));
}
return privileges;
}
@Override
public void saveUser(final User user) {
final String sql = " insert into user_(guid,archived,create_time,email,password,username,phone) " +
" values (?,?,?,?,?,?,?) ";
this.jdbcTemplate.update(sql, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, user.guid());
ps.setBoolean(2, user.archived());
ps.setTimestamp(3, new Timestamp(user.createTime().getTime()));
ps.setString(4, user.email());
ps.setString(5, user.password());
ps.setString(6, user.username());
ps.setString(7, user.phone());
}
});
}
@Override
public void updateUser(final User user) {
final String sql = " update user_ set username = ?, password = ?, phone = ?,email = ? where guid = ? ";
this.jdbcTemplate.update(sql, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, user.username());
ps.setString(2, user.password());
ps.setString(3, user.phone());
ps.setString(4, user.email());
ps.setString(5, user.guid());
}
});
}
@Override
public User findByUsername(String username) {
final String sql = " select * from user_ where username = ? and archived = 0 ";
final List<User> list = this.jdbcTemplate.query(sql, new Object[]{username}, userRowMapper);
User user = null;
if (!list.isEmpty()) {
user = list.get(0);
user.privileges().addAll(findPrivileges(user.id()));
}
return user;
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2015 MONKEYK Information Technology Co. Ltd
* www.monkeyk.com
* All rights reserved.
*
* This software is the confidential and proprietary information of
* MONKEYK Information Technology Co. Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with MONKEYK Information Technology Co. Ltd.
*/
package cc.wdcy.infrastructure.jdbc;
import cc.wdcy.domain.user.User;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 2015/11/16
*
* @author Shengzhao Li
*/
public class UserRowMapper implements RowMapper<User> {
public UserRowMapper() {
}
@Override
public User mapRow(ResultSet rs, int i) throws SQLException {
User user = new User();
user.id(rs.getInt("id"));
user.guid(rs.getString("guid"));
user.archived(rs.getBoolean("archived"));
user.createTime(rs.getTimestamp("create_time"));
user.email(rs.getString("email"));
user.phone(rs.getString("phone"));
user.password(rs.getString("password"));
user.username(rs.getString("username"));
user.lastLoginTime(rs.getTimestamp("last_login_time"));
return user;
}
}

View File

@ -1,9 +0,0 @@
package cc.wdcy.infrastructure.mybatis;
import cc.wdcy.domain.oauth.OauthRepository;
/**
* @author Shengzhao Li
*/
public interface OauthRepositoryMyBatis extends OauthRepository {
}

View File

@ -1,9 +0,0 @@
package cc.wdcy.infrastructure.mybatis;
import cc.wdcy.domain.user.UserRepository;
/**
* @author Shengzhao Li
*/
public interface UserRepositoryMyBatis extends UserRepository {
}

View File

@ -28,7 +28,7 @@ public class UserServiceImpl implements UserService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if (user == null) {
if (user == null || user.archived()) {
throw new UsernameNotFoundException("Not found any user for username[" + username + "]");
}

View File

@ -1,51 +0,0 @@
<?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="cc.wdcy.infrastructure.mybatis.OauthRepositoryMyBatis">
<resultMap type="OauthClientDetails" id="oauthClientDetailsMap">
<id property="clientId" column="client_id"/>
<result property="resourceIds" column="resource_ids"/>
<result property="clientSecret" column="client_secret"/>
<result property="scope" column="scope"/>
<result property="authorizedGrantTypes" column="authorized_grant_types"/>
<result property="webServerRedirectUri" column="web_server_redirect_uri"/>
<result property="authorities" column="authorities"/>
<result property="accessTokenValidity" column="access_token_validity"/>
<result property="refreshTokenValidity" column="refresh_token_validity"/>
<result property="additionalInformation" column="additional_information"/>
<result property="createTime" column="create_time"/>
<result property="archived" column="archived"/>
<result property="trusted" column="trusted"/>
</resultMap>
<select id="findOauthClientDetails" parameterType="String" resultMap="oauthClientDetailsMap">
select * from oauth_client_details
where client_id = #{clientId}
</select>
<select id="findAllOauthClientDetails" resultMap="oauthClientDetailsMap">
select * from oauth_client_details order by create_time desc
</select>
<update id="updateOauthClientDetailsArchive">
update oauth_client_details set archived = #{archive}
where client_id = #{clientId}
</update>
<insert id="saveOauthClientDetails" parameterType="OauthClientDetails">
insert into oauth_client_details(client_id,resource_ids,client_secret,scope,authorized_grant_types,web_server_redirect_uri,
authorities,access_token_validity,refresh_token_validity,additional_information,trusted)
values
(#{clientId},#{resourceIds},#{clientSecret},#{scope},#{authorizedGrantTypes}, #{webServerRedirectUri},
#{authorities},#{accessTokenValidity},#{refreshTokenValidity},#{additionalInformation}, #{trusted})
</insert>
</mapper>

View File

@ -1,59 +0,0 @@
<?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="cc.wdcy.infrastructure.mybatis.UserRepositoryMyBatis">
<resultMap type="User" id="userMap">
<id property="id" column="id"/>
<result property="guid" column="guid"/>
<result property="archived" column="archived"/>
<result property="createTime" column="create_time"/>
<result property="email" column="email"/>
<result property="phone" column="phone"/>
<result property="defaultUser" column="default_user"/>
<result property="password" column="password"/>
<result property="username" column="username"/>
<result property="lastLoginTime" column="last_login_time"/>
<collection property="privileges" column="id" select="findPrivilegesByUserId" ofType="Privilege"/>
</resultMap>
<select id="findByGuid" parameterType="String" resultMap="userMap">
select * from user_ where guid = #{guid}
</select>
<select id="findUserById" parameterType="Integer" resultMap="userMap">
select * from user_ where id = #{id}
</select>
<select id="findPrivilegesByUserId" parameterType="Integer" resultType="Privilege">
select privilege from user_privilege where user_id = #{userId}
</select>
<select id="findByUsername" parameterType="String" resultMap="userMap">
select * from user_
where username = #{username}
and archived = 0
</select>
<insert id="saveUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
insert into user_(guid,archived,create_time,email,password,username,phone, last_login_time)
values
(#{guid},#{archived},#{createTime},#{email},#{password},#{username}, #{phone}, #{lastLoginTime})
</insert>
<update id="updateUser" parameterType="User" flushCache="true">
update user_ set
username = #{username},
password = #{password},
phone = #{phone},
email = #{email}
where guid = #{guid}
</update>
</mapper>

View File

@ -1,24 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="lazyLoadingEnabled" value="false"/>
<setting name="useGeneratedKeys" value="true"/>
</settings>
<typeAliases>
<typeAlias alias="User" type="cc.wdcy.domain.user.User"/>
<typeAlias alias="Privilege" type="cc.wdcy.domain.user.Privilege"/>
<typeAlias alias="OauthClientDetails" type="cc.wdcy.domain.oauth.OauthClientDetails"/>
</typeAliases>
<mappers>
<mapper resource="cc/wdcy/infrastructure/mybatis/UserMapper.xml"/>
<mapper resource="cc/wdcy/infrastructure/mybatis/OauthMapper.xml"/>
</mappers>
</configuration>

View File

@ -10,7 +10,7 @@
<!--annotation configuration -->
<context:annotation-config/>
<context:component-scan base-package="cc.wdcy.service"/>
<context:component-scan base-package="cc.wdcy"/>
<!-- property configuration -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
@ -46,20 +46,5 @@
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
<constructor-arg index="1" value="BATCH"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cc.wdcy.infrastructure.mybatis"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>

View File

@ -1,31 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!--<import resource="security.xml"/>-->
<!--annotation configuration -->
<context:annotation-config/>
<context:component-scan base-package="cc.wdcy.web"/>
<!--static resource-->
<mvc:resources mapping="/index.jsp*" location="/index.jsp"/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!--<import resource="security.xml"/>-->
<!--annotation configuration -->
<context:annotation-config/>
<context:component-scan base-package="cc.wdcy.web"/>
<!--static resource-->
<mvc:resources mapping="/index.jsp*" location="/index.jsp"/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<!--<mvc:annotation-driven/>-->
<!--<mvc:default-servlet-handler/>-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

View File

@ -75,14 +75,14 @@
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--hy mvc-->
<!--mvc-->
<servlet>
<servlet-name>wdcy</servlet-name>
<servlet-name>mkk</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>wdcy</servlet-name>
<servlet-name>mkk</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

View File

@ -7,7 +7,7 @@ import org.springframework.test.context.transaction.BeforeTransaction;
/**
* @author Shengzhao Li
*/
@ContextConfiguration(locations = {"classpath:testApplicationContext.xml"})
@ContextConfiguration(locations = {"classpath:/spring/*.xml"}, initializers = {TestApplicationContextInitializer.class})
public abstract class ContextTest extends AbstractTransactionalTestNGSpringContextTests {
@BeforeTransaction

View File

@ -0,0 +1,21 @@
package cc.wdcy;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.io.ClassPathResource;
/**
* @author Shengzhao Li
*/
public class TestApplicationContextInitializer implements ApplicationContextInitializer<AbstractApplicationContext> {
@Override
public void initialize(AbstractApplicationContext applicationContext) {
PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
//load test.properties
propertyPlaceholderConfigurer.setLocation(new ClassPathResource("test.properties"));
applicationContext.addBeanFactoryPostProcessor(propertyPlaceholderConfigurer);
}
}

View File

@ -1,7 +1,6 @@
package cc.wdcy.infrastructure;
import cc.wdcy.ContextTest;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
@ -14,13 +13,6 @@ public abstract class AbstractRepositoryTest extends ContextTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
public SqlSessionTemplate sqlSessionTemplate() {
return sqlSessionTemplate;
}
public JdbcTemplate jdbcTemplate() {
return jdbcTemplate;

View File

@ -1,4 +1,15 @@
package cc.wdcy.infrastructure.mybatis;
/*
* Copyright (c) 2015 MONKEYK Information Technology Co. Ltd
* www.monkeyk.com
* All rights reserved.
*
* This software is the confidential and proprietary information of
* MONKEYK Information Technology Co. Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with MONKEYK Information Technology Co. Ltd.
*/
package cc.wdcy.infrastructure.jdbc;
import cc.wdcy.domain.oauth.OauthClientDetails;
import cc.wdcy.domain.oauth.OauthRepository;
@ -11,10 +22,10 @@ import java.util.List;
import static org.testng.Assert.*;
/**
* @author Shengzhao Li
*/
public class OauthRepositoryMyBatisTest extends AbstractRepositoryTest {
/*
* @author Shengzhao Li
*/
public class OauthRepositoryJdbcTest extends AbstractRepositoryTest {
@Autowired
@ -56,4 +67,5 @@ public class OauthRepositoryMyBatisTest extends AbstractRepositoryTest {
oauthRepositoryMyBatis.updateOauthClientDetailsArchive("ddooelddd", true);
}
}

View File

@ -1,4 +1,15 @@
package cc.wdcy.infrastructure.mybatis;
/*
* Copyright (c) 2015 MONKEYK Information Technology Co. Ltd
* www.monkeyk.com
* All rights reserved.
*
* This software is the confidential and proprietary information of
* MONKEYK Information Technology Co. Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with MONKEYK Information Technology Co. Ltd.
*/
package cc.wdcy.infrastructure.jdbc;
import cc.wdcy.domain.user.User;
import cc.wdcy.domain.user.UserRepository;
@ -6,12 +17,16 @@ import cc.wdcy.infrastructure.AbstractRepositoryTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.Test;
import static org.testng.AssertJUnit.*;
import static org.testng.Assert.*;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertNull;
/*
* @author Shengzhao Li
*/
public class UserRepositoryJdbcTest extends AbstractRepositoryTest {
/**
* @author Shengzhao Li
*/
public class UserRepositoryMyBatisTest extends AbstractRepositoryTest {
@Autowired
private UserRepository userRepository;

View File

@ -1,49 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- property configuration -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:test.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
<constructor-arg index="1" value="BATCH"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cc.wdcy.infrastructure.mybatis"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>