Commit a5a267c0 authored by 李健华's avatar 李健华

添加金融数据库,实现创建用户双写

parent 4146c7a9
package cn.quantgroup.xyqb.config.data;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;
/**
* 金融数据库
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"cn.quantgroup.xyqb.financial.repository"},
entityManagerFactoryRef = "financialEntityManager",
transactionManagerRef = "financialTransactionManager")
public class FinancialJpaConfig {
@Value("${financial.datasource.url}")
private String dataSourceUrl;
@Value("${financial.datasource.password}")
private String password;
@Value("${financial.datasource.username}")
private String username;
@Value("${xyqb.data.mysql.max-pool-size}")
private Integer maxPoolSize;
private static final String DATABASE_TYPE_DEFAULT = "com.mysql.jdbc.Driver";
@Bean(name = "financialEntityManager")
@DependsOn("financialDataSource")
public EntityManagerFactory entityManagerFactory(
@Qualifier("financialDataSource") DataSource dataSource
) {
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setDataSource(dataSource);
entityManager.setPackagesToScan("cn.quantgroup.xyqb.financial");
entityManager.setPersistenceUnitName("financialDataSource");
Properties properties = new Properties();
properties.put("hibernate.jdbc.batch_size", 30);
properties.put("hibernate.order_inserts", true);
properties.put("hibernate.order_updates", true);
entityManager.setJpaProperties(properties);
entityManager.setJpaVendorAdapter(jpaVendorAdapter());
entityManager.afterPropertiesSet();
return entityManager.getObject();
}
private JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql(false);
hibernateJpaVendorAdapter.setGenerateDdl(false);
hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
return hibernateJpaVendorAdapter;
}
@Bean(name = "financialTransactionManager")
@DependsOn("financialEntityManager")
public PlatformTransactionManager transactionManager(
@Qualifier("financialEntityManager") EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
@Bean(name = "financialJdbcTemplate")
@DependsOn("financialDataSource")
public JdbcTemplate jdbcTemplate(@Qualifier("financialDataSource") DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
/**
* private static final String DATASOURCE_TYPE_DEFAULT =
* "com.zaxxer.hikari.HikariDataSource";
* private static final String DATABASE_TYPE_DEFAULT =
* "com.mysql.jdbc.Driver";
* private static final String MASTER_PREFIX =
* "spring.datasource.";
* private static final String SLAVE_PREFIX =
* "slave.datasource.";
*
* @return
*/
@Bean(name = "financialDataSource")
public DataSource financialDataSource() {
DataSourceBuilder factory = DataSourceBuilder
.create()
.driverClassName(DATABASE_TYPE_DEFAULT)
.url(dataSourceUrl)
.username(username)
.password(password)
.type(HikariDataSource.class);
return factory.build();
}
}
package cn.quantgroup.xyqb.financial.entity;
import cn.quantgroup.xyqb.entity.BaseEntity;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import java.io.Serializable;
import java.util.Objects;
/**
* Created by Miraculous on 15/7/4.
*/
@Getter
@Setter
@ToString
@Entity
@Table(name = "user",
uniqueConstraints = @UniqueConstraint(columnNames = "phone_no"))
public class User extends BaseEntity implements Serializable {
private static final long serialVersionUID = -1L;
//手机号
@Column(name = "phone_no")
private String phoneNo;
//uuid
@Column(name = "password")
private String password;
//第一次用户来源 channel_id
@Column(name = "registered_from")
private Long registeredFrom;
//uuid
@Column(name = "uuid")
private String uuid;
@Column(name = "enable")
private Boolean enable;
/**
* 是否有密码
*
* @return
*/
public boolean getHasPassword() {
return Objects.nonNull(password) && !Objects.equals("", password);
}
}
package cn.quantgroup.xyqb.financial.repository;
import cn.quantgroup.xyqb.financial.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* Created by Miraculous on 15/7/4.
*/
public interface IFinancialUserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
}
......@@ -10,6 +10,7 @@ import cn.quantgroup.xyqb.exception.UserRegisterLoginException;
import cn.quantgroup.xyqb.model.UserRegisterParam;
import cn.quantgroup.xyqb.service.register.IUserDeregisterService;
import cn.quantgroup.xyqb.service.register.IUserRegisterService;
import cn.quantgroup.xyqb.service.user.IFinancialUserService;
import cn.quantgroup.xyqb.service.user.IUserService;
import cn.quantgroup.xyqb.util.DateUtils;
import cn.quantgroup.xyqb.util.PasswordUtil;
......@@ -17,6 +18,7 @@ import cn.quantgroup.xyqb.util.TenantUtil;
import cn.quantgroup.xyqb.util.encrypt.Md5Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -43,6 +45,9 @@ public class UserRegisterServiceImpl implements IUserRegisterService {
@Resource
private IUserDeregisterService userDeregisterService;
@Resource
private IFinancialUserService financialUserService;
@Transactional(rollbackFor = Exception.class)
@Override
public User register(Long registerFrom, String phoneNo) {
......@@ -155,6 +160,9 @@ public class UserRegisterServiceImpl implements IUserRegisterService {
user.setPassword(PasswordUtil.MD5WithSalt(password));
}
user = userService.saveUser(user);
cn.quantgroup.xyqb.financial.entity.User financialUser = new cn.quantgroup.xyqb.financial.entity.User();
BeanUtils.copyProperties(user, financialUser);
financialUserService.saveUser(financialUser);
userRegisterParam.setUser(user);
return user;
}
......
package cn.quantgroup.xyqb.service.user;
import cn.quantgroup.xyqb.controller.internal.user.resp.UserFullResp;
import cn.quantgroup.xyqb.entity.Merchant;
import cn.quantgroup.xyqb.financial.entity.User;
import cn.quantgroup.xyqb.model.JsonResult;
import cn.quantgroup.xyqb.model.LoginProperties;
import cn.quantgroup.xyqb.model.UserInfo;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
/**
* Created by Miraculous on 15/7/5.
*/
public interface IFinancialUserService {
User saveUser(User user);
}
package cn.quantgroup.xyqb.service.user.impl;
import cn.quantgroup.xyqb.controller.IBaseController;
import cn.quantgroup.xyqb.financial.entity.User;
import cn.quantgroup.xyqb.financial.repository.IFinancialUserRepository;
import cn.quantgroup.xyqb.service.user.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
/**
* Created by Miraculous on 15/7/5.
*/
@Service
@Slf4j
public class FinancialUserServiceImpl implements IFinancialUserService, IBaseController {
@Autowired
private RedisTemplate<String, String> stringRedisTemplate;
@Autowired
private IFinancialUserRepository financialUserRepository;
@Override
@CacheEvict(value = "usercache", key = "'xyqbuser' + #user.phoneNo", cacheManager = "cacheManager")
public User saveUser(User user) {
return financialUserRepository.saveAndFlush(user);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment