引入maven坐标
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apereo.casgroupId>
<artifactId>cas-server-support-bomartifactId>
<version>${cas.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apereo.casgroupId>
<artifactId>cas-server-webapp${app.server}artifactId>
<version>${cas.version}version>
<type>wartype>
<scope>runtimescope>
dependency>
<dependencies>
<dependency>
<groupId>org.apereo.casgroupId>
<artifactId>cas-server-support-jpa-utilartifactId>
dependency>
<dependency>
<groupId>org.apereo.casgroupId>
<artifactId>cas-server-core-util-apiartifactId>
dependency>
dependencies>
dependencies>
定义entity
这里使用了lombok来简化实体类的模版代码
package net.pubone.cas.entity;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
@Entity
@Table(name = "user")
@ToString
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class User {
@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
@GenericGenerator(name = "native", strategy = "native")
@JsonProperty("id")
@org.springframework.data.annotation.Id
@Id
private Integer id;
@JsonProperty("phoneNumber")
@Column(nullable = false)
private String phoneNumber;
@JsonProperty("username")
@Column(nullable = false)
private String username;
}
定义jpa数据库连接配置类
package net.pubone.cas.config;
import org.apereo.cas.configuration.model.support.jpa.AbstractJpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ConfigurationProperties(prefix = "mobile.jpa" ,ignoreUnknownFields = false)
@PropertySource("classpath:application-jdbc.properties")
public class MobileJpaProperties extends AbstractJpaProperties {
}
在resources下创建 application-jdbc.properties
mobile.jpa.user= root
mobile.jpa.password=123456
mobile.jpa.url=jdbc:mysql://localhost:3306/dev_cas
mobile.jpa.ddl-auto=update
mobile.jpa.dialect=org.hibernate.dialect.MySQL57Dialect
mobile.jpa.driver-class=com.mysql.jdbc.Driver
mobile.jpa.pool.minSize=6
mobile.jpa.pool.maxSize=18
mobile.jpa.pool.timeoutMillis=1000
其他属性请看AbstractJpaProperties这个类Spring会自动根据配置文件key的名称映射到bean的属性上
配置jpa
package net.pubone.cas.config;
import lombok.val;
import net.pubone.cas.dao.JpaMobileUserRepository;
import net.pubone.cas.entity.Puser;
import net.pubone.cas.repository.UserRepository;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.configuration.model.support.jpa.JpaConfigDataHolder;
import org.apereo.cas.configuration.support.JpaBeans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.apereo.cas.util.CollectionUtils;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.List;
@Configuration("jpaMobileConfig")
@EnableConfigurationProperties(value = {CasConfigurationProperties.class,MobileJpaProperties.class})
@EnableTransactionManagement(proxyTargetClass = true)
public class JpaMobileConfig {
@Autowired
private CasConfigurationProperties casProperties;
@RefreshScope
@Bean
public HibernateJpaVendorAdapter mobileUserVendorAdapter() {
return JpaBeans.newHibernateJpaVendorAdapter(casProperties.getJdbc());
}
@Bean
public MobileJpaProperties mobileJpaProperties(){
return new MobileJpaProperties();
}
@Bean
public DataSource dataSource() {
MobileJpaProperties mobileJpaProperties = mobileJpaProperties();
return JpaBeans.newDataSource(mobileJpaProperties);
}
public List<String> jpaMobilePackagesToScan() {
return CollectionUtils.wrap(Puser.class.getPackage().getName());
}
@Lazy
@Bean
public LocalContainerEntityManagerFactoryBean mobileUserEntityManagerFactory() {
return JpaBeans.newHibernateEntityManagerFactoryBean(
new JpaConfigDataHolder(
mobileUserVendorAdapter(),
"jpaMobileContext",
jpaMobilePackagesToScan(),
dataSource()),
mobileJpaProperties());
}
@Autowired
@Bean
public PlatformTransactionManager transactionManagerMobileId(@Qualifier("mobileUserEntityManagerFactory") final EntityManagerFactory emf) {
val mgmr = new JpaTransactionManager();
mgmr.setEntityManagerFactory(emf);
return mgmr;
}
@Bean
public UserRepository mobileUserRepository() {
return new JpaMobileUserRepository();
}
}
在resources/META-INF/spring.factories中添加
org.springframework.boot.autoconfigure.EnableAutoConfiguration=net.pubone.cas.config.JpaMobileConfig
让SpringBoot扫描到我们的配置类
定义Repository
package net.pubone.cas.repository;
import net.pubone.cas.entity.User;
public interface UserRepository {
User getPrincipal(String phoneNumber);
}
package net.pubone.cas.dao;
import lombok.ToString;
import lombok.val;
import net.pubone.cas.entity.User;
import net.pubone.cas.repository.UserRepository;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@EnableTransactionManagement(proxyTargetClass = true)
@Transactional(transactionManager = "transactionManagerMobileId")
@ToString
public class JpaMobileUserRepository implements UserRepository {
@PersistenceContext(unitName = "mobileUserEntityManagerFactory")
private transient EntityManager entityManager;
private final String SELECT_QUERY = "select r from Puser r ";
@Override
public User getPrincipal(String phoneNumber) {
val query = SELECT_QUERY.concat("where r.phoneNumber = :phoneNumber");
return entityManager.createQuery(query,User.class)
.setParameter("phoneNumber",phoneNumber).getSingleResult();
}
}
接着就可以在我们上一篇中的MobileHandler的类中使用@Autowired注入UserRepository进行数据库的crud的操作
@Autowired
UserRepository jpaMobileUserRepository;
参考 https://github.com/apereo/cas/tree/master/support/cas-server-support-events-jpa