以前做过一个 关于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

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

<?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的配置和使用(http://www.baikeyang.com/code/985.html),可以查看此篇博文并参考里面步骤配置Maven。

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

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

本次演示例子源码完整下载:http://pan.baidu.com/s/1o6n9yyA(百度网盘)

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

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

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

 

 

 

标签: MyBatis框架整合演示, Struts2+Spring+MyBatis整合, Struts2+Spring+MyBatis+Maven的Web整合实例(附DB数据), Struts2+Spring+MyBatis+Maven项目整合

已有 6 条评论

  1. bianxinhua

    哥们,有没Struts2+Spring+MyBatis+Maven的Web整合的基础教程呀。我想学习下。

    1. 老兄,我硬盘上周末不小心格式化了。什么资料都没有了。我的这个Demo就是整合的呢,你哪里不明白可以问我哟~

  2. 豆豆

    可以吧jar包法下不 或者上传百度云

    1. 朋友,现在可以利用阿里云提供Maven源,速度很快的。

  3. web

    数据库好像有点问题啊

    1. 朋友,你好。数据库是出了什么问题呢?可以将你的问题告诉我么?

添加新评论