Browse Source

mybatis project change6

zhang 2 years ago
parent
commit
32e45f8fec

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

@@ -1,6 +1,7 @@
1
 package com.mapper;
1
 package com.mapper;
2
 
2
 
3
 import com.pojo.entity.AccountEntity;
3
 import com.pojo.entity.AccountEntity;
4
+import com.pojo.entity.AccountRealEntity;
4
 import org.apache.ibatis.annotations.Mapper;
5
 import org.apache.ibatis.annotations.Mapper;
5
 import org.springframework.stereotype.Component;
6
 import org.springframework.stereotype.Component;
6
 import org.springframework.stereotype.Repository;
7
 import org.springframework.stereotype.Repository;
@@ -11,4 +12,5 @@ public interface AccountMapper {
11
     List<AccountEntity> findAll(Integer id);
12
     List<AccountEntity> findAll(Integer id);
12
     void updateOne(AccountEntity accountEntity);
13
     void updateOne(AccountEntity accountEntity);
13
     void insertOne(AccountEntity accountEntity);
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
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
2
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3
 
3
 
4
 <mapper namespace="com.mapper.AccountMapper">
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
     <select id="findAll" resultType="account"
10
     <select id="findAll" resultType="account"
6
             parameterType="java.lang.Integer"
11
             parameterType="java.lang.Integer"
7
     >
12
     >
@@ -13,4 +18,7 @@
13
     <update id="updateOne" parameterType="account">
18
     <update id="updateOne" parameterType="account">
14
         update account set user_name= #{user_name},balance=#{balance} where id = #{id}
19
         update account set user_name= #{user_name},balance=#{balance} where id = #{id}
15
     </update>
20
     </update>
21
+    <select id="findByResultMap" resultType="com.pojo.entity.AccountRealEntity">
22
+        select * from account where id = 3
23
+    </select>
16
 </mapper>
24
 </mapper>

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

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

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

@@ -1,5 +1,6 @@
1
 import com.mapper.AccountMapper;
1
 import com.mapper.AccountMapper;
2
 import com.pojo.entity.AccountEntity;
2
 import com.pojo.entity.AccountEntity;
3
+import com.pojo.entity.AccountRealEntity;
3
 import org.apache.ibatis.io.Resources;
4
 import org.apache.ibatis.io.Resources;
4
 import org.apache.ibatis.session.SqlSession;
5
 import org.apache.ibatis.session.SqlSession;
5
 import org.apache.ibatis.session.SqlSessionFactory;
6
 import org.apache.ibatis.session.SqlSessionFactory;
@@ -43,4 +44,13 @@ public class Test1 {
43
         AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
44
         AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
44
         List<AccountEntity> all = mapper.findAll(9);
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
         List<UserEntity> userForEach1 = mapper1.findUserForEach(integers);
71
         List<UserEntity> userForEach1 = mapper1.findUserForEach(integers);
72
         sqlSession.close();
72
         sqlSession.close();
73
         List<UserEntity> userForEach2 = mapper2.findUserForEach(integers);
73
         List<UserEntity> userForEach2 = mapper2.findUserForEach(integers);
74
-        sqlSession2.close();
75
 
74
 
76
     }
75
     }
77
 
76