dao层:dao层叫数据访问层,全称为data access object,属于一种比较底层,比较基础的操作,具体到对于某个表、某个实体的增删改查。
推荐课程:Java教程。
Dao层
先声明一个接口类,类里声明一些将会用的方法,
同一层内写一个实现这个接口类的类,重写接口类里的方法
以实现Mybatis的写法
方法主要是处理数据的方法;
复制代码1
2
3
4
5
6
7
8
9
10
11
12
public interface IStuClassDao {
//全表查询方法
public List findAllStuClassInfo();
//classID查询
public Map<String, Object> findStuClassById(int classId) ;
//增加方法
public void addStuClassById(Stuclass sc) ;
//更新方法
public void updateStuClassById(Stuclass sc) ;
//查询方法
public String findClassNamesByIds(String ids);
}
登录后复制
以对于user的操作为例进行说明:
未实现Mybatis的写法
AnimalDAO:
复制代码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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import util.JDBCUtil;
import entry.Animal;
/**
* 对数据库进行操作
* @author dell-
*
*/
public class AnimalDAO {
//添加动物信息
public void addAnimal(Animal animal){
//1建立连接
Connection conn= JDBCUtil.getConnection();
//2创建sql语句
String sql = "insert into animal (aid,aname,atime)values(?,?,?)";
//3创建sql执行对象
PreparedStatement ps =null;
try {
ps=conn.prepareStatement(sql);
ps.setInt(1, animal.getAid());
ps.setString(2, animal.getAname());
ps.setDate(3, new java.sql.Date(animal.getAtime().getTime()));
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
JDBCUtil.release(null,ps,conn);
}
}
//查询所有信息
public List<Animal> getAll(){
List<Animal> list = new ArrayList<Animal>();
//1连接数据库
Connection conn = JDBCUtil.getConnection();
//2拼装sql
String sql="select * from animal";
//3创建sql执行对象
PreparedStatement ps =null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
Animal animal = new Animal();
animal.setAid(rs.getInt("aid"));
animal.setAname(rs.getString("aname"));
animal.setAtime(rs.getDate("atime"));
list.add(animal);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
JDBCUtil.release(rs, ps, conn);
}
return list;
}
//通过aid 删除动物信息
public void deleteAnimal(int aid){
//1建立数据库连接
Connection conn = JDBCUtil.getConnection();
//2拼装sql
String sql = "delete from animal where aid=?";
//3创建sql执行对象
PreparedStatement ps =null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, aid);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
JDBCUtil.release(null, ps, conn);
}
}
//通过aid修改动物信息
public void updateAnimal(Animal animal){
//1建立连接
Connection conn = JDBCUtil.getConnection();
//2拼装sql
String sql = "update animal set aname=?,atime=? where aid=?";
//3创建sql执行对象
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, animal.getAname());
ps.setDate(2, new java.sql.Date(animal.getAtime().getTime()));
ps.setInt(3, animal.getAid());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
JDBCUtil.release(null, ps, conn);
}
}
public Animal getAnimalByid(int aid){
//1链接数据库
Connection conn= JDBCUtil.getConnection();
//2创建sql语句
String sql = "select * from animal where aid=?";
//3创建sql执行对象
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, aid);
rs = ps.executeQuery();
if(rs.next()){
Animal animal = new Animal();
animal.setAid(rs.getInt("aid"));
animal.setAname(rs.getString("aname"));
animal.setAtime(rs.getDate("atime"));
return animal;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtil.release(rs, ps, conn);
}
return null;
}
}
登录后复制
以上就是servlet的dao层怎么写的详细内容,更多请关注靠谱客其它相关文章!
最后
以上就是辛勤白猫最近收集整理的关于servlet的dao层怎么写的全部内容,更多相关servlet内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复