模块

  1. 1. 编写数据库表
  2. 2. 编写员工模块的javaBean
  3. 3. 编写员工模块的dao和测试
  4. 4. 编写员工模块的service和测试service
  5. 5. 编写员工模块的web层 和页面联测试
  6. 6. 心得

编写数据库表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.white.pojo;

public class Byue {


private Integer id;
private String name;
private String sclass;
private String bm;
private String zw;
private Integer year;
private String imgPath="static/img/code.jpg";

public Byue(//Integer id) {
//this.id = id;这边不能含参数
}

public Byue(Integer id, String name, String sclass, String bm, String zw, Integer year, String imgPath) {
this.id = id;
this.name = name;
this.sclass = sclass;
this.bm = bm;
this.zw = zw;
this.year = year;

//要求给定的图书封面,图片路径不能为空
if(imgPath!= null&&!"".equals(imgPath)){
this.imgPath = imgPath;
}
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSclass() {
return sclass;
}

public void setSclass(String sclass) {
this.sclass = sclass;
}

public String getBm() {
return bm;
}

public void setBm(String bm) {
this.bm = bm;
}

public String getZw() {
return zw;
}

public void setZw(String zw) {
this.zw = zw;
}

public Integer getYear() {
return year;
}

public void setYear(Integer year) {
this.year = year;
}

public String getImgPath() {
return imgPath;
}

public void setImgPath(String imgPath) {
if(imgPath!= null&&!"".equals(imgPath)){
this.imgPath = imgPath;
}
}

@Override
public String toString() {
return "Byue{" +
"id=" + id +
", name='" + name + '\'' +
", sclass='" + sclass + '\'' +
", bm='" + bm + '\'' +
", zw='" + zw + '\'' +
", year=" + year +
", imgPath='" + imgPath + '\'' +
'}';
}
}

编写员工模块的javaBean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.white.dao;

import com.white.pojo.Byue;

import java.util.List;

public interface ByueDao {

public int addByue(Byue byue);

public int deleteByueId(Integer id);

public int updateByue(Byue byue);

public Byue queryByueById(Integer id);

public List<Byue> querByue();

}
package com.white.dao.impl;


import com.white.utils.Jdbcutil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public abstract class BaseDao {//服用代码不需要实例

private QueryRunner queryRunner = new QueryRunner();
//update来执行/增删改查
public int update(String sql,Object ... args){
Connection connection = Jdbcutil.getConnection();
try {
return queryRunner.update(connection,sql,args);
} catch (SQLException e) {
e.printStackTrace();
}finally {
Jdbcutil.close(connection);
}
return -1;
//返回-1表示失败,返回其他表示影响的行数
}

public <T> T queryForOne(Class<T>type,String sql,Object ... args){
Connection con = Jdbcutil.getConnection();

try {
return queryRunner.query(con,sql,new BeanHandler<T>(type),args);
} catch (SQLException e) {
e.printStackTrace();
}finally {
Jdbcutil.close(con);
}
return null;
}

//查询返回多个java
public <T>List<T> queryForyList(Class<T> type,String sql,Object ... args){
Connection con = Jdbcutil.getConnection();

try {
return queryRunner.query(con,sql,new BeanListHandler<T>(type),args);
} catch (SQLException e) {
e.printStackTrace();
}finally {
Jdbcutil.close(con);
}
return null;
}

public Object queryForSingleValue(String sql,Object ... args){
Connection conn = Jdbcutil.getConnection();
try {
return queryRunner.query(conn, sql,new ScalarHandler(),args);
} catch (SQLException e) {
e.printStackTrace();
}
finally {
Jdbcutil.close(conn);
}
return null;
}

}


` '两个不同,书写sql语句时切忌
在sql语句配置时url=jdbc:mysql://localhost:3306/xmt?characterEncoding=utf8增加说明utf-8
在传参时候记得设置无参

编写员工模块的dao和测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.white.text;

import com.white.dao.ByueDao;
import com.white.dao.impl.ByueDaoImpl;
import com.white.pojo.Byue;
import org.junit.Test;

import static org.junit.Assert.*;

public class ByueDaoTest {
private ByueDao byueDap = new ByueDaoImpl();
@Test
public void addByue() {
byueDap.addByue(new Byue(null,"李旭洋","化学1902","影像部","部长",3,null));
}

@Test
public void deleteByueId() {
byueDap.deleteByueId(16);
}

@Test
public void updateByue() {
byueDap.updateByue(new Byue(16,"李旭洋","化学1902","影像部","部长",3,null));
}

@Test
public void queryByueById() {
System.out.println(byueDap.queryByueById(1));
}

@Test
public void querByue() {
for (Byue queryByue : byueDap.querByue()){
System.out.println(queryByue);
}
}

}

编写员工模块的service和测试service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.white.text;

import com.white.dao.ByueDao;
import com.white.dao.impl.ByueDaoImpl;
import com.white.pojo.Byue;
import org.junit.Test;

import static org.junit.Assert.*;

public class ByueDaoTest {
private ByueDao byueDap = new ByueDaoImpl();
@Test
public void addByue() {
byueDap.addByue(new Byue(null,"李旭洋","化学1902","影像部","部长",3,null));
}

@Test
public void deleteByueId() {
byueDap.deleteByueId(16);
}

@Test
public void updateByue() {
byueDap.updateByue(new Byue(16,"李旭洋","化学1902","影像部","部长",3,null));
}

@Test
public void queryByueById() {
System.out.println(byueDap.queryByueById(1));
}

@Test
public void querByue() {
for (Byue queryByue : byueDap.querByue()){
System.out.println(queryByue);
}
}

}

编写员工模块的web层 和页面联测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.white.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public abstract class BaseServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = req.getParameter("action");
try {
Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class);
//反射牛逼
System.out.println(method);
method.invoke(this,req,resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}

package com.white.web;

import com.white.pojo.Byue;
import com.white.service.ByueService;
import com.white.service.impl.ByueServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ByueServlet extends BaseServlet {

private ByueService byueService = new ByueServiceImpl();

protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}

protected void updata(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}

protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//1.查询全部个人信息
List<Byue> byues = byueService.queryByues();
//2.保存到request域中
System.out.println("list成功调用");
req.setAttribute("byues",byues);
req.getRequestDispatcher("/pages/home.jsp").forward(req,resp);
//3.请求转发到home.jsp页面
}

}

心得

数据库提前设计好,后序变量名尽量少进行改动
符号一定要注意,千万不要因为符号导致报错
少抽烟,会死的