欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MyBatis(12)- mybatis实现 DAO开发

程序员文章站 2022-06-17 15:28:21
...

1 查询列表

1.1 DAO

  • 接口
package com.tzb.dao;
import com.tzb.domain.User;
import java.util.List;

public interface IUserDao {
    List<User> findAll();

    void saveUser(User user);

    void updateUser(User user);

    void deleteUser(Integer userId);

    User findById(Integer userId);

    // 模糊查询
    List<User> findbyName(String username);

    /**
     * 查询总的用户数
     * @return
     */
    int findTotal();


}
  • IUserDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.tzb.dao.IUserDao">

    <!--配置查询所有-->
    <select id="findAll" resultType="com.tzb.domain.User">
        select * from user
    </select>

    <insert id="saveUser" parameterType="com.tzb.domain.User">
        <!--配置插入数据后,获取插入数据的 id-->
        <selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
            select last_insert_id();
        </selectKey>
        insert into user(username,address,sex,birthday)
        values (#{username},#{address},#{sex},#{birthday})
    </insert>

    <update id="updateUser" parameterType="com.tzb.domain.User">

        update user
        set
        username=#{username},
        address=#{address},
        sex=#{sex},
        birthday=#{birthday}
        where id=#{id}

    </update>

    <delete id="deleteUser" parameterType="java.lang.Integer">
        delete from user where id=#{userId}
    </delete>

    <select id="findById" parameterType="int" resultType="com.tzb.domain.User">
        select * from user where id=#{userId}
    </select>

    <select id="findbyName" parameterType="String" resultType="com.tzb.domain.User">
       <!-- select * from user where username like #{username}-->
       select * from user where username like '%${value}%'
    </select>

    <select id="findTotal" resultType="int">
        select count(id) from user;
    </select>


</mapper>


  • 实现类
package com.tzb.dao.impl;

import com.tzb.dao.IUserDao;
import com.tzb.domain.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class UserDaoImpl implements IUserDao {

    private SqlSessionFactory factory;

    public UserDaoImpl(SqlSessionFactory factory) {
        this.factory = factory;
    }

    @Override
    public List<User> findAll() {
        //1.根据 factory 获取 sqlSession 对象
        SqlSession session = factory.openSession();
        //2.调用SqlSession方法实现查询列表
        // 参数就是获取配置信息的key,配置文件的 namespace + id
        List<User> users = session.selectList("com.tzb.dao.IUserDao.findAll");
        // 3.释放资源
        session.close();
        return users;
    }

    @Override
    public void saveUser(User user) {

    }

    @Override
    public void updateUser(User user) {

    }

    @Override
    public void deleteUser(Integer userId) {

    }

    @Override
    public User findById(Integer userId) {
        return null;
    }

    @Override
    public List<User> findbyName(String username) {
        return null;
    }

    @Override
    public int findTotal() {
        return 0;
    }
}


1.2 单元测试

package com.tzb.test;

import com.tzb.dao.IUserDao;
import com.tzb.dao.impl.UserDaoImpl;
import com.tzb.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.After;
import org.junit.Before;
import org.junit.Test;

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

public class MyBatisTest2 {

    private InputStream in;
    private IUserDao userDao;

    @Before
    public void init() throws IOException {
        // 1.读取配置文件
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建 SqlSessionFactory 工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        // 3.使用工厂对象创建 到对象
        userDao = new UserDaoImpl(factory);
    }

    @After
    public void destory() throws IOException {
        in.close();
    }

    @Test
    public void testFindAll() throws IOException {
        List<User> users = userDao.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }

}

2 保存操作

2.1 DAO实现类

 @Override
    public void saveUser(User user) {
        SqlSession session = factory.openSession();
        session.insert("com.tzb.dao.IUserDao.saveUser",user);
        // 提交事务
        session.commit();
        session.close();
    }


单元测试

   @Test
    public void testSave() throws IOException {
        User user = new User();
        user.setUsername("钢铁侠");
        user.setAddress("US");
        user.setSex("男");
        user.setBirthday(new Date());

        System.out.println("保存前:"+user);
        userDao.saveUser(user);
        System.out.println("保存后:"+user);
    }

MyBatis(12)- mybatis实现 DAO开发
MyBatis(12)- mybatis实现 DAO开发

2.2 执行过程分析

MyBatis(12)- mybatis实现 DAO开发
MyBatis(12)- mybatis实现 DAO开发
MyBatis(12)- mybatis实现 DAO开发
MyBatis(12)- mybatis实现 DAO开发
MyBatis(12)- mybatis实现 DAO开发
MyBatis(12)- mybatis实现 DAO开发
MyBatis(12)- mybatis实现 DAO开发
MyBatis(12)- mybatis实现 DAO开发
MyBatis(12)- mybatis实现 DAO开发
MyBatis(12)- mybatis实现 DAO开发
MyBatis(12)- mybatis实现 DAO开发
MyBatis(12)- mybatis实现 DAO开发
MyBatis(12)- mybatis实现 DAO开发
MyBatis(12)- mybatis实现 DAO开发