package org.test.service.impl;
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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.test.Response.Result;
import org.test.dao.AffairsDao;
import org.test.dao.UserDao;
import org.test.entity.Affairs;
import org.test.service.AffairsService;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@Service
public class AffairsServiceImpl implements AffairsService {
@Override
public List
getAffairs(String username) {
String config = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(config);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = factory.openSession();
AffairsDao dao = session.getMapper(AffairsDao.class);
List list;
list = dao.queryAffairs(username);
if(list.size()==0){
System.out.println("没有数据");
return null;
}else{
System.out.println(list);
System.out.println("有数据");
return list;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public Result addAffairs(Affairs affairs) {
String config = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(config);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = factory.openSession();
AffairsDao dao = session.getMapper(AffairsDao.class);
System.out.println(affairs);
int rel = dao.addAffairs(affairs);
System.out.println("更新了" + rel + "条数据");
session.commit();
return Result.success();
} catch (Exception e) {
e.printStackTrace();
return Result.failure();
}
}
@Override
public Result updateAffairs(Affairs affairs) {
String config = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(config);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = factory.openSession();
AffairsDao dao = session.getMapper(AffairsDao.class);
int rel = dao.updateAffairs(affairs);
System.out.println("更新了" + rel + "条数据");
session.commit();
return Result.success();
} catch (Exception e) {
e.printStackTrace();
return Result.failure();
}
}
@Override
public Result deleteAffairs(Integer id) {
String config = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(config);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = factory.openSession();
AffairsDao dao = session.getMapper(AffairsDao.class);
int rel = dao.deleteAffairs(id);
System.out.println("删除了" + rel + "条数据");
session.commit();
return Result.success();
} catch (Exception e) {
e.printStackTrace();
return Result.failure();
}
}
}