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>
  <!-- 根据类别代码查询数据 -->
    <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);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容