三:Mapper接口方式的mybatis的增,删,改,查实现

4.1、Mapper接口编程的命名习惯Mapper接口方式的编程,需要先有一个接口。这个接口的命名一般是xxxxMapper。
比如:
User模块的Mapper,接口命名为UserMapper。
Book模块的Mapper,接口命名为BookMapper。
4.2、Mapper接口开发有四个开发规范必须遵守

1. 对应的mapper配置文件的namespace属性值必须是Mapper接口的全类名。
2. Mapper接口中的方法名必须与mapper配置文件中对应的id值相同。
3. Mapper接口的方法的参数类型必须与mapper配置文件中配置的parameterType类型匹配上
4. Mapper接口的方法返回值类型必须与mapper配置文件中配置的resultType 类型匹配上

4.3、编写Mapper接口

package com.atguigu.mapper;

import java.util.List;

import com.atguigu.pojo.User;

public interface UserMapper {

// 保存用户

public int saveUser(User user);

// 更新用户

public int updateUser(User user);

// 根据id删除用户

public int deleteUserById(int id);

// 根据id搜索用户

public User findUserById(int id);

// 搜索全部用户

public List findUsers();

}
4.4、修改原来UserMapper.xml配置文件
修改原来UserMapper.xml配置文件的namespace属性值为刚创建的UserMapper的全类名
4.5、编写UserMapper测试
package com.atguigu.mapper;

import java.io.InputStream;

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.BeforeClass;

import org.junit.Test;

import com.atguigu.pojo.User;

public class UserMapperTest {

static SqlSessionFactory sqlSessionFactory;

@BeforeClass

public static void setUpBeforeClass() throws Exception {

   String url = "mybatis-config.xml";

   // 读取配置文件

   InputStream inputStream = Resources.getResourceAsStream(url);

   // 创建SqlSessionFactory对象

   sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

}

@Test

public void testSaveUser() {

   SqlSession session = sqlSessionFactory.openSession();

   try {

       UserMapper userMapper = session.getMapper(UserMapper.class);

       User user = new User(0, "ddddd", 1);

       userMapper.saveUser(user);

       session.commit();

       System.out.println(user);

   } finally {

       session.close();

   }

}

@Test

public void testUpdateUser() {

   SqlSession session = sqlSessionFactory.openSession();

   try {

       UserMapper userMapper = session.getMapper(UserMapper.class);

       User user = new User(5, "eeeee", 1);

       userMapper.updateUser(user);

       session.commit();

   } finally {

       session.close();

   }

}

@Test

public void testDeleteUserById() {

   SqlSession session = sqlSessionFactory.openSession();

   try {

       UserMapper userMapper = session.getMapper(UserMapper.class);

       userMapper.deleteUserById(5);

       session.commit();

   } finally {

       session.close();

   }

}

@Test

public void testFindUserById() {

   SqlSession session = sqlSessionFactory.openSession();

   try {

       UserMapper userMapper = session.getMapper(UserMapper.class);

       System.out.println(userMapper.findUserById(7));

   } finally {

       session.close();

   }

}

@Test

public void testFindUsers() {

   SqlSession session = sqlSessionFactory.openSession();

   try {

       UserMapper userMapper = session.getMapper(UserMapper.class);

       System.out.println(userMapper.findUsers());

   } finally {

       session.close();

   }

}
}

你可能感兴趣的:(MyBatis)