java中ssm项目搭建_SSM项目的搭建

本文示例在如下环境下搭建一个Maven+Druid+SSM+Shiro+Mysql+PageHelper以及Mybatis Generator反向生成代码的项目

一.开发环境:

系统:Windows7-旗舰版

Tomcat:8.0.43

二.搭建流程:

一.环境准备

1.1 配置JDK

在window--》Preferences下搜索Java,选择本地对应的JDK安装目录,点击添加完成:

1368ebc174a796748ab4b5a18a997e29.png

配置完开发环境JDK后,需要修改项目默认的编译环境:

7c8e67573749a2202ac80801fe81a385.png

1.2 配置Tomcat

在window--》Preferences下搜索Server,选择自己本地对应的Tomcat版本及JRE环境,点击添加完成:

67c60e15fea457664a432becd453ff6e.png

添加完成之后,配置Tomcat,如图所示,分别对应项目发布路径、自动部署、发布超时时间、访问端口等配置

d06a1249307701a13f8c6bf213dc44f5.png

1.3 配置Maven

在window--》Preferences下搜索Maven,选择本地Maven安装路径:

a901fb916fc3e3fb69510dae062df356.png

添加Maven后配置自定义属性,替换自己的Maven settings配置文件及Maven仓库:

da84cc15dded96340112378ae57e1a48.png

二.创建项目

2.1 使用Eclipse创建Maven项目:

28297f6b3f6f8adaa319109477eceb45.png

2.2 项目创建完成后,根据自己习惯的包名结构创建对应的目录,项目结构如图所示:

8394025a07a86a6398d4db672a0c6351.png

三.配置pom.xml依赖

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4 4.0.0

5 com

6 mySpringMVC

7 war

8 0.0.1-SNAPSHOT

9 mySpringMVC Maven Webapp

10 http://maven.apache.org

11

12

13 UTF-8

14

15 1.8

16 4.1.9.RELEASE

17 1.8.9

18 8.0.13

19 1.0.18

20 3.2.8

21 1.2.3

22 1.3.5

23 1.2.3

24

25

26

27

28

29

30 junit

31 junit

32 3.8.1

33

34 test

35

36

37

38

39 org.springframework

40 spring-core

41 ${spring.version}

42

43

44

45

46 org.springframework

47 spring-web

48 ${spring.version}

49

50

51

52

53 org.springframework

54 spring-webmvc

55 ${spring.version}

56

57

58

59

60 org.springframework

61 spring-jdbc

62 ${spring.version}

63

64

65

66

67 org.springframework

68 spring-context

69 ${spring.version}

70

71

72

73

74 org.springframework

75 spring-context-support

76 ${spring.version}

77

78

79

80

81 org.springframework

82 spring-aop

83 ${spring.version}

84

85

86

87

88 org.aspectj

89 aspectjrt

90 ${aspectj.version}

91

92

93

94 org.aspectj

95 aspectjweaver

96 ${aspectj.version}

97

98

99

100 cglib

101 cglib

102 3.1

103

104

105

106

107 org.springframework

108 spring-test

109 ${spring.version}

110

111

112

113

114 org.springframework

115 spring-oxm

116 ${spring.version}

117

118

119

120

121 org.springframework

122 spring-tx

123 ${spring.version}

124

125

126

127

128 javax.servlet

129 javax.servlet-api

130 3.0.1

131 provided

132

133

134

135 javax.servlet

136 jstl

137 1.1.2

138 provided

139

140

141

142

143 javax.servlet.jsp

144 javax.servlet.jsp-api

145 2.3.1

146 provided

147

148

149

150

151 org.codehaus.jackson

152 jackson-jaxrs

153 1.9.11

154

155

156

157

158 org.apache.commons

159 commons-lang3

160 3.3.2

161

162

163

164 commons-io

165 commons-io

166 2.4

167

168

169

170 org.apache.commons

171 commons-collections4

172 4.0

173

174

175

176 commons-logging

177 commons-logging

178 1.1.3

179

180

181

182 commons-codec

183 commons-codec

184 1.8

185

186

187 commons-beanutils

188 commons-beanutils

189 1.8.3

190

191

192

193 commons-chain

194 commons-chain

195 1.2

196

197

198

199 commons-fileupload

200 commons-fileupload

201 1.3.1

202

203

204

205 org.apache.commons

206 commons-math3

207 3.3

208

209

210

211 org.apache.commons

212 commons-pool2

213 2.2

214

215

216

217 org.apache.commons

218 commons-digester3

219 3.2

