一、Service层
StudentService代码
@Service
public class StudentService implements IStudentSrvice {
@Autowired
private IStudentDao studentDao;
public int add(String mobile,String name) {
String id= UUID.randomUUID().toString();
return studentDao.add(id, name, mobile);
}
public int delete(String id){
return studentDao.delete(id);
}
public int update(String id,String name,String mobile){
return studentDao.update(id,name,mobile);
}
public Student get(String id){
return studentDao.get(id);
}
}
IStudentService代码
public interface IStudentSrvice {
int add(String mobile,String name);
int delete(String id);
int update(String id,String name,String mobile);
Student get(String id);
}
二、Dao层
StudentDao代码
@Repository
public class StudentDao implements IStudentDao{
@Autowired
private JdbcTemplate jdbcTemplate;
//增
public int add(String id,String mobile,String name){
String sql = "insert into student(id,name,mobile) values(?,?,?)";
if(jdbcTemplate.update(sql,id,name,mobile)>0){
return 1;
}else {
return 0;
}
}
//删
public int delete(String id){
String sql = "delete from student where id=?";
if(jdbcTemplate.update(sql,id)>0){
return 1;
}else{
return 0;
}
}
//改
public int update(String id,String name,String mobile){
String sql = "update student set name=?,mobile=? where id=?";
if(jdbcTemplate.update(sql,id,name,mobile)>0){
return 1;
}else{
return 0;
}
}
//查
public Student get(String id){
String sql = "select * from student where id=?";
return jdbcTemplate.queryForObject(sql,new RowMapper<Student>() {
@Override
public Student mapRow(ResultSet rs, int i) throws SQLException {
return new Student(rs.getString("id"),rs.getString("name"),rs.getString("mobile"));
}
},id);
}
}
IStudentDao代码
public interface IStudentDao {
int add(String id,String mobile,String name);
int delete(String id);
int update(String id ,String name,String mobile);
Student get(String id);
}