SpringBoot + Jpa(Hibernate) 架構基本配置

一、基於springboot-1.4.0.RELEASE版本測試css

二、springBoot + Hibernate + Druid + Mysql + servlet(jsp)html

1、maven的pom文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?xml version= "1.0" encoding= "UTF-8" ?>
         <modelVersion> 4.0 . 0 </modelVersion>
         <groupId>com.zsx</groupId>
         <artifactId>demo</artifactId>
         <packaging>war</packaging>
         <version> 0.0 . 1 </version>
         <name>zsx Maven Webapp</name>
         <url>http: //maven.apache.org</url>
 
         <properties>
             <project.build.sourceEncoding>UTF- 8 </project.build.sourceEncoding>
             <jdk.version> 1.7 </jdk.version>
             <tomcat.version> 7.0 . 69 </tomcat.version>
         </properties>
 
         <parent>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-parent</artifactId>
             <version> 1.4 . 0 .RELEASE</version>
         </parent>
 
         <dependencies>
 
             <!-- 添加對jsp視圖解析的支持 -->
             <dependency>
                 <groupId>org.apache.tomcat.embed</groupId>
                 <artifactId>tomcat-embed-jasper</artifactId>
             </dependency>
             <dependency>
                 <groupId>javax.servlet</groupId>
                 <artifactId>jstl</artifactId>
             </dependency>
 
 
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-web</artifactId>
             </dependency>
 
 
             <!-- 下面兩個引入爲了操做數據庫 -->
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-data-jpa</artifactId>
             </dependency>
             <dependency>
                 <groupId>mysql</groupId>
                 <artifactId>mysql-connector-java</artifactId>
             </dependency>
 
 
 
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-tomcat</artifactId>
                 <scope>provided</scope>
             </dependency>
 
             <!-- 只需引入spring-boot-devtools 便可實現熱部署 -->
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-devtools</artifactId>
             </dependency>
 
             <!-- Json包 -->
             <dependency>
                 <groupId>com.alibaba</groupId>
                 <artifactId>fastjson</artifactId>
                 <version> 1.2 . 16 </version>
             </dependency>
 
             <!-- 爲了監控數據庫 -->
             <dependency>
                 <groupId>com.alibaba</groupId>
                 <artifactId>druid</artifactId>
                 <version> 1.0 . 25 </version>
             </dependency>
 
 
             <dependency>
                 <groupId>org.apache.poi</groupId>
                 <artifactId>poi</artifactId>
                 <version> 3.14 </version>
             </dependency>
 
             <!-- Junit 單元測試 -->
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-test</artifactId>
                 <scope>test</scope>
             </dependency>
 
             <dependency>
                 <groupId>io.springfox</groupId>
                 <artifactId>springfox-swagger2</artifactId>
                 <version> 2.6 . 0 </version>
             </dependency>
             <dependency>
                 <groupId>io.springfox</groupId>
                 <artifactId>springfox-swagger-ui</artifactId>
                 <version> 2.6 . 0 </version>
             </dependency>
 
         </dependencies>
         <build>
             <finalName>/</finalName>
 
             <plugins>
 
                 <plugin>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-maven-plugin</artifactId>
                     <dependencies>
                         <!-- 熱部署 -->
                         <dependency>
                             <groupId>org.springframework</groupId>
                             <artifactId>springloaded</artifactId>
                             <version> 1.2 . 6 .RELEASE</version>
                         </dependency>
                     </dependencies>
                 </plugin>
 
             </plugins>
 
         </build>
 
         <repositories>
             <repository>
                 <id>ali</id>
                 <name>ali Repository</name>
                 <url>http: //maven.aliyun.com/nexus/content/groups/public/</url>
                 <snapshots>
                     <enabled> false </enabled>
                 </snapshots>
             </repository>
         </repositories>
     </project>

2、項目架構

