Mybatis------代理开发

Mybatis------代理开发_第1张图片

什么是代理开发呢。

传统方式实现 DAO 层,需要写接口和实现类。采用 Mybatis 的代理开发方式实现 DAO 层的开发,只需要编写 Mapper 接口(相当于 Dao 接口),由 Mybatis 框架根据接口定义创建接口的动态代理对象

Mybatis------代理开发_第2张图片

第一个是对接数据库的,比如我的数据库有一个表是id + name。

package com.xuexi.domain;

import java.io.Serializable;

public class User implements Serializable{
    private int id;
    private String name;

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "id: "+ id+" name:" + name;
    }
}

Mybatis------代理开发_第3张图片

第二个就是mapper接口了。只需要编写 Mapper 接口(相当于 Dao 接口)

package com.xuexi.Mapper;

import com.xuexi.domain.User;

import java.util.List;

public interface UserMapper {
    List selectedById(int id);

    public Integer saverUser(User user);

    public  Integer deleteUser(int id);


}

Mybatis------代理开发_第4张图片

里面有查询,增加,和删除。 

第三个很简单啦,Mybatis配置文件。



    
        
        
    
    
        
            
            
                
                
                
                
            
        
    
    
        
    

Mybatis------代理开发_第5张图片

第四个写mysql的地方





    
    


    

    
        insert into users (id,name) values(#{id},#{name});

    

    
        delete from users where id = #{id};
    


Mybatis------代理开发_第6张图片

第五个不用说了吧,写测试的地方

import com.xuexi.Mapper.UserMapper;
import com.xuexi.domain.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.Test;

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

public class Mytest{

    static SqlSession sqlSession;


    static {

        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象,用它来执行sql
        sqlSession = sqlSessionFactory.openSession();

    }


    @Test
    public void test() throws Exception
    {
        //获取路径
        //3. 执行sql
        //List users = sqlSession.selectList("test.Querry");
        //代理开发
        UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
        //UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List users = usermapper.selectedById(1);
        System.out.println(users);
        //4. 释放资源
        sqlSession.close();

    }

    @Test
    public void inserttest() throws Exception
    {
        UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
        //UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = new User();
        user1.setId(7);
        user1.setName("小青龙");
        Integer success =  usermapper.saverUser(user1);
        sqlSession.commit();
        if(success <=0)
        {
            System.out.println("失败了!");
        }
        else
        {
            System.out.println("成功了!");
        }
        //4. 释放资源
        sqlSession.close();
    }

    @Test
    public void deletetest() throws Exception
    {
        UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
        //UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Integer success =  usermapper.deleteUser(7);
        sqlSession.commit();
        if(success <=0)
        {
            System.out.println("失败了!");
        }
        else
        {
            System.out.println("成功了!");
        }
        //4. 释放资源
        sqlSession.close();
    }

}

你可能感兴趣的:(java,java,mybatis)