SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

 

 SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

 

 SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

 

 SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

 

 SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

 

 SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

 

 SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

 SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

 

 

 

 1 package com.haifei.mapper;
 2 
 3 import com.haifei.domain.User;
 4 
 5 import java.util.List;
 6 
 7 public interface UserMapper {
 8 
 9     public List<User> findByCondition(User user);
10     public List<User> findByIds(List<Integer> ids);
11 
12 }
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 
 4 <mapper namespace="com.haifei.mapper.UserMapper">
 5 
 6     <!--<select id="findByCondition" parameterType="user" resultType="user">
 7         select * from user where id=#{id} and username=#{username} and password=#{password}
 8     </select>-->
 9 
10     <!--<select id="findByCondition" parameterType="user" resultType="user">
11         select * from user where 1=1
12         <if test="id!=0">
13             and id=#{id}
14         </if>
15         <if test="username!=null">
16             and username=#{username}
17         </if>
18         <if test="password!=null">
19             and password=#{password}
20         </if>
21     </select>-->
22     <!--
23         <where>标签可以动态添加查询条件
24         当没有条件时sql语句中就没有where,有哪个条件就where哪个条件
25         可以代替 "where 1=1"
26     -->
27     <select id="findByCondition" parameterType="user" resultType="user">
28         select * from user
29         <where>
30             <if test="id!=0">
31                 and id=#{id}
32             </if>
33             <if test="username!=null">
34                 and username=#{username}
35             </if>
36             <if test="password!=null">
37                 and password=#{password}
38             </if>
39         </where>
40     </select>
41 
42     <!--select * from user where id in(1,2,3)-->
43     <!--<foreach>标签中,当传入的参数是列表时collection值为list,当传入的是数组时collection值为array-->
44     <select id="findByIds" parameterType="list" resultType="user" >
45         select * from user
46         <where>
47             <foreach collection="list" open="id in(" close=")" item="id" separator=",">
48                 #{id}
49             </foreach>
50         </where>
51     </select>
52 
53 </mapper>

 

 

 1 package com.haifei.test;
 2 
 3 import com.haifei.domain.User;
 4 import com.haifei.mapper.UserMapper;
 5 import org.apache.ibatis.io.Resources;
 6 import org.apache.ibatis.session.SqlSession;
 7 import org.apache.ibatis.session.SqlSessionFactory;
 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 9 import org.junit.Test;
10 
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.util.ArrayList;
14 import java.util.List;
15 
16 public class MapperTest {
17 
18     @Test
19     public void test1() throws IOException {
20         InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
21         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
22         SqlSession sqlSession = sqlSessionFactory.openSession();
23 
24         UserMapper mapper = sqlSession.getMapper(UserMapper.class);
25 
26         //模拟查询条件
27         User condition = new User();
28         condition.setId(1);
29         condition.setUsername("zhangsan");
30         condition.setPassword("123");
31 
32         List<User> userList = mapper.findByCondition(condition);
33         System.out.println(userList); //[User{id=1, username=‘zhangsan‘, password=‘123‘}]
34     }
35 
36     @Test
37     public void test2() throws IOException {
38         InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
39         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
40         SqlSession sqlSession = sqlSessionFactory.openSession();
41 
42         UserMapper mapper = sqlSession.getMapper(UserMapper.class);
43 
44         //模拟查询条件
45         User condition = new User();
46         condition.setId(1);
47 //        condition.setUsername("zhangsan");
48 //        condition.setPassword("123");
49 
50         List<User> userList = mapper.findByCondition(condition);
51         System.out.println(userList); //[User{id=1, username=‘zhangsan‘, password=‘123‘}]
52     }
53 
54     @Test
55     public void test3() throws IOException {
56         InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
57         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
58         SqlSession sqlSession = sqlSessionFactory.openSession();
59 
60         UserMapper mapper = sqlSession.getMapper(UserMapper.class);
61 
62         //模拟ids数据
63         List<Integer> ids = new ArrayList<>();
64         ids.add(1);
65         ids.add(2);
66         ids.add(3);
67 
68         List<User> userList = mapper.findByIds(ids);
69         System.out.println(userList);
70     }
71 
72 }

SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

 

 SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

 

 SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

 

 SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

 

 SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

 

 SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

 

 SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

 

SSM13.1【Mybatis:Mybatis映射文件深入--动态sql语句】

上一篇:使用ODBC连接MySQL和EXCEL


下一篇:【Mysql】InnoDB 引擎中的页目录