想一想仍是介紹一下項目的目錄結構,這樣方便梳理總體的架構配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
src
├─main
     │  ├─java
     │  │  └─com
     │  │      └─zsx
     │  │          │  Application.java
     │  │          │  SpringBootStartApplication.java
     │  │          │ 
     │  │          ├─common
     │  │          │  ├─config
     │  │          │  │      DruidDBConfig.java
     │  │          │  │      MultipartConfig.java
     │  │          │  │     
     │  │          │  ├─filter
     │  │          │  │      DruidStatFilter.java
     │  │          │  │     
     │  │          │  ├─interceptors
     │  │          │  │      AuthInterceptor.java
     │  │          │  │      WebAppConfigurer.java
     │  │          │  │     
     │  │          │  ├─servlet
     │  │          │  │      DruidStatViewServlet.java
     │  │          │  │     
     │  │          │  └─swagger
     │  │          │          Swagger2.java
     │  │          │         
     │  │          ├─controller
     │  │          │  │  LoginController.java
     │  │          │  │  TestController.java
     │  │          │  │  UserController.java
     │  │          │         
     │  │          ├─dao
     │  │          │  │  TUserDao.java
     │  │          │  │ 
     │  │          │  └─impl
     │  │          ├─entity
     │  │          │  │  BaseEntity.java
     │  │          │         
     │  │          ├─model
     │  │          │  │  Tree.java
     │  │          │         
     │  │          ├─service
     │  │          │  │  UserService.java
     │  │          │  │ 
     │  │          │  └─impl
     │  │          │          UserServiceImpl.java
     │  │          │         
     │  │          └─util
     │  │                  GeneratePageable.java
     │  │                 
     │  ├─resources
     │  │  │  application.properties
     │  │  │  logback-test.xml
     │  │  │ 
     │  │  └─ static
     │  │      ├─css
     │  │      ├─img
     │  │      └─js
     │  │                 
     │  └─webapp
     │      │  index.jsp
     │      │ 
     │      └─WEB-INF
     │          │  web.xml
     │          │ 
     │          └─view
     │              │  login.jsp
     │              │     
     │              ├─error
     │              │      500 .jsp  
     │              ├─jsp
     │                     main.jsp
     │                     
     └─test
         └─java
                 UtilTest.java

標準的maven項目結構,其中java下是dao、service、controller ,還有實體類映射entity,其餘配置configjava

3、resources下的應用配置文件application.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#server.port= 9090
 
 
# 數據庫訪問配置
# 主數據源,默認的
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql: //localhost:3306/test
spring.datasource.username= root
spring.datasource.password= root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
 
 
# 下面爲鏈接池的補充設置,應用到上面全部數據源中
# 初始化大小,最小,最大
spring.datasource.initialSize= 5
spring.datasource.minIdle= 5
spring.datasource.maxActive= 20
# 配置獲取鏈接等待超時的時間
spring.datasource.maxWait= 60000
# 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis= 60000
# 配置一個鏈接在池中最小生存的時間,單位是毫秒
spring.datasource.minEvictableIdleTimeMillis= 300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle= true
spring.datasource.testOnBorrow= false
spring.datasource.testOnReturn= false
# 打開PSCache,而且指定每一個鏈接上PSCache的大小
spring.datasource.poolPreparedStatements= true
spring.datasource.maxPoolPreparedStatementPerConnectionSize= 20
# 配置監控統計攔截的filters,去掉後監控界面sql沒法統計, 'wall' 用於防火牆
spring.datasource.filters=stat,wall,log4j
# 經過connectProperties屬性來打開mergeSql功能;慢SQL記錄
spring.datasource.connectionProperties=druid.stat.mergeSql= true ;druid.stat.slowSqlMillis= 5000
# 合併多個DruidDataSource的監控數據
spring.datasource.useGlobalDataSourceStat= true
 
 
#JPA Configuration: 
spring.jpa.database=MYSQL
# Show or not log for each sql query
spring.jpa.show-sql= false
spring.jpa.generate-ddl= true 
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto=create 
#spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect 
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 
#spring.jpa.database=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
 
 
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
#spring.resources. static -locations=classpath:/resources/,classpath:/ static /

4、啓動應用主類文件 Application.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.zsx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
 
@SpringBootApplication
@ServletComponentScan // 掃描使用註解方式的servlet
public class Application {
 
     public static void main(String[] args) {
         SpringApplication.run(Application. class , args);
     }
}

