SSM框架的JDBC增添改查——Dao层,以及自己封装的父类:BaseDao

本文档详细介绍了如何在SSM(Spring、SpringMVC、Mybatis)框架下创建一个BaseDao父接口及其实现,用于简化JDBC的增删改查操作。文中展示了BaseDao的接口定义,包括deleteByPrimaryKey、insert、selectByPrimaryKey等方法,并提供了UserDao子类作为示例。同时,给出了对应的XML映射文件,展示具体的SQL语句,包括条件查询、分页查询和防止SQL注入的占位符使用。
  • java 个人新手记录

SSM框架(网上很多搭配ssm),MySql数据库;    记录BaseDao以及子类,和JDBC增添改查语句

SSM框架是spring MVC ,spring和mybatis框架的整合,是标准的MVC模式;其包含Dao层,Service层,Controller层;

对象的调用流程:     前端(JSP,Android,iOS) ——> Controller(Controller app) ——>  Service ——> Dao ——> 数据库

Dao层父类接口,子类直接继承即可!

Dao父类:

import java.util.HashMap;
import java.util.List;

public interface BaseDao<T> {
    
    /*删除数据*/
    int deleteByPrimaryKey(int id);
    
    int deleteByPrimaryKey(String src);


    /*插入数据*/
    int insert(T calss);

    int insertSelective(T calss);


    /*查询数据*/
    T selectByPrimaryKey(int id);
    
    T selectByPrimaryKey(String src);
    
    T selectByPrimaryKeyMap(HashMap<String,Object> map);
    
    List<T> selectByPrimary(HashMap<String,Object> map);


    /*更新数据*/
    int updateByPrimaryKeySelective(T calss);

    int updateByPrimaryKey(T calss);
}

 

子类:

import com.javen.model.UserBean;

public interface UserDao extends BaseDao<UserBean>{
}

 

mapping--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.javen.dao.UserDao" >
  <resultMap id="BaseResultMap" type="com.javen.model.UserBean">
    <id column="userid" property="userid" jdbcType="INTEGER" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="account" property="account" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="sex" property="sex" jdbcType="INTEGER" />
    <result column="age" property="age" jdbcType="INTEGER" />
    <result column="headphoto" property="headphoto" jdbcType="VARCHAR" />
  </resultMap>
  
  <sql id="Base_Column_List" >
    userid, username, account, password, sex, age, headphoto
  </sql>
  
  <!-- ///////////////////////////////////////////// -->
  <!-- 1.#{}与${}
    #{}表示一个占位符,使用占位符可以防止sql注入,
    ${}通过${}可以将parameterType传入的内容拼接在sql中,不能防止sql注入,但是有时方便 -->
    
  
  <!-- //根据id进行查询 -->
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from user
    where userid = #{userid,jdbcType=INTEGER} or account = #{account,jdbcType=VARCHAR}
  </select>
  
  <!-- //根据多参数进行查询 -->
  <select id="selectByPrimaryKeyMap" resultMap="BaseResultMap" parameterType="Map" >
    select 
    <include refid="Base_Column_List" />
    from user
    where account = #{account,jdbcType=VARCHAR} and password = #{password,jdbcType=VARCHAR}
  </select>
  
  
  <!-- //根据id进行查询多个 -->
  <select id="selectByPrimary" resultMap="BaseResultMap" parameterType="Map" >
    select 
    <include refid="Base_Column_List" />
    from shangpin1
    where userid = #{userid,jdbcType=INTEGER}
    <if test="start!=null and size!=null">
            limit #{start},#{size} <!-- //分页查询 -->
        </if>
  </select>
  
  <!-- //根据id删除 -->
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from user
    where userid = #{userid,jdbcType=INTEGER}
  </delete>
  
  <!-- //插入 -->
  <insert id="insert" parameterType="com.javen.model.UserBean" >
    insert into user (userid, username, account, password, sex, age, headphoto)
    values (#{userid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{account,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
            , #{sex,jdbcType=INTEGER}, #{age,jdbcType=INTEGER}, #{headphoto,jdbcType=VARCHAR})
  </insert>
  
  <!-- //选择性插入 -->
  <insert id="insertSelective" parameterType="com.javen.model.UserBean" >
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="userid != null" >
        userid,
      </if>
      <if test="username != null" >
        username,
      </if>
      <if test="account != null" >
        account,
      </if>
      <if test="password != null" >
        password,
      </if>
      <if test="sex != null" >
        sex,
      </if>
      <if test="age != null" >
        age,
      </if>
      <if test="headphoto != null" >
        headphoto,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="userid != null" >
        #{userid,jdbcType=INTEGER},
      </if>
      <if test="username != null" >
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="account != null" >
        #{account,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="sex != null" >
        #{sex,jdbcType=INTEGER},
      </if>
      <if test="age != null" >
        #{age,jdbcType=INTEGER},
      </if>
      <if test="headphoto != null" >
        #{headphoto,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  
  <!-- //选择性更新 -->
  <update id="updateByPrimaryKeySelective" parameterType="com.javen.model.UserBean" >
    update user
    <set >
      <if test="username != null" >
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="account != null" >
        account = #{account,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="sex != null" >
        sex = #{sex,jdbcType=INTEGER},
      </if>
      <if test="age != null" >
        age = #{age,jdbcType=INTEGER},
      </if>
      <if test="headphoto != null" >
        headphoto = #{headphoto,jdbcType=VARCHAR},
      </if>
    </set>
    where userid = #{userid,jdbcType=INTEGER}
  </update>
  
  <!-- //全部更新 -->
  <update id="updateByPrimaryKey" parameterType="com.javen.model.UserBean" >
    update user
    set username = #{username,jdbcType=VARCHAR},
      account = #{account,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      sex = #{sex,jdbcType=INTEGER},
      age = #{age,jdbcType=INTEGER},
      headphoto = #{headphoto,jdbcType=VARCHAR}
    where userid = #{userid,jdbcType=INTEGER}
  </update>
</mapper>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值