mybatis:一、初识mybatis

mybatis:一、初识mybatis,第1张

mybatis:一、初识mybatis mybatis:一、初识mybatis

1 mybatis依赖配置

依赖如下:


    org.mybatis
    mybatis
    3.5.7


    mysql
    mysql-connector-java
    8.0.27

额外的依赖如:


    junit
    junit
    4.13.1
    test


    org.projectlombok
    lombok
    1.18.22


    org.springframework
    spring-webmvc
    5.3.14

整个pom.xml如下:



    4.0.0

    org.example
    Ymybatis
    1.0-SNAPSHOT

    
        8
        8
    

    

        
            org.mybatis
            mybatis
            3.5.7
        
        
            mysql
            mysql-connector-java
            8.0.27
        

        
            junit
            junit
            4.13.1
            test
        
        
            org.projectlombok
            lombok
            1.18.22
        
        
            org.springframework
            spring-webmvc
            5.3.14
        
    

    
        
        
            
                src/main/java
                
                    ***.xml
                
                false
            
        
    


2 创建数据库和相应的表

数据库环境为mysql8,在workbench上连接本地的mysql,新建数据库和数据表,创建myuser表(表名只能是小写)


查看具体的表结构信息:

插入测试数据:

3 创建User实体类

tips:domain意思,和pojo、entity都是实体类的意思

package com.xiaoxu.domain;

import lombok.Data;

import java.math.BigDecimal;


@Data
public class User {
    //属性名和列名一样
    private int id;
    private String name;
    private byte age;
    private BigDecimal money;

    @Override
    public String toString() {
        return "用户实体信息{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age=" + age +
                ", money=" + money +
                '}';
    }
}

mybatis文档如下:https://mybatis.net.cn/getting-started.html

4 创建UserDao接口和UserDao.xml:

UserDao.xml放在UserDao一个目录下:

UserDao接口:

package com.xiaoxu.dao;

import com.xiaoxu.domain.User;


public interface UserDao {
    User selectUserById(int id);
}

UserDao.xml:





    
        select id,name,age,money from myuser where id=#{my_id}
    

    
    
    
        insert into myuser(name,age,money) values
        
            (#{item.name},#{item.age},#{item.money})
        
    


UserDaoImpl实现UserDao的接口方法:

package com.xiaoxu.impl;

import com.xiaoxu.dao.UserDao;
import com.xiaoxu.domain.User;
import com.xiaoxu.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class UserDaoImpl implements UserDao {
    @Override
    public User selectUserById(int id) {
        SqlSession s= MybatisUtil.getSqlSession();
        String sqlId="com.xiaoxu.dao.UserDao.selectUserById";
        User u=s.selectOne(sqlId,id);
        System.out.println(u);
        s.close();
        return u;
    }

    @Override
    public List selectUsers() {
        SqlSession s= MybatisUtil.getSqlSession();
        String sqlId = "com.xiaoxu.dao.UserDao.selectStudents";
        List userList = s.selectList(sqlId);
        userList.forEach(System.out::println);
        s.close();
        return userList;
    }

    @Override
    public int insertUsers(List userList) {
        SqlSession s = MybatisUtil.getAutoCommitSqlSession();
        String sqlId = "com.xiaoxu.dao.UserDao.insertUsers";
        int rows = s.insert(sqlId,userList);
        System.out.println("插入的数据的行数为:"+rows);
        s.close();
        return rows;
    }
}

新建spring的配置类:

com.xiaoxu.config包中:

package com.xiaoxu.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;


@Configuration
@ComponentScan(basePackages = {"com.xiaoxu"})
public class MyConfig {
}

新建Mybatis工具类的使用测试类:

import com.xiaoxu.config.MyConfig;
import com.xiaoxu.dao.UserDao;
import com.xiaoxu.domain.User;
import com.xiaoxu.impl.UserDaoImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


public class MybatisUtilTest {
    @Test
    public void test_01(){
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        UserDao u = context.getBean("userDaoImpl",UserDaoImpl.class);
        u.selectUserById(2);
        u.selectUserById(1);
    }

    @Test
    public void test_02(){
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        UserDao u = context.getBean("userDaoImpl", UserDaoImpl.class);
        u.selectUsers();
    }

    @Test
    public void test_03(){
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        UserDao u = context.getBean("userDaoImpl",UserDaoImpl.class);

        User u1 = new User();
        u1.setName("小王");
        u1.setAge((byte) 28);
        u1.setMoney(new BigDecimal("9.99"));
        User u2 = new User();
        u2.setName("小张");
        u2.setAge((byte) 55);
        u2.setMoney(new BigDecimal("12.89"));
        List my_user_list = new ArrayList(){{
            add(u1);
            add(u2);
        }};

        u.insertUsers(my_user_list);
    }
}

在执行测试类前,开启Mybatis的sql日志打印出来:

修改mybatis.xml:




    
    
    
    
        
    
    
    
        
            
            
            
            
                
                
                
                
                
            
        
    
    
    
        
        
    

预备数据库数据:

测试类执行效果如下:

test_01:

test_02:

test_03:

插入后,表中新增自增id为9和10的数据:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5681951.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存