若須要部署到外部的tomcat容器中,則添加下面類便可。mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.zsx;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
/**
  * 修改啓動類,繼承 SpringBootServletInitializer 並重寫 configure 方法
  * @author ZSX
  *
  */
public class SpringBootStartApplication extends SpringBootServletInitializer {
 
     private static final Logger logger = LoggerFactory.getLogger(SpringBootStartApplication. class );
 
     @Override
     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
         return builder.sources(Application. class );
     }
 
}

5、數據庫鏈接池Druid的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.zsx.common.config;
 
import java.sql.SQLException;
 
import javax.sql.DataSource;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
 
import com.alibaba.druid.pool.DruidDataSource;
 
/**
  * DruidDBConfig類被@Configuration標註,用做配置信息;
  * DataSource對象被@Bean聲明,爲Spring容器所管理,
  * @Primary表示這裏定義的DataSource將覆蓋其餘來源的DataSource。
  * @author ZSX
  *jdbc.url=${jdbc.url}
  *最新的支持方式以下:
  *jdbc.url=@jdbc.url@ 
  */
@Configuration
public class DruidDBConfig {
//  private Logger logger = LoggerFactory.getLogger(DruidDBConfig.class);
 
     @Value ( "${spring.datasource.url}" )
     private String dbUrl;
 
     @Value ( "${spring.datasource.username}" )
     private String username;
 
     @Value ( "${spring.datasource.password}" )
     private String password;
 
     @Value ( "${spring.datasource.driverClassName}" )
     private String driverClassName;
 
     @Value ( "${spring.datasource.initialSize}" )
     private int initialSize;
 
     @Value ( "${spring.datasource.minIdle}" )
     private int minIdle;
 
     @Value ( "${spring.datasource.maxActive}" )
     private int maxActive;
 
     @Value ( "${spring.datasource.maxWait}" )
     private int maxWait;
 
     @Value ( "${spring.datasource.timeBetweenEvictionRunsMillis}" )
     private int timeBetweenEvictionRunsMillis;
 
     @Value ( "${spring.datasource.minEvictableIdleTimeMillis}" )
     private int minEvictableIdleTimeMillis;
 
     @Value ( "${spring.datasource.validationQuery}" )
     private String validationQuery;
 
     @Value ( "${spring.datasource.testWhileIdle}" )
     private boolean testWhileIdle;
 
     @Value ( "${spring.datasource.testOnBorrow}" )
     private boolean testOnBorrow;
 
     @Value ( "${spring.datasource.testOnReturn}" )
     private boolean testOnReturn;
 
     @Value ( "${spring.datasource.poolPreparedStatements}" )
     private boolean poolPreparedStatements;
 
     @Value ( "${spring.datasource.maxPoolPreparedStatementPerConnectionSize}" )
     private int maxPoolPreparedStatementPerConnectionSize;
 
     @Value ( "${spring.datasource.filters}" )
     private String filters;
 
     @Value ( "{spring.datasource.connectionProperties}" )
     private String connectionProperties;
 
     @Bean // 聲明其爲Bean實例
     @Primary // 在一樣的DataSource中,首先使用被標註的DataSource
     public DataSource dataSource() {
         DruidDataSource datasource = new DruidDataSource();
 
         datasource.setUrl( this .dbUrl);
         datasource.setUsername(username);
         datasource.setPassword(password);
         datasource.setDriverClassName(driverClassName);
 
         // configuration
         datasource.setInitialSize(initialSize);
         datasource.setMinIdle(minIdle);
         datasource.setMaxActive(maxActive);
         datasource.setMaxWait(maxWait);
         datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
         datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
         datasource.setValidationQuery(validationQuery);
         datasource.setTestWhileIdle(testWhileIdle);
         datasource.setTestOnBorrow(testOnBorrow);
         datasource.setTestOnReturn(testOnReturn);
         datasource.setPoolPreparedStatements(poolPreparedStatements);
         datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
         try {
             datasource.setFilters(filters);
         } catch (SQLException e) {
 
         }
         datasource.setConnectionProperties(connectionProperties);
 
         return datasource;
     }
}

