Spring4 -- Bean(dao、service)01(注解的解释)

 

笔记,spring的注解:

@Service用于标注业务层组件(我们通常定义的service层就用这个)

@Controller用于标注控制层组件(如struts中的action)

@Repository用于标注数据访问组件,即DAO组件

@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

 

 

@Resource、@Autowired、@Qualifier

当需要在某个类中定义一个属性,并且该属性是一个已存在的bean,要为该属性赋值我们就用着三个。我们看上面的代码可以看到这三个都是定义在一个属性上的,比如

@Resource
private IIocDao iocDao;

@Autowired
private IocService service;

***************************************************************************

 

目录结构:

 

Spring4 -- Bean(dao、service)01(注解的解释)_第1张图片

配置jdbc.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_spring
jdbc.username=root

jdbc.password=123456

model:

package com.model;

public class Student {
private int id;
private String name;
private int age; 
public Student() {
} 
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}

}

Dao:

package com.dao;
import java.util.List;
import com.model.Student;
public interface StudentDao {
public int addStudent(Student student);

public int updateStudent(Student student);

public int deleteStudent(int id);

public List findStudents();
}

 

DaoImpl:

package com.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import com.dao.StudentDao;
import com.model.Student;

public class StudentDaoImpl implements StudentDao{

private JdbcTemplate jdbcTemplate;

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

@Override
public int addStudent(Student student) {
String sql="insert into t_student values(null,?,?)";
Object []params=new Object[]{student.getName(),student.getAge()};
return jdbcTemplate.update(sql,params);
}


@Override
public int updateStudent(Student student) {
String sql="update t_student set name=?,age=? where id=?";
Object []params=new Object[]{student.getName(),student.getAge(),student.getId()};
return jdbcTemplate.update(sql,params);
}


@Override
public int deleteStudent(int id) {
String sql="delete from t_student where id=?";
Object []params=new Object[]{id};
return jdbcTemplate.update(sql,params);
}


@Override
public List findStudents() {
String sql="select * from t_student";
final List studentList=new ArrayList();
jdbcTemplate.query(sql, new RowCallbackHandler(){
@Override
public void processRow(ResultSet rs) throws SQLException {
Student student=new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
studentList.add(student);
} 
});
return studentList;
}

}

service:

package com.service;
import java.util.List;
import com.model.Student;

public interface StudentService {
public int addStudent(Student student);
public int updateStudent(Student student);
public int deleteStudent(int id);
public List findStudents();

}

 

serviceImpl:

package com.service.impl;
import java.util.List;
import com.dao.StudentDao;
import com.model.Student;
import com.service.StudentService;
public class StudentServiceImpl implements StudentService{
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public int addStudent(Student student) {
return studentDao.addStudent(student);
}
@Override
public int updateStudent(Student student) {
return studentDao.updateStudent(student);
}
@Override
public int deleteStudent(int id) {
return studentDao.deleteStudent(id);
}
@Override
public List findStudents() {
return studentDao.findStudents();
}

}

 

测试类:

package com.test;

import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.model.Student;
import com.service.StudentService;
public class T {
private ApplicationContext ac;

@Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
}


@Test
public void addStudent() {
StudentService studentService=(StudentService)ac.getBean("studentService");
int addNums=studentService.addStudent(new Student("王五", 1));
if(addNums==1){
System.out.println("添加成功");
}
}

@Test
public void updateStudent() {
StudentService studentService=(StudentService)ac.getBean("studentService");
int updateNums=studentService.updateStudent(new Student(8,"王五2", 2));
if(updateNums==1){
System.out.println("更新成功");
}
}

@Test
public void deleteStudent() {
StudentService studentService=(StudentService)ac.getBean("studentService");
int deleteNums=studentService.deleteStudent(8);
if(deleteNums==1){
System.out.println("删除成功");
}
}

@Test
public void findStudents() {
StudentService studentService=(StudentService)ac.getBean("studentService");
List studentList=studentService.findStudents();
for(Student student:studentList){
System.out.println(student);
}
}

}



最后配置beans.xml



        

        
        
        
        
    
    

    
    
    
    




 



 

 

你可能感兴趣的:(spring)