Mybatis integrated spring of mybatis learning 12 (Continued)

Keywords: Java Mybatis Spring Spring Boot

1. Will Mybatis reverse engineering of mybatis learning 11 Copy the com.pp.maper and com.pp.pojo files generated in to spring_ Under the src/main/java file of mybatis

2. Spring_ The mapper point in the applicationContext.xml file in mybatis is changed to com.pp.mapper point to the mapper abstract class under the java file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
">
    <!--Annotation scan-->
    <context:property-placeholder location="classpath:db.properties"/>
    <context:component-scan base-package="com.pp"/>

   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
       <property name="driverClassName" value="${jdbc.driver}" />
       <property name="url" value="${jdbc.url}" />
       <property name="username" value="${jdbc.username}" />
       <property name="password" value="${jdbc.password}" />
    </bean>
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:SqlMapConfig.xml"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <property name="basePackage" value="com.pp.dao"/>
    </bean>
</beans>

3. Spring_ The name in < mapper > in the SqlMapConfig.xml configuration file in mybatis is written as com.pp.mapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.pp.pojo"/>
    </typeAliases>

    <mappers>
        <package name="com.pp.dao"/>
    </mappers>
</configuration>

4. Write test methods in the test/java/Demo test class.

The UserMappe interface class and UerMapper.xml configuration file are created through reverse engineering, so the fun() method needs to be commented out and rewritten here.

  1. findUserByid()
import com.pp.dao.UserMapper;
import com.pp.pojo.User;
import com.pp.pojo.UserExample;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class Demo {
    private ApplicationContext applicationContext;

    @Test
    @Before
    public void setUP(){
        applicationContext=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    }
   /* @Test
    public void fun(){
        UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");
        User user = userMapper.findUserByid(1);
        System.out.println(user);
    }*/


   @Test
    public void fun(){
       UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");
       User user = userMapper.selectByPrimaryKey(1);  //Query id by primary key
       System.out.println(user);
   }
}

As shown in figure fun(), call selectByPrimaryKey(). Returns a value of type User.

  1. selectByExample()
//
    @Test
    public void fun1(){
        UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameLike("%Zhang%");
        criteria.andSexEqualTo("male");
        List<User> list = userMapper.selectByExample(userExample);
        System.out.println(list);
    }
  • This method needs to pass an Example inside, so before using this method again, you need to new a UaserExample(),
  • Then set the condition through userExample and use createCriteria() to get a criteria parameter. Then call the conditions under criteria.
  • Here, the andUsernameLike("% sheets%") under criteria is called to query those with sheets before and after. Later, the class criteria.andSexEqualTo("male") is called to query the male gender.
  • The whole combination is to query people who contain Zhang characters and whose gender is male.
  • Finally, use userMapper.selectByExample(userExample) to pass in userExample.
  1. countByExample
    @Test
    public void fun2(){
        UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");
        UserExample userExample = new UserExample();
        int i = userMapper.countByExample(userExample);
        System.out.println(i);
    }

countByExample() needs to pass a UserExample, so first create a new UserExample, and then use it as the parameter of i userMapper.countByExample(). This function means to return all rows of the user class.

  1. deleteByPrimaryKey()
 @Test
    public void fun3(){
        UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");
        int i = userMapper.deleteByPrimaryKey(28);
        System.out.println(i);
    }

userMapper.deleteByPrimaryKey() deletes the row with id 28, that is, deletes the row with id 28.

  1. updateByPrimaryKey()
  @Test
    public void fun4(){
        UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");
        User user = new User();
        user.setUsername("Wang Mazi");
        user.setId(1);
        userMapper.updateByPrimaryKey(user);
    }

userMapper.updateByPrimaryKey() updates through the primary key. This method needs to pass a user type value. Therefore, before this method, you need to first create a user, then assign values to some attributes of the user class, and finally pass the most parameters to the userMapper.updateByPrimaryKey() method. The whole function means to change the name of the data with id 1 to "Wang Mazi".

  1. insert()
 @Test
    public void fun5(){
        UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");
        User user = new User();
        user.setUsername("Thankson ");
        userMapper.insert(user);
    }

Does userMapper.insert() need an object of type Use, so here we first create an object of user, then assign "shawson" to username, and then pass user as a parameter to the userMapper.insert() parameter.
7. selectByExample

  @Test
    public void fun6(){
        UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        List<Integer> list = new ArrayList<Integer>();
        list.add(24);
        list.add(25);
        list.add(26);
        criteria.andIdIn(list);
        List<User> list1 = userMapper.selectByExample(userExample);
        System.out.println(list1);
    }

userMapper.selectByExample() needs a userExample instance, so choose to create a userExample instance, then use the userExample.createCriteria() method to define query criteria, and pass a list to this method. Use criteria.andInt() to add a method, and finally pass userExample as a parameter to userMapper.selectByExample() Therefore, this test class is used to input 24, 25 and 26, query these three IDS in the database, and put back the corresponding row data.

Posted by GreyBoy on Sat, 25 Sep 2021 16:54:50 -0700