220

221

222

223 commons-net

224 commons-net

225 3.3

226

227

228

229 commons-dbutils

230 commons-dbutils

231 1.5

232

233

234

235 org.apache.commons

236 commons-email

237 1.3.3

238

239

240

241 commons-dbcp

242 commons-dbcp

243 1.4

244

245

246

247

248 jstl

249 jstl

250 1.2

251

252

253

254

255 taglibs

256 standard

257 1.1.2

258

259

260

261

262 log4j

263 log4j

264 1.2.16

265

266

267

268 org.slf4j

269 slf4j-api

270 1.7.5

271

272

273

274

275 com.alibaba

276 druid

277 ${druid.version}

278

279

280

281

282 com.alibaba

283 fastjson

284 1.1.41

285

286

287

288

289 com.fasterxml.jackson.core

290 jackson-databind

291 2.5.1

292

293

294

295

296 mysql

297 mysql-connector-java

298 ${mysql-connector}

299

300

301

302

303 org.mybatis

304 mybatis

305 ${mybatis.version}

306

307

308

309

310 org.mybatis

311 mybatis-spring

312 ${mybatis-spring.version}

313

314

315

316

317 org.mybatis.generator

318 mybatis-generator-core

319 ${mybatis-generator.version}

320

321

322

323

324 com.github.pagehelper

325 pagehelper

326 4.0.0

327

328

329

330

331 org.apache.shiro

332 shiro-core

333 ${shiro.version}

334

335

336

337 org.apache.shiro

338 shiro-web

339 ${shiro.version}

340

341

342

343 org.apache.shiro

344 shiro-spring

345 ${shiro.version}

346

347

348

349 org.apache.shiro

350 shiro-ehcache

351 ${shiro.version}

352

353

354

355

356

357

358

359 src/main/resources

360 true

361

362

363

364 mySpringMVC

365

366

367

368

369 org.mybatis.generator

370 mybatis-generator-maven-plugin

371 1.3.2

372

373

374 Generate MyBatis Files

375

376 generate

377

378 generate

379

380 true

381 true

382

383

384

385

386

387

388 mysql

389 mysql-connector-java

390 ${mysql-connector}

391

392

393

394 org.mybatis.generator

395 mybatis-generator-core

396 ${mybatis-generator.version}

397

398

399

400

401 org.mybatis

402 mybatis

403 ${mybatis.version}

404

405

406

407

408

409

410

View Code

四.搭建框架

在如图所示目录下,分别创建

mapping:用于存放实体与数据库映射的Mybatis配置文件

application.properties:项目通用配置文件

generatorConfig.xml:Mybatis反向生成配置文件

log4j.properties:log4j日志记录配置文件

mybatis-config.xml:Mybatis配置文件

spring-mvc.xml:SpringMVC配置文件

spring-mybatis.xml:Spring整合Mybatis配置文件

spring-shiro.xml:Spring整合Shiro配置文件

6ca43917e4d58fb2b20dab5e6f298d5d.png

4.1  项目基本配置:application.properties

#默认访问页面

web.view.default=../index

#最多文件上传大小

web.maxUploadSize=10485760000

#定义jsp页面访问路径前后缀

web.view.prefix=/WEB-INF/views/

web.view.suffix=.jsp

#JDBC配置

jdbc.driver=com.mysql.cj.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useAffectedRows=true&serverTimezone=GMT

jdbc.username=root

jdbc.password=root

jdbc.initialSize=0

jdbc.maxActive=20

jdbc.maxIdle=20

jdbc.minIdle=1

jdbc.maxWait=60000

#阿里druid连接池配置

dbcp.initialSize=15

dbcp.maxActive=5000

dbcp.maxIdle=0

dbcp.maxWait=900000

dbcp.defaultAutoCommit=true

dbcp.removeAbandoned=true

dbcp.removeAbandonedTimeout=30

dbcp.whenExhaustedAction=1

dbcp.validationQuery=select 1

dbcp.testOnBorrow=fasle

dbcp.testOnReturn=false

4.2  日志记录:log4j.properties

#定义LOG输出级别

log4j.rootLogger=INFO,Console,File

#定义日志输出目的地为控制台

log4j.appender.Console=org.apache.log4j.ConsoleAppender

log4j.appender.Console.Target=System.out

#可以灵活地指定日志输出格式,下面一行是指定具体的格式

log4j.appender.Console.layout = org.apache.log4j.PatternLayout

log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

#文件大小到达指定尺寸的时候产生一个新的文件

