mybatis基础的crud

查看数据select

详情见 mybatis初识里面的例子

package com.maj.dao;

import com.maj.domain.Account;
import java.util.List;

// 接口操作account表
public interface AccountDao {

    // 查询account表中所有数据
    public abstract List<Account> showAccount();

}
<mapper namespace="com.maj.dao.AccountDao">
    <select id="showAccount" resultType="com.maj.domain.Account">
        select id,name,money from account order by id;
  </select>
</mapper>
package com.maj;

import com.maj.domain.Account;
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 MyBatisTest {
    @Test
    public void test1() throws IOException {
        // 1.定义mybatis主配置文件名称,从类路径的根开始(classes/后面)
        String config = "mybatis.xml";
        // 2.读取配置文件
        InputStream in = Resources.getResourceAsStream(config);
        // 3.创建SqlSessionFactory对象,目的是获取SqlSession
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        // 4.获取SqlSession(因为SqlSession能够执行SQL语句)***
        SqlSession session = factory.openSession();
        // 5.执行SqlSession的selectList()  ***
        // 参数是 映射文件的namespace值 + "." + ql语句的id
        List<Account> accountList = session.selectList("com.maj.dao.AccountDao.showAccount");
        // 6.输出结果
        accountList.forEach(System.out::println);
        // 7.释放资源
        session.close();
    }
}

增加数据insert

package com.maj.dao;

import com.maj.domain.Account;

import java.util.List;

// 接口操作account表
public interface AccountDao {

    // 插入操作
    /*
    * 参数:account:需要插入的数据
    * 返回值int:表示insert操作,影响到的数据行数
    * */
    public int insertAccount(Account account);
}

<mapper namespace="com.maj.dao.AccountDao">
	<insert id="insertAccount">  <!--这里的 #{实体类中属性} 是占位符-->
        insert into account values (#{id},#{name},#{money});
    </insert>
</mapper>
	@Test
    public void test2() throws IOException {
        String config = "mybatis.xml";
        InputStream in = Resources.getResourceAsStream(config);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

//        SqlSession session = factory.openSession(true);  // 参数autoCommit设置为true,即自动提交事务
        SqlSession session = factory.openSession();  // 默认不自动提交事务
        Account account = new Account(7,"张三",666.0D);
        // 插入数据
        int nums = session.insert("com.maj.dao.AccountDao.insertAccount",account);
        session.commit();  // 手动提交事务
        System.out.println("影响数据的行数:" + nums);
        session.close();

    }

更新数据update

package com.maj.dao;

import com.maj.domain.Account;

import java.util.List;

// 接口操作account表
public interface AccountDao {

    // 更新操作
    public int updateAccount(Account account);
}
<mapper namespace="com.maj.dao.AccountDao">
    <update id="updateAccount">
        update account set name = #{name} where id = #{id};
    </update>
</mapper>
    @Test
    public void test3() throws IOException {
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = factory.openSession();

        Account account = new Account();
        account.setId(7);
        account.setName("何西西");

        int nums = session.update("com.maj.dao.AccountDao.updateAccount", account);
        session.commit();
        System.out.println("更新的数据行数:" + nums);
        session.close();
    }

删除操作delete

package com.maj.dao;

import com.maj.domain.Account;

import java.util.List;

// 接口操作account表
public interface AccountDao {

     // 删除操作
//    public int deleteAccount(int id);
    public int deleteAccount(Account account);
}
<mapper namespace="com.maj.dao.AccountDao">
    <delete id="deleteAccount">
        delete from account where id = #{id};
    </delete>
</mapper>
    @Test
    public void test4() throws IOException {
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = factory.openSession();

//        int id = 7;
        Account account = new Account();
        account.setId(7);

//        int nums = session.delete("com.maj.dao.AccountDao.deleteAccount", id);
        int nums = session.delete("com.maj.dao.AccountDao.deleteAccount", account);
        session.commit();
        System.out.println("删除数据的行数:" + nums);
        session.close();
    }

mybatis工具类封装

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

public class MyBatisUtil {
    private static SqlSessionFactory factory = null;

    static {
        try {
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            factory = new SqlSessionFactoryBuilder().build(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession(){
        SqlSession session = null;
        if(factory!=null){
            session = factory.openSession();
        }
        return session;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值