Struts2+Spring+MyBatis+Maven的Web整合实例(附DB数据)
发布日期:2021-06-29 19:32:38 浏览次数:2 分类:技术文章

本文共 16368 字,大约阅读时间需要 54 分钟。

以前做过一个 关于Spring MVC + Spring + MyBatis(简称 SSM)的一个CRUD的完整Web 演示例子。周末利用空闲时间做了个 关于Struts2+Spring+MyBatis+Maven 的 web版CRUD的整合完整版演示例子。在功能上和上次做的 Spring MVC + Spring + MyBatis 实例相似,基本的,记得上次的那个实例好像没有分页,所以我这次在做这个Demo的时候,已经把分页做上了。如果你也是刚好学习这几个框架的新手,或许我做的这个例子对你刚好有所帮助哦!这次的这个实例个人感觉比较粗糙,所以我会在后期做些微动的修改,比如  优化代码注释,优化代码(删除一些冗余的代码片段;在MyBatis的映射文件中有部分方法是我以前做的实验例子;)等。

下面我就将代码的运行效果图贴出来:

Eclipse_file

演示工程的目录结构

index

项目的首页

showList

查询出的数据列表

add

新增数据的界面

edit

编辑数据的界面

pageUp

分页中的上一页

pageDown

分页中的下一页

 好了,以上一项目运行时截图。

这个整合项目使用Eclilpse中开发,使用框架 Struts2、Spring、MyBatis、Maven框架,数据库使用的是MySQL。主要功能:增、删、改、查。采用的数据库连接池是来自阿里巴巴的Druid。下面贴出项目 中的部分代码以供大家参考。

UserActioin.java

 

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

package com.bkybk.action;

 

import java.util.List;

 

import org.springframework.beans.factory.annotation.Autowired;

 

import com.bkybk.model.JsonModel;

import com.bkybk.model.Ucategory;

import com.bkybk.model.User;

import com.bkybk.service.CategoryServiceI;

import com.bkybk.service.UserServiceI;

 

public class UserAction extends BaseAction {

private static final long serialVersionUID = -3675623781845348379L;

private UserServiceI userService;

private CategoryServiceI categoryService;

private List<User> userList;

private User user;

private List<Ucategory> categoryList;

public String execute(){

return SUCCESS;

}

public String getUserAll(){

JsonModel j = new JsonModel();

try {

user = new User();

getParams(user);

user.setSort("user.id");

user.setOrder("desc");

userList = userService.getUserList(user);

user.setTotal(userService.getUserSize(user));

int totalPageNum = user.getTotal()/user.getRows();

if(user.getTotal() % user.getRows() > 0){

totalPageNum++;

}

user.setTotalPage(totalPageNum);

j.setSuccess(true);

j.setMsg("OK");

j.setObj(userList);

} catch (Exception e) {

System.out.println(e);

j.setMsg(e.getMessage());

}

//super.writeJson(j);

return "userList";

}

public String addUser(){

categoryList = categoryService.getAll();

return "add";

}

public String editUser(){

categoryList = categoryService.getAll();

user = new User();

getParams(user);

user = userService.getUserById(user.getId());

return "add";

}

public String saveOrUpdate(){

user = new User();

getParams(user);

if(null == user.getId()){

userService.save(user);

}else{

userService.updateUser(user);

}

return "goList";

}

public String delUser(){

user = new User();

getParams(user);

if(null !=user.getId()){

userService.delUserById(user.getId());

}

return "goList";

}

 

public UserServiceI getUserService() {

return userService;

}

 

@Autowired

public void setUserService(UserServiceI userService) {

this.userService = userService;

}

public List<User> getUserList() {

return userList;

}

 

public void setUserList(List<User> userList) {

this.userList = userList;

}

 

public User getUser() {

return user;

}

 

public void setUser(User user) {

this.user = user;

}

 

public CategoryServiceI getCategoryService() {

return categoryService;

}

 

public void setCategoryService(CategoryServiceI categoryService) {

this.categoryService = categoryService;

}

 

public List<Ucategory> getCategoryList() {

return categoryList;

}

 

public void setCategoryList(List<Ucategory> categoryList) {

this.categoryList = categoryList;

}

 

}

UserMapper.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

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

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

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

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.bkybk.dao.UserMapper" >

  <resultMap id="BaseResultMap" type="com.bkybk.model.User" >

    <id column="id" property="id" jdbcType="INTEGER" />

    <result column="Login_name" property="loginName" jdbcType="VARCHAR" />

    <result column="password" property="password" jdbcType="VARCHAR" />

    <result column="name" property="name" jdbcType="VARCHAR" />

    <result column="Email" property="email" jdbcType="VARCHAR" />

    <result column="Phone" property="phone" jdbcType="VARCHAR" />

    <result column="Address" property="address" jdbcType="VARCHAR" />

    <result column="last_Login_Time" property="lastLoginTime" jdbcType="TIMESTAMP" />

    <result column="Regist_Time" property="registTime" jdbcType="TIMESTAMP" />

    <result column="Category_id" property="categoryId" jdbcType="INTEGER" />

  </resultMap>

  <sql id="Base_Column_List" >

    id, Login_name, password, name, Email, Phone, Address, last_Login_Time, Regist_Time,

    Category_id

  </sql>

  <sql id="Example_Where_Clause">

