mybatis入门

目录

一.mybatis入门及环境搭建:

目录

配置pom.xml文件

 建立jdbc.properties连接数据库

配置web.xml

安装插件

二.mybatis的增删改查:

   生成的代码

​编辑 

建立BookBiz

 建立BookBizimpl

建立工具类SessionUtil

编写demo


一.mybatis入门及环境搭建:

目录

mybatis入门_第1张图片

 

 

 

配置pom.xml文件




  4.0.0

  com.zking
  mybatis1
  1.0-SNAPSHOT
  war

  mybatis1 Maven Webapp
  
  http://www.example.com

  
    1.8
    1.8
  

  
    
    
      junit
      junit
      4.12
      test
    

    
    
      javax.servlet
      javax.servlet-api
      4.0.0
      provided
    

    
    
      org.mybatis
      mybatis
      3.4.5
    

    
    
      mysql
      mysql-connector-java
      5.1.44
    

    
    
    
    
    
      org.apache.logging.log4j
      log4j-core
      2.9.1
    
    
      org.apache.logging.log4j
      log4j-api
      2.9.1
    
    
    
      org.apache.logging.log4j
      log4j-web
      2.9.1
    
  

  
    mybatis1
    
      
      
        src/main/java
        
          **/*.xml
        
      
      
      
        src/main/resources
        
          jdbc.properties
          *.xml
        
      
    

    

      
        org.mybatis.generator
        mybatis-generator-maven-plugin
        1.3.2
        
          
          
            mysql
            mysql-connector-java
            5.1.44
          
        
        
          true
        
      


        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
  

 建立jdbc.properties连接数据库

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=UTF-8
jdbc.username=mybatis_ssm
jdbc.password=123456

配置web.xml


  Archetype Created Web Application

安装插件

点击file选中settings 安装Free mybatis

mybatis入门_第2张图片

 安装Mybatis generatormybatis入门_第3张图片

 安装mybatis tools

mybatis入门_第4张图片

安装maven helper 

mybatis入门_第5张图片 配置mybatis.cfg.xml




    
    

    
        
    

    
    
        
    


    
    
        
            
            

            
            
            
            
            
                
                
                
                
            
        
    

    
        
    

 配置generatorConfig.xml




    
    

    
    

    
    
        
        
            
             
        

        
        

        
        
            
            
        

        
        
        
        
            
            
            
            
            
            
            
            
        

        
        
            
            
        

        
        
        
        
        
            
            
        

        
        
        
        
        
        
        
        
        
        
        
        
        

        

二.mybatis的增删改查:

   生成的代码

mybatis入门_第6张图片

mybatis入门_第7张图片 

建立BookBiz

package com.zking.biz;

import com.zking.model.Book;

public interface BookBiz {
    int deleteByPrimaryKey(Integer bid);

    int insert(Book record);

    int insertSelective(Book record);

    Book selectByPrimaryKey(Integer bid);

    int updateByPrimaryKeySelective(Book record);

    int updateByPrimaryKey(Book record);
}

 建立BookBizimpl

        

package com.zking.biz.impl;

import com.zking.biz.BookBiz;
import com.zking.mapper.BookMapper;
import com.zking.model.Book;

/**
 * @author bing人
 * @site
 * @company xy集团
 * @create 2023-08-21 10:16
 */
public class BookBizimpl implements BookBiz {
    private BookMapper bookMapper;
    @Override
    public int deleteByPrimaryKey(Integer bid) {
        return bookMapper.deleteByPrimaryKey(bid);
    }

    @Override
    public int insert(Book record) {
        return bookMapper.insert(record);
    }

    @Override
    public int insertSelective(Book record) {
        return bookMapper.insertSelective(record);
    }

    @Override
    public Book selectByPrimaryKey(Integer bid) {
        return bookMapper.selectByPrimaryKey(bid);
    }

    @Override
    public int updateByPrimaryKeySelective(Book record) {
        return bookMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Book record) {
        return bookMapper.updateByPrimaryKeySelective(record);
    }
}

建立工具类SessionUtil

package com.zking.utils;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * @author bing人
 * @site www.javaxl.com
 * @company xy公司
 * @create  2023-8-20 21:59
 */
public class SessionUtil {
    private static SqlSessionFactory sessionFactory;
    private static ThreadLocal threadLocal = new ThreadLocal();
    static {
        sessionFactory = new SqlSessionFactoryBuilder().build(SessionUtil.class.getResourceAsStream("/mybatis.cfg.xml"));
    }

    public static SqlSession openSession() {
        SqlSession session = threadLocal.get();
        if (null == session) {
            session = sessionFactory.openSession();
            threadLocal.set(session);
        }
        return session;
    }

    public static void main(String[] args) {
        SqlSession session = openSession();
        System.out.println(session.getConnection());
        session.close();
//        System.out.println(session.getConnection());
    }
}

编写demo

package com.zking.demo;

import com.zking.biz.BookBiz;
import com.zking.biz.impl.BookBizimpl;
import com.zking.mapper.BookMapper;
import com.zking.model.Book;
import com.zking.utils.SessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * @author bing人
 * @site
 * @company xy集团
 * @create 2023-08-21 10:28
 */
public class Demo1 {
    private BookBiz bookBiz;
    private SqlSession sqlSession;

    @Before
    public void a(){
        System.out.println("执行测试方法之前会执行的初始化代码块方法");
        this.sqlSession = SessionUtil.openSession();
        BookBizimpl book = new BookBizimpl();
        BookMapper mapper = this.sqlSession.getMapper(BookMapper.class);
        book.setBookMapper(mapper);
        this.bookBiz = book;
    }
    @After
    public void b(){
        System.out.println("执行测试方法之后的后执行的方法");
        //提交事务
        this.sqlSession.commit();
    }

    @Test
    public void text1(){
        System.out.println("测试查询方法方法..");
        Book book = bookBiz.selectByPrimaryKey(25);
        System.out.println(book);
    }
    @Test
    public void text2(){
        System.out.println("删除的方法");
        int insert = bookBiz.deleteByPrimaryKey(30);
    }


}

输出结果

mybatis入门_第8张图片

 mybatis入门_第9张图片

 

你可能感兴趣的:(mybatis,java,开发语言)