Maven项目集成Mybatis增删改查操作(一)

1、新建Maven项目,在pom.xml中导入依赖
数据库要使用Mysql,需导入mysql包
接口测试要使用到junit,需导入junit包
使用Mybatis框架,需要导入mybatis包

 
        
            mysql
            mysql-connector-java
            runtime
        

        
            org.mybatis
            mybatis
            3.5.2
        

        
            junit
            junit
            4.13.1
            test
        

2、项目运行时要加载配置文件,pom.xml中需要加入

  
        
            
                src/main/java
                
                    **/*.xml
                    **/*.properties
                
            

            
                src/main/resources
                
                    **/*.xml
                    **/*.properties
                
            
        
    

3、在resource下创建db.properties

driver=com.mysql.cj.jdbc.Driver
url= jdbc:mysql://w-bzrds-lavj.service.testdb:3306/member_dy?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username=member_dy
password=123456

4、在resource下创建 mybatis-config.xml





    
    
   

        
    
    
    

        
        
            
            
            
            
                
                
                
                
                
            
        
    
    
        
    


上述配置文件中 属性来实现引用配置文件,resource中填写文件名称的前提是db.properties是直接存放在resource目录下。
< typeAliases>属性为Java 类型设置一个缩写名字,意在降低冗余的全限定类名书写。
typeAlias指定具体的实体类,package直接指定需要的包名,
Mybatis会在包名下面搜索需要的Java Bean,
比如:扫描实体类的包,它的默认别名就为这个类的类名,首字母小写
可以配置成多套环境,Mybatis默认的事务管理器就是JDBC,
连接池POOLED,

5、创建实体类 TdyMemberInfo.class

package com.sc.pojo;

import java.math.BigDecimal;
import java.util.Date;

public class TdyMemberInfo {
    private Long memberId;
    private Long shopId;
    private String memberNo;
    private String mobile;
    private String openId;
 

    public Long getMemberId() {
        return memberId;
    }

    public Long getShopId() {
        return shopId;
    }

   public String getMemberNo() {
        return memberNo;
    }
 
    public String getMobile() {
        return mobile;
    }

    public String getOpenId() {
        return openId;
    }
    public void setMemberId(Long memberId) {
        this.memberId = memberId;
    }

    public void setShopId(Long shopId) {
        this.shopId = shopId;
    }

    public void setMemberNo(String memberNo) {
        this.memberNo = memberNo;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    
    public TdyMemberInfo() {
    }

    public TdyMemberInfo(Long memberId, Long shopId, String memberNo, String mobile, String openId) {
        this.memberId = memberId;
        this.shopId = shopId;
        this.memberNo = memberNo;
        this.mobile = mobile;
        this.openId = openId;
    }
}

6、创建TdyMemberInfoDao.class

package com.sc.dao;

import com.sc.pojo.TdyMemberInfo;

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

public interface TdyMemberInfoDao {
    List selectAllmember();

    /**
     * 根据手机号模糊查 询
     * @param value
     * @return
     */
    List selectLikeMember(String value);

    int addMember(TdyMemberInfo tdyMemberInfo);
    int addMemberByMap(Mapmap);
    int updateMember(TdyMemberInfo tdyMemberInfo);
    int deleteMember(long memberId);
}

7、创建Mapper文件,TdyMemberInfoMapper.xml









    
    

    

    
    
        INSERT INTO t_dy_member_info
        (shop_id,member_no,mobile,open_id)
        VALUES (9730231,#{memberNo},#{mobile},#{openId})
    

   
        INSERT INTO t_dy_member_info
           (shop_id,member_no,mobile,open_id)
        VALUES (9730231,#{memberNo},#{mobile},#{openId})
   


    
        update t_dy_member_info set open_id=#{openId} ,member_no=#{memberNo} where member_id =#{memberId}
    

    
        delete from t_dy_member_info where member_id=#{memberId}
    


8、创建MyBatis工具类MyBatisUtils.class
使用Mybatis第一步,获取SqlSessionFactory对象 ,然后通过SqlSessionFactory对象获得SqlSession的实例
SqlSession 完全包含了面向数据库执行SQL 命令所需的所有方法

package com.sc.config;

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 MyBatisUtils {
    private static SqlSessionFactory factory;
    static {
        String resource = "mybatis-config.xml";
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream(resource);
            factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession(){
        return factory.openSession();
    }

}

9、创建测试类,MemberDaoTest.class

package dao;

import com.sc.config.MyBatisUtils;
import com.sc.dao.TdyMemberInfoDao;
import com.sc.pojo.TdyMemberInfo;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

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

public class MemberDaoTest {

    @Test
    public void selectAll(){
       SqlSession sqlSession =  MyBatisUtils.getSqlSession();
        TdyMemberInfoDao mapper = sqlSession.getMapper(TdyMemberInfoDao.class);
        List allMembers = mapper.selectAllmember();
        for(TdyMemberInfo member:allMembers){
            System.out.println(member);
        }
        sqlSession.close();
    }

    @Test
    public void selectLikeMember(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        TdyMemberInfoDao mapper = sqlSession.getMapper(TdyMemberInfoDao.class);
        List tdyMemberInfos = mapper.selectLikeMember("18660467%");
        for (TdyMemberInfo tdyMemberInfo : tdyMemberInfos) {
            System.out.println(tdyMemberInfo);
        }
        sqlSession.close();
    }

    @Test
    public void addMember(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        TdyMemberInfoDao mapper = sqlSession.getMapper(TdyMemberInfoDao.class);
        TdyMemberInfo tdyMemberInfo = new TdyMemberInfo();
        tdyMemberInfo.setMemberNo("20211209100001");
        tdyMemberInfo.setMobile("18660467744");
        tdyMemberInfo.setOpenId("PDH2JKKE3JUKAG6M7VOX4");

        int code = mapper.addMember(tdyMemberInfo);
        if(code>0){
            System.out.println("新增员工成功。");
        }
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void addMemberByMap(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        TdyMemberInfoDao mapper = sqlSession.getMapper(TdyMemberInfoDao.class);
        Map map = new HashMap<>();
        map.put("no","20211209100002");
        map.put("mobile","18660467799");
        map.put("open","111PDH2JKKE3JUKAG111");

        int code = mapper.addMemberByMap(map);
        if(code>0){
            System.out.println("使用Map传参,新增员工成功。");
        }
        sqlSession.commit();
        sqlSession.close();
    }



    @Test
    public void updateMember(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        TdyMemberInfoDao mapper = sqlSession.getMapper(TdyMemberInfoDao.class);
        TdyMemberInfo tdyMemberInfo = new TdyMemberInfo();
        tdyMemberInfo.setMemberId(new Long(479));
        tdyMemberInfo.setMemberNo("20211209100001111");
        tdyMemberInfo.setOpenId("PDH2JKKE3JUKAG6M7VOX4555");
        int code = mapper.updateMember(tdyMemberInfo);
        if(code>0){
            System.out.println("修改员工成功。");
        }
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void deleteMember(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        TdyMemberInfoDao mapper = sqlSession.getMapper(TdyMemberInfoDao.class);
        int code = mapper.deleteMember(new Long(479));
        if(code>0){
            System.out.println("删除员工成功。");
        }
        sqlSession.commit();
        sqlSession.close();
    }
}

项目如下:


image.png

测试结果:


image.png

你可能感兴趣的:(Maven项目集成Mybatis增删改查操作(一))