【千锋逆战】MyBatis的xml+接口方式实现crud功能

Mybatis的xml+接口方式实现crud功能

  1. User.java
package com.qfedu.pojo;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {

    private int uid;
    private String username;
    private String password;
    private int age;
    private String addr;
}

2.IUserDao.java


import com.qfedu.pojo.User;

import java.util.List;

public interface IUserDao {

    /**
     *  查询所有的User对象
     * @return User对象的列表
     */
    List getAllUsers();

    /**
     * 新增一个User对象
     * @param u 要新增的用户对象
     * @return 受影响的行数
     */
    int saveUser(User u);
}

3.UserMapper.xml






    
    
    

    
        insert into user values(null, #{username}, #{password}, #{age}, #{addr})
    

    
        update user set username = #{username}, password =#{password}, age=#{age}, addr = #{addr} where uid = #{uid}
    

4.SessionUtils.java

package com.qfedu.util;

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 java.io.IOException;

public class SessionUtils {

    private static SqlSession mSession = null;
    private static SqlSessionFactory mFactory = null;

    static {
        try {
            mFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取SqlSession对象
     * @return
     */
    public static SqlSession getSession(){

        mSession = mFactory.openSession(true);

        return mSession;
    }

    /**
     * 关闭SqlSession对象
     * @param session 要关闭的SqlSession对象
     */
    public static void closeSession(SqlSession session){
        if(session != null){
            session.close();
            session = null;
        }
    }
}

5.Mybatis.xml







    
    
    

    
    
        
    

    
        
        
    

    
        
            
            
                
                
                
                
            
        
    
    
        
    

6.TestUser.java

package com.qfedu.pojo;

import org.junit.Test;

public class TestUser {

    @Test
    public void testUser(){

        User u = new User();

        u.setUid(1);
        u.setUsername("chenggang");
        u.setPassword("888888");
        u.setAddr("shenzhen");
        u.setAge(18);

        System.out.println(u);
    }
}

7.TestUserDao.java


import com.qfedu.dao.IUserDao;
import com.qfedu.util.SessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class TestUserDao {

    private SqlSession session = null;

    @Before
    public void setUp(){

        session = SessionUtils.getSession();
    }

    @After
    public void tearDown(){
        SessionUtils.closeSession(session);
    }

    @Test
    public void testGetAllUsers(){
        IUserDao mapper = session.getMapper(IUserDao.class);

        List users = mapper.getAllUsers();

        for (User u : users) {
            System.out.println(u);
        }
    }

    @Test
    public void testSaveUser(){
        IUserDao userDao = session.getMapper(IUserDao.class);

        User u = new User(17, "liuchang", "888888", 20, "guangzhou");
        int result = userDao.saveUser(u);
        System.out.println(result);
    }
}

你可能感兴趣的:(【千锋逆战】MyBatis的xml+接口方式实现crud功能)