MyBatis增刪改查

  1. mapper.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.company.combine.mapper.UserMapper">
    <resultMap id="result" type="com.company.combine.model.User">
        <id property="id" column="id"/>
    </resultMap>
    <insert id="insert" parameterType="com.company.combine.model.User"
            useGeneratedKeys="true" keyProperty="id">
        <!--<selectKey resultType="Integer" keyProperty="id" order="AFTER">-->
        <!--SELECT LAST_INSERT_ID()-->
        <!--</selectKey>-->
        INSERT INTO USER (username,sex,address) VALUES (#{username},#{sex},#{address})
    </insert>
    <delete id="deleteByPrimaryKey" parameterType="long">
        DELETE FROM user WHERE id = #{id}
    </delete>
    <update id="updateByPrimaryKey" parameterType="com.company.combine.model.User" keyProperty="id"
            useGeneratedKeys="true">
        UPDATE user
        SET username = #{username},sex = #{sex}
        <!--<where>-->
        <!--<if test="sex!=null and sex!=''">-->
        <!--and sex = #{sex}-->
        <!--</if>-->
        <!--<if test="username!=null and username!=''">-->
        <!--and username = #{username}-->
        <!--</if>-->

        <!--</where>-->
        WHERE id = #{id}
    </update>
  <!-- 根據(jù)類別代碼查詢數(shù)據(jù) -->
    <select id="selectAll"
            resultType="com.company.combine.model.User">
        SELECT * FROM USER
    </select>

    <select id="selectByPrimaryKey" parameterType="long"
            resultMap="result">
        SELECT * FROM USER WHERE id = #{id}
    </select>
</mapper>
  1. UserMapper.java
@Repository
public interface UserMapper {

//
//    @Insert({
//        "insert into user (id, username, ",
//        "birthday, sex, address)",
//        "values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, ",
//        "#{birthday,jdbcType=DATE}, #{sex,jdbcType=CHAR}, #{address,jdbcType=VARCHAR})"
//    })
    int insert(User user);

//    @Delete({
//        "delete from user",
//        "where id = #{id,jdbcType=INTEGER}"
//    })
    int deleteByPrimaryKey(Long id);
//
//    @Select({
//        "select",
//        "id, username, birthday, sex, address",
//        "from user",
//        "where id = #{id,jdbcType=INTEGER}"
//    })
//    @Results({
//        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
//        @Result(column="username", property="username", jdbcType=JdbcType.VARCHAR),
//        @Result(column="birthday", property="birthday", jdbcType=JdbcType.DATE),
//        @Result(column="sex", property="sex", jdbcType=JdbcType.CHAR),
//        @Result(column="address", property="address", jdbcType=JdbcType.VARCHAR)
//    })
    User selectByPrimaryKey(Long id);

    List<User> selectByPrimaryKeys(User user);
    List<User> selectByPrimaryKeys(Integer[] ids);
    List<User> selectByPrimaryKeys(List<Integer> idList);
    List<Orders> selectOrdersList();
    List<User> selectUserList();


//    @Select({
//        "select",
//        "id, username, birthday, sex, address",
//        "from user"
//    })
//    @Results({
//        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
//        @Result(column="username", property="username", jdbcType=JdbcType.VARCHAR),
//        @Result(column="birthday", property="birthday", jdbcType=JdbcType.DATE),
//        @Result(column="sex", property="sex", jdbcType=JdbcType.CHAR),
//        @Result(column="address", property="address", jdbcType=JdbcType.VARCHAR)
//    })
    List<User> selectAll();

//    @Update({
//        "update user",
//        "set username = #{username,jdbcType=VARCHAR},",
//          "birthday = #{birthday,jdbcType=DATE},",
//          "sex = #{sex,jdbcType=CHAR},",
//          "address = #{address,jdbcType=VARCHAR}",
//        "where id = #{id,jdbcType=INTEGER}"
//    })
    int updateByPrimaryKey(User user);
    List<User> selectUserByUsername(String username);
    List<User> selectUserByUsernameAndSex(User user);
}
  1. service.java
package com.company.combine.serviceimpl;

import com.company.combine.mapper.UserMapper;
import com.company.combine.model.User;
import com.company.combine.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by liuqun on 2017/8/21.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public User selectByPrimaryKey(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> selectUserByUsername(String username) {
        return userMapper.selectUserByUsername(username);
    }

    public int insert(User user){
        return userMapper.insert(user);
    }

    public int deleteByPrimaryKey(Long id){
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(User user) {
        return userMapper.updateByPrimaryKey(user);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一.原生jdbc程序中的問題 總結(jié): 1、數(shù)據(jù)庫連接,使用時就創(chuàng)建,不使用立即釋放,對數(shù)據(jù)庫進行頻繁連接開啟和關(guān)閉...
    Mr_歡先生閱讀 1,317評論 2 16
  • /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home...
    光劍書架上的書閱讀 4,196評論 2 8
  • 每個線程都應(yīng)該有它自己的SqlSession實例。SqlSession的實例不能共享使用,它是線程不安全的 配置文...
    蕊er閱讀 522評論 0 0
  • jHipster - 微服務(wù)搭建 CC_簡書[http://www.itdecent.cn/u/be0d56c4...
    quanjj閱讀 934評論 0 2
  • 紅燭高燒亞丁灣, 癡心一片鑒青天。 不教魑魅翻惡浪, 萬里海疆共嬋娟。 (新韻) 海軍官兵蔣林芝和陳賢榜今...
    任爾風云我自逍閱讀 519評論 2 10

友情鏈接更多精彩內(nèi)容