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=""
    <!--Annotation scan-->
    <context:property-placeholder location=""/>
    <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 id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:SqlMapConfig.xml"/>
        <property name="dataSource" ref="dataSource"/>

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

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 "-// Config 3.0//EN"
        <package name="com.pp.pojo"/>

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

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 java.util.ArrayList;
import java.util.List;

public class Demo {
    private ApplicationContext applicationContext;

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

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

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

  1. selectByExample()
    public void fun1(){
        UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        List<User> list = userMapper.selectByExample(userExample);
  • 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
    public void fun2(){
        UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");
        UserExample userExample = new UserExample();
        int i = userMapper.countByExample(userExample);

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()
    public void fun3(){
        UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");
        int i = userMapper.deleteByPrimaryKey(28);

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

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

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()
    public void fun5(){
        UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");
        User user = new User();
        user.setUsername("Thankson ");

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

    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<User> list1 = userMapper.selectByExample(userExample);

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