log4j.appender.File = org.apache.log4j.RollingFileAppender

#指定输出目录

log4j.appender.File.File = logs/springmvcMybist/ssm.log

#定义文件最大大小

log4j.appender.File.MaxFileSize = 10MB

#输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志

log4j.appender.File.Threshold = ALL

log4j.appender.File.layout = org.apache.log4j.PatternLayout

log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

4.3  配置Mybatis框架及PageHelper插件:mybatis-config.xml

1 <?xml version="1.0" encoding="UTF-8"?>

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

4.4  整合MVC:spring-mvc.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"

4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd5 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd6 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

7

8 SpringMVC配置文件

9

10

11

12 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

35

36 class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

37

38

39 text/html;charset=UTF-8

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

63

64

65

66

67

68

69

70

4.5  整合Mybatis:spring-mybatis.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"

4 xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jee="http://www.springframework.org/schema/jee"

5 xmlns:tx="http://www.springframework.org/schema/tx"xmlns:util="http://www.springframework.org/schema/util"

6 xmlns:task="http://www.springframework.org/schema/task"

7 xsi:schemaLocation="8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd10 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd11 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd12 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd13 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd14 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"

15 default-lazy-init="true">

16

17 Spring整合Mybatis配置文件

18

19

20

21 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

22

23

24

25

26

27

28

29

30 destroy-method="close">

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 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

84

85

86

87

88

89

90

4.6  整合Shiro

4.6.1 配置spring-shiro.xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4 xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="5 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

7

8 Spring整合Shiro配置文件

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 /login

121

122 error/unauthorized

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154 /login= anon

155 /login.jsp= anon

156 /createImgCode =anon

157 /error/= anon

158 /logout = logout

159 /auth= anon

160 /static/** = anon

161 ${user.headImg.path}=anon162 /** = user163

164

165

166

View Code

4.6.2 自定义用户验证及授权

①在shiro包下创建CustomRealm类

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.shiro.security;2

3 importjava.util.List;4

5 importorg.apache.shiro.SecurityUtils;6 importorg.apache.shiro.authc.AuthenticationException;7 importorg.apache.shiro.authc.AuthenticationInfo;8 importorg.apache.shiro.authc.AuthenticationToken;9 importorg.apache.shiro.authc.SimpleAuthenticationInfo;10 importorg.apache.shiro.authc.UnknownAccountException;11 importorg.apache.shiro.authc.UsernamePasswordToken;12 importorg.apache.shiro.authz.AuthorizationInfo;13 importorg.apache.shiro.authz.SimpleAuthorizationInfo;14 importorg.apache.shiro.realm.AuthorizingRealm;15 importorg.apache.shiro.subject.PrincipalCollection;16 importorg.apache.shiro.util.ByteSource;17 importorg.springframework.beans.factory.annotation.Autowired;18

19 importcom.entity.Menu;20 importcom.entity.Role;21 importcom.entity.User;22 importcom.service.RoleMenuService;23 importcom.service.UserRoleService;24 importcom.service.UserService;25

26 /**

27 * 自定义Shiro身份认证和授权处理28 *@authorljh29 */

30 public class CustomRealm extendsAuthorizingRealm{31

32 @Autowired33 privateUserService userService;34

35 @Autowired36 privateUserRoleService userRoleService;37

38 @Autowired39 privateRoleMenuService roleMenuService;40

41 /**

42 * 身份验证--登录43 */

44 @Override45 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throwsAuthenticationException {46 UsernamePasswordToken token =(UsernamePasswordToken) authenticationToken;47 String userName =token.getUsername();48

49 User user =userService.getUserByName(userName);50 if (null ==user) {51 throw new UnknownAccountException("此用户不存在");52 }53

54 ByteSource credentialsSalt = ByteSource.Util.bytes(userName);//使用账号作为盐值

55 SimpleAuthenticationInfo info = newSimpleAuthenticationInfo(user, user.getPassword(), credentialsSalt, getName());56 returninfo;57 }58

59 /**

60 * 身份授权--权限61 */

62 @Override63 protectedAuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {64 if (null ==principals) {65 return null;66 }67

68 //因为非正常退出,即没有显式调用 SecurityUtils.getSubject().logout()69 //(可能是关闭浏览器,或超时),但此时缓存依旧存在(principals),所以会自己跑到授权方法里。

70 if (!SecurityUtils.getSubject().isAuthenticated()) {71 doClearCache(principals);72 SecurityUtils.getSubject().logout();73 return null;74 }75

76 //获取用户

77 User user =(User) SecurityUtils.getSubject().getPrincipal();78

79 List roleList=userRoleService.getRoleListByUserId(user.getId());80 List

82 SimpleAuthorizationInfo info = newSimpleAuthorizationInfo();83

84 //加入角色

85 for(Role role : roleList) {86 info.addRole(role.getRole());87 }88

89 //加入权限

90 for(Menu menu : menuList) {91 if (null != menu.getPermission() && !"".equals(menu.getPermission())) {92 for (String permission : menu.getPermission().split(",")) {93 info.addStringPermission(permission);94 }95 }96 }97

98 returninfo;99 }100

101

102

103

104 }