<where>

<if test="id != null">

user.id = #{id,jdbcType=INTEGER}

</if>

<if test="loginName != null">

AND user.Login_name like CONCAT('%',#{loginName,jdbcType=VARCHAR},'%')

</if>

<if test="password != null">

AND user.password = #{password,jdbcType=VARCHAR}

</if>

<if test="name != null">

AND user.name like CONCAT('%',#{name,jdbcType=VARCHAR},'%')

</if>

<if test="email != null">

AND user.Email like CONCAT('%',#{email,jdbcType=VARCHAR},'%')

</if>

<if test="phone != null">

AND user.Phone like CONCAT('%',#{phone,jdbcType=VARCHAR},'%')

</if>

<if test="address != null">

AND user.Address like CONCAT('%',#{address,jdbcType=VARCHAR},'%')

</if>

<if test="lastLoginTime != null">

AND user.last_Login_Time = #{lastLoginTime,jdbcType=TIMESTAMP}

</if>

<if test="registTime != null">

AND user.Regist_Time = #{registTime,jdbcType=TIMESTAMP}

</if>

<if test="categoryId != null">

AND user.Category_id = #{categoryId,jdbcType=INTEGER}

</if>

</where>

</sql>

  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >

    select

    <include refid="Base_Column_List" />

    from user

    where id = #{id,jdbcType=INTEGER}

  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >

    delete from user

    where id = #{id,jdbcType=INTEGER}

  </delete>

  <insert id="insert" parameterType="com.bkybk.model.User" >

    insert into user (id, Login_name, password,

      name, Email, Phone,

      Address, last_Login_Time, Regist_Time,

      Category_id)

    values (#{id,jdbcType=INTEGER}, #{loginName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},

      #{name,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR},

      #{address,jdbcType=VARCHAR}, #{lastLoginTime,jdbcType=TIMESTAMP}, #{registTime,jdbcType=TIMESTAMP},

      #{categoryId,jdbcType=INTEGER})

  </insert>

  <insert id="insertSelective" useGeneratedKeys="true" keyProperty="id" parameterType="com.bkybk.model.User" >

    insert into user

    <trim prefix="(" suffix=")" suffixOverrides="," >

      <if test="id != null" >

        id,

      </if>

      <if test="loginName != null" >

        Login_name,

      </if>

      <if test="password != null" >

        password,

      </if>

      <if test="name != null" >

        name,

      </if>

      <if test="email != null" >

        Email,

      </if>

      <if test="phone != null" >

        Phone,

      </if>

      <if test="address != null" >

        Address,

      </if>

      <if test="lastLoginTime != null" >

        last_Login_Time,

      </if>

      <if test="registTime != null" >

        Regist_Time,

      </if>

      <if test="categoryId != null" >

        Category_id,

      </if>

    </trim>

    <trim prefix="values (" suffix=")" suffixOverrides="," >

      <if test="id != null" >

        #{id,jdbcType=INTEGER},

      </if>

      <if test="loginName != null" >

        #{loginName,jdbcType=VARCHAR},

      </if>

      <if test="password != null" >

        #{password,jdbcType=VARCHAR},

      </if>

      <if test="name != null" >

        #{name,jdbcType=VARCHAR},

      </if>

      <if test="email != null" >

        #{email,jdbcType=VARCHAR},

      </if>

      <if test="phone != null" >

        #{phone,jdbcType=VARCHAR},

      </if>

      <if test="address != null" >

        #{address,jdbcType=VARCHAR},

      </if>

      <if test="lastLoginTime != null" >

        #{lastLoginTime,jdbcType=TIMESTAMP},

      </if>

      <if test="registTime != null" >

        #{registTime,jdbcType=TIMESTAMP},

      </if>

      <if test="categoryId != null" >

        #{categoryId,jdbcType=INTEGER},

      </if>

    </trim>

  </insert>

  <update id="updateByPrimaryKeySelective" parameterType="com.bkybk.model.User" >

    update user

    <set >

      <if test="loginName != null" >

        Login_name = #{loginName,jdbcType=VARCHAR},

      </if>

      <if test="password != null" >

        password = #{password,jdbcType=VARCHAR},

      </if>

      <if test="name != null" >

        name = #{name,jdbcType=VARCHAR},

      </if>

      <if test="email != null" >

        Email = #{email,jdbcType=VARCHAR},

      </if>

      <if test="phone != null" >

        Phone = #{phone,jdbcType=VARCHAR},

      </if>

      <if test="address != null" >

        Address = #{address,jdbcType=VARCHAR},

      </if>

      <if test="lastLoginTime != null" >

        last_Login_Time = #{lastLoginTime,jdbcType=TIMESTAMP},

      </if>

      <if test="registTime != null" >

        Regist_Time = #{registTime,jdbcType=TIMESTAMP},

      </if>

      <if test="categoryId != null" >

        Category_id = #{categoryId,jdbcType=INTEGER},

      </if>

    </set>

    where id = #{id,jdbcType=INTEGER}

  </update>

  <update id="updateByPrimaryKey" parameterType="com.bkybk.model.User" >

    update user

    set Login_name = #{loginName,jdbcType=VARCHAR},

      password = #{password,jdbcType=VARCHAR},

      name = #{name,jdbcType=VARCHAR},

      Email = #{email,jdbcType=VARCHAR},

      Phone = #{phone,jdbcType=VARCHAR},

      Address = #{address,jdbcType=VARCHAR},

      last_Login_Time = #{lastLoginTime,jdbcType=TIMESTAMP},

      Regist_Time = #{registTime,jdbcType=TIMESTAMP},

      Category_id = #{categoryId,jdbcType=INTEGER}

    where id = #{id,jdbcType=INTEGER}

  </update>

  

  <select id="selectUserCategory" parameterType="java.lang.Integer" resultType="com.bkybk.model.UserCategory">

   SELECT id,uid,cid FROM usercategory where usercategory.uid = #{id,jdbcType=INTEGER}

  </select>

  <resultMap type="com.bkybk.model.User" id="userResultMap" extends="BaseResultMap">

   <collection property="userCate" column="id" javaType="list" select="selectUserCategory"/>

  </resultMap>

  <select id="getAll" resultMap="BaseResultMap">

   select id, Login_name, password, name, Email, Phone, Address, last_Login_Time, Regist_Time,

    Category_id from user

  </select>

  <resultMap type="com.bkybk.model.User" id="userResultMap2" extends="BaseResultMap">

   <collection property="userCate" javaType="list" ofType="com.bkybk.model.UserCategory">

   <id property="id" column="usercate_id"/>

   <result property="uid" column="user_id"/>

   <result property="cid" column="cate_id"/>

   </collection>

  </resultMap>

  <select id="getAll2" resultMap="userResultMap2">

SELECT

`user`.id,

`user`.Login_name,

`user`.`password`,

`user`.`name`,

`user`.Email,

`user`.Phone,

`user`.Address,

`user`.last_Login_Time,

`user`.Regist_Time,

`user`.Category_id,

usercategory.id usercate_id,

usercategory.uid user_id,

usercategory.cid cate_id

FROM

`user`

JOIN usercategory ON `user`.id = usercategory.uid

  </select>

  

  <resultMap type="com.bkybk.model.User" id="userResultMap3" extends="BaseResultMap">

   <collection property="userCate" javaType="list" ofType="com.bkybk.model.UserCategory">

   <id property="id" column="usercate_id"/>

   <result property="uid" column="user_id"/>

   <result property="cid" column="cate_id"/>

   <association property="category" javaType="com.bkybk.model.Ucategory">

   <id property="id" column="ucategory_id"/>

   <result property="name" column="ucategory_name"/>

   <result property="description" column="ucategory_description"/>

   </association>

   </collection>

  </resultMap>

  <select id="getAll3" resultMap="userResultMap3">

  SELECT

`user`.id,

`user`.Login_name,

`user`.`password`,

`user`.`name`,

`user`.Email,

`user`.Phone,

`user`.Address,

`user`.last_Login_Time,

`user`.Regist_Time,

`user`.Category_id,

usercategory.id usercate_id,

usercategory.uid user_id,

usercategory.cid cate_id,

ucategory.id ucategory_id,

ucategory.`name` ucategory_name,

ucategory.description ucategory_description

FROM

`user`

JOIN usercategory ON `user`.id = usercategory.uid

JOIN ucategory ON usercategory.cid = ucategory.id

  

  </select>

  

  <resultMap type="com.bkybk.model.User" id="userResultMap4" extends="BaseResultMap">

   <collection property="category" javaType="list" ofType="com.bkybk.model.Ucategory">

   <id property="id" column="ucategory_id"/>

   <result property="name" column="ucategory_name"/>

   <result property="description" column="ucategory_description"/>

   </collection>

  </resultMap>

  <select id="getAll4" resultMap="userResultMap4">

   SELECT

`user`.id,

`user`.Login_name,

`user`.`password`,

`user`.`name`,

`user`.Email,

`user`.Phone,

`user`.Address,

`user`.last_Login_Time,

`user`.Regist_Time,

`user`.Category_id,

usercategory.id ,

usercategory.uid ,

usercategory.cid ,

ucategory.id ucategory_id,

ucategory.`name` ucategory_name,

ucategory.description ucategory_description

FROM

`user`

JOIN usercategory ON `user`.id = usercategory.uid

JOIN ucategory ON usercategory.cid = ucategory.id

  

  </select>

  

  <resultMap type="com.bkybk.model.User" id="userResultMap5" extends="BaseResultMap">

   <id property="categoryId" column="ucategory_id"/>

   <result property="cname" column="ucategory_name"/>

   <result property="cdescription" column="ucategory_description"/>

  </resultMap>

  <select id="getAll5" resultMap="userResultMap5">

   SELECT

`user`.id,

`user`.Login_name,

`user`.`password`,

`user`.`name`,

`user`.Email,

`user`.Phone,

`user`.Address,

`user`.last_Login_Time,

`user`.Regist_Time,

`user`.Category_id,

usercategory.id ,

usercategory.uid ,

usercategory.cid ,

ucategory.id ucategory_id,

ucategory.`name` ucategory_name,

ucategory.description ucategory_description

FROM

`user`

JOIN usercategory ON `user`.id = usercategory.uid

JOIN ucategory ON usercategory.cid = ucategory.id

  

  </select>

  

  

  <resultMap type="com.bkybk.model.User" id="userResultMap6" extends="BaseResultMap">

   <id property="categoryId" column="ucategory_id"/>

   <result property="cname" column="ucategory_name"/>

   <result property="cdescription" column="ucategory_description"/>

  </resultMap>

  <select id="getAll6" resultMap="userResultMap6" parameterType="com.bkybk.model.User">

   SELECT

`user`.id,

`user`.Login_name,

`user`.`password`,

`user`.`name`,

`user`.Email,

`user`.Phone,

`user`.Address,

`user`.last_Login_Time,

`user`.Regist_Time,

`user`.Category_id,

usercategory.id ,

usercategory.uid ,

usercategory.cid ,

ucategory.id ucategory_id,

ucategory.`name` ucategory_name,

ucategory.description ucategory_description

FROM

`user`

JOIN usercategory ON `user`.id = usercategory.uid

JOIN ucategory ON usercategory.cid = ucategory.id

<include refid="Example_Where_Clause" />

<if test="sort != null" >

      ORDER BY ${sort} ${order}

    </if>

<if test="page!=null">

LIMIT #{min},#{max}

</if>

  </select>

  

  

  <select id="selectByUserInfo" resultMap="BaseResultMap" parameterType="com.bkybk.model.User" >

    select

    <include refid="Base_Column_List" />

    from user

    <include refid="Example_Where_Clause" />

  </select>

  

  <select id="getTotal" parameterType="com.bkybk.model.User" resultType="java.lang.Integer">

   select count(id) from user

   <include refid="Example_Where_Clause" />

  </select>

  

</mapper>

 

以上就是几个关键位置的代码,我全部贴出来了(其他代码我就不全部贴出来了,感兴趣的可以下载哟~)。至于配置文件什么的,由于时间原因没有贴出。如果大家要是感兴趣的话,可以下载我的这个演示项目包,里面的东西都齐全着,以Maven项目形式导入到Eclipse里面,然后部署到服务器上面就可以运行。数据库就是里面的那个.sql文件。建个库然后将数据导入就是。哦,对了。导完数据后,记得别忘了到jdbc.properties里面去把数据库的连接信息换成你自己哦!

还有,如果有朋友没有用过Maven 或 不知道怎么配置Maven的,我以前整理过一篇关于Maven的配置和使用(),可以查看此篇博文并参考里面步骤配置Maven。

当然,如果你的Eclipse 默认的Maven 是可以正常使用的话,可以忽略上面的关于 Maven的配置的问题,直接将项目导入即可。

特别要说明的是,导入的项目要是Maven,导入后 最好 先 执行 一次 maven clean 。~

本次演示例子源码完整下载:(百度网盘)

假如百度云分享链接失效,请联系站长,我会补上的。

好了。到这里为止,关于 S2SM框架整合的一个CRUD的完整Web例子到此就结束了。如果你在阅读代码的时候有什么疑惑或者不懂,欢迎和我探讨哦!

后记:由于时间问题,这次的代码有些粗糙,可能有些代码写的不是很好。后续我会抽空优化一次的。同时谢谢大家的支持和谅解。

转载地址:https://daqiang.blog.csdn.net/article/details/102619149 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:SSH框架整合(实现分页查询)
下一篇:Spring+SpringMVC+Hibernate整合(封装CRUD操作)

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年05月02日 11时10分28秒