springboot裏默認使用tomcat的上傳文件大小限制,即1MB,
修改用下面的配置類:web

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import javax.servlet.MultipartConfigElement;
 
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class MultipartConfig {
 
     @Bean
     public MultipartConfigElement multipartConfigElement(){
         MultipartConfigFactory factory = new MultipartConfigFactory();
         factory.setMaxFileSize( "10MB" );
         factory.setMaxRequestSize( "10MB" );
         return factory.createMultipartConfig();
     }
 
}

6、開啓Druid的數據庫監控配置

一、配置Filterspring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
 
import com.alibaba.druid.support.http.WebStatFilter;
 
/**
  * 配置druid監控統計功能
  * 配置Filter
  * @author ZSX
  *
  */
 
@WebFilter (filterName = "druidWebStatFilter" , urlPatterns = "/*" ,
     initParams = {
             @WebInitParam (name= "exclusions" ,value= "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*" ) // 忽略資源
     }
)
 
public class DruidStatFilter extends WebStatFilter {
 
}

二、 配置web訪問的servletsql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
 
import com.alibaba.druid.support.http.StatViewServlet;
 
 
/**
  * 配置druid監控統計功能
  * 在SpringBoot項目中基於註解的配置,若是是web.xml配置,按規則配置便可
  * @author ZSX
  *
  */
 
@WebServlet (urlPatterns = "/druid/*" ,
     initParams = {
//          @WebInitParam(name = "allow", value = "192.168.16.110,127.0.0.1"), // IP白名單 (沒有配置或者爲空,則容許全部訪問)
//          @WebInitParam(name="deny",value="192.168.16.111"), // IP黑名單 (存在共同時,deny優先於allow)
             @WebInitParam (name= "loginUsername" ,value= "druid" ), // 用戶名
             @WebInitParam (name= "loginPassword" ,value= "druid" ), // 密碼
             @WebInitParam (name= "resetEnable" ,value= "false" ) // 禁用HTML頁面上的「Reset All」功能
     }
)
public class DruidStatViewServlet extends StatViewServlet {
 
}

這樣啓動項目後在瀏覽器中輸入地址:端口/druid,就能夠看到druid的監控web頁面了數據庫

7、 攔截器配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 
@Configuration
public class WebAppConfigurer extends WebMvcConfigurerAdapter {
 
     /**
      * 配置攔截器
      */
     @Override
     public void addInterceptors(InterceptorRegistry registry) {
         // TODO Auto-generated method stub
         // 多個攔截器組成一個攔截器鏈
         // addPathPatterns 用於添加攔截規則
         // excludePathPatterns 用戶排除攔截
 
         registry.addInterceptor( new AuthInterceptor()).addPathPatterns( "/**" );
 
 
         super .addInterceptors(registry);
     }
 
     /**
      * 添加自定義的靜態資源映射
       這裏使用代碼的方式自定義目錄映射,並不影響Spring Boot的默認映射,能夠同時使用。
      */
     @Override
     public void addResourceHandlers(ResourceHandlerRegistry registry) {
 
//      registry.addResourceHandler("/new/**").addResourceLocations("classpath:/new/");
//      registry.addResourceHandler("/**").addResourceLocations("/");
         super .addResourceHandlers(registry);
     }
 
}

8、swagger發佈api測試配置(可忽略)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@Configuration
@EnableSwagger2
public class Swagger2 {
 
     @Bean
     public Docket createRestApi(){
         return new Docket(DocumentationType.SWAGGER_2)
                 .apiInfo(apiInfo())
                 .select()
                 .apis(RequestHandlerSelectors.basePackage( "com.zsx.controller.api" ))
                 .paths(PathSelectors.any())
                 .build();
     }
 
     private ApiInfo apiInfo(){
         return new ApiInfoBuilder()
                 .title( "Spring Boot中使用Swagger2構建RESTful APIs" )
                 .description( "描述" )
                 .termsOfServiceUrl( "http://zsx.com.cn" )
                 .version( "1.0" )
                 .build();
     }
 
}

至此,全部的配置已完成,下面是一個操做數據的簡單demoapache

