Bladeren bron

mybatis change 7

zhangxiaoyu 2 jaren geleden
bovenliggende
commit
9543675e3c

+ 15 - 0
mybatis_train/src/main/java/com/mapper/SqlMapper.java

@@ -0,0 +1,15 @@
1
+package com.mapper;
2
+
3
+import com.pojo.entity.UserEntity;
4
+import org.apache.ibatis.annotations.Param;
5
+
6
+import java.util.List;
7
+
8
+public interface SqlMapper {
9
+    // 模糊查询
10
+    List<UserEntity> getUserByLike(@Param("name") String name);
11
+    List<UserEntity> getUserByList(@Param("ids") List<Integer> ids);
12
+    List<UserEntity> getUserByTableName(@Param("tableName") String tab);
13
+    int insertGeneratedKey(UserEntity userEntity);
14
+
15
+}

+ 29 - 0
mybatis_train/src/main/resources/com/mapper/SqlMapper.xml

@@ -0,0 +1,29 @@
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.mapper.SqlMapper">
6
+<!--    sql语句抽取-->
7
+    <select id="getUserByLike" resultType="com.pojo.entity.UserEntity">
8
+        <!--       select *  from user where name like '%${name}%'
9
+                select *  from user where name like  concat('%',#{name},'%') -->
10
+                select *  from user where name like "%"#{name}"%"
11
+    </select>
12
+    <select id="getUserByList" resultType="com.pojo.entity.UserEntity">
13
+        select *  from user where id in (#{ids})
14
+    </select>
15
+    <select id="getUserByTableName" resultType="com.pojo.entity.UserEntity">
16
+        <!-- 动态设置表名只能用$符号 -->
17
+        select *  from  ${tableName}
18
+    </select>
19
+    <!--
20
+        useGeneratedKeys 设置当前标签中sql使用了自增的主键
21
+        keyProperty 将自增主键的值赋值给映射文件中参数的某个属性值
22
+     -->
23
+    <insert id="insertGeneratedKey"
24
+            useGeneratedKeys="true"
25
+            keyProperty="age"
26
+            parameterType="com.pojo.entity.UserEntity">
27
+        insert into user(name,age,address) value(#{name},#{age},#{address})
28
+    </insert>
29
+</mapper>

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

@@ -36,6 +36,7 @@
36 36
         <mapper class="com.mapper.AccountMapper"></mapper>
37 37
         <mapper class="com.mapper.UserMapper"></mapper>
38 38
         <mapper class="com.mapper.DepartmentMapper"></mapper>
39
+        <mapper class="com.mapper.SqlMapper"></mapper>
39 40
 <!--        <package name="com.mapper"/>-->
40 41
     </mappers>
41 42
 

+ 57 - 0
mybatis_train/src/test/java/TestSqlMapper.java

@@ -0,0 +1,57 @@
1
+import com.mapper.SqlMapper;
2
+import com.mapper.UserMapper;
3
+import com.pojo.entity.UserEntity;
4
+import org.apache.ibatis.io.Resources;
5
+import org.apache.ibatis.session.SqlSession;
6
+import org.apache.ibatis.session.SqlSessionFactory;
7
+import org.apache.ibatis.session.SqlSessionFactoryBuilder;
8
+import org.junit.Before;
9
+import org.junit.Test;
10
+
11
+import java.io.IOException;
12
+import java.io.InputStream;
13
+import java.util.Arrays;
14
+import java.util.HashMap;
15
+import java.util.List;
16
+import java.util.Map;
17
+
18
+public class TestSqlMapper {
19
+    SqlMapper sqlMapper;
20
+    @Before
21
+    public void before() throws IOException {
22
+        InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
23
+        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
24
+        SqlSession sqlSession = build.openSession(true);
25
+        sqlMapper = sqlSession.getMapper(SqlMapper.class);
26
+    }
27
+
28
+    /**
29
+     * 模糊查询
30
+     */
31
+    @Test
32
+    public void testLike() {
33
+        List<UserEntity> na = sqlMapper.getUserByLike("na");
34
+    }
35
+    @Test
36
+    public void testList() {
37
+        List<Integer> integers = Arrays.asList(1, 2);
38
+        List<UserEntity> na = sqlMapper.getUserByList(integers);
39
+    }
40
+    @Test
41
+    public void testTableName() {
42
+        List<UserEntity> na = sqlMapper.getUserByTableName("user");
43
+    }
44
+
45
+    /**
46
+     * 插入获取自增主键
47
+     */
48
+    @Test
49
+    public void addUserGeneratedKey() {
50
+        UserEntity userEntity = new UserEntity();
51
+        userEntity.setName("张贤胜");
52
+        userEntity.setAge(27);
53
+        userEntity.setAddress("地址");
54
+        int i = sqlMapper.insertGeneratedKey(userEntity);
55
+
56
+    }
57
+}