Merge branch 'master' of github.com:heibaiying/Spring-All-In-One
This commit is contained in:
commit
d02dc25475
BIN
pictures/spring-druid-mybatis-annotation.png
Normal file
BIN
pictures/spring-druid-mybatis-annotation.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
BIN
pictures/spring-druid-mybatis.png
Normal file
BIN
pictures/spring-druid-mybatis.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
BIN
pictures/spring-jdbc-annotation.png
Normal file
BIN
pictures/spring-jdbc-annotation.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
BIN
pictures/spring-jdbc.png
Normal file
BIN
pictures/spring-jdbc.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
BIN
pictures/spring-mybatis-annotation.png
Normal file
BIN
pictures/spring-mybatis-annotation.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 18 KiB |
BIN
pictures/spring-mybatis.png
Normal file
BIN
pictures/spring-mybatis.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 18 KiB |
@ -8,7 +8,7 @@
|
||||
2. 自定义切面位于advice下,其中CustomAdvice是标准的自定义切面,FirstAdvice和SecondAdvice用于测试多切面共同作用于同一个被切入点时的执行顺序;
|
||||
3. OrderService是待切入方法。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-aop-annotation.png"/> </div>
|
||||
|
||||
|
||||
|
||||
@ -166,4 +166,4 @@ public class AopTest {
|
||||
|
||||
- 优先级高的切面在切入方法前执行的通知(before)会优先执行,但是位于方法后执行的通知(after,afterReturning)反而会延后执行,类似于同心圆原理。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/aop执行顺序.png"/> </div>
|
@ -6,7 +6,7 @@
|
||||
|
||||
切面配置位于resources下的aop.xml文件,其中CustomAdvice是自定义切面类,OrderService是待切入的方法。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-aop.png"/> </div>
|
||||
|
||||
|
||||
|
||||
|
@ -1,112 +1,48 @@
|
||||
# spring +druid+ mybatis(注解配置方式)
|
||||
# spring +druid+ mybatis(注解方式)
|
||||
|
||||
1、创建标准web maven工程,导入依赖
|
||||
### 项目目录结构
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-druid-mybatis-annotation.png"/> </div>
|
||||
|
||||
#### 1、创建maven工程,除了Spring基本依赖外,还需要导入mybatis和druid的相关依赖
|
||||
|
||||
```xml
|
||||
<?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.heibaiying</groupId>
|
||||
<!--jdbc 相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
<spring-base-version>5.1.3.RELEASE</spring-base-version>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--jdbc 相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<!--单元测试相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3.0</version>
|
||||
</dependency>
|
||||
<!--mybatis 依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>3.4.6</version>
|
||||
</dependency>
|
||||
<!--druid 依赖-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>1.1.12</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3.0</version>
|
||||
</dependency>
|
||||
<!--mybatis 依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>3.4.6</version>
|
||||
</dependency>
|
||||
<!--druid 依赖-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>1.1.12</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
2、新建 DispatcherServletInitializer.java继承自AbstractAnnotationConfigDispatcherServletInitializer,等价于我们在web.xml中配置的前端控制器
|
||||
#### 2、新建 DispatcherServletInitializer.java继承自AbstractAnnotationConfigDispatcherServletInitializer,等价于我们在web.xml中配置的前端控制器
|
||||
|
||||
```java
|
||||
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
@ -125,18 +61,11 @@ public class DispatcherServletInitializer extends AbstractAnnotationConfigDispat
|
||||
}
|
||||
```
|
||||
|
||||
3、基础servlet 3.0的支持,可以采用注解的方式注册druid的servlet和filter
|
||||
#### 3、基础servlet 3.0的支持,可以采用注解的方式注册druid的servlet和filter
|
||||
|
||||
注:关于servlet 注解支持可以查看[Servlet 规范文档](https://github.com/heibaiying/spring-samples-for-all/blob/master/referenced%20documents/Servlet3.1%E8%A7%84%E8%8C%83%EF%BC%88%E6%9C%80%E7%BB%88%E7%89%88%EF%BC%89.pdf)中**8.1小节 注解和可插拔性**
|
||||
注:关于servlet 更多注解支持可以查看[Servlet 规范文档](https://github.com/heibaiying/spring-samples-for-all/blob/master/referenced%20documents/Servlet3.1%E8%A7%84%E8%8C%83%EF%BC%88%E6%9C%80%E7%BB%88%E7%89%88%EF%BC%89.pdf)中**8.1小节 注解和可插拔性**
|
||||
|
||||
```java
|
||||
package com.heibaiying.config.druid;
|
||||
|
||||
import com.alibaba.druid.support.http.StatViewServlet;
|
||||
|
||||
import javax.servlet.annotation.WebInitParam;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 配置监控页面用户名密码
|
||||
@ -153,12 +82,6 @@ public class DruidStatViewServlet extends StatViewServlet {
|
||||
```
|
||||
|
||||
```java
|
||||
package com.heibaiying.config.druid;
|
||||
|
||||
import com.alibaba.druid.support.http.WebStatFilter;
|
||||
|
||||
import javax.servlet.annotation.WebFilter;
|
||||
import javax.servlet.annotation.WebInitParam;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
@ -175,7 +98,7 @@ public class DruidStatFilter extends WebStatFilter {
|
||||
|
||||
```
|
||||
|
||||
4、在resources文件夹下新建数据库配置文件mysql.properties、oracle.properties
|
||||
#### 4、在resources文件夹下新建数据库配置文件mysql.properties、oracle.properties
|
||||
|
||||
```properties
|
||||
# mysql 数据库配置
|
||||
@ -193,21 +116,9 @@ oracle.username=用户名
|
||||
oracle.password=密码
|
||||
```
|
||||
|
||||
5、在新建数据库配置映射类DataSourceConfig.java
|
||||
#### 5、在新建数据库配置映射类DataSourceConfig.java
|
||||
|
||||
```java
|
||||
package com.heibaiying.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
@PropertySource(value = "classpath:mysql.properties")
|
||||
@Data
|
||||
@ -226,30 +137,9 @@ public class DataSourceConfig {
|
||||
|
||||
```
|
||||
|
||||
6、新建ServletConfig.java,进行数据库相关配置
|
||||
#### 6、新建ServletConfig.java,进行数据库相关配置
|
||||
|
||||
```java
|
||||
package com.heibaiying.config;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.mybatis.spring.mapper.MapperScannerConfigurer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
|
||||
;
|
||||
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
@ -351,7 +241,7 @@ public class ServletConfig implements WebMvcConfigurer {
|
||||
|
||||
```
|
||||
|
||||
7、新建mybtais 配置文件 更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html)
|
||||
#### 7、新建mybtais 配置文件,按需要进行额外参数配置, 更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html)
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
@ -374,7 +264,7 @@ public class ServletConfig implements WebMvcConfigurer {
|
||||
|
||||
```
|
||||
|
||||
8、新建查询接口及其对应的mapper文件
|
||||
#### 8、新建查询接口及其对应的mapper文件
|
||||
|
||||
```java
|
||||
public interface MysqlDao {
|
||||
@ -421,25 +311,9 @@ public interface OracleDao {
|
||||
</mapper>
|
||||
```
|
||||
|
||||
9.新建测试controller进行测试
|
||||
#### 9.新建测试controller进行测试
|
||||
|
||||
```java
|
||||
package com.heibaiying.controller;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import com.heibaiying.dao.MysqlDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RestController
|
||||
public class MysqlController {
|
||||
|
||||
@ -455,19 +329,6 @@ public class MysqlController {
|
||||
```
|
||||
|
||||
```java
|
||||
package com.heibaiying.controller;
|
||||
|
||||
import com.heibaiying.dao.OracleDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RestController
|
||||
public class OracleController {
|
||||
|
||||
@ -482,7 +343,7 @@ public class OracleController {
|
||||
|
||||
```
|
||||
|
||||
10、druid 监控页面访问地址http://localhost:8080/druid/index.html
|
||||
#### 10、druid 监控页面访问地址http://localhost:8080/druid/index.html
|
||||
|
||||

|
||||
|
||||
|
@ -15,12 +15,6 @@ import org.springframework.context.annotation.PropertySource;
|
||||
@Data
|
||||
public class DataSourceConfig {
|
||||
|
||||
/**
|
||||
* 感觉这种注入的方式并不够好
|
||||
* 没有spring-boot中使用@ConfigurationProperties(prefix = "config")指定前缀注入的方式优雅
|
||||
*/
|
||||
@Value("${mysql.driverClassName}")
|
||||
private String driverClassName;
|
||||
@Value("${mysql.url}")
|
||||
private String url;
|
||||
@Value("${mysql.username}")
|
||||
|
@ -1,4 +1,4 @@
|
||||
# mysql 数据库配置
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
|
||||
mysql.username=root
|
||||
mysql.password=root
|
||||
|
@ -1,112 +1,48 @@
|
||||
# spring +druid+ mybatis(xml配置方式)
|
||||
|
||||
#### 1、创建标准web maven工程,导入依赖
|
||||
### 项目目录结构
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-druid-mybatis.png"/> </div>
|
||||
|
||||
#### 1、创建maven工程,除了Spring基本依赖外,还需要导入mybatis和druid的相关依赖
|
||||
|
||||
```xml
|
||||
<?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.heibaiying</groupId>
|
||||
<!--jdbc 相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
<spring-base-version>5.1.3.RELEASE</spring-base-version>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--jdbc 相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<!--单元测试相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3.0</version>
|
||||
</dependency>
|
||||
<!--mybatis 依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>3.4.6</version>
|
||||
</dependency>
|
||||
<!--druid 依赖-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>1.1.12</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3.0</version>
|
||||
</dependency>
|
||||
<!--mybatis 依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>3.4.6</version>
|
||||
</dependency>
|
||||
<!--druid 依赖-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>1.1.12</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
#### 2、在web.xml 进行如下配置
|
||||
#### 2、在web.xml 中配置spring前端控制器、druid监控台servlet和filter
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
@ -302,7 +238,7 @@ oracle.password=密码
|
||||
</beans>
|
||||
```
|
||||
|
||||
#### 5、新建mybtais 配置文件 更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html)
|
||||
#### 5、新建mybtais 配置文件,按需要进行额外配置,更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html)
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
|
@ -1,5 +1,5 @@
|
||||
# mysql 数据库配置
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
|
||||
mysql.username=root
|
||||
mysql.password=root
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
1.2 本项目dubbo的搭建采用zookeeper作为注册中心, 关于zookeeper的安装和基本操作可以参见我的手记[Zookeeper 基础命令与Java客户端](https://github.com/heibaiying/LearningNotes/blob/master/notes/%E4%B8%AD%E9%97%B4%E4%BB%B6/ZooKeeper/ZooKeeper%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA%E4%B8%8EJava%E5%AE%A2%E6%88%B7%E7%AB%AF.md)
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-dubbo.png"/> </div>
|
||||
|
||||
|
||||
|
||||
@ -51,11 +51,11 @@
|
||||
- api 下为公共的调用接口;
|
||||
- bean 下为公共的实体类。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/dubbo-ano-common.png"/> </div>
|
||||
|
||||
## 四、 服务提供者(dubbo-ano-provider)
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/dubbo-ano-provider.png"/> </div>
|
||||
|
||||
#### 4.1 提供方配置
|
||||
|
||||
@ -147,7 +147,7 @@ public class ProductService implements IProductService {
|
||||
|
||||
## 五、服务消费者(dubbo-ano-consumer)
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/dubbo-ano-consumer.png"/> </div>
|
||||
|
||||
#### 1.消费方的配置
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
1.2 本项目dubbo的搭建采用zookeeper作为注册中心, 关于zookeeper的安装和基本操作可以参见我的手记[Zookeeper 基础命令与Java客户端](https://github.com/heibaiying/LearningNotes/blob/master/notes/%E4%B8%AD%E9%97%B4%E4%BB%B6/ZooKeeper/ZooKeeper%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA%E4%B8%8EJava%E5%AE%A2%E6%88%B7%E7%AB%AF.md)
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-dubbo.png"/> </div>
|
||||
|
||||
|
||||
|
||||
@ -51,11 +51,11 @@
|
||||
- api 下为公共的调用接口;
|
||||
- bean 下为公共的实体类。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/dubbo-common.png"/> </div>
|
||||
|
||||
## 四、 服务提供者(dubbo-provider)
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/dubbo-provider.png"/> </div>
|
||||
|
||||
#### 4.1 productService是服务的提供者( 商品数据用模拟数据展示)
|
||||
|
||||
@ -124,7 +124,7 @@ public class ProductService implements IProductService {
|
||||
|
||||
## 五、服务消费者(dubbo-consumer)
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/dubbo-consumer.png"/> </div>
|
||||
|
||||
#### 1.在dubbo.xml调用远程的服务
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-email-annotation.png"/> </div>
|
||||
|
||||
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-email.png"/> </div>
|
||||
|
||||
|
||||
|
||||
|
@ -1,114 +1,41 @@
|
||||
# spring 整合 jdbc template(注解配置方式)
|
||||
# spring 整合 jdbc template(注解方式)
|
||||
|
||||
1、创建标准web maven工程,导入依赖
|
||||
## 1.说明
|
||||
|
||||
#### 1.1 项目目录结构
|
||||
|
||||
1. 数据源配置位于config目录下的DatabaseConfig.java和DataSourceConfig.java
|
||||
2. 项目以单元测试的方法进行测试
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-jdbc-annotation.png"/> </div>
|
||||
|
||||
|
||||
|
||||
#### 1.2 项目依赖
|
||||
|
||||
```xml
|
||||
<?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.heibaiying</groupId>
|
||||
<!--jdbc 相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
<spring-base-version>5.1.3.RELEASE</spring-base-version>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--jdbc 相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<!--单元测试相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
```
|
||||
|
||||
2、新建 DispatcherServletInitializer.java继承自AbstractAnnotationConfigDispatcherServletInitializer,等价于我们在web.xml中配置的前端控制器
|
||||
## 二、spring 整合 jdbc template
|
||||
|
||||
```java
|
||||
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return new Class[0];
|
||||
}
|
||||
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return new Class[]{ServletConfig.class};
|
||||
}
|
||||
|
||||
protected String[] getServletMappings() {
|
||||
return new String[]{"/"};
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
3、在resources文件夹下新建数据库配置文件mysql.properties、oracle.properties
|
||||
#### 2.1 在resources文件夹下新建数据库配置文件mysql.properties、oracle.properties及其映射类
|
||||
|
||||
```properties
|
||||
# mysql 数据库配置
|
||||
@ -126,21 +53,7 @@ oracle.username=用户名
|
||||
oracle.password=密码
|
||||
```
|
||||
|
||||
4、在新建数据库配置映射类DataSourceConfig.java
|
||||
|
||||
```java
|
||||
package com.heibaiying.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
@PropertySource(value = "classpath:mysql.properties")
|
||||
@Data
|
||||
@ -159,27 +72,13 @@ public class DataSourceConfig {
|
||||
|
||||
```
|
||||
|
||||
5、新建ServletConfig.java,进行数据库相关配置
|
||||
#### 2.2 新建数据库配置类DatabaseConfig.java
|
||||
|
||||
```java
|
||||
package com.heibaiying.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
@Configuration
|
||||
@EnableTransactionManagement // 开启声明式事务处理 等价于xml中<tx:annotation-driven/>
|
||||
@ComponentScan(basePackages = {"com.heibaiying.*"})
|
||||
public class ServletConfig implements WebMvcConfigurer {
|
||||
public class DatabaseConfig {
|
||||
|
||||
/**
|
||||
* 配置数据源
|
||||
@ -194,6 +93,7 @@ public class ServletConfig implements WebMvcConfigurer {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 配置jdbcTemplate
|
||||
* @param dataSource 这个参数的名称需要保持和上面方法名一致 才能自动注入,因为
|
||||
@ -217,36 +117,12 @@ public class ServletConfig implements WebMvcConfigurer {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
6、新建查询接口及其实现类,这里我查询的表是mysql和oracle中的字典表
|
||||
#### 2.3 新建查询接口及其实现类,这里我查询的表是mysql和oracle中的字典表
|
||||
|
||||
```java
|
||||
public interface MysqlDao {
|
||||
|
||||
List<Relation> get();
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import com.heibaiying.dao.impl.MysqlDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Repository
|
||||
public class MysqlDaoImpl implements MysqlDao {
|
||||
|
||||
@ -274,42 +150,9 @@ public class MysqlDaoImpl implements MysqlDao {
|
||||
|
||||
```
|
||||
|
||||
```mysql
|
||||
package com.heibaiying.dao.impl;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
public interface OracleDao {
|
||||
|
||||
List<Flow> get();
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
```java
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import com.heibaiying.dao.impl.OracleDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Repository
|
||||
public class OracleDaoImpl implements OracleDao {
|
||||
|
||||
@ -338,30 +181,11 @@ public class OracleDaoImpl implements OracleDao {
|
||||
|
||||
```
|
||||
|
||||
7、新建测试类进行测试
|
||||
#### 2.4 新建测试类进行测试
|
||||
|
||||
```java
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import com.heibaiying.config.DispatcherServletInitializer;
|
||||
import com.heibaiying.config.ServletConfig;
|
||||
import com.heibaiying.dao.impl.MysqlDao;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {DispatcherServletInitializer.class, ServletConfig.class})
|
||||
@ContextConfiguration(classes = {ServletConfig.class})
|
||||
public class MysqlDaoTest {
|
||||
|
||||
@Autowired
|
||||
@ -380,28 +204,8 @@ public class MysqlDaoTest {
|
||||
```
|
||||
|
||||
```java
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import com.heibaiying.config.DispatcherServletInitializer;
|
||||
import com.heibaiying.config.ServletConfig;
|
||||
import com.heibaiying.dao.impl.OracleDao;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {DispatcherServletInitializer.class, ServletConfig.class})
|
||||
@ContextConfiguration(classes = {ServletConfig.class})
|
||||
public class OracleDaoTest {
|
||||
|
||||
/*注入接口时: 如果接口有多个实现类 可以用这个指定具体的实现类*/
|
||||
|
@ -7,7 +7,6 @@ import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
@ -15,10 +14,6 @@ import org.springframework.context.annotation.PropertySource;
|
||||
@Data
|
||||
public class DataSourceConfig {
|
||||
|
||||
/**
|
||||
* 感觉这种注入的方式并不够好
|
||||
* 没有spring-boot中使用@ConfigurationProperties(prefix = "config")指定前缀注入的方式优雅
|
||||
*/
|
||||
@Value("${mysql.driverClassName}")
|
||||
private String driverClassName;
|
||||
@Value("${mysql.url}")
|
||||
|
@ -7,7 +7,6 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
@ -15,7 +14,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
@Configuration
|
||||
@EnableTransactionManagement // 开启声明式事务处理 等价于xml中<tx:annotation-driven/>
|
||||
@ComponentScan(basePackages = {"com.heibaiying.*"})
|
||||
public class ServletConfig implements WebMvcConfigurer {
|
||||
public class DatabaseConfig {
|
||||
|
||||
/**
|
||||
* 配置数据源
|
@ -1,22 +0,0 @@
|
||||
package com.heibaiying.config;
|
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
|
||||
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return new Class[0];
|
||||
}
|
||||
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return new Class[]{ServletConfig.class};
|
||||
}
|
||||
|
||||
protected String[] getServletMappings() {
|
||||
return new String[]{"/"};
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
# mysql 数据库配置
|
||||
mysql.driverClassName=com.mysql.jdbc.Driver
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql
|
||||
mysql.driverClassName=com.mysql.cj.jdbc.Driver
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
|
||||
mysql.username=root
|
||||
mysql.password=root
|
||||
|
@ -1,8 +1,7 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import com.heibaiying.config.DispatcherServletInitializer;
|
||||
import com.heibaiying.config.ServletConfig;
|
||||
import com.heibaiying.config.DatabaseConfig;
|
||||
import com.heibaiying.dao.impl.MysqlDao;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@ -18,7 +17,7 @@ import java.util.List;
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {DispatcherServletInitializer.class, ServletConfig.class})
|
||||
@ContextConfiguration(classes = {DatabaseConfig.class})
|
||||
public class MysqlDaoTest {
|
||||
|
||||
@Autowired
|
||||
|
@ -1,8 +1,7 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import com.heibaiying.config.DispatcherServletInitializer;
|
||||
import com.heibaiying.config.ServletConfig;
|
||||
import com.heibaiying.config.DatabaseConfig;
|
||||
import com.heibaiying.dao.impl.OracleDao;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@ -19,7 +18,7 @@ import java.util.List;
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {DispatcherServletInitializer.class, ServletConfig.class})
|
||||
@ContextConfiguration(classes = {DatabaseConfig.class})
|
||||
public class OracleDaoTest {
|
||||
|
||||
/*注入接口时: 如果接口有多个实现类 可以用这个指定具体的实现类*/
|
||||
|
@ -1,124 +1,38 @@
|
||||
# spring 整合 jdbc template(xml配置方式)
|
||||
|
||||
1、创建标准web maven工程,导入依赖
|
||||
## 一、说明
|
||||
|
||||
#### 1.1 项目结构
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-jdbc.png"/> </div>
|
||||
|
||||
#### 1.2 项目依赖
|
||||
|
||||
```xml
|
||||
<?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.heibaiying</groupId>
|
||||
<!--jdbc 相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
<spring-base-version>5.1.3.RELEASE</spring-base-version>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--jdbc 相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<!--单元测试相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
```
|
||||
|
||||
2、在web.xml 进行如下配置
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
|
||||
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||
version="3.1">
|
||||
|
||||
<!--配置spring前端控制器-->
|
||||
<servlet>
|
||||
<servlet-name>springMvc</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<init-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>classpath:springApplication.xml</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
## 二、 spring 整合 jdbc template
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>springMvc</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
</web-app>
|
||||
```
|
||||
|
||||
3、在resources文件夹下新建数据库配置文件jdbc.properties
|
||||
#### 1、在resources文件夹下新建数据库配置文件jdbc.properties
|
||||
|
||||
```properties
|
||||
# mysql 数据库配置
|
||||
@ -134,7 +48,7 @@ oracle.username=用户名
|
||||
oracle.password=密码
|
||||
```
|
||||
|
||||
4、在resources文件夹下创建springApplication.xml 配置文件
|
||||
#### 2、配置Jdbc数据源并定义事务管理器
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
@ -182,34 +96,9 @@ oracle.password=密码
|
||||
</beans>
|
||||
```
|
||||
|
||||
5、新建查询接口及其实现类,这里我查询的表是mysql和oracle中的字典表
|
||||
#### 3、新建查询接口及其实现类,这里我查询的表是mysql和oracle中的字典表
|
||||
|
||||
```java
|
||||
public interface MysqlDao {
|
||||
|
||||
List<Relation> get();
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import com.heibaiying.dao.impl.MysqlDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Repository
|
||||
public class MysqlDaoImpl implements MysqlDao {
|
||||
|
||||
@ -237,42 +126,7 @@ public class MysqlDaoImpl implements MysqlDao {
|
||||
|
||||
```
|
||||
|
||||
```mysql
|
||||
package com.heibaiying.dao.impl;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
public interface OracleDao {
|
||||
|
||||
List<Flow> get();
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
```java
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import com.heibaiying.dao.impl.OracleDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Repository
|
||||
public class OracleDaoImpl implements OracleDao {
|
||||
|
||||
@ -298,29 +152,11 @@ public class OracleDaoImpl implements OracleDao {
|
||||
return flows;
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
6.新建测试类进行测试
|
||||
#### 4.新建测试类进行测试
|
||||
|
||||
```java
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import com.heibaiying.dao.impl.MysqlDao;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration({"classpath:springApplication.xml"})
|
||||
public class MysqlDaoTest {
|
||||
@ -342,24 +178,6 @@ public class MysqlDaoTest {
|
||||
```
|
||||
|
||||
```java
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import com.heibaiying.dao.impl.OracleDao;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration({"classpath:springApplication.xml"})
|
||||
public class OracleDaoTest {
|
||||
|
@ -1,6 +1,6 @@
|
||||
# mysql 数据库配置
|
||||
mysql.driverClassName=com.mysql.jdbc.Driver
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql
|
||||
mysql.driverClassName=com.mysql.cj.jdbc.Driver
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
|
||||
mysql.username=root
|
||||
mysql.password=root
|
||||
|
||||
|
@ -10,7 +10,7 @@ XMemcached是基于java nio的memcached高性能客户端,支持完整的memca
|
||||
|
||||
1. memcached的整合配置位于com.heibaiying.config文件夹下。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-memcached-annotation.png"/> </div>
|
||||
|
||||
### 1.3 依赖说明
|
||||
|
||||
|
@ -11,7 +11,7 @@ XMemcached是基于java nio的memcached高性能客户端,支持完整的memca
|
||||
1. memcached的整合配置位于resources下的memcached文件夹下,其中集群配置用cluster开头。所有配置按照需要在springApplication.xml用import导入。
|
||||
2. 实体类Programmer.java用于测试memcached序列化与反序列化
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-memcached.png"/> </div>
|
||||
|
||||
**springapplication.xml文件:**
|
||||
|
||||
|
@ -2,6 +2,7 @@ package com.heibaiying.memcached;
|
||||
|
||||
import net.rubyeye.xmemcached.MemcachedClient;
|
||||
import net.rubyeye.xmemcached.exception.MemcachedException;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -28,6 +29,6 @@ public class MemSamples {
|
||||
System.out.println("hello=" + value);
|
||||
memcachedClient.delete("hello");
|
||||
value = memcachedClient.get("hello");
|
||||
System.out.println("hello=" + value);
|
||||
Assert.assertNull(value);
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
配置文件位于com.heibaiying.config下,项目以单元测试的方式进行测试。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-mongodb-annotation.png"/> </div>
|
||||
|
||||
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
配置文件位于resources下,项目以单元测试的方式进行测试。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-mongodb.png"/> </div>
|
||||
|
||||
|
||||
|
||||
|
@ -1,125 +1,48 @@
|
||||
# spring 整合 mybatis(注解配置方式)
|
||||
# spring 整合 mybatis(注解方式)
|
||||
|
||||
1、创建标准web maven工程,导入依赖
|
||||
## 一、说明
|
||||
|
||||
#### 1.1 项目结构
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-mybatis-annotation.png"/> </div>
|
||||
|
||||
#### 1.2 项目依赖
|
||||
|
||||
除了spring相关依赖外,还需要导入数据库驱动和对应的mybatis依赖包
|
||||
|
||||
```xml
|
||||
<?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.heibaiying</groupId>
|
||||
<!--jdbc 相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
<spring-base-version>5.1.3.RELEASE</spring-base-version>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--jdbc 相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<!--单元测试相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3.0</version>
|
||||
</dependency>
|
||||
<!--mybatis 依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>3.4.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3.0</version>
|
||||
</dependency>
|
||||
<!--mybatis 依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>3.4.6</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
2、新建 DispatcherServletInitializer.java继承自AbstractAnnotationConfigDispatcherServletInitializer,等价于我们在web.xml中配置的前端控制器
|
||||
## 二、spring 整合 mybatis
|
||||
|
||||
```java
|
||||
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return new Class[0];
|
||||
}
|
||||
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return new Class[]{ServletConfig.class};
|
||||
}
|
||||
|
||||
protected String[] getServletMappings() {
|
||||
return new String[]{"/"};
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
3、在resources文件夹下新建数据库配置文件mysql.properties、oracle.properties
|
||||
#### 2.1 在resources文件夹下新建数据库配置文件jdbc.properties及其映射类
|
||||
|
||||
```properties
|
||||
# mysql 数据库配置
|
||||
@ -127,9 +50,7 @@ mysql.driverClassName=com.mysql.jdbc.Driver
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql
|
||||
mysql.username=root
|
||||
mysql.password=root
|
||||
```
|
||||
|
||||
```properties
|
||||
# oracle 数据库配置
|
||||
oracle.driverClassName=oracle.jdbc.driver.OracleDriver
|
||||
oracle.url=jdbc:oracle:thin:@//IP地址:端口号/数据库实例名
|
||||
@ -137,21 +58,7 @@ oracle.username=用户名
|
||||
oracle.password=密码
|
||||
```
|
||||
|
||||
4、在新建数据库配置映射类DataSourceConfig.java
|
||||
|
||||
```java
|
||||
package com.heibaiying.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
@PropertySource(value = "classpath:mysql.properties")
|
||||
@Data
|
||||
@ -167,35 +74,18 @@ public class DataSourceConfig {
|
||||
private String password;
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
5、新建ServletConfig.java,进行数据库相关配置
|
||||
#### 2.2 配置数据源和mybatis会话工厂、定义事务管理器
|
||||
|
||||
```java
|
||||
package com.heibaiying.config;
|
||||
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.mybatis.spring.mapper.MapperScannerConfigurer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
@Configuration
|
||||
@EnableTransactionManagement // 开启声明式事务处理 等价于xml中<tx:annotation-driven/>
|
||||
@ComponentScan(basePackages = {"com.heibaiying.*"})
|
||||
public class ServletConfig implements WebMvcConfigurer {
|
||||
public class DatabaseConfig {
|
||||
|
||||
/* @Autowired
|
||||
* private DataSourceConfig sourceConfig;
|
||||
@ -216,7 +106,6 @@ public class ServletConfig implements WebMvcConfigurer {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 配置mybatis 会话工厂
|
||||
*
|
||||
@ -258,7 +147,7 @@ public class ServletConfig implements WebMvcConfigurer {
|
||||
|
||||
```
|
||||
|
||||
5、新建mybtais 配置文件 更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html)
|
||||
#### 2.3 新建mybtais配置文件,按照需求配置额外参数, 更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html)
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
@ -281,7 +170,7 @@ public class ServletConfig implements WebMvcConfigurer {
|
||||
|
||||
```
|
||||
|
||||
5、新建查询接口及其对应的mapper文件
|
||||
#### 2.4 新建查询接口及其对应的mapper文件
|
||||
|
||||
```java
|
||||
public interface MysqlDao {
|
||||
@ -328,30 +217,11 @@ public interface OracleDao {
|
||||
</mapper>
|
||||
```
|
||||
|
||||
6.新建测试类进行测试
|
||||
#### 2.5 新建测试类进行测试
|
||||
|
||||
```java
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import com.heibaiying.config.DataSourceConfig;
|
||||
import com.heibaiying.config.DispatcherServletInitializer;
|
||||
import com.heibaiying.config.ServletConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {DispatcherServletInitializer.class, ServletConfig.class})
|
||||
@ContextConfiguration(classes = {DatabaseConfig.class})
|
||||
public class MysqlDaoTest {
|
||||
|
||||
@Autowired
|
||||
@ -370,27 +240,8 @@ public class MysqlDaoTest {
|
||||
```
|
||||
|
||||
```java
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import com.heibaiying.config.DataSourceConfig;
|
||||
import com.heibaiying.config.DispatcherServletInitializer;
|
||||
import com.heibaiying.config.ServletConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {DispatcherServletInitializer.class, ServletConfig.class})
|
||||
@ContextConfiguration(classes = {DatabaseConfig.class})
|
||||
public class OracleDaoTest {
|
||||
|
||||
@Autowired
|
||||
@ -406,5 +257,6 @@ public class OracleDaoTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
@ -7,7 +7,6 @@ import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
@ -15,10 +14,6 @@ import org.springframework.context.annotation.PropertySource;
|
||||
@Data
|
||||
public class DataSourceConfig {
|
||||
|
||||
/**
|
||||
* 感觉这种注入的方式并不够好
|
||||
* 没有spring-boot中使用@ConfigurationProperties(prefix = "config")指定前缀注入的方式优雅
|
||||
*/
|
||||
@Value("${mysql.driverClassName}")
|
||||
private String driverClassName;
|
||||
@Value("${mysql.url}")
|
||||
|
@ -20,7 +20,7 @@ import java.io.IOException;
|
||||
@Configuration
|
||||
@EnableTransactionManagement // 开启声明式事务处理 等价于xml中<tx:annotation-driven/>
|
||||
@ComponentScan(basePackages = {"com.heibaiying.*"})
|
||||
public class ServletConfig implements WebMvcConfigurer {
|
||||
public class DatabaseConfig {
|
||||
|
||||
/* @Autowired
|
||||
* private DataSourceConfig sourceConfig;
|
@ -1,22 +0,0 @@
|
||||
package com.heibaiying.config;
|
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
|
||||
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return new Class[0];
|
||||
}
|
||||
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return new Class[]{ServletConfig.class};
|
||||
}
|
||||
|
||||
protected String[] getServletMappings() {
|
||||
return new String[]{"/"};
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
# mysql 数据库配置
|
||||
mysql.driverClassName=com.mysql.jdbc.Driver
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql
|
||||
mysql.driverClassName=com.mysql.cj.jdbc.Driver
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
|
||||
mysql.username=root
|
||||
mysql.password=root
|
||||
|
@ -1,9 +1,7 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import com.heibaiying.config.DataSourceConfig;
|
||||
import com.heibaiying.config.DispatcherServletInitializer;
|
||||
import com.heibaiying.config.ServletConfig;
|
||||
import com.heibaiying.config.DatabaseConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -18,7 +16,7 @@ import java.util.List;
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {DispatcherServletInitializer.class, ServletConfig.class})
|
||||
@ContextConfiguration(classes = {DatabaseConfig.class})
|
||||
public class MysqlDaoTest {
|
||||
|
||||
@Autowired
|
||||
|
@ -1,9 +1,7 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import com.heibaiying.config.DataSourceConfig;
|
||||
import com.heibaiying.config.DispatcherServletInitializer;
|
||||
import com.heibaiying.config.ServletConfig;
|
||||
import com.heibaiying.config.DatabaseConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -18,7 +16,7 @@ import java.util.List;
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {DispatcherServletInitializer.class, ServletConfig.class})
|
||||
@ContextConfiguration(classes = {DatabaseConfig.class})
|
||||
public class OracleDaoTest {
|
||||
|
||||
@Autowired
|
||||
|
@ -1,135 +1,48 @@
|
||||
# spring 整合 mybatis(xml配置方式)
|
||||
|
||||
1、创建标准web maven工程,导入依赖
|
||||
## 一、说明
|
||||
|
||||
#### 1.1 项目结构
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-mybatis.png"/> </div>
|
||||
|
||||
#### 1.2 项目依赖
|
||||
|
||||
除了spring相关依赖外,还需要导入数据库驱动和对应的mybatis依赖包
|
||||
|
||||
```xml
|
||||
<?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.heibaiying</groupId>
|
||||
<!--jdbc 相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
<spring-base-version>5.1.3.RELEASE</spring-base-version>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--jdbc 相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<!--单元测试相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3.0</version>
|
||||
</dependency>
|
||||
<!--mybatis 依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>3.4.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3.0</version>
|
||||
</dependency>
|
||||
<!--mybatis 依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>3.4.6</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
2、在web.xml 进行如下配置
|
||||
## 二、spring 整合 mybatis
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
|
||||
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||
version="3.1">
|
||||
|
||||
<!--配置spring前端控制器-->
|
||||
<servlet>
|
||||
<servlet-name>springMvc</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<init-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>classpath:springApplication.xml</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>springMvc</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
</web-app>
|
||||
```
|
||||
|
||||
3、在resources文件夹下新建数据库配置文件jdbc.properties
|
||||
#### 2.1 在resources文件夹下新建数据库配置文件jdbc.properties
|
||||
|
||||
```properties
|
||||
# mysql 数据库配置
|
||||
@ -145,7 +58,7 @@ oracle.username=用户名
|
||||
oracle.password=密码
|
||||
```
|
||||
|
||||
4、在resources文件夹下创建springApplication.xml 配置文件
|
||||
#### 2.2 配置数据源和mybatis会话工厂、定义事务管理器
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
@ -205,7 +118,7 @@ oracle.password=密码
|
||||
</beans>
|
||||
```
|
||||
|
||||
5、新建mybtais 配置文件 更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html)
|
||||
#### 2.3 新建mybtais配置文件,按照需求配置额外参数, 更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html)
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
@ -228,7 +141,7 @@ oracle.password=密码
|
||||
|
||||
```
|
||||
|
||||
5、新建查询接口及其对应的mapper文件
|
||||
#### 2.4 新建查询接口及其对应的mapper文件
|
||||
|
||||
```java
|
||||
public interface MysqlDao {
|
||||
@ -275,26 +188,9 @@ public interface OracleDao {
|
||||
</mapper>
|
||||
```
|
||||
|
||||
6.新建测试类进行测试
|
||||
#### 2.5 新建测试类进行测试
|
||||
|
||||
```java
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import com.heibaiying.dao.impl.MysqlDao;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration({"classpath:springApplication.xml"})
|
||||
public class MysqlDaoTest {
|
||||
@ -316,24 +212,6 @@ public class MysqlDaoTest {
|
||||
```
|
||||
|
||||
```java
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import com.heibaiying.dao.impl.OracleDao;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration({"classpath:springApplication.xml"})
|
||||
public class OracleDaoTest {
|
||||
|
@ -1,6 +1,6 @@
|
||||
# mysql 数据库配置
|
||||
mysql.driverClassName=com.mysql.jdbc.Driver
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql
|
||||
mysql.driverClassName=com.mysql.cj.jdbc.Driver
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
|
||||
mysql.username=root
|
||||
mysql.password=root
|
||||
|
||||
|
@ -14,7 +14,7 @@
|
||||
|
||||
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-rabbitmq-annotation.png"/> </div>
|
||||
|
||||
|
||||
|
||||
|
@ -14,7 +14,7 @@
|
||||
|
||||
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-rabbitmq.png"/> </div>
|
||||
|
||||
|
||||
|
||||
|
@ -11,7 +11,7 @@
|
||||
|
||||
注:关于redis其他语言官方推荐的客户端可以在[客户端](http://www.redis.cn/clients.html)该网页查看,其中官方推荐的用了黄色星星:star:标注。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/redis官方推荐客户端.png"/> </div>
|
||||
|
||||
|
||||
|
||||
@ -24,7 +24,7 @@
|
||||
1. jedis和redisson的配置类和单元测试分别位于config和test下对应的包中,其中集群的配置类以cluster开头。
|
||||
2. 实体类Programmer.java用于测试Redisson序列化与反序列化
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-redis-annotation.png"/> </div>
|
||||
|
||||
|
||||
|
||||
|
@ -11,7 +11,7 @@
|
||||
|
||||
注:关于redis其他语言官方推荐的客户端可以在[客户端](http://www.redis.cn/clients.html)该网页查看,其中官方推荐的用了黄色星星:star:标注。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/redis官方推荐客户端.png"/> </div>
|
||||
|
||||
|
||||
|
||||
@ -24,7 +24,7 @@
|
||||
1. jedis和redisson的配置和单元测试分别位于resources和test下对应的包中,其中集群的配置文件以cluster结尾。所有配置按照需要在springApplication.xml用import导入。
|
||||
2. 实体类Programmer.java用于测试Redisson序列化与反序列化
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-redis.png"/> </div>
|
||||
|
||||
**springapplication.xml文件:**
|
||||
|
||||
@ -281,7 +281,7 @@ public class JedisClusterSamples {
|
||||
<!-- 最基本配置 -->
|
||||
<redisson:client>
|
||||
<!--集群更多配置参数见官方文档 2.4.2 通过JSON、YAML和Spring XML文件配置集群模式
|
||||
<a src="https://github.com/redisson/redisson/wiki/2.-%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95#24-%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F"> -->
|
||||
<a href="https://github.com/redisson/redisson/wiki/2.-%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95#24-%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F"> -->
|
||||
<redisson:cluster-servers>
|
||||
<redisson:node-address value="redis://127.0.0.1:6379"/>
|
||||
<redisson:node-address value="redis://127.0.0.1:6380"/>
|
||||
|
@ -8,7 +8,7 @@
|
||||
<!-- 最基本配置 -->
|
||||
<redisson:client>
|
||||
<!--集群更多配置参数见官方文档 2.4.2 通过JSON、YAML和Spring XML文件配置集群模式
|
||||
<a src="https://github.com/redisson/redisson/wiki/2.-%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95#24-%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F"> -->
|
||||
<a href="https://github.com/redisson/redisson/wiki/2.-%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95#24-%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F"> -->
|
||||
<redisson:cluster-servers>
|
||||
<redisson:node-address value="redis://127.0.0.1:6379"/>
|
||||
<redisson:node-address value="redis://127.0.0.1:6380"/>
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
关于任务的调度配置定义在ServletConfig.java中,为方便观察项目定时执行的情况,项目以web的方式构建。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-scheduling-annotation.png"/> </div>
|
||||
|
||||
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
关于任务的调度配置定义在springApplication.xml中,为方便观察项目定时执行的情况,项目以web的方式构建。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-scheduling.png"/> </div>
|
||||
|
||||
|
||||
|
||||
|
@ -11,7 +11,7 @@
|
||||
5. webSocketConfig 是websocket 的主要配置类;
|
||||
6. 项目以web的方式构建。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-websocket-annotation.png"/> </div>
|
||||
|
||||
|
||||
|
||||
|
@ -9,7 +9,7 @@
|
||||
3. CustomHandershakerInterceptor为自定义的 websocket 的握手拦截器;
|
||||
4. 项目以web的方式构建。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-websocket.png"/> </div>
|
||||
|
||||
|
||||
|
||||
|
71
z-utils/ChangeImageUrl.java
Normal file
71
z-utils/ChangeImageUrl.java
Normal file
@ -0,0 +1,71 @@
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 转换本地图片为Github图片工具类
|
||||
*/
|
||||
public class ChangeImageUrl {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
if (args.length < 1) {
|
||||
System.out.println("请传递路径");
|
||||
return;
|
||||
}
|
||||
|
||||
String dir = args[0];
|
||||
|
||||
String preUrl = "https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/";
|
||||
String regex = "(!\\[(\\S*)]\\(D:\\\\spring-samples-for-all\\\\pictures\\\\(\\S*)\\)[^(</br>)]*?)";
|
||||
|
||||
List<String> filesList = getAllFile(dir, new ArrayList<>());
|
||||
for (String filePath : filesList) {
|
||||
changeImageUrl(filePath, preUrl, regex);
|
||||
}
|
||||
System.out.println("图片地址转换成功!");
|
||||
}
|
||||
|
||||
|
||||
private static void changeImageUrl(String filePath, String preUrl, String oldImageUrlRegex) throws IOException {
|
||||
|
||||
FileReader reader = new FileReader(filePath);
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
char[] chars = new char[1024 * 1024];
|
||||
int read = 0;
|
||||
while ((read = reader.read(chars)) != -1) {
|
||||
stringBuilder.append(new String(chars, 0, read));
|
||||
}
|
||||
reader.close();
|
||||
String content = stringBuilder.toString();
|
||||
//github 居中方式 <div align="center"> <img src=""/> </div>
|
||||
String newContent = content.replaceAll(oldImageUrlRegex,
|
||||
String.format("<div align=\"center\"> <img src=\"%s$3\"/> </div>", preUrl));
|
||||
FileWriter fileWriter = new FileWriter(new File(filePath));
|
||||
fileWriter.write(newContent);
|
||||
fileWriter.flush();
|
||||
|
||||
}
|
||||
|
||||
private static List<String> getAllFile(String dir, List<String> filesList) {
|
||||
File file = new File(dir);
|
||||
//如果是文件 则不遍历
|
||||
if (file.isFile() && file.getName().endsWith(".md")) {
|
||||
filesList.add(file.getAbsolutePath());
|
||||
}
|
||||
//如果是文件夹 则遍历下面的所有文件
|
||||
File[] files = file.listFiles();
|
||||
if (files != null) {
|
||||
for (File f : files) {
|
||||
if (f.isDirectory() && !f.getName().startsWith(".")) {
|
||||
getAllFile(f.getAbsolutePath(), filesList);
|
||||
} else if (f.getName().endsWith(".md")) {
|
||||
filesList.add(f.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
return filesList;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user