View Code

②在util包下创建ShiroMd5Util类,用于用户密码加密

1 packagecom.util;2

3 importorg.apache.shiro.crypto.hash.SimpleHash;4 importorg.apache.shiro.util.ByteSource;5

6 importcom.entity.User;7

8 /**

9 * MD5密码加密工具10 *@authorljh11 */

12 public classShiroMd5Util {13

14 /**

15 * User的密码加密方法16 *@paramuser17 *@return 加密后的密码

18 */

19 public staticString SysMd5(User user) {20 String hashAlgorithmName = "MD5";//加密方式

21 Object crdentials =user.getPassword();//密码原值

22 ByteSource salt = ByteSource.Util.bytes(user.getName());//以账号作为盐值

23 int hashIterations = 1024;//加密1024次

24 SimpleHash hash = newSimpleHash(hashAlgorithmName,crdentials,salt,hashIterations);25

26 returnhash.toString();27 }28

29 }

4.7  配置反向生成:generatorConfig.xml

/p>

PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"

"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

配置完成反向生成文件后,配置如下图所示Run Configurations,右键Run as Configurations 即可在对应的包下面反向生成代码:

af2ffd316eda81a963ca6a9caf0f3db9.png

默认生成的Dao层格式为:XXXMapper,这里根据个人习惯可另外自行修改名称:

76cc0c1588d6df31c2816b7babcbd4d3.png

4.8 配置web.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns="http://java.sun.com/xml/ns/javaee"

4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

5 version="3.0">

6 Archetype Created Web Application

7

8

9

10 contextConfigLocation

11

12 classpath:spring-mybatis.xml13 classpath:spring-shiro.xml14

15

16

17

18

19 org.springframework.web.context.ContextLoaderListener

20

21

22

23

24 org.springframework.web.util.IntrospectorCleanupListener

25

26

27

28

29 encodingFilter

30 org.springframework.web.filter.CharacterEncodingFilter

31 true

32

33 encoding

34 UTF-8

35

36

37

38 encodingFilter

39 /*

40

41

42

43

44 shiroFilter

45 org.springframework.web.filter.DelegatingFilterProxy

46

47

48 targetFilterLifecycle

49 true

50

51

52

53 shiroFilter

54 /*

55

56

57

58

59

60 SpringMVC

61 org.springframework.web.servlet.DispatcherServlet

62

63 contextConfigLocation

64 classpath:spring-mvc.xml

65

66 1

67 true

68

69

70

71 SpringMVC

72

73 /

74

75

76

77

78 default

79 *.css

80 *.scss

81 *.map

82 *.js

83 *.svg

84 *.jpg

85 *.jpeg

86 *.bmp

87 *.png

88 *.gif

89 *.ico

90 *.woff

91 *.woff2

92

93

94

95

96 DruidWebStatFilter

97 com.alibaba.druid.support.http.WebStatFilter

98

99 exclusions

100 *.js,*.gif,*.jpg,*.png,*.css,*.scss,*.ico,/druid/*

101

102

103

104 DruidWebStatFilter

105 /*

106

107

108

109 DruidStatView

110 com.alibaba.druid.support.http.StatViewServlet

111

112

113 DruidStatView

114 /druid/*

115

116

117

118

119

120

121

122 SSL

123 /*

124

125

126 CONFIDENTIAL

127

128

129

130

131 404

132 /WEB-INF/views/error/404.jsp

133

134

135

136 500

137 /WEB-INF/views/error/error.jsp

138

139

五.测试准备

5.1 Web访问测试

在Controller包下新建TestController,此Controller只有一个helloWorld方法用于向页面返回文字:

1 packagecom.controller;2

3 importorg.springframework.stereotype.Controller;4 importorg.springframework.web.bind.annotation.RequestMapping;5 importorg.springframework.web.bind.annotation.ResponseBody;6

7 /**

8 * 测试Controller9 *@authorljh10 */

