content: // Copyright (c) 2025 Alibaba Group and its affiliates
  
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  
  //     http://www.apache.org/licenses/LICENSE-2.0
  
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  package com.example.service;
  
  import com.example.dao.ProductMapper;
  import com.example.model.Product;
  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.Reader;
  import java.util.List;
  
  public class ProductService {
  
      private static SqlSessionFactory sqlSessionFactory;
  
      static {
          try {
              Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
              sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
          } catch (IOException e) {
              throw new ExceptionInInitializerError(e);
          }
      }
  
      public int insertProduct(Product product) {
          try (SqlSession session = sqlSessionFactory.openSession()) {
              ProductMapper mapper = session.getMapper(ProductMapper.class);
              int result = mapper.insert(product);
              session.commit();
              return result;
          }
      }
  
      public int updateProduct(Product product) {
          try (SqlSession session = sqlSessionFactory.openSession()) {
              ProductMapper mapper = session.getMapper(ProductMapper.class);
              int result = mapper.update(product);
              session.commit();
              return result;
          }
      }
  
      public int deleteProduct(String name) {
          try (SqlSession session = sqlSessionFactory.openSession()) {
              ProductMapper mapper = session.getMapper(ProductMapper.class);
              int result = mapper.delete(name);
              session.commit();
              return result;
          }
      }
  
      public List<Product> selectProductByName(String productName) {
          try (SqlSession session = sqlSessionFactory.openSession()) {
              ProductMapper mapper = session.getMapper(ProductMapper.class);
              return mapper.select(productName);
          }
      }
  }