forked from ityouknow/spring-boot-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
487 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.neo</groupId> | ||
<artifactId>spring-boot-atomikos</artifactId> | ||
<version>1.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>spring-boot-atomikos</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.0.0.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-jta-atomikos</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false"> | ||
<output url="file://$MODULE_DIR$/target/classes" /> | ||
<output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
<excludeFolder url="file://$MODULE_DIR$/target" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
12 changes: 12 additions & 0 deletions
12
spring-boot-atomikos/src/main/java/com/neo/JtaAtomikosApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.neo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class JtaAtomikosApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(JtaAtomikosApplication.class, args); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
spring-boot-atomikos/src/main/java/com/neo/config/DataSourceConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.neo.config; | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
|
||
import javax.sql.DataSource; | ||
|
||
@Configuration | ||
public class DataSourceConfig { | ||
|
||
@Bean(name = "primaryDataSource") | ||
@Qualifier("primaryDataSource") | ||
@ConfigurationProperties(prefix="spring.primary.datasource") | ||
public DataSource primaryDataSource() { | ||
return new AtomikosDataSourceBean(); | ||
} | ||
|
||
@Bean(name = "secondaryDataSource") | ||
@Qualifier("secondaryDataSource") | ||
@Primary | ||
@ConfigurationProperties(prefix="spring.secondary.datasource") | ||
public DataSource secondaryDataSource() { | ||
return new AtomikosDataSourceBean(); | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
spring-boot-atomikos/src/main/java/com/neo/config/PrimaryConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.neo.config; | ||
|
||
import java.util.Map; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.sql.DataSource; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; | ||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
@Configuration | ||
@EnableTransactionManagement | ||
@EnableJpaRepositories( | ||
entityManagerFactoryRef="entityManagerFactoryPrimary", | ||
transactionManagerRef="transactionManagerPrimary", | ||
basePackages= { "com.neo.repository.test1" })//设置dao(repo)所在位置 | ||
public class PrimaryConfig { | ||
|
||
@Autowired | ||
private JpaProperties jpaProperties; | ||
|
||
@Autowired | ||
@Qualifier("primaryDataSource") | ||
private DataSource primaryDataSource; | ||
|
||
@Bean(name = "entityManagerPrimary") | ||
@Primary | ||
public EntityManager entityManager(EntityManagerFactoryBuilder builder) { | ||
return entityManagerFactoryPrimary(builder).getObject().createEntityManager(); | ||
} | ||
|
||
@Bean(name = "entityManagerFactoryPrimary") | ||
@Primary | ||
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) { | ||
return builder | ||
.dataSource(primaryDataSource) | ||
.properties(getVendorProperties(primaryDataSource)) | ||
.packages("com.neo.domain") //设置实体类所在位置 | ||
.persistenceUnit("primaryPersistenceUnit") | ||
.build(); | ||
} | ||
|
||
private Map<String, String> getVendorProperties(DataSource dataSource) { | ||
return jpaProperties.getHibernateProperties(dataSource); | ||
} | ||
|
||
@Bean(name = "transactionManagerPrimary") | ||
@Primary | ||
PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) { | ||
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject()); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
spring-boot-atomikos/src/main/java/com/neo/config/SecondaryConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.neo.config; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; | ||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.sql.DataSource; | ||
import java.util.Map; | ||
|
||
@Configuration | ||
@EnableTransactionManagement | ||
@EnableJpaRepositories( | ||
entityManagerFactoryRef="entityManagerFactorySecondary", | ||
transactionManagerRef="transactionManagerSecondary", | ||
basePackages= { "com.neo.repository.test2" }) | ||
public class SecondaryConfig { | ||
@Autowired | ||
private JpaProperties jpaProperties; | ||
|
||
@Autowired | ||
@Qualifier("secondaryDataSource") | ||
private DataSource secondaryDataSource; | ||
|
||
@Bean(name = "entityManagerSecondary") | ||
public EntityManager entityManager(EntityManagerFactoryBuilder builder) { | ||
return entityManagerFactorySecondary(builder).getObject().createEntityManager(); | ||
} | ||
|
||
@Bean(name = "entityManagerFactorySecondary") | ||
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) { | ||
return builder | ||
.dataSource(secondaryDataSource) | ||
.properties(getVendorProperties(secondaryDataSource)) | ||
.packages("com.neo.domain") | ||
.persistenceUnit("secondaryPersistenceUnit") | ||
.build(); | ||
} | ||
|
||
private Map<String, String> getVendorProperties(DataSource dataSource) { | ||
return jpaProperties.getHibernateProperties(dataSource); | ||
} | ||
|
||
@Bean(name = "transactionManagerSecondary") | ||
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) { | ||
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject()); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
spring-boot-atomikos/src/main/java/com/neo/controller/HelloController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.neo.controller; | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class HelloController { | ||
|
||
@RequestMapping("/") | ||
public String index() { | ||
return "Hello Spring Boot 2.0!"; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
spring-boot-atomikos/src/main/java/com/neo/model/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.neo.domain; | ||
|
||
|
||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
|
||
import java.io.Serializable; | ||
|
||
@Entity | ||
public class User implements Serializable { | ||
|
||
private static final long serialVersionUID = 1L; | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
@Column(nullable = false, unique = true) | ||
private String userName; | ||
@Column(nullable = false) | ||
private String passWord; | ||
@Column(nullable = false, unique = true) | ||
private String email; | ||
@Column(nullable = true, unique = true) | ||
private String nickName; | ||
@Column(nullable = false) | ||
private String regTime; | ||
|
||
public User() { | ||
} | ||
|
||
public User(String userName, String passWord, String email, String nickName, String regTime) { | ||
this.userName = userName; | ||
this.passWord = passWord; | ||
this.email = email; | ||
this.nickName = nickName; | ||
this.regTime = regTime; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getUserName() { | ||
return userName; | ||
} | ||
|
||
public void setUserName(String userName) { | ||
this.userName = userName; | ||
} | ||
|
||
public String getPassWord() { | ||
return passWord; | ||
} | ||
|
||
public void setPassWord(String passWord) { | ||
this.passWord = passWord; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getNickName() { | ||
return nickName; | ||
} | ||
|
||
public void setNickName(String nickName) { | ||
this.nickName = nickName; | ||
} | ||
|
||
public String getRegTime() { | ||
return regTime; | ||
} | ||
|
||
public void setRegTime(String regTime) { | ||
this.regTime = regTime; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
spring-boot-atomikos/src/main/java/com/neo/repository/test1/UserTest1Repository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.neo.repository.test1; | ||
|
||
import com.neo.domain.User; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import javax.annotation.Resource; | ||
import java.util.List; | ||
|
||
public interface UserTest1Repository extends JpaRepository<User, Long> { | ||
User findByUserName(String userName); | ||
User findByUserNameOrEmail(String username, String email); | ||
} |
16 changes: 16 additions & 0 deletions
16
spring-boot-atomikos/src/main/java/com/neo/repository/test2/UserTest2Repository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.neo.repository.test2; | ||
|
||
import com.neo.domain.User; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
public interface UserTest2Repository extends JpaRepository<User, Long> { | ||
User findByUserName(String userName); | ||
User findByUserNameOrEmail(String username, String email); | ||
} |
Oops, something went wrong.