-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
54 changed files
with
1,947 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
3.x/spring-boot-mybatis/spring-boot-mybatis-annotation-mulidatasource/pom.xml
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,67 @@ | ||
<?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-mybatis-annotation-mulidatasource</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>spring-boot-mybatis-annotation-mulidatasource</name> | ||
<description>Demo project for Spring Boot and mybatis with annotation</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>3.0.0</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>17</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>3.0.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-core</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
12 changes: 12 additions & 0 deletions
12
...s/spring-boot-mybatis-annotation-mulidatasource/src/main/java/com/neo/MAMApplication.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 MAMApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(MAMApplication.class, args); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...mybatis-annotation-mulidatasource/src/main/java/com/neo/datasource/DataSource1Config.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,51 @@ | ||
package com.neo.datasource; | ||
|
||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.mybatis.spring.SqlSessionFactoryBean; | ||
import org.mybatis.spring.SqlSessionTemplate; | ||
import org.mybatis.spring.annotation.MapperScan; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.jdbc.DataSourceBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.jdbc.datasource.DataSourceTransactionManager; | ||
|
||
import javax.sql.DataSource; | ||
|
||
/** | ||
* Created by summer on 2016/11/25. | ||
*/ | ||
@Configuration | ||
@MapperScan(basePackages = "com.neo.mapper.test1", sqlSessionTemplateRef = "test1SqlSessionTemplate") | ||
public class DataSource1Config { | ||
|
||
@Bean(name = "test1DataSource") | ||
@ConfigurationProperties(prefix = "spring.datasource.test1") | ||
@Primary | ||
public DataSource testDataSource() { | ||
return DataSourceBuilder.create().build(); | ||
} | ||
|
||
@Bean(name = "test1SqlSessionFactory") | ||
@Primary | ||
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception { | ||
SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); | ||
bean.setDataSource(dataSource); | ||
return bean.getObject(); | ||
} | ||
|
||
@Bean(name = "test1TransactionManager") | ||
@Primary | ||
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) { | ||
return new DataSourceTransactionManager(dataSource); | ||
} | ||
|
||
@Bean(name = "test1SqlSessionTemplate") | ||
@Primary | ||
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { | ||
return new SqlSessionTemplate(sqlSessionFactory); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...mybatis-annotation-mulidatasource/src/main/java/com/neo/datasource/DataSource2Config.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,46 @@ | ||
package com.neo.datasource; | ||
|
||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.mybatis.spring.SqlSessionFactoryBean; | ||
import org.mybatis.spring.SqlSessionTemplate; | ||
import org.mybatis.spring.annotation.MapperScan; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.jdbc.DataSourceBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.jdbc.datasource.DataSourceTransactionManager; | ||
|
||
import javax.sql.DataSource; | ||
|
||
/** | ||
* Created by summer on 2016/11/25. | ||
*/ | ||
@Configuration | ||
@MapperScan(basePackages = "com.neo.mapper.test2", sqlSessionTemplateRef = "test2SqlSessionTemplate") | ||
public class DataSource2Config { | ||
|
||
@Bean(name = "test2DataSource") | ||
@ConfigurationProperties(prefix = "spring.datasource.test2") | ||
public DataSource testDataSource() { | ||
return DataSourceBuilder.create().build(); | ||
} | ||
|
||
@Bean(name = "test2SqlSessionFactory") | ||
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception { | ||
SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); | ||
bean.setDataSource(dataSource); | ||
return bean.getObject(); | ||
} | ||
|
||
@Bean(name = "test2TransactionManager") | ||
public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) { | ||
return new DataSourceTransactionManager(dataSource); | ||
} | ||
|
||
@Bean(name = "test2SqlSessionTemplate") | ||
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { | ||
return new SqlSessionTemplate(sqlSessionFactory); | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...pring-boot-mybatis-annotation-mulidatasource/src/main/java/com/neo/enums/UserSexEnum.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,5 @@ | ||
package com.neo.enums; | ||
|
||
public enum UserSexEnum { | ||
MAN, WOMAN | ||
} |
35 changes: 35 additions & 0 deletions
35
...oot-mybatis-annotation-mulidatasource/src/main/java/com/neo/mapper/test1/User1Mapper.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,35 @@ | ||
package com.neo.mapper.test1; | ||
|
||
import com.neo.model.User; | ||
import com.neo.enums.UserSexEnum; | ||
import org.apache.ibatis.annotations.*; | ||
|
||
import java.util.List; | ||
|
||
public interface User1Mapper { | ||
|
||
|
||
@Select("SELECT * FROM users") | ||
@Results({ | ||
@Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class), | ||
@Result(property = "nickName", column = "nick_name") | ||
}) | ||
List<User> getAll(); | ||
|
||
@Select("SELECT * FROM users WHERE id = #{id}") | ||
@Results({ | ||
@Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class), | ||
@Result(property = "nickName", column = "nick_name") | ||
}) | ||
User getOne(Long id); | ||
|
||
@Insert("INSERT INTO users(userName,passWord,user_sex) VALUES(#{userName}, #{passWord}, #{userSex})") | ||
void insert(User user); | ||
|
||
@Update("UPDATE users SET userName=#{userName},nick_name=#{nickName} WHERE id =#{id}") | ||
void update(User user); | ||
|
||
@Delete("DELETE FROM users WHERE id =#{id}") | ||
void delete(Long id); | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...oot-mybatis-annotation-mulidatasource/src/main/java/com/neo/mapper/test2/User2Mapper.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,35 @@ | ||
package com.neo.mapper.test2; | ||
|
||
import java.util.List; | ||
|
||
import com.neo.model.User; | ||
import com.neo.enums.UserSexEnum; | ||
import org.apache.ibatis.annotations.*; | ||
|
||
public interface User2Mapper { | ||
|
||
|
||
@Select("SELECT * FROM users") | ||
@Results({ | ||
@Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class), | ||
@Result(property = "nickName", column = "nick_name") | ||
}) | ||
List<User> getAll(); | ||
|
||
@Select("SELECT * FROM users WHERE id = #{id}") | ||
@Results({ | ||
@Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class), | ||
@Result(property = "nickName", column = "nick_name") | ||
}) | ||
User getOne(Long id); | ||
|
||
@Insert("INSERT INTO users(userName,passWord,user_sex) VALUES(#{userName}, #{passWord}, #{userSex})") | ||
void insert(User user); | ||
|
||
@Update("UPDATE users SET userName=#{userName},nick_name=#{nickName} WHERE id =#{id}") | ||
void update(User user); | ||
|
||
@Delete("DELETE FROM users WHERE id =#{id}") | ||
void delete(Long id); | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
...batis/spring-boot-mybatis-annotation-mulidatasource/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,73 @@ | ||
package com.neo.model; | ||
|
||
import java.io.Serializable; | ||
|
||
import com.neo.enums.UserSexEnum; | ||
|
||
public class User implements Serializable { | ||
|
||
private static final long serialVersionUID = 1L; | ||
private Long id; | ||
private String userName; | ||
private String passWord; | ||
private UserSexEnum userSex; | ||
private String nickName; | ||
|
||
public User() { | ||
super(); | ||
} | ||
|
||
public User(String userName, String passWord, UserSexEnum userSex) { | ||
super(); | ||
this.passWord = passWord; | ||
this.userName = userName; | ||
this.userSex = userSex; | ||
} | ||
|
||
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 UserSexEnum getUserSex() { | ||
return userSex; | ||
} | ||
|
||
public void setUserSex(UserSexEnum userSex) { | ||
this.userSex = userSex; | ||
} | ||
|
||
public String getNickName() { | ||
return nickName; | ||
} | ||
|
||
public void setNickName(String nickName) { | ||
this.nickName = nickName; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
// TODO Auto-generated method stub | ||
return "userName " + this.userName + ", pasword " + this.passWord + "sex " + userSex.name(); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...ring-boot-mybatis-annotation-mulidatasource/src/main/java/com/neo/web/UserController.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,50 @@ | ||
package com.neo.web; | ||
|
||
import java.util.List; | ||
|
||
import com.neo.mapper.test1.User1Mapper; | ||
import com.neo.model.User; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.neo.mapper.test2.User2Mapper; | ||
|
||
@RestController | ||
public class UserController { | ||
|
||
@Autowired | ||
private User1Mapper user1Mapper; | ||
|
||
@Autowired | ||
private User2Mapper user2Mapper; | ||
|
||
@RequestMapping("/getUsers") | ||
public List<User> getUsers() { | ||
List<User> users=user1Mapper.getAll(); | ||
return users; | ||
} | ||
|
||
@RequestMapping("/getUser") | ||
public User getUser(Long id) { | ||
User user=user2Mapper.getOne(id); | ||
return user; | ||
} | ||
|
||
@RequestMapping("/add") | ||
public void save(User user) { | ||
user2Mapper.insert(user); | ||
} | ||
|
||
@RequestMapping(value="update") | ||
public void update(User user) { | ||
user2Mapper.update(user); | ||
} | ||
|
||
@RequestMapping(value="/delete/{id}") | ||
public void delete(@PathVariable("id") Long id) { | ||
user1Mapper.delete(id); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...s/spring-boot-mybatis-annotation-mulidatasource/src/main/resources/application.properties
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 @@ | ||
mybatis.type-aliases-package=com.neo.model | ||
|
||
spring.datasource.test1.jdbc-url=jdbc:mysql://localhost:3306/test1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true | ||
spring.datasource.test1.username=root | ||
spring.datasource.test1.password=root | ||
spring.datasource.test1.driver-class-name=com.mysql.cj.jdbc.Driver | ||
|
||
spring.datasource.test2.jdbc-url=jdbc:mysql://localhost:3306/test2?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true | ||
spring.datasource.test2.username=root | ||
spring.datasource.test2.password=root | ||
spring.datasource.test2.driver-class-name=com.mysql.cj.jdbc.Driver | ||
|
Oops, something went wrong.