MyBatis的动态SQL!!!

一.if和where的使用。

1.首先在UserMapper接口中声明一个方法:

package com.by.mapper;

import com.by.pojo.User;

import java.util.List;

/**
 * 

Project: mybatis - UserMapper

*

Powered by scl On 2023-12-22 15:52:05

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public interface UserMapper { /** * 动态SQL的练习 if/where * @return */ List findAllById(User user); }

2.在UserMapper.xml文件中实现这个接口:





    
    

3.测试类:

/*
 * Copyright (c) 2020, 2023,  All rights reserved.
 *
 */
package com.by;

import com.by.mapper.AccountMapper;
import com.by.mapper.RoleMapper;
import com.by.mapper.UserMapper;
import com.by.pojo.Account;
import com.by.pojo.Role;
import com.by.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


/**
 * 

Project: mybatis - MyBatisTest

*

Powered by scl On 2023-12-18 11:44:53

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public class MyBatisTestRole { private InputStream inputStream; private SqlSession sqlSession; @Before public void init() throws IOException { // 加载配置文件 String resource = "mybatis-config.xml"; inputStream = Resources.getResourceAsStream(resource); // 创建sqlSessionFActory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获得数据的绘画实例 sqlSession = sessionFactory.openSession(); } /** * 动态SQL if/where * * @throws IOException */ @Test public void findUserById() { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = new User(); user.setUsername("李星云"); user.setSex("男"); user.setId(42); List userList = userMapper.findAllById(user); for (User user1 : userList) { System.out.println(user1); } } @After public void close() throws IOException { inputStream.close(); sqlSession.close(); } }

4.结果展示:

二. set的使用

1.首先在UserMapper接口中声明一个方法:

package com.by.mapper;

import com.by.pojo.User;

import java.util.List;

/**
 * 

Project: mybatis - UserMapper

*

Powered by scl On 2023-12-22 15:52:05

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public interface UserMapper { /** * 动态SQL的练习 set */ void updateUser(User user); }

2.在UserMapper.xml文件中实现这个方法:




   
    
    
        update user
        
            
                username=#{username},
            
            
                sex=#{sex},
            
        
        where id=#{id}
    

3.实现类:

/*
 * Copyright (c) 2020, 2023,  All rights reserved.
 *
 */
package com.by;

import com.by.mapper.AccountMapper;
import com.by.mapper.RoleMapper;
import com.by.mapper.UserMapper;
import com.by.pojo.Account;
import com.by.pojo.Role;
import com.by.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


/**
 * 

Project: mybatis - MyBatisTest

*

Powered by scl On 2023-12-18 11:44:53

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public class MyBatisTestRole { private InputStream inputStream; private SqlSession sqlSession; @Before public void init() throws IOException { // 加载配置文件 String resource = "mybatis-config.xml"; inputStream = Resources.getResourceAsStream(resource); // 创建sqlSessionFActory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获得数据的绘画实例 sqlSession = sessionFactory.openSession(); } /** * 动态SQL set * * @throws IOException */ @Test public void updateUser() { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = new User(); user.setUsername("侯卿"); user.setSex("男"); user.setId(42); userMapper.updateUser(user); } @After public void close() throws IOException { inputStream.close(); sqlSession.close(); } }

4.结果展示:

三.trim的使用

1.首先在User Mapper接口中声明方法:

package com.by.mapper;

import com.by.pojo.User;

import java.util.List;

/**
 * 

Project: mybatis - UserMapper

*

Powered by scl On 2023-12-22 15:52:05

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public interface UserMapper { /** * 动态SQL练习 trim */ void insertUser(User user); }

2.在UserMapper.xml文件中实现这个方法:、




    
    
        insert into user
        
            
                username,
            
            
                sex,
            
            
                address,
            
            
                birthday,
            
        
        
            
                #{username},
            
            
                #{sex},
            
            
               #{address},
            
            
                #{birthday},
            
        
    


3.实现类:

/*
 * Copyright (c) 2020, 2023,  All rights reserved.
 *
 */
package com.by;

import com.by.mapper.AccountMapper;
import com.by.mapper.RoleMapper;
import com.by.mapper.UserMapper;
import com.by.pojo.Account;
import com.by.pojo.Role;
import com.by.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


/**
 * 

Project: mybatis - MyBatisTest

*

Powered by scl On 2023-12-18 11:44:53

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public class MyBatisTestRole { private InputStream inputStream; private SqlSession sqlSession; @Before public void init() throws IOException { // 加载配置文件 String resource = "mybatis-config.xml"; inputStream = Resources.getResourceAsStream(resource); // 创建sqlSessionFActory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获得数据的绘画实例 sqlSession = sessionFactory.openSession(); } /** * 动态SQL trim * * @throws IOException */ @Test public void insertUser() { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = new User(); user.setUsername("侯卿"); user.setSex("男"); userMapper.insertUser(user); } @After public void close() throws IOException { inputStream.close(); sqlSession.close(); } }

4.结果展示:

四.foreach的使用

1.UserMapper接口中定义方法:


    /**
     * 动态SQL练习 foreach
     */
    void deleteUserByIds(@Param("arrIds") Integer[] arrIds);

2.UserMapper.xml文件中实现方法:

 
    
        delete from user where id in
        
            #{id}
        
    

3.测试类:

/**
     * 动态SQL foreach
     *
     * @throws IOException
     */
    @Test
    public void deleteUserByIds() {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //List list=new ArrayList<>();
        //list.add(41);
        //list.add(42);
        //list.add(43);
        Integer[] integers = new Integer[]{41,42,43};
        userMapper.deleteUserByIds(integers);
    }

你可能感兴趣的:(mybatis,sql,java,单元测试,intellij-idea,maven,log4j)