package com.landaiqing.service;


import com.landaiqing.entity.UserEntity;
import com.landaiqing.mapper.userMapper;
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.ArrayList;

public class UserService {
    private SqlSession sqlSession;
    private userMapper userMapper;

    public UserService() throws IOException{
        // 通过无参构造方法 初始化mybatis 得到flightMapper
        // mybatis-config.xml 目录位置
        String resource = "mybatis-config.xml";
        // 1.解析mybatis-config.xml 得到数据库相关的配置信息
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //2.创建得到一个sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //3.获取到sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        userMapper = sqlSession.getMapper(userMapper.class);
//        sqlSession.close();
    }
   public ArrayList<UserEntity> list(){
        return userMapper.list();
    }

    public int insert(UserEntity userEntity){
        int insert = userMapper.insert(userEntity);
//        sqlSession.commit();
        return insert;
    }
    public int reply(UserEntity userEntity){
        int reply = userMapper.reply(userEntity);
//        sqlSession.commit();
        return reply;
    }
    public int deleteUser(Integer userId){
        int deleteUser = userMapper.deleteUser(userId);
//        sqlSession.commit();
        return deleteUser;
    }
    public int update(UserEntity userEntity){
        int update = userMapper.update(userEntity);
//        sqlSession.commit();
        return update;
    }



}
