Browse Source

mybatis project change5

zhang 2 years ago
parent
commit
dfbaf16b22

+ 20 - 0
mybatis_train/pom.xml

@@ -37,6 +37,26 @@
37 37
             <artifactId>lombok</artifactId>
38 38
             <version>1.18.24</version>
39 39
         </dependency>
40
+        <dependency>
41
+            <groupId>org.apache.logging.log4j</groupId>
42
+            <artifactId>log4j-api</artifactId>
43
+            <version>2.11.1</version>
44
+        </dependency>
45
+        <dependency>
46
+            <groupId>org.apache.logging.log4j</groupId>
47
+            <artifactId>log4j-core</artifactId>
48
+            <version>2.11.1</version>
49
+        </dependency>
50
+        <dependency>
51
+            <groupId>org.apache.logging.log4j</groupId>
52
+            <artifactId>log4j-web</artifactId>
53
+            <version>2.11.1</version>
54
+        </dependency>
55
+        <dependency>
56
+            <groupId>org.apache.logging.log4j</groupId>
57
+            <artifactId>log4j-slf4j-impl</artifactId>
58
+            <version>2.11.1</version>
59
+        </dependency>
40 60
     </dependencies>
41 61
 
42 62
 </project>

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

@@ -7,4 +7,6 @@ import java.util.List;
7 7
 
8 8
 public interface UserMapper {
9 9
     List<UserEntity> findUserListByCondition(UserEntity userEntity);
10
+    List<UserEntity> findUserForEach(List<Integer> users);
11
+    void deleteOne(Integer id);
10 12
 }

+ 28 - 3
mybatis_train/src/main/resources/com/mapper/UserMapper.xml

@@ -3,8 +3,33 @@
3 3
         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 4
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 5
 <mapper namespace="com.mapper.UserMapper">
6
-<select id="findUserListByCondition" resultType="com.pojo.entity.UserEntity"
7
-        parameterType="com.pojo.entity.UserEntity" >
8
-    select * from user;
6
+<!--    sql语句抽取-->
7
+    <sql id="all">select * from user</sql>
8
+<select id="findUserListByCondition" resultType="user"
9
+        parameterType="user" >
10
+    <include refid="all"></include>
11
+    <where>
12
+        <if test="id !=null">
13
+            and id = #{id}
14
+        </if>
15
+        <if test="name !=null">
16
+            and name = #{name}
17
+        </if>
18
+        <if test="age !=null">
19
+            and age = #{age}
20
+        </if>
21
+    </where>
9 22
 </select>
23
+<select id="findUserForEach" parameterType="list" resultType="user">
24
+    <include refid="all"></include>
25
+    <where>
26
+    <foreach collection="list" open="id in(" close=")" item="item" separator=",">
27
+        #{item}
28
+    </foreach>
29
+</where>
30
+</select>
31
+<delete id="deleteOne" parameterType="integer">
32
+    delete from user where id=#{id}
33
+</delete>
34
+
10 35
 </mapper>

+ 16 - 0
mybatis_train/src/main/resources/log4j2.xml

@@ -0,0 +1,16 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<Configuration status="WARN">
3
+    <Appenders>
4
+        <Console name="Console" target="SYSTEM_OUT">
5
+            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %l %msg%n"/>
6
+        </Console>
7
+    </Appenders>
8
+    <Loggers>
9
+        <Root level="error">
10
+            <AppenderRef ref="Console"/>
11
+        </Root>
12
+        <Logger name="com.mapper" level="DEBUG" additivity="false">
13
+            <AppenderRef ref="Console"/>
14
+        </Logger>
15
+    </Loggers>
16
+</Configuration>

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

@@ -4,6 +4,9 @@
4 4
 <configuration>
5 5
     <!--数据源的配置信息会单独放在jdbc.properties文件中,这里加载到mybatis配置文件来,使用的时候用EL表达式${}-->
6 6
     <properties resource="jdbc.properties"></properties>
7
+    <settings>
8
+        <setting name="logImpl" value="LOG4J2"/>
9
+    </settings>
7 10
     <!--    Mapper文件注册-->
8 11
     <typeAliases>
9 12
         <typeAlias type="com.pojo.entity.AccountEntity" alias="account"></typeAlias>

+ 29 - 7
mybatis_train/src/test/java/Test3.java

@@ -4,10 +4,13 @@ import org.apache.ibatis.io.Resources;
4 4
 import org.apache.ibatis.session.SqlSession;
5 5
 import org.apache.ibatis.session.SqlSessionFactory;
6 6
 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
7
+import org.junit.After;
8
+import org.junit.Before;
7 9
 import org.junit.Test;
8 10
 
9 11
 import java.io.IOException;
10 12
 import java.io.InputStream;
13
+import java.util.ArrayList;
11 14
 import java.util.List;
12 15
 
13 16
 /**
@@ -15,19 +18,38 @@ import java.util.List;
15 18
  */
16 19
 
17 20
 public class Test3 {
18
-    @Test
19
-    public void testCondition() throws IOException {
21
+    UserMapper userMapper;
22
+    SqlSession sqlSession;
23
+    @Before
24
+    public void before() throws IOException {
20 25
         InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
21 26
         SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
22
-        SqlSession sqlSession = build.openSession(true);
23
-        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
27
+        sqlSession = build.openSession(true);
28
+        userMapper = sqlSession.getMapper(UserMapper.class);
29
+    }
30
+    @Test
31
+    public void testCondition()  {
24 32
         UserEntity userEntity = new UserEntity();
25
-        userEntity.setId(1);
33
+//        userEntity.setId(1);
26 34
         userEntity.setName("name");
27 35
         userEntity.setAge(13);
28 36
         userEntity.setAddress("address");
29
-        List<UserEntity> userListByCondition = mapper.findUserListByCondition(userEntity);
37
+        List<UserEntity> userListByCondition = userMapper.findUserListByCondition(userEntity);
38
+    }
39
+    @Test
40
+    public void testForEach() {
41
+        ArrayList<Integer> integers = new ArrayList<>();
42
+//        integers.add(1);
43
+//        integers.add(2);
44
+        List<UserEntity> userForEach = userMapper.findUserForEach(integers);
45
+    }
46
+    @Test
47
+    public void delete() {
48
+        userMapper.deleteOne(2);
49
+    }
50
+    @After
51
+    public void after() {
30 52
         sqlSession.close();
31
-
32 53
     }
54
+
33 55
 }