9、實體類

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Entity
@Table (name = "t_user" )
public class Tuser implements java.io.Serializable {
 
     /**
      *
      */
     private static final long serialVersionUID = 1L;
 
     @Id
     @GeneratedValue (strategy=GenerationType.IDENTITY)
     private Long id;
 
     @Column (name = "username" )
     private String userName;
 
     @Column (name = "password" )
     private String passWord;
 
     @Column (name = "email" )
     private String email;
 
     @Column (name = "mobile" )
     private String mobile;
 
     @Column (name = "nickname" )
     private String nickName;
 
     // 省略getter 和 setter
 
}

10、dao層

一、使用jpa基本能夠實現不寫sql,(但實際開發中,業務邏輯會很複雜,一點不寫sql徹底不現實)json

二、注意添加@Repository註解, 添加JpaSpecificationExecutor繼承能夠方便分頁

三、 看些jpa的查詢語法資料

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.List;
import java.util.Map;
 
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
 
@Repository
public interface TuserDao extends PagingAndSortingRepository<tuser, long = "" >, JpaSpecificationExecutor<tuser> {
 
     Tuser findByUserName(String userName);
 
     @Query ( "from Tuser t where id = :id" )
     List<tuser> queryFamilyList( @Param ( "id" ) Long id, Pageable pageable);
 
}

11、service和controller沒啥好說的,跟原先的同樣,下面再提供一個單元測試的demo

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.List;
import javax.persistence.EntityManager;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
 
import com.alibaba.fastjson.JSON;
import com.golden.Application;
import com.golden.dao.TUserDao;
import com.golden.entity.Tuser;
import com.golden.util.GeneratePageable;
 
@RunWith (SpringJUnit4ClassRunner. class )
 
//指定咱們SpringBoot工程的Application啓動類
@SpringApplicationConfiguration (classes = Application. class )
 
//因爲是Web項目,Junit須要模擬ServletContext,所以咱們須要給咱們的測試類加上@WebAppConfiguration
@WebAppConfiguration
public class UtilTest {
 
     @Autowired
     private TUserDao dao;
 
     @Autowired
     private EntityManager em;
 
     @Test
     public void test1(){
         dao.findByUserName( "admin" );
     }
 
     @Test
     public void test2(){
         // 使用jpa提供的分頁類
         java.util.List<order> list = new ArrayList<sort.order>();
         Order order = new Order(Direction.DESC, "createTime" );
 
         list.add(order);
 
         Sort sort = new Sort(list);
 
         Pageable pageable = new PageRequest( 0 , 10 , sort);
 
         Page<tuser> findAll = dao.findAll(pageable);
 
     }
 
     @Test
     public void test3(){
 
         EntityManager em = dao.getEntityManager();
         Query query = em.createNativeQuery( "select * from t_user limit 1" );
         Object singleResult = query.getSingleResult();
         System.out.println(singleResult);
     }
 
     /*
//執行原生SQL
Query nativeQuery = em.createNativeQuery(String sql);
//指定返回對象類型
nativeQuery.unwrap(SQLQuery.class).setResultTransformer(Transformers.aliasToBean( Class resultType));
//返回對象
List<t> resultList = nativeQuery.getResultList(); 
      */
}

後記:

不用Druid的能夠把有關Druid的配置所有刪掉,swagger的同理

這裏沒有使用hibernate.cfg.xml配置文件,主要習慣了在實體類裏配置字段了,不怎麼用hibernate的映xml文件了,但其實配置起來跟springmvc項目同樣

說實話這裏使用jpa操做數據庫,沒感受有多方便,由於總有各類奇葩的需求,固然也多是我沒深刻研究,因此建議改用Mybatis,這個我會再寫一篇springboot加mybatis的配置教程的,最後,還可使用原生的sql查詢,即便用單元測試裏的EntityManager對象去執行sql,返回結果能夠指定對象類型,也很方便

還須要注意的一個點是靜態文件的存放位置,這個跟原先的項目不同,原先是在webapp下,但springboot是默認放在resources下的static目錄下的,還有其餘默認目錄和配置.

相關文章
相關標籤/搜索