MyBatis实现CRUD

2020-05-17  本文已影响0人  凡哥爱丽姐

我们在上一节已经介绍过MyBatis实现用户的新增,下面我们将介绍对users数据库的其它操作。

1、实现用户数据的删除操作

    1.1、UserDao接口类中用户的删除方法

    //删除用户
    public int deleteUser(int id);

    1.2、配置UserMapper.xml文件

    <!--根据id值删除用户-->
    <delete id="deleteUser">
        delete from users where id=#{id}
    </delete>

    1.3、删除操作的测试类

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

public class Demo2 {
    public static void main(String[] args) {
        Reader resourceAsReader=null;
        SqlSession sqlSession=null;
        try {
            resourceAsReader = Resources.getResourceAsReader("mybatis-config.xml");
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory factory = builder.build(resourceAsReader);
            sqlSession = factory.openSession();
            //删除id为1的用户
            int i = sqlSession.delete("com.fan.dao.UserDao.deleteUser",1);
            sqlSession.commit();
            System.out.println("删除成功"+i+"条");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(sqlSession!=null){
                sqlSession.close();
            }
            if(resourceAsReader!=null){
                try {
                    resourceAsReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

2、实现用户数据的更新操作

    2.1、UserDao接口类中用户的更新方法

    //更新用户
    public int updateUser(User user);

    2.2、配置UserMapper.xml文件

    <!--根据id更新用户信息-->
    <update id="updateUser">
        update users set name=#{name},sex=#{sex} where id=#{id}
    </update>   

    2.3、更新操作的测试类

import com.fan.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class Demo3 {
    public static void main(String[] args) {
        Reader resourceAsReader=null;
        SqlSession sqlSession=null;
        User user=new User(1,"张三","女");
        try {

            resourceAsReader = Resources.getResourceAsReader("mybatis-config.xml");
            sqlSession = new SqlSessionFactoryBuilder().build(resourceAsReader).openSession();
            //更新id号为1的用户信息
            int i = sqlSession.update("com.fan.dao.UserDao.updateUser", user);
            sqlSession.commit();
            System.out.println("更新成功"+i+"条");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(sqlSession!=null){
                sqlSession.close();
            }
            if(resourceAsReader!=null){
                try {
                    resourceAsReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

3、根据用户id查询用户

    3.1、UserDao接口类中用户的查询方法

    //主键查询(根据用户id查询用户)
    public User findById(int id);

    3.2、配置UserMapper.xml文件

    <!--根据id查询用户信息-->
    <select id="findById" resultType="com.fan.entity.User">
        select * from users where id=#{id}
    </select>

    3.3、id查询操作的测试类

import com.fan.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class Demo4 {
    public static void main(String[] args) {
        Reader resourceAsReader=null;
        SqlSession sqlSession=null;
        try {
            resourceAsReader = Resources.getResourceAsReader("mybatis-config.xml");
            sqlSession = new SqlSessionFactoryBuilder().build(resourceAsReader).openSession();
            //查询id为1的用户
            User user = sqlSession.selectOne("com.fan.dao.UserDao.findById", 1);
            System.out.println(user.getName()+"..."+user.getSex());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(sqlSession!=null){
                sqlSession.close();
            }
            if(resourceAsReader!=null){
                try {
                    resourceAsReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

4、查询数据库中所有的用户

    4.1、UserDao接口类中所有用户信息查询方法

    //多行查询(查询users数据库中所有用户信息)
    public List<User> findAll();

    4.2、配置UserMapper.xml文件

    <!--查询所有用户信息-->
    <select id="findAll" resultType="com.fan.entity.User">
        select * from users
    </select>

    4.3、所有用户查询操作的测试类

import com.fan.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

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

public class Demo5 {
    public static void main(String[] args) {
        Reader resourceAsReader=null;
        SqlSession sqlSession=null;
        try {
            resourceAsReader = Resources.getResourceAsReader("mybatis-config.xml");
            sqlSession = new SqlSessionFactoryBuilder().build(resourceAsReader).openSession();
            //查询数据库中所有用户信息
            List<User> userList = sqlSession.selectList("com.fan.dao.UserDao.findAll");
            for (User users:userList) {
                System.out.println(users.getName()+"..."+users.getSex());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(sqlSession!=null){
                sqlSession.close();
            }
            if(resourceAsReader!=null){
                try {
                    resourceAsReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

5、条件查询(根据name和sex查询用户)

    5.1、UserDao接口类中条件查询方法

    //条件查询(根据name和sex查询)
    public User login(Map map); 

    5.2、配置UserMapper.xml文件

    <!--条件查询(根据name和sex查询)-->
    <select id="login" resultType="com.fan.entity.User">
        select * from users where name=#{username} and sex=#{usersex}
    </select>  

    5.3、条件查询操作的测试类

import com.fan.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo6 {
    public static void main(String[] args) {
        Reader resourceAsReader=null;
        SqlSession sqlSession=null;
        Map map=new HashMap();
        map.put("username","张三");
        map.put("usersex","女");
        try {
            resourceAsReader = Resources.getResourceAsReader("mybatis-config.xml");
            sqlSession = new SqlSessionFactoryBuilder().build(resourceAsReader).openSession();
            //条件查询(根据name和sex查询)
            User user= sqlSession.selectOne("com.fan.dao.UserDao.login",map);
            System.out.println(user.getId());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(sqlSession!=null){
                sqlSession.close();
            }
            if(resourceAsReader!=null){
                try {
                    resourceAsReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

6、修改测试类Demo6,调用getMapper(Mapper.class)执行Mapper接口方法来实现条件查询,关键代码如下:

            //条件查询(根据name和sex查询)
            //User user= sqlSession.selectOne("com.fan.dao.UserDao.login",map);
            UserDao userDaoMapper = sqlSession.getMapper(UserDao.class);
            User loginuser = userDaoMapper.login(map);
            System.out.println(loginuser.getId());
注:这种方式是MyBatis官方推荐使用的,其表达方式也更加直白,代码更加清晰,类型更加安全,也不用担心易错的字符串字面值以及强制类型的转换。
上一篇下一篇

猜你喜欢

热点阅读