当前位置:   article > 正文

spring mybatis 动态SQL的insert_insert into 动态sql

insert into 动态sql

在INSERT 动态插入中使用if标签

插入数据库的记录中,不是每一个字段都有值,此时就可以使用if标签。

(1)条件:只有非空属性才插入。

(2)动态SQL。

 

 数据库,建表

  1. create table z_student(
  2. id int not null auto_increment primary key,
  3. name varchar(20) not null,
  4. phone varchar(20) null,
  5. email varchar(50) null,
  6. sex tinyint(4) null comment '0女,1男',
  7. locked tinyint null comment '状态,0正常,1锁定',
  8. gmt_created datetime default CURRENT_TIMESTAMP comment '存入库的时间',
  9. gmt_modified datetime default CURRENT_TIMESTAMP comment '修改时间',
  10. deletes int(11) null
  11. ) comment '学生表'
  12. insert into z_student values(null,'tom','130','xx@xx.com',1,0,null,null,0);
  13. insert into z_student values(null,'tom2','130','xx@xx.com',1,0,null,null,0);
  14. insert into z_student values(null,'tom3','130','xx@xx.com',1,0,null,null,0);
  15. insert into z_student values(null,'tom4','130','xx@xx.com',1,0,null,null,0);

代码如下:

Student.java

  1. package com.shrimpking.code03;
  2. import java.util.Date;
  3. /**
  4. * @author user1
  5. */
  6. public class Student
  7. {
  8. private int id;
  9. private String name;
  10. private String phone;
  11. private String email;
  12. private int sex;
  13. private int locked;
  14. private Date gmtCreated;
  15. private Date gmtModified;
  16. private int deletes;
  17. public Student()
  18. {
  19. }
  20. public Student(int id, String name, String phone, String email, int sex, int locked, Date gmtCreated, Date gmtModified, int deletes)
  21. {
  22. this.id = id;
  23. this.name = name;
  24. this.phone = phone;
  25. this.email = email;
  26. this.sex = sex;
  27. this.locked = locked;
  28. this.gmtCreated = gmtCreated;
  29. this.gmtModified = gmtModified;
  30. this.deletes = deletes;
  31. }
  32. public int getId()
  33. {
  34. return id;
  35. }
  36. public void setId(int id)
  37. {
  38. this.id = id;
  39. }
  40. public String getName()
  41. {
  42. return name;
  43. }
  44. public void setName(String name)
  45. {
  46. this.name = name;
  47. }
  48. public String getPhone()
  49. {
  50. return phone;
  51. }
  52. public void setPhone(String phone)
  53. {
  54. this.phone = phone;
  55. }
  56. public String getEmail()
  57. {
  58. return email;
  59. }
  60. public void setEmail(String email)
  61. {
  62. this.email = email;
  63. }
  64. public int getSex()
  65. {
  66. return sex;
  67. }
  68. public void setSex(int sex)
  69. {
  70. this.sex = sex;
  71. }
  72. public int getLocked()
  73. {
  74. return locked;
  75. }
  76. public void setLocked(int locked)
  77. {
  78. this.locked = locked;
  79. }
  80. public Date getGmtCreated()
  81. {
  82. return gmtCreated;
  83. }
  84. public void setGmtCreated(Date gmtCreated)
  85. {
  86. this.gmtCreated = gmtCreated;
  87. }
  88. public Date getGmtModified()
  89. {
  90. return gmtModified;
  91. }
  92. public void setGmtModified(Date gmtModified)
  93. {
  94. this.gmtModified = gmtModified;
  95. }
  96. public int getDeletes()
  97. {
  98. return deletes;
  99. }
  100. public void setDeletes(int deletes)
  101. {
  102. this.deletes = deletes;
  103. }
  104. @Override
  105. public String toString()
  106. {
  107. return "Student{" + "id=" + id + ", name='" + name + '\'' + ", phone='" + phone + '\'' + ", email='" + email + '\'' + ", sex=" + sex + ", locked=" + locked + ", gmtCreated=" + gmtCreated + ", gmtModified=" + gmtModified + ", deletes=" + deletes + '}';
  108. }
  109. }

StudentMapper.java

  1. package com.shrimpking.code03;
  2. public interface StudentMapper
  3. {
  4. public int insertStudent(Student student);
  5. }

StudentMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.shrimpking.code03.StudentMapper">
  6. <insert id="insertStudent" parameterType="Student">
  7. insert into z_student
  8. <trim prefix="(" suffix=")" suffixOverrides=",">
  9. <if test="id != null">
  10. id,
  11. </if>
  12. <if test="name != null">
  13. name,
  14. </if>
  15. <if test="phone != null">
  16. phone,
  17. </if>
  18. <if test="email != null">
  19. email,
  20. </if>
  21. <if test="sex != null">
  22. sex,
  23. </if>
  24. <if test="locked != null">
  25. locked,
  26. </if>
  27. <if test="gmtCreated != null">
  28. gmt_created,
  29. </if>
  30. <if test="gmtModified != null">
  31. gmt_modified,
  32. </if>
  33. </trim>
  34. <trim prefix="values (" suffix=")" suffixOverrides=",">
  35. <if test="id != null">
  36. #{id,jdbcType=INTEGER},
  37. </if>
  38. <if test="name != null">
  39. #{name,jdbcType=VARCHAR},
  40. </if>
  41. <if test="phone != null">
  42. #{phone,jdbcType=VARCHAR},
  43. </if>
  44. <if test="email != null">
  45. #{email,jdbcType=VARCHAR},
  46. </if>
  47. <if test="sex != null">
  48. #{sex,jdbcType=TINYINT},
  49. </if>
  50. <if test="locked != null">
  51. #{locked,jdbcType=TINYINT},
  52. </if>
  53. <if test="gmtCreated != null">
  54. #{gmtCreated,jdbcType=TIMESTAMP},
  55. </if>
  56. <if test="gmtModified != null">
  57. #{gmtModified,jdbcType=TIMESTAMP},
  58. </if>
  59. </trim>
  60. </insert>
  61. </mapper>

mybatis.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!-- 起别名 -->
  7. <typeAliases>
  8. <package name="com.shrimpking.code03"/>
  9. </typeAliases>
  10. <!-- 环境 -->
  11. <environments default="development">
  12. <environment id="development">
  13. <!-- 默认事务管理 -->
  14. <transactionManager type="JDBC"/>
  15. <!-- 默认的数据库连接 -->
  16. <dataSource type="POOLED">
  17. <property name="driver" value="com.mysql.jdbc.Driver"/>
  18. <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimeZone=UTC"/>
  19. <property name="username" value="root"/>
  20. <property name="password" value="mysql123"/>
  21. </dataSource>
  22. </environment>
  23. </environments>
  24. <mappers>
  25. <mapper resource="com/shrimpking/code03/StudentMapper.xml"/>
  26. </mappers>
  27. </configuration>

StudentTest.java

  1. package com.shrimpking.code03;
  2. import com.shrimpking.utils.DaoUtils;
  3. import org.apache.ibatis.session.SqlSession;
  4. import org.junit.Test;
  5. import java.io.IOException;
  6. import java.util.Date;
  7. public class StudentTest
  8. {
  9. @Test
  10. public void insert() throws IOException
  11. {
  12. SqlSession sqlSession = DaoUtils.getSqlSession("code03/mybatis.xml");
  13. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  14. Student stu = new Student();
  15. stu.setName("虾米");
  16. stu.setPhone("18099999999");
  17. stu.setEmail("zz@zz.com");
  18. stu.setSex(1);
  19. stu.setLocked(0);
  20. stu.setGmtCreated(new Date());
  21. stu.setDeletes(0);
  22. int i = mapper.insertStudent(stu);
  23. if(i > 0)
  24. {
  25. System.out.println("插入成功");
  26. }
  27. sqlSession.commit();
  28. sqlSession.close();
  29. }
  30. }

DaoUtils.java

  1. package com.shrimpking.utils;
  2. import org.apache.ibatis.io.Resources;
  3. import org.apache.ibatis.session.SqlSession;
  4. import org.apache.ibatis.session.SqlSessionFactory;
  5. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  6. import java.io.IOException;
  7. import java.io.Reader;
  8. /**
  9. * @author user1
  10. */
  11. public class DaoUtils
  12. {
  13. private static Reader reader;
  14. private static SqlSessionFactory sqlSessionFactory;
  15. public static SqlSession getSqlSession(String config) throws IOException
  16. {
  17. try
  18. {
  19. reader = Resources.getResourceAsReader(config);
  20. //
  21. sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  22. }
  23. catch (IOException e)
  24. {
  25. e.printStackTrace();
  26. }
  27. finally
  28. {
  29. reader.close();
  30. }
  31. return sqlSessionFactory.openSession();
  32. }
  33. }

运行截图

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/855330
推荐阅读
相关标签
  

闽ICP备14008679号