码迷,mamicode.com
首页 > 其他好文 > 详细

一、Mybatis 单表 常用增删改查

时间:2018-12-27 20:57:51      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:inter   odi   font   turn   批量添加数据   this   bsp   time   value   

1.编写sql表,插入原始数据

 1 -- 删除表
 2 DROP TABLE testA;
 3 -- 创建表
 4 CREATE TABLE testA(
 5     id INT AUTO_INCREMENT PRIMARY KEY,
 6     username VARCHAR(50),
 7     date1 DATETIME,
 8     str_date VARCHAR(50),
 9     date_str DATE
10 )
11 -- 插入数据
12 INSERT INTO testA(username,date1,str_date,date_str) VALUES(夕阳游子,NOW(),DATE_FORMAT(SYSDATE(),%Y-%m-%d %h:%i:%s),STR_TO_DATE(2018-12-26,%Y-%m-%d %h:%i:%s));

2.编写实体类 TestA.java

 

package com.jtfr.pojo;

import java.util.Date;

public class TestA {
    private int id;
    private String username;
    private Date date;
    private String strDate;
    private Date dateStr;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public String getStrDate() {
        return strDate;
    }
    public void setStrDate(String strDate) {
        this.strDate = strDate;
    }
    public Date getDateStr() {
        return dateStr;
    }
    public void setDateStr(Date dateStr) {
        this.dateStr = dateStr;
    }
    @Override
    public String toString() {
        return "TestA [id=" + id + ", username=" + username + ", date=" + date
                + ", strDate=" + strDate + ", dateStr=" + dateStr + "]";
    }
}

 

 

3.编写Dao层 TestADao.java

 

 1 package com.jtfr.mapper;
 2 
 3 import java.util.List;
 4 
 5 import com.jtfr.pojo.TestA;
 6 
 7 public interface TestADao {
 8 
 9     void insertTestA(TestA testA);
10 
11     void updateTestA(TestA testA);
12 
13     void deleteTestA(TestA testA);
14 
15     List<TestA> findAll();
16     
17 }

 

4.编写Dao层对应的 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">
<!-- namespace必需是接口的全路径名 -->
<mapper namespace="com.jtfr.mapper.TestADao">
    <resultMap id="TestA" type="com.jtfr.pojo.TestA"></resultMap>
    <sql id="query_sql">
        SELECT 
            t.`id`,
            t.`username`,
            t.`date1` date,
            DATE_FORMAT(t.`str_date`,‘%Y-%m-%d %h:%i:%s‘) strDate,
            STR_TO_DATE(t.`date_str`, ‘%Y-%m-%d‘) dateStr
        FROM testA t;
    </sql>
    <!-- 批量添加数据 -->
    <!-- <insert id="insertUserList" parameterType="list">
        INSERT INTO usertable VALUES
        <foreach collection="list" item="user" index="0" separator=","  >
            ( #{id}, #{username}, #{passwd})  
        </foreach>
    </insert> -->
    <insert id="insertTestA">
        INSERT INTO testA
            (
            username
            <if test="date != null">
                ,date1
            </if>
            <if test="strDate != null">
                ,str_date
            </if>
            <if test="dateStr != null">
                ,date_str
            </if>
            ) 
        VALUES
            (
            #{username, jdbcType=VARCHAR}
            <if test="date != null">
                ,#{date, jdbcType=TIMESTAMP}
            </if>
            <if test="strDate != null">
                ,DATE_FORMAT(#{dateStr}, ‘%Y-%m-%d %h:%i:%s‘)
            </if>
            <if test="dateStr != null">
                ,STR_TO_DATE(#{strDate}, ‘%Y-%m-%d %h:%i:%s‘)
            </if>
            )
    </insert>
    <update id="updateTestA">
        UPDATE testA t SET
            t.username=#{username}
            <if test="date != null">
                ,t.date1=#{date}
            </if>
            <if test="strDate != null">
                ,t.str_date=DATE_FORMAT(#{dateStr},‘%Y-%m-%d %h:%i:%s‘)
            </if>
            <if test="dateStr != null">
                ,t.date_str=STR_TO_DATE(#{strDate},‘%Y-%m-%d %h:%i:%s‘)
            </if>
        WHERE t.id=#{id};
    </update>
    <delete id="deleteTestA">
        <!-- 注意不能写成 testA t -->
        DELETE FROM testA WHERE username=#{username};
    </delete>
    <select id="findAll" resultMap="TestA">
        <include refid="query_sql"></include>
    </select>
</mapper>

5.项目源码,Maven结构

技术分享图片

下载地址:https://files.cnblogs.com/files/jtfr/MybatisMapperDemo.zip

 

一、Mybatis 单表 常用增删改查

标签:inter   odi   font   turn   批量添加数据   this   bsp   time   value   

原文地址:https://www.cnblogs.com/jtfr/p/10187189.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!