dao layer: The dao layer is called the data access layer, which is called data access object in full. It is a relatively low-level and basic operation, including the addition, deletion, modification and query of a certain table or entity.
Recommended course: Java Tutorial.

Dao layer
First declare an interface class, declare some methods that will be used in the class,
Write a class in the same layer that implements this interface class , rewrite the methods in the interface class
to implement the writing method of Mybatis
The method is mainly a method of processing data;
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);
}
Take the operation of user as an example to illustrate:
The writing method of Mybatis is not implemented
AnimalDAO:
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;
}
}The above is the detailed content of How to write the dao layer of servlet. For more information, please follow other related articles on the PHP Chinese website!