2 Commits ced9659f94 ... ff73211d44

Autor SHA1 Mensagem Data
  starlight_0208 ff73211d44 2024年4月14日 12点16分 há 7 meses atrás
  starlight_0208 1631b844bb 2024年4月13日 21点59分 há 7 meses atrás

+ 1 - 1
.idea/sqldialects.xml

@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
   <component name="SqlDialectMappings">
-    <file url="file://$PROJECT_DIR$/src/main/resources/org/starter/myBatis/mapper/UserMapper.xml" dialect="GenericSQL" />
+    <file url="file://$PROJECT_DIR$/src/main/resources/org/starter/myBatis/mapper/UserMapper.xml" dialect="MariaDB" />
     <file url="PROJECT" dialect="MySQL" />
   </component>
 </project>

+ 10 - 5
src/main/java/Main3.java

@@ -7,9 +7,7 @@ import org.starter.myBatis.pojo.User;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 public class Main3 {
     public static void main(String[] args) throws IOException {
@@ -25,11 +23,18 @@ public class Main3 {
         UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
 
         User user = new User();
+        user.setUsername("Genshin Impact");
+        user.setPassword("Launch!");
         user.setGender("男");
-        user.setProvince("山西");
+        user.setProvince("上海");
+        user.setCity("上海");
+        user.setId(8);
         // 执行查询
-        List<User> users = userMapper.selectUserByGenderAndProvince("男", "山西");
-        System.out.println(users);
+        int changedLine = userMapper.updateUser(user);
+        System.out.println("Line Changed: " + changedLine);
+
+        // 提交事务
+        sqlSession.commit();
 
         // 关闭会话
         sqlSession.close();

+ 5 - 0
src/main/java/org/starter/myBatis/mapper/UserMapper.java

@@ -10,4 +10,9 @@ public interface UserMapper {
     List<User> selectAllUser();
     List<User> selectUserByGender(String gender);
     List<User> selectUserByGenderAndProvince(@Param("gender") String gender, @Param("province") String province);
+    List<User> selectUserByConfidence(User user);
+    List<User> selectUserByConfidences(User user);
+    void addUser(User user);
+    int updateUser(User user);
+    int updateUserInfo(User user);
 }

+ 73 - 0
src/main/resources/org/starter/myBatis/mapper/UserMapper.xml

@@ -29,5 +29,78 @@
             </if>
         </where>
     </select>
+    <select id="selectUserByConfidence" resultType="org.starter.myBatis.pojo.User">
+        select *
+        from tb_user
+        <where>
+            <choose>
+                <when test="username != null and user != ''">
+                    username like #{userName}
+                </when>
+                <when test="gender != null and gender != ''">
+                    gender = #{gender}
+                </when>
+                <when test="province != null and province != ''">
+                    province like #{province}
+                </when>
+                <when test="city != null and city != ''">
+                    city like #{city}
+                </when>
+            </choose>
+        </where>
+    </select>
+    <select id="selectUserByConfidences" resultType="org.starter.myBatis.pojo.User">
+        select *
+        from tb_user
+        <where>
+            <if test="username != null and username != ''">
+                username like #{userName}
+            </if>
+            <if test="gender != null and gender != ''">
+                and gender like #{gender}
+            </if>
+            <if test="province != null and province != ''">
+                and province like #{province}
+            </if>
+            <if test="city != null and province != ''">
+                and city like #{city}
+            </if>
+        </where>
+    </select>
+    <insert id="addUser" useGeneratedKeys="true" keyProperty="id">
+        insert into tb_user (username, password, gender, province, city)
+        values (#{username}, #{password}, #{gender}, #{province}, #{city});
+    </insert>
+    <update id="updateUser">
+        update tb_user
+        set
+            username = #{username},
+            password = #{password},
+            gender = #{gender},
+            province = #{province},
+            city = #{city}
+        where id = #{id};
+    </update>
+    <update id="updateUserInfo">
+        update tb_user
+        <set>
+            <if test="username != null and user != ''">
+                username = #{username}
+            </if>
+            <if test="password != null and password != ''">
+                password = #{password}
+            </if>
+            <if test="gender != null and gender != ''">
+                gender = #{gender}
+            </if>
+            <if test="province != null and province != ''">
+                province = #{province}
+            </if>
+            <if test="city != null and province != ''">
+                city = #{city}
+            </if>
+        </set>
+    </update>
+
 
 </mapper>