MyBatis通过接口代理的方式实现dao

  在入门案例中,我们使用传统的方法对数据进行增删改查,但在实际的应用中,我们一般会在数据处理层对数据进行增删改查等操作,传统的数据处理层需要有接口、接口的实现类以及测试类,这样有点麻烦,所以我们来使用接口代理的方式实现对数据的处理。

第一步:创建User类

package demo01;

import java.util.Date;

public class User {
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;

    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address="
                + address + "]";
    }

}

第二步:创建接口

package demo02;

import demo01.User;

public interface UserMapperInterface {
    //查询数据
    User findUser(Integer id);
    //插入数据
    void addUser(User user);
    //更新数据
    void updateUser(User user);
    //删除数据
    void deleteUser(Integer id);
}

第三步:创建UserMapperInterface.xml配置文件

注意:

  • namespace的属性要是接口的全限定名称
  • 接口名称与我们的mapper文件名称一致
  • 接口位置要和我们xml文件位置保持一致
  • 接口中的方法名就是我们mapper文件中的sql语句的id



    
    
    
        insert into user (username,sex,birthday,address) values (#{username},#{sex},#{birthday},#{address})
    
    
    
        update user set address = #{address} where id = #{id}
    

    
        delete from user where id = #{id}
    

将mapper文件配置到SqlMapConfig.xml文件中

配置文件

第四步:创建测试类

package demo02;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.Date;

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.Before;
import org.junit.Test;

import demo01.User;

public class UserMapperInterfaceTest {
    SqlSession sqlSession = null;
    
//  public UserMapperInterfaceTest(SqlSession sqlSession){
//      this.sqlSession = sqlSession;
//  }
//  
    /**
     * 获得sqlSession
     * @throws IOException
     */
    @Before
    public void getSession() throws IOException{
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = builder.build(Resources.getResourceAsStream("SqlMapConfig.xml"));
        sqlSession = build.openSession();
    }
    
    /**
     * 查询数据
     */
    @Test
    public void selectUserById(){
        UserMapperInterface mapper = sqlSession.getMapper(UserMapperInterface.class);
        User user = mapper.findUser(16);
        System.out.println(user.toString());
        sqlSession.close();
    }
    
    /**
     * 插入数据
     */
    @Test
    public void insert() throws Exception {
        User user = new User();
        user.setUsername("赵六");
        user.setSex("男");
        user.setBirthday(new Date());
        user.setAddress("上海");
        UserMapperInterface mapper = sqlSession.getMapper(UserMapperInterface.class);
        mapper.addUser(user);
        sqlSession.commit();
        sqlSession.close();
    }
    
    /**
     * 更新数据
     */
    @Test
    public void update() throws Exception {
        UserMapperInterface mapper = sqlSession.getMapper(UserMapperInterface.class);
        User user = mapper.findUser(29);
        user.setAddress("河南郑州");
        mapper.updateUser(user);
        sqlSession.commit();
        sqlSession.close();
    }
    
    /**
     * 删除数据
     */
    @Test
    public void delete() throws Exception {
        UserMapperInterface mapper = sqlSession.getMapper(UserMapperInterface.class);
        mapper.deleteUser(30);
        sqlSession.commit();
        sqlSession.close();
    }
    
}

你可能感兴趣的:(MyBatis通过接口代理的方式实现dao)