mybatis基础的crud

Posted 小马Mark

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    


以上是关于mybatis基础的crud的主要内容,如果未能解决你的问题,请参考以下文章

3-使用MyBatis实现CRUD

JavaLearn#(26)MyBatis基础:认识框架MyBatis环境搭建基本CRUD配置文件日志管理别名属性文件ThreadLocal保存sqlSession本地DTD模板

JavaLearn#(26)MyBatis基础:认识框架MyBatis环境搭建基本CRUD配置文件日志管理别名属性文件ThreadLocal保存sqlSession本地DTD模板

MyBatis-----2.通过映射接口实现CRUD

Spring boot整合mybaties+thymeleaf实现基础crud

Mybatis02 快速入门Part2 补完CRUD