11 @Controller12 public classTestController {13

14 /**

15 * 测试访问16 *@return

17 */

18 @RequestMapping("/helloWorld")19 @ResponseBody20 publicString helloWorld(){21 return "HelloWorld";22 }23 }

5.2  持久层数据库连接测试

5.2.1 创建Service

在service包下创建UserService接口,与UserServiceImpl接口实现:

1 packagecom.service;2

3 importjava.util.List;4

5 importcom.entity.User;6 importcom.github.pagehelper.PageInfo;7

8 /**

9 * User service层接口10 *@authorljh11 */

12 public interfaceUserService {13 intdeleteByID(Integer id);14

15 intinsert(User user);16

17 User selectByID(Integer id);18

19 intupdate(User record);20

21 ListselectUserList(User user);22

23 /**

24 * 分页查询用户列表25 *@paramuser 过滤条件26 *@parampageNo 页码27 *@parampageSize 页宽28 *@return

29 */

30 PageInfo selectUserByPage(User user,int pageNo,intpageSize);31 }

1 packagecom.service.impl;2

3 importjava.util.List;4

5 importorg.springframework.beans.factory.annotation.Autowired;6 importorg.springframework.stereotype.Service;7

8 importcom.dao.UserDao;9 importcom.entity.User;10 importcom.github.pagehelper.PageHelper;11 importcom.github.pagehelper.PageInfo;12 importcom.service.UserService;13

14 /**

15 * User service层接口实现16 *@authorljh17 */

18 @Service19 public class UserServiceImpl implementsUserService{20 @Autowired21 privateUserDao userDao;22

23 @Override24 public intdeleteByID(Integer id) {25 returnuserDao.deleteByID(id);26 }27

28 @Override29 public intinsert(User record) {30 returnuserDao.insert(record);31 }32

33 @Override34 publicUser selectByID(Integer id) {35 returnuserDao.selectByID(id);36 }37

38 @Override39 public intupdate(User record) {40 returnuserDao.update(record);41 }42

43 @Override44 public ListselectUserList(User user) {45 returnuserDao.selectUserList(user);46 }47

48 /**

49 * 分页查询用户列表50 *@paramuser 过滤条件51 *@parampageNo 页码52 *@parampageSize 页宽53 *@return

54 */

55 @Override56 public PageInfo selectUserByPage(User user, int pageNo, intpageSize) {57 PageHelper.startPage(pageNo, pageSize);58 List list =userDao.selectUserList(user);59 //用PageInfo对结果进行包装

60 PageInfo pageInfo = new PageInfo(list);61 returnpageInfo;62 }63

64 }

5.2.2 创建Junit单元测试

在test包下创建TestUserService,并添加Junit测试相关注解:

1 packagetest;2

3 importorg.junit.Test;4 importorg.junit.runner.RunWith;5 importorg.springframework.beans.factory.annotation.Autowired;6 importorg.springframework.test.context.ContextConfiguration;7 importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;8 importcom.entity.User;9 importcom.github.pagehelper.PageInfo;10 importcom.service.UserService;11

12 @RunWith(SpringJUnit4ClassRunner.class)13 @ContextConfiguration(locations = {"classpath:spring-mybatis.xml","classpath:spring-mvc.xml"})14 public classTestUserService {15

16 @Autowired17 privateUserService userService;18

19 @Test20 public voidtest(){21 PageInfo userPageInfo = userService.selectUserByPage(new User(), 1, 10);22 for(User user : userPageInfo.getList()) {23 System.out.println(user.getName());24 }25 }26 }

六.运行与测试

6.1 将项目添加进Tomcat应用服务器,并启动Tomcat:

f2e89e11829bc9209e70bd16b976ff30.png

76de03acd61c6e156d5f14904fabae85.png

6.2  测试Web访问

b155d60c52e7adcffc66911529fdc0df.png

6.3  测试持久层数据库连接

在TestUserService类中右键运行Junit测试方法:

5bcb62051890216645385f3feb4f926a.png

7. 总结

配置maven项目依赖

配置 web.xml,配置Spring MVC核心控制器DispatcherServlet,Shiro监控,编码过滤器,静态文件过滤,监听器,加载 spring-mvc、spring-myabtis与其他Spring集成框架

在spring-mvc.xml文件中配置视图解析器,文件上传,AOP,拦截器等

在spring-myabtis.xml文件中配置数据库连接、 映射Dao层、事务管理等

在spring-shiro.xml文件中配置自定义身份认证、权限设置、会话管理等

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值