Spring + SpringMVC + Mybatis 开发

Spring

  根据个人理解Spring 负责帮你实例化对象,帮你管理这些对象。包括配置文件等等

Spring MVC 

  帮助你把三层分开,目前看满足了 Struts的功能。

MyBatis

  负责持久化对象,和数据库交互。


基本结构包括

  数据库 + Model层 + DAO层(java+xml) + Service(接口)+ Service实现 + Controller层(类Servlet)​

1、Model层与数据库中每个表的字段对应

2、DAO也是接口,对应数据库操作语句SQL​。具体SQL语句在xml文件中配置

3、​Service层:调用DAO层

4、Controller调用Service层



一、Model层要和数据库中字段对应。具体一对一、一对多等关系,还没有研究

package viplanModel;

public class groups {
    private Long groupid;

    private String name;

    private Integer internal;

    private Integer flags;

    public Long getGroupid() {
        return groupid;
    }

    public void setGroupid(Long groupid) {
        this.groupid = groupid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public Integer getInternal() {
        return internal;
    }

    public void setInternal(Integer internal) {
        this.internal = internal;
    }

    public Integer getFlags() {
        return flags;
    }

    public void setFlags(Integer flags) {
        this.flags = flags;
    }
}


做model对应的DAO层和配置对应的Mapper.xml文件

  DAO仅仅是接口,此处暂时理解为Mapper.xml配置的方法为DAO的实现。

DAO代码   (要点:DAO和配置文件的文件名要相同,里头的方法也要相同)

<pre style="font-family: 宋体; font-size: 9pt; background-color: rgb(255, 255, 255);"><pre name="code" class="java"><pre style="font-family: 宋体; font-size: 9pt; background-color: rgb(255, 255, 255);"><pre name="code" class="java">package viplanDAO;

import viplanModel.groups;

import java.util.List;

public interface groupsMapper {
    int deleteByPrimaryKey(Long groupid);

    int insert(groups record);

    int insertSelective(groups record);

    groups selectByPrimaryKey(Long groupid);

    int updateByPrimaryKeySelective(groups record);

    int updateByPrimaryKey(groups record);

    List<groups> selectByID(Long id);

    List<groups> selectByNameKey(String key);
}

 相对应的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="viplanDAO.groupsMapper" >
  <resultMap id="BaseResultMap" type="viplanModel.groups" >
    <id column="groupid" property="groupid" jdbcType="BIGINT" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="internal" property="internal" jdbcType="INTEGER" />
    <result column="flags" property="flags" jdbcType="INTEGER" />
  </resultMap>

  <select id="selectByID" resultMap="BaseResultMap"  parameterType="java.lang.Long">
    select * from groups where groupid=#{groupid,jdbcType=BIGINT}
  </select>

  <select id="selectByNameKey" resultMap="BaseResultMap"  parameterType="java.lang.String">
    select * from groups where name=#{name,jdbcType=VARCHAR}
  </select>

  <sql id="Base_Column_List" >
    groupid, name, internal, flags
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select 
    <include refid="Base_Column_List" />
    from groups
    where groupid = #{groupid,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
    delete from groups
    where groupid = #{groupid,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="viplanModel.groups" >
    insert into groups (groupid, name, internal, 
      flags)
    values (#{groupid,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{internal,jdbcType=INTEGER}, 
      #{flags,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="viplanModel.groups" >
    insert into groups
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="groupid != null" >
        groupid,
      </if>
      <if test="name != null" >
        name,
      </if>
      <if test="internal != null" >
        internal,
      </if>
      <if test="flags != null" >
        flags,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="groupid != null" >
        #{groupid,jdbcType=BIGINT},
      </if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="internal != null" >
        #{internal,jdbcType=INTEGER},
      </if>
      <if test="flags != null" >
        #{flags,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="viplanModel.groups" >
    update groups
    <set >
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="internal != null" >
        internal = #{internal,jdbcType=INTEGER},
      </if>
      <if test="flags != null" >
        flags = #{flags,jdbcType=INTEGER},
      </if>
    </set>
    where groupid = #{groupid,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="viplanModel.groups" >
    update groups
    set name = #{name,jdbcType=VARCHAR},
      internal = #{internal,jdbcType=INTEGER},
      flags = #{flags,jdbcType=INTEGER}
    where groupid = #{groupid,jdbcType=BIGINT}
  </update>
</mapper>

Service接口层
 
该层是调用DAO中方法的
package viplanService;
import java.util.List;
import viplanModel.groups;

/**
 * Created by liubin on 2016/11/9.
 */
public interface groupsService {
    public List<groups> selectById(Long id);
    public  List<groups> selectByNameKey(String key);
}
serviceimpl 实现

package viplanServiceImpl;


import org.springframework.stereotype.Service;
import viplanModel.groups;
import viplanService.groupsService;
import viplanDAO.groupsMapper;


import javax.annotation.Resource;
import java.util.List;
import viplanModel.groups;

/**
 * Created by liubin on 2016/11/9.
 */

@Service("groupsService")

public class groupsServiceImpl implements groupsService{

    private  groupsMapper groupsDao;

    @Resource(name = "groupsMapper")
    public void setgroupsDao(groupsMapper groupsDao) {
        this.groupsDao = groupsDao;
    }

    public List<groups> selectById(Long id) {
        // TODO Auto-generated method stub
        return groupsDao.selectByID(id);
    }

    public List<groups> selectByNameKey(String key)
    {
        return groupsDao.selectByNameKey(key);
    }

}


Controller
 
package viplanController;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import viplanService.groupsService;

/**
 * Created by liubin on 2016/11/9.
 */

@RestController
public class groupsController extends Controller{
    @Autowired
    private groupsService gs;

    @RequestMapping(value = "api/groups/{groupid}/{father}/", method = RequestMethod.GET )
    public ControllerInfo getBabyByParentById(@PathVariable("groupid") Integer groupid,@PathVariable("father") Integer father) {
        try{
            if(father==1)
            {
                System.out.print("root is true");
            }
            else
            {
                System.out.print("root is false");
            }
            coninfo.setSuccess(true);
            coninfo.setData(gs.selectById(Long.valueOf(groupid)));
        }catch (Exception e)
        {
            coninfo.setSuccess(false);
            coninfo.setData(null);
            coninfo.setTotal(Long.valueOf(0));
            coninfo.setMessage(e.toString());
        }
        return coninfo;
    }


其中 coninfo是对返回进行了封装
 
package viplanController;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.ModelAttribute;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * Created by liubin on 2016/11/8.
 */

public abstract class Controller {
    ControllerInfo coninfo = new ControllerInfo();

    protected HttpServletResponse response;
    protected HttpServletRequest request;

    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        this.request = request;
        this.response = response;
        this.response.setCharacterEncoding("UTF-8");
        this.response.setContentType("text/json;charset=UTF-8");
        this.response.setHeader("Access-Control-Allow-Origin","*");
        this.request.setCharacterEncoding("UTF-8");
    }
}

package viplanController;

/**
 * Created by liubin on 2016/11/8.
 */
public class ControllerInfo {
    //消息状态
    private Boolean success;
    //数据
    private Object data;
    //总记录数
    private Long total;
    //当前页数
    private Long currPage;
    //提示信息
    private String message;
    //消息类型
    private Integer messageType;
    //tokenID
    private String tokenID;

    private Object title;

    public void setTitle(Object title) {
        this.title = title;
    }

    public Object getTitle() {
        return title;
    }

    public Boolean getSuccess() {
        return success;
    }


    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public Long getCurrPage() {
        return currPage;
    }

    public void setCurrPage(Long currPage) {
        this.currPage = currPage;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getMessageType() {
        return messageType;
    }

    public void setMessageType(Integer messageType) {
        this.messageType = messageType;
    }

    public String getTokenID() {
        return tokenID;
    }

    public void setTokenID(String id) {
        this.tokenID = id;
    }


}



 

 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值