发表于: 2020-04-30 23:33:48

3 1335


今天在做的事情:


4.Interface和ServiceImpl分开

之前逻辑一直都写在 DAO 里面,也曾感觉到修改起来很麻烦,搜索了一下这个步骤的关键词之后终于明白了。

DAO:负责直接操作数据库,里面只做原子级别的操作(增删改查)

Service:负责编写业务逻辑,使用 DAO 里面编写好的接口。

这样终于把业务逻辑剥离开了。

DAO 层的接口如下:

public interface DiscipleDao {
List<Disciple> query(Disciple disciple);
int insert(Disciple disciple);
int update(Disciple disciple);
int delete(int id);
}

DAO 的实现:

public class DiscipleDaoImpl extends SqlSessionDaoSupport implements DiscipleDao {


   @Override
   public List<Disciple> query(Disciple disciple){
SqlSession sqlSession = this.getSqlSession();
List<Disciple> discipleList = null;
try {
discipleList = sqlSession.selectList("mapper.DiscipleDao.query", disciple);
}catch (DataAccessException e){
System.out.println(e);
}
return discipleList;
}

@Override
   public int insert(Disciple disciple){
SqlSession sqlSession = this.getSqlSession();
       int result = -1;
try {
result = sqlSession.insert("mapper.DiscipleDao.insert", disciple);
}catch (DataAccessException e){
System.out.println(e);
}
return result;
}

@Override
   public int update(Disciple disciple){
SqlSession sqlSession = this.getSqlSession();
int result = -1;
try {
result = sqlSession.update("mapper.DiscipleDao.update", disciple);
}catch (DataAccessException e){
System.out.println(e);
}
return result;
}

@Override
   public int delete(int id){
SqlSession sqlSession = this.getSqlSession();
int result = -1;
try {
result = sqlSession.delete("mapper.DiscipleDao.delete", id);
}catch (DataAccessException e){
System.out.println(e);
}
return result;
}

}

业务代码(Service)我定为以下操作:

(a)报名弟子的增删改查

(b)根据名字或者学号查找日报链接

这里满足的步骤 8.添加数据返回ID,删除或更新数据返回True/False

public interface Service {
List<Disciple> query(Disciple disciple);
int insert(Disciple disciple);
boolean update(Disciple disciple);
boolean delete(int id);
   String queryForLink(Map<String,String> name_OR_id);
}

service 的实现:

public class ServiceImpl implements Service {

private DiscipleDao discipleDao = new DiscipleDaoImpl();

@Override
   public List<Disciple> query(Disciple disciple){
return discipleDao.query(disciple);
}

@Override
   public int insert(Disciple disciple){
return discipleDao.insert(disciple);
}

@Override
   public boolean update(Disciple disciple){
return discipleDao.update(disciple) == 1 ? true: false;
}

@Override
   public boolean delete(int id){
return discipleDao.delete(id) == 1 ? true: false;
}


   @Override
   public String queryForLink(Map<String, String> name_OR_id){
Disciple disciple = new Disciple();

if (!name_OR_id.isEmpty()){
disciple.setStudent_name(name_OR_id.get("name"));

if (name_OR_id.containsKey("id")){
int id = 0;

try {
id = Integer.parseInt(name_OR_id.get("id"));
}catch (NumberFormatException e){
return "The value of 'id' is not Integer.";
}
disciple.setStudent_id(id);
}

List<Disciple> discipleList = discipleDao.query(disciple);
if (discipleList.size() == 1){
return discipleList.get(1).getDaily_report();
}
else {
return "Too many context.";
}

}
else {
return "Empty Map.";
}
}
}


spring 的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/cache
      http://www.springframework.org/schema/cache/spring-cache.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- 配置数据库相关参数properties的属性:${url} -->
   <context:property-placeholder location="classpath:db.properties" />
<!--配置c3p0连接池-->
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
   </bean>
<!--配置sqlSqlSessionFactory-->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入mybatis核心配置文件-->
       <property name="configLocation" value="mybatis-config.xml"></property>
<!--注入数据源-->
       <property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置DiscipleDaoImpl,注入sqlSessionFactory-->
   <bean id="discipleDao" class="cn.mogeek.dao.DiscipleDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>


测试的时候一直跑不通,打印出来的 sqlsession 一直是空的,暂时还没找出原因来,今天一直卡在这里。

感觉是 bean 里面给 DaoImpl 注入 sqlSessionFactory 出问题了,也说不准,先提交了日报,慢慢排查····


返回列表 返回列表
评论

    分享到