Maven多模块项目搭建+SSM框架整合(二、Dao层添加测试,服务层添加)
</div>
</div>
</div>
</div>
<!-- 手记详情 -->
<div class="detail-content-wrap">
<div class="detail-content js-lookimg">
<div><h3>Spring+SpringMVC+Mybatis整合篇</h3>
在这里推荐大家有事儿没事逛一下GitHub总会有很多信息好玩的项目被你发现.
进入正题
一、添加依赖
添加相关依赖,在这里某些依赖可能用不上(可以注释掉),但是希望有精力的话都去了解一下,许多在工作中都会用上,学无止境~
</project>
二、下载相关依赖Jar
只要点击install就会帮你下载相关依赖并放到本地仓库
/**
-
Created by songl on 2017/8/8.
*/
public class Student {
private Integer id;
private String name;//姓名
private Boolean sex;//性别
private String address;//地址public Integer getId(){return id;}
public String getName(){return name;}
public Boolean getSex(){return sex;}
public String getAddress(){return address;}
public void setId(Integer id){this.id = id;}
public void setName(String name){this.name = name;}
public void setSex(Boolean sex){this.sex = sex;}
public void setAddress(String address){this.address = address;}@Override
public String toString() {
return “Student : id:”+this.id+" name:"+this.name+" sex:"+this.sex+" address:"+this.address;
}
}
dao
然后在mytest-dao下创建包package com.songci.mytest_one.dao
并在该包下创建StudentDao
和base这个包
,在base包下创建baseDao
import java.util.List;
/**
- Created by songl on 2017/8/8.
*/
public interface BaseDao<T> {
public Boolean insert(T var);
public Boolean delete(T var);
public Boolean update(T var);
public List<T> select(T var);
}
StudentDao
继承BaseDao其他方法可以根据需求再添加
package com.songci.mytest_one.dao;
import com.songci.mytest_one.dao.base.BaseDao;
import com.songci.mytest_one.model.Student;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
- Created by songl on 2017/8/8.
*/
@Repository
public interface StudentDao extends BaseDao<Student> {
}
import com.songci.mytest_one.model.Student;
import java.util.List;
/**
-
Created by songl on 2017/8/8.
/
public interface StudentService {
/*- 添加学生
- @param student
- @return
*/
Boolean addStudent(Student student);
/**
- 根据ID删除学生
- @param id
- @return
*/
Boolean deleteStudentById(Integer id);
/**
- 根据ID修改学生信息
- @param student
- @return
*/
Boolean updateStudentById(Student student);
/**
- 按条件查找所有学生
- @param student
- @return
*/
List<Student> findAllStudent(Student student);
}
创建实现类StudentServiceImpl
package com.songci.mytest_one.service.impl;
import com.songci.mytest_one.dao.StudentDao;
import com.songci.mytest_one.model.Student;
import com.songci.mytest_one.service.StudentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
-
Created by songl on 2017/8/8.
*/
@Service(“StudentService”)
public class StudentServiceImpl implements StudentService{
@Resource
private StudentDao studentDao;
public Boolean addStudent(Student student) {
return studentDao.insert(student);
}public Boolean deleteStudentById(Integer id) {
Student student=new Student();
student.setId(id);
return studentDao.delete(student);
}public Boolean updateStudentById(Student student) {
return studentDao.update(student);
}public List<Student> findAllStudent(Student student) {
return studentDao.select(student);
}
}
下一篇
Maven-maven多模块项目搭建+SSM框架整合(三、配置文件添加,服务层测试)
相关代码在GitHub上,包括数据库sql文件
GitHub地址:https://github.com/iamsongci/mytest_one
将持续更新 ~~~
未完待续~~~
</div>