Quellcode durchsuchen

mybatis project change6

zhang vor 2 Jahren
Ursprung
Commit
32e45f8fec

+ 2 - 0
mybatis_train/src/main/java/com/mapper/AccountMapper.java

@@ -1,6 +1,7 @@
1 1
 package com.mapper;
2 2
 
3 3
 import com.pojo.entity.AccountEntity;
4
+import com.pojo.entity.AccountRealEntity;
4 5
 import org.apache.ibatis.annotations.Mapper;
5 6
 import org.springframework.stereotype.Component;
6 7
 import org.springframework.stereotype.Repository;
@@ -11,4 +12,5 @@ public interface AccountMapper {
11 12
     List<AccountEntity> findAll(Integer id);
12 13
     void updateOne(AccountEntity accountEntity);
13 14
     void insertOne(AccountEntity accountEntity);
15
+    AccountRealEntity findByResultMap();
14 16
 }

+ 10 - 0
mybatis_train/src/main/java/com/pojo/entity/AccountRealEntity.java

@@ -0,0 +1,10 @@
1
+package com.pojo.entity;
2
+
3
+import lombok.Data;
4
+
5
+@Data
6
+public class AccountRealEntity {
7
+    private Integer id;
8
+    private String userName;
9
+    private Double balance;
10
+}

+ 8 - 0
mybatis_train/src/main/resources/com/mapper/AccountMapper.xml

@@ -2,6 +2,11 @@
2 2
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3 3
 
4 4
 <mapper namespace="com.mapper.AccountMapper">
5
+    <resultMap id="accountResultMap" type="com.pojo.entity.AccountRealEntity">
6
+        <id property="id" column="id"></id>
7
+        <result property="userName" column="user_name"></result>
8
+        <result property="balance" column="balance"></result>
9
+    </resultMap>
5 10
     <select id="findAll" resultType="account"
6 11
             parameterType="java.lang.Integer"
7 12
     >
@@ -13,4 +18,7 @@
13 18
     <update id="updateOne" parameterType="account">
14 19
         update account set user_name= #{user_name},balance=#{balance} where id = #{id}
15 20
     </update>
21
+    <select id="findByResultMap" resultType="com.pojo.entity.AccountRealEntity">
22
+        select * from account where id = 3
23
+    </select>
16 24
 </mapper>

+ 1 - 0
mybatis_train/src/main/resources/mybatisConfig.xml

@@ -6,6 +6,7 @@
6 6
     <properties resource="jdbc.properties"></properties>
7 7
     <settings>
8 8
         <setting name="logImpl" value="LOG4J2"/>
9
+        <setting name="mapUnderscoreToCamelCase" value="true"/>
9 10
     </settings>
10 11
     <!--    Mapper文件注册-->
11 12
     <typeAliases>

+ 10 - 0
mybatis_train/src/test/java/Test1.java

@@ -1,5 +1,6 @@
1 1
 import com.mapper.AccountMapper;
2 2
 import com.pojo.entity.AccountEntity;
3
+import com.pojo.entity.AccountRealEntity;
3 4
 import org.apache.ibatis.io.Resources;
4 5
 import org.apache.ibatis.session.SqlSession;
5 6
 import org.apache.ibatis.session.SqlSessionFactory;
@@ -43,4 +44,13 @@ public class Test1 {
43 44
         AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
44 45
         List<AccountEntity> all = mapper.findAll(9);
45 46
     }
47
+    // 测试resultMap
48
+    @Test
49
+    public void testResultMap() throws IOException {
50
+        InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
51
+        SqlSessionFactory sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder().build(resourceAsStream);
52
+        SqlSession sqlSession = sqlSessionFactoryBuilder.openSession(true);
53
+        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
54
+        AccountRealEntity byResultMap = mapper.findByResultMap();
55
+    }
46 56
 }

+ 0 - 1
mybatis_train/src/test/java/Test3.java

@@ -71,7 +71,6 @@ public class Test3 {
71 71
         List<UserEntity> userForEach1 = mapper1.findUserForEach(integers);
72 72
         sqlSession.close();
73 73
         List<UserEntity> userForEach2 = mapper2.findUserForEach(integers);
74
-        sqlSession2.close();
75 74
 
76 75
     }
77 76