本文介紹了Maven工程搭建spring boot+spring mvc+JPA的示例,分享給大家,具體如下:
添加Spring boot支持,引入相關包:
1、maven工程,少不了pom.xml,spring boot的引入可參考官網:
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
|
< parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >1.5.9.RELEASE</ version > </ parent > < dependencies > < dependency > < groupId >javax.servlet</ groupId > < artifactId >javax.servlet-api</ artifactId > < scope >provided</ scope > <!-- 編譯需要而發布不需要的jar包 --> </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-webmvc</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > <!--jpa的jar包 ,操作數據庫的--> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-data-jpa</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-data-redis</ artifactId > </ dependency > <!--mysql驅動--> < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > </ dependency > < dependency > < groupId >org.apache.shiro</ groupId > < artifactId >shiro-core</ artifactId > < version >1.2.2</ version > </ dependency > < dependency > < groupId >org.apache.shiro</ groupId > < artifactId >shiro-spring</ artifactId > < version >1.2.2</ version > </ dependency > <!-- shiro ehcache --> < dependency > < groupId >org.apache.shiro</ groupId > < artifactId >shiro-ehcache</ artifactId > < version >1.2.2</ version > </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > < executions > < execution > < goals > < goal >repackage</ goal > </ goals > </ execution > </ executions > </ plugin > </ plugins > < finalName >name</ finalName > </ build > |
2、以上代碼引入了spring boot。spring mvc 和jpa,以及mysql數據庫的驅動jar;
編寫啟動類,并加裝配置文件:
1、啟動類如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import java.io.IOException; import com.my.config.CommonProperties; @SpringBootApplication @EnableAutoConfiguration @EnableJpaAuditing public class Application { public static void main(String[] args) throws IOException{ String loc = CommonProperties.loadProperties2System(System.getProperty( "spring.config.location" )); System.getProperties().setProperty( "application.version" , CommonProperties.getVersion(Application. class )); System.getProperties().setProperty( "app.home" , loc + "/.." ); SpringApplication.run(Application. class , args); } } |
2、配置文件的位置放到classpath外邊,方便在不重新打包的情況下修改,spring boot工程一般都打成jar包:
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.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import org.springframework.util.StringUtils; public final class CommonProperties { public static final String PPT_KEY_APP_HOME = "app.home" ; public static final String DEFAULT_APP_HOME = "./" ; public static final String getAppHome() { return System.getProperty( "./" , "./" ); } public static String loadProperties2System(String location) throws IOException { String configLocation = location; File cnf; if (!StringUtils.hasLength(location)) { configLocation = "./config" ; cnf = new File(configLocation); if (!cnf.exists() || !cnf.isDirectory()) { configLocation = "../config" ; cnf = new File(configLocation); } } else { cnf = new File(location); } File[] arg2 = cnf.listFiles(); int arg3 = arg2.length; for ( int arg4 = 0 ; arg4 < arg3; ++arg4) { File file = arg2[arg4]; if (file.isFile() && file.getName().endsWith( ".properties" )) { Properties ppt = new Properties(); FileInputStream fi = new FileInputStream(file); Throwable arg8 = null ; try { ppt.load(fi); System.getProperties().putAll(ppt); } catch (Throwable arg17) { arg8 = arg17; throw arg17; } finally { if (fi != null ) { if (arg8 != null ) { try { fi.close(); } catch (Throwable arg16) { arg8.addSuppressed(arg16); } } else { fi.close(); } } } } } return configLocation; } public static String getVersion(Class<?> clazz) { Package pkg = clazz.getPackage(); String ver = pkg != null ? pkg.getImplementationVersion() : "undefined" ; return ver == null ? "undefined" : ver; } |
將配置文件放到jar包同級目錄的config文件夾下,包括日志配置,application.yml文件,其他配置文件等;
編寫自動配置類
用于掃描compan* ,代替spring mvc的spring.xml配置文件:
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
|
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan (basePackages = { "com.my.rs" , "com.my.service" , "com.my.repository" }) public class AppAutoConfiguration { } import org.springframework.boot.autoconfigure.web.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * 預配置 * */ @Configuration public class MyConfiguration extends WebMvcConfigurerAdapter{ @Bean public HttpMessageConverters customConverters() { return new HttpMessageConverters(); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //registry.addResourceHandler("/**") // .addResourceLocations("classpath:/META-INF/resources/**"); } |
編寫rs,service,repository
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
|
package com.my.rs; import java.util.List; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.my.entity.User; @RequestMapping ({ "/api/user" }) public interface UserRS { @RequestMapping (value= "/add" ,method={RequestMethod.POST}) @ResponseBody public User saveUser( @RequestBody User user); @RequestMapping (value= "/update" ,method={RequestMethod.POST}) @ResponseBody public User updateUser( @RequestBody User user); @RequestMapping (value= "/delete" ,method={RequestMethod.POST,RequestMethod.DELETE}) public void deleteUser( @RequestParam String[] userIds); @RequestMapping (value= "/get" ,method={RequestMethod.GET}) @ResponseBody public User getUser( @RequestParam String userId); @RequestMapping (value= "/query/all" ,method={RequestMethod.GET}) public List<User> queryAll(); @RequestMapping (value= "/query/byName" ,method={RequestMethod.GET}) public List<User> queryByName( @RequestParam String name); @RequestMapping (value= "/query/byParentId" ,method={RequestMethod.GET}) public List<User> queryChildren( @RequestParam String parentId); //無參數分頁查詢 @RequestMapping (value= "/query/page" ,method={RequestMethod.GET}) public List<User> queryByPage( @RequestParam int pageNo, @RequestParam int pageSize, @RequestBody (required= false ) User user); } |
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
|
package com.my.rs.impl; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import com.my.entity.User; import com.my.rs.UserRS; import com.my.service.UserService; @RestController public class UserRSImpl implements UserRS{ public static Logger logger = LoggerFactory.getLogger(UserRSImpl. class ); @Autowired UserService _userService; @Override public User saveUser( @RequestBody User user){ try { return _userService.save(user); } catch (Throwable e) { logger.error(e.getMessage(),e); throw e; } } @Override public User updateUser( @RequestBody User user) { return _userService.update(user); } @Override public void deleteUser(String[] userIds) { for (String userId : userIds) { _userService.deleteById(userId); } } @Override public List<User> queryAll() { return _userService.queryAll(); } @Override public List<User> queryByName(String name) { return _userService.findByName(name); } @Override public List<User> queryChildren(String parentId) { return _userService.findByParentId(parentId); } @Override public User getUser(String userId) { return _userService.findById(userId); } @Override public List<User> queryByPage( int pageNo, int pageSize, User user) { return null ; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.my.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.my.entity.User; import com.my.repository.UserRepository; @Service public class UserService extends BaseService<User>{ @Autowired UserRepository _userRepository; public List<User> findByName(String name){ return _userRepository.findByName(name); } public List<User> findByParentId(String parentId){ return _userRepository.findByParentId(parentId); } } |
1
2
3
4
5
6
7
|
package com.my.repository; import java.util.List; import com.my.entity.User; public interface UserRepository extends BaseRepository<User>{ List<User> findByName(String name); List<User> findByParentId(String parentId); } |
以上采用了分層模式,有點繁瑣,但是對之后修改每層的業務邏輯比較方便
JPA相關的類如下:
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
|
package com.my.service; import java.io.Serializable; import javax.persistence.EntityManager; import javax.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired; import com.my.repository.BaseRepository; /** * 一些共有的方法放這里 * */ @Transactional public class BaseService<E extends Serializable> { @Autowired BaseRepository<E> _baseRepository; @Autowired EntityManager em; public E save(E baseUnit){ return _baseRepository.saveAndFlush(baseUnit); } public E update(E baseUnit){ return _baseRepository.saveAndFlush(baseUnit); } public void deleteById(String id) { _baseRepository.delete(id); } public java.util.List<E> queryAll(){ return _baseRepository.findAll(); } public E findById(String id){ return _baseRepository.getOne(id); } } |
1
2
3
4
5
6
7
|
package com.my.repository; import java.io.Serializable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.NoRepositoryBean; @NoRepositoryBean public interface BaseRepository<E> extends JpaRepository<E, Serializable>{ } |
實體類:與數據庫字段相關,需要注意下父類中的注解@MappedSuperclass
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
|
package com.my.entity; import java.util.ArrayList; import java.util.List; import javax.persistence.Entity; import javax.persistence.ManyToMany; import org.hibernate.annotations.DynamicInsert; import org.hibernate.annotations.DynamicUpdate; import org.hibernate.validator.constraints.Email; @Entity (name = "db_user" ) @DynamicInsert @DynamicUpdate public class User extends BaseUnit { /** * 賬戶狀態 */ public static enum AccountStatus { /** * 正常 */ Enable, // /** * 停用 */ Disable } private static final long serialVersionUID = -3101319619397064425L; private String password; private String salt; /** 賬戶狀態 */ private AccountStatus status; /** 認證郵箱 */ @Email (message = "User.email屬性必須符合郵箱格式" ) private String email; /** 移動電話號碼 */ private String mobileNo; /** 身份證號碼 */ private String cardId; @ManyToMany (targetEntity=Role. class ) private List<String> roleIds; /** 昵稱。可選。 */ private String nickName; public String getCardId() { return cardId; } public String getEmail() { return email; } public String getMobileNo() { return mobileNo; } public String getNickName() { return nickName; } public String getPassword() { return password; } public List<String> getRoleIds() { if (roleIds == null ) { roleIds = new ArrayList<>(); } return roleIds; } public String getSalt() { return salt; } public AccountStatus getStatus() { return status; } public void setCardId(String cardId) { this .cardId = cardId; } public void setEmail(String email) { this .email = email; } public void setMobileNo(String mobileNo) { this .mobileNo = mobileNo; } public void setNickName(String nickName) { this .nickName = nickName; } public void setPassword(String password) { this .password = password; } public void setRoleIds(List<String> roleIds) { this .roleIds = roleIds; } public void setSalt(String salt) { this .salt = salt; } public void setStatus(AccountStatus status) { this .status = status; } } |
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
|
package com.my.entity; import java.io.Serializable; import java.util.Date; import javax.persistence.Id; import javax.persistence.MappedSuperclass; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import org.springframework.data.annotation.CreatedBy; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedBy; import org.springframework.data.annotation.LastModifiedDate; @MappedSuperclass public class BaseUnit implements Serializable { @Id @NotNull public String id; /** * 父單元ID */ @Size (max = 32 , message = "BaseUnit.parentId屬性長度不能大于32" ) public String parentId; /** 父單元的類型 */ public ParentType parentType; /** * 單元的名稱 */ @NotNull (message = "BaseUnit.name屬性不能為空" ) public String name; @CreatedBy public String createBy; @CreatedDate public Date createDate; @LastModifiedBy public String lastModifiedBy; /** * 最后更新日期 */ @LastModifiedDate public Date lastModifiedDate; public String getId() { return id; } public void setId(String id) { this .id = id; } /** * 獲取單元的名稱 * * @return 必填 */ public String getName() { return name; } /** * * * @return UUID,不含{}和- */ public String getParentId() { return parentId; } public ParentType getParentType() { return parentType; } public String getStationId() { return stationId; } public String getThumbnailId() { return thumbnailId; } public String getCreateBy() { return createBy; } public void setCreateBy(String createBy) { this .createBy = createBy; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this .createDate = createDate; } /** * 設置單元的名稱 * * @param name * 必填 */ public void setName(String name) { this .name = name; } /** * 設置父單元ID * * @param parentId * UUID,不含{}和- */ public void setParentId(String parentId) { this .parentId = parentId; } public String getLastModifiedBy() { return lastModifiedBy; } public void setLastModifiedBy(String lastModifiedBy) { this .lastModifiedBy = lastModifiedBy; } public Date getLastModifiedDate() { return lastModifiedDate; } public void setLastModifiedDate(Date lastModifiedDate) { this .lastModifiedDate = lastModifiedDate; } } |
配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
server: port: 16800 contextPath: / logging: config: ./config/logback.xml spring: http: multipart: enabled: false datasource: url : jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&characterEncoding=utf-8 username : root password : 123456 driverClassName : com.mysql.jdbc.Driver jpa: database : MYSQL show-sql : true hibernate: ddl-auto : update jackson: serialization: INDENT_OUTPUT : true |
1
2
3
4
5
6
|
#hibernate:配置了實體類維護數據庫表結構的具體行為,update表示當實體類的屬性發生變化時,表結構跟著更新, 這里我們也可以取值create,這個create表示啟動的時候刪除上一次生成的表,并根據實體類重新生成表, 這個時候之前表中的數據就會被清空;還可以取值create-drop,這個表示啟動時根據實體類生成表,但是當sessionFactory關閉的時候表會被刪除; validate表示啟動時驗證實體類和數據表是否一致;none表示啥都不做。 #show-sql表示hibernate在操作的時候在控制臺打印真實的sql語句 #jackson表示格式化輸出的json字符串 |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/liangblog/p/8323606.html