栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis学习笔记

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis学习笔记

package Test;

import Test.pojo.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 java.io.IOException;
import java.io.InputStream;
import java.util.List;


public class MybatisDemo {
    public static void main(String[] args) throws IOException {
//        1、加载Mybatis的核心配置文件,获取SqlSessionFactory;
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

//        2、获取SqlSession对象,用它来执行sql;
        SqlSession sqlSession = sqlSessionFactory.openSession();

//        3、执行Sql语句
        List users = sqlSession.selectList("test.selectAll");

        System.out.println(users);

//        4、释放资源
        sqlSession.close();
    }
}

package Test;

import Test.mapper.AccountMapper;
import Test.pojo.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 java.io.IOException;
import java.io.InputStream;
import java.util.List;


public class MybatisDemo2 {
    public static void main(String[] args) throws IOException {
//        1、加载Mybatis的核心配置文件,获取SqlSessionFactory;
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

//        2、获取SqlSession对象,用它来执行sql;
        SqlSession sqlSession = sqlSessionFactory.openSession();

//        3、执行Sql语句
//        List users = sqlSession.selectList("test.selectAll");

//        3、1 获取 UserMapper 接口的代理对象
        AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
        List users = accountMapper.selectAll();

        System.out.println(users);

//        4、释放资源
        sqlSession.close();
    }
}

 













    

        
        
    
    
    
        select *
        from tb_brand where id = #{id};
    


    









package test.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import test.pojo.Brand;

import java.util.List;
import java.util.Map;


public interface BrandMapper {

    
    List selectAll();

    
    @Select(value = "SELECT * FROM tb_brand WHERe id = #{id}")
    Brand selectById(@Param("id") int id);

    
    List selectByCondition(@Param("status") int status, @Param("companyName") String companyName, @Param("brandName") String brandName);

    
    List selectByCondition(Brand brand);

    
    List selectByCondition(Map map);

    
    List selectByConditionSingle(Brand brand);

    
    void add(Brand brand);

    
    int update(Brand brand);

    
    void deleteById(Integer id);

    
    void deleteByIds(@Param("ids")int[] ids);
}













    

        
        
    


    
        select *
        from tb_brand
        
            
                and status = #{status}
            
            
                and company_name like #{companyName}
            
            
                and brand_name like #{brandName}