发表于: 2016-01-27 00:13:19
1 6455
由于编辑器不支持MarkDown
我的个人Blog也发表了。
http://www.xingfly.com/article?articleId=7
#任务
- 参考线上学员报名填写的资料,设计数据库,数据库要有三个基本的字段,ID(自增Long),create_at,update_at(所有的时间都用Long)。
- 搭建本地JDK(7)和Maven(3)环境,使用Eclipse或者是Idea创建Maven项目。
- 编写Junit,使用Mybatis读写数据库。
单元测试通过。
# 要求
1. 明确Service和Impl的差别
2. 所有的方法都要写单元测试、CRUD和列表查询
3. 使用Spring 配置IOC
#任务一ing。。。
- 安装mysql 略
- 连接数据库 略
- 创建数据库 ```CREATE DATABASE JnShu;```
- 选择数据库 ```USE JnShu;```
- 创建学员报名表
1. 字段名称:ID|StudentName|StudentPW|StudentSlogan(口号)|Create_at|Update_at
2. ```
CREATE TABLE Student
(
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(20) NOT NULL,
`password` VARCHAR(20) NOT NULL,
`slogan` TEXT NOT NULL,
`createdata` LONG NOT NULL,
`updatedata` LONG NOT NULL
);
```
# 任务二ing。。。
- JDK 环境搭建 略
- Maven 环境搭建(OSX 下)
1. 安装HomeBrew
2. ```brew install maven```
3. HomeBrew还可以安装Git、tomcat等。。
- IDEA 创建 Maven项目 略
- 配置项目下pom.xml
#任务三ing。。。
## MyBatis读写数据库
- 编写Student类
```
public class Student {
private long id;
private String name;
private String password;
private String slogan;
private Long createData;
private Long updateData;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSlogan() {
return slogan;
}
public void setSlogan(String slogan) {
this.slogan = slogan;
}
public Long getCreateData() {
return createData;
}
public void setCreateData(Long createData) {
this.createData = createData;
}
public Long getUpdateData() {
return updateData;
}
public void setUpdateData(Long updateData) {
this.updateData = updateData;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", slogan='" + slogan + '\'' +
", createData=" + createData +
", updateData=" + updateData +
'}';
}
}
```
- MyBatis读写数据库
1. MyBatis配置
![mybatisconfig](http://7b1gp4.com1.z0.glb.clouddn.com/jnshumybatisconfig.png)
2. mapper配置
![mybatisconfig](http://7b1gp4.com1.z0.glb.clouddn.com/jnshumybatis.png)
3. DAO
```
public interface StudentDAO {
public Student getStudent(Integer id);
public void saveStudent(Student s);
public void deleteStudent(Integer id);
public void updateStudent(Student student);
public List<Student> getListStudents();
}
```
```java
public class StudentDAOImpl implements StudentDAO {
public Student getStudent(Integer id) {
DBAccess dbAccess = new DBAccess();
SqlSession sqlSession = null;
Student student = null;
try {
sqlSession = dbAccess.getSqlSession();
StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);
student = studentDAO.getStudent(id);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(sqlSession!=null) {
sqlSession.close();
}
}
return student;
}
public void saveStudent(Student s) {
DBAccess dbAccess = new DBAccess();
SqlSession sqlSession = null;
try {
sqlSession = dbAccess.getSqlSession();
StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);
studentDAO.saveStudent(s);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(sqlSession != null){
sqlSession.close();
}
}
}
public void deleteStudent(Integer id) {
DBAccess dbAccess = new DBAccess();
SqlSession sqlSession = null;
try {
sqlSession = dbAccess.getSqlSession();
StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);
studentDAO.deleteStudent(id);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(sqlSession!=null){
sqlSession.close();
}
}
}
public void updateStudent(Student student) {
DBAccess dbAccess = new DBAccess();
SqlSession sqlSession = null;
try {
sqlSession = dbAccess.getSqlSession();
StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);
studentDAO.updateStudent(student);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(sqlSession!=null){
sqlSession.close();
}
}
}
public List<Student> getListStudents() {
DBAccess dbAccess = new DBAccess();
SqlSession sqlSession = null;
List<Student> students = null;
try {
sqlSession = dbAccess.getSqlSession();
StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);
students = studentDAO.getListStudents();
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(sqlSession!=null){
sqlSession.close();
}
}
return students;
}
}
```
```
public class DBAccess {
public SqlSession getSqlSession() throws IOException{
Reader reader = Resources.getResourceAsReader("Mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}
```
## 编写Junit单元测试
```
public class StudentDAOImplTest {
@Test
public void save() {
Student student = new Student();
student.setName("fpf");
student.setPassword("123456");
student.setCreateData(new Date().getTime());
student.setUpdateData(new Date().getTime());
student.setSlogan("cool!");
StudentDAO studentDAO = new StudentDAOImpl();
studentDAO.saveStudent(student);
}
@Test
public void delete() {
StudentDAO studentDAO = new StudentDAOImpl();
studentDAO.deleteStudent(7);
}
@Test
public void update() {
StudentDAO studentDAO = new StudentDAOImpl();
Student student = studentDAO.getStudent(8);
student.setName("a");
studentDAO.updateStudent(student);
}
@Test
public void get(){
Student student = null;
StudentDAO studentDAO = new StudentDAOImpl();
student = studentDAO.getStudent(8);
}
@Test
public void getUsers(){
List<Student> students = null;
StudentDAO studentDAO = new StudentDAOImpl();
students = studentDAO.getListStudents();
}
}
```
#Service和Impl的差别
Service是接口 定义的 是规范。
Impl是实现 Service可以有很多种实现方式,只要定义的接口不变,实现的方式可以任意改动。
就像自动钻笔刀一样,我们知道自动钻笔刀可以钻笔。把笔放进去但是 里面用了 什么样的 刀片,什么样的齿轮。我们是不知道。只要钻笔刀的插笔孔(也就是接口)不变,哪怕字段钻笔刀拿到厂家升级 钻笔速度。我们一样可以把之前的 笔放进去削。
评论