系列十二、Spring5整合mybatis(传统方式)

一、业务代码

1.1、pom


    
    
      mysql
      mysql-connector-java
      8.0.26
    
    
    
    
    
      org.springframework
      spring-jdbc
      5.3.27
    
    
      org.mybatis
      mybatis
      3.5.11
    
    
      org.mybatis
      mybatis-spring
      2.1.0
    



    
      org.springframework
      spring-beans
      5.2.5.RELEASE
    
    
      org.springframework
      spring-context
      5.2.5.RELEASE
    
    
      org.springframework
      spring-core
      5.2.5.RELEASE
    
    
      org.springframework
      spring-expression
      5.2.5.RELEASE
    


    
      commons-logging
      commons-logging
      1.1.1
    
    
      com.alibaba
      druid
      1.2.16
    
    
      junit
      junit
      4.13.2
    
    
      com.alibaba
      fastjson
      1.2.76
    

    
    
      org.projectlombok
      lombok
      1.18.22
    
    
      org.slf4j
      slf4j-api
      1.7.32
    
    
      ch.qos.logback
      logback-classic
      1.2.10
    

1.2、UserDO

package org.star.entity.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Author:
 * @Date: 2023/8/28 16:51
 * @Description:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserDO implements Serializable {
    /**
     * 编号
     */
    private Long id;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 手机号码
     */
    private String mobile;

}

1.3、UserMapper

package org.star.mapper;

import org.star.entity.model.UserDO;

import java.util.List;

/**
 * @Author:
 * @Date: 2023/8/28 16:53
 * @Description:
 */
public interface UserMapper {

    /**
     * 查询所有用户信息
     * @return
     */
    List listAllUser();

}

1.4、UserMapper.xml





    

1.5、UserService

package org.star.service;

import org.star.entity.model.UserDO;

import java.util.List;

/**
 * @Author: 
 * @Date: 2023/8/28 17:41
 * @Description:
 */
public interface UserService {

    /**
     * 查询所有用户信息
     * @return
     */
    List listAllUser();

}

1.6、UserServiceImpl

package org.star.service.impl;

import org.springframework.stereotype.Service;
import org.star.entity.model.UserDO;
import org.star.mapper.UserMapper;
import org.star.service.UserService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 
 * @Date: 2023/8/28 17:41
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public List listAllUser() {
        return userMapper.listAllUser();
    }
}

二、配置信息

2.1、jdbc.properties

db.driverClass=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/20230828_spring5?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT
db.username=root
db.password=123456

2.2、mybatis-config.xml





    
        
    

    
        
    

    
    
        
    

2.3、applicationContext.xml




    
    

    
    
        
        
        
        
    


    
    
        
        
        
    

    
    
        
        
    

三、测试

3.1、AppTest

package org.star;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.star.entity.model.UserDO;
import org.star.mapper.UserMapper;
import org.star.service.UserService;
import org.star.service.impl.UserServiceImpl;

import java.util.List;

/**
 * Unit test for simple App.
 */
public class AppTest {

    private ClassPathXmlApplicationContext context = null;

    @Before
    public void initApplicationContext() {
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void listAllUserTest() {
        // userMapperTest();
        userServiceTest();
    }

    private void userServiceTest() {
        UserService userService = context.getBean(UserServiceImpl.class);
        List userDOS = userService.listAllUser();
        System.out.println("userService userDOS = " + userDOS);
    }

    private void userMapperTest() {
        UserMapper userMapper = context.getBean(UserMapper.class);
        List userDOS = userMapper.listAllUser();
        System.out.println("userMapper userDOS = " + userDOS);
    }


}

你可能感兴趣的:(Spring5系列,mybatis,xml)