发表于: 2018-03-17 23:44:03

1 624


一。学习了以前项目中的几个注释

@Service
public class StudentServiceImpl implements StudentService {
private Logger logger = Logger.getLogger(StudentServiceImpl.class);
//在属性中使用@Autowired注释来除去setter方法。
   @Autowired
   private StudentDao studentDao;

//重写父类的方法
   @Override
   public Student selectStudentById(Integer id) {
//long start = System.currentTimeMillis();
       Student student = studentDao.selectStudentById(id);
//        long end = System.currentTimeMillis();
//        logger.info("删除数据用时:" + (end - start) + "ms");
       return student;


二。做了个新项目,学习Autowired注释的使用

可以在XML文件总的setter方法中使用@Autowired注释来除去元素,当Spring遇到一个在setter方法中使用的@Autowried注释,他会在方法试图执行byType自动连接。

private SpellChecker spellChecker;
@Autowired
public void setSpellChecker( SpellChecker spellChecker ){
   
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker( ) {
   
return spellChecker;
}
public void spellCheck() {
   
spellChecker.checkSpelling();
}

 

还有一种就是在属性中使用@Autowired注释来除去setter方法。当时使用为自动连接属性传递的时候,Spring会将这些传递过来的值或者引用自动分配给那些属性。

 

  @Autowired
   
private SpellChecker spellChecker;
    public
TextEditor() {
        System.
out.println("Inside TextEditor constructor." );
   
}
   
public SpellChecker getSpellChecker( ){
       
return spellChecker;
   
}
   
public void spellCheck(){
       
spellChecker.checkSpelling();
   
}
}

 

也可以在构造函数中使用@Autowired,一个构造函数@Autowored说明创建bean时,即使在XML文件中没有使用元素配置bean,构造函数也会被自动连接

private SpellChecker spellChecker;
   
@Autowired
   
public TextEditor(SpellChecker spellChecker) {
        System.
out.println("Inside TextEditor constructor.");
        this
.spellChecker = spellChecker;
   
}
   
public void spellCheck(){
       
spellChecker.checkSpelling();
   
}
}

 

 

三。在Spring+mybatis里面修改以前的表,把以前的main方法改成Test方法

改表SQL语句。查询整个表,用的是select * from user,查询一个表不需要任何参数,所以前面List<User> getUser(),括号里不用写任何东西,因为不需要任何参数。

刚开始,UserDao.xml里面,关于整体查询的语句是:

<select id="getUser" resultType="comNaNteng.model.User">
   
SELECT * FROM user
</select>

idresultType之间原来还有个parameterType参数类型,因为不需要任何参数,所以就删掉了。

UserDao里面,括号里本来是User user,也删掉了。

public interface UserDao {
  
// public User getUser(User user);
   
public List<User> getUser();

,变成了上图的形状。

 

测试类里面,改成这样

@Autowired
private UserDao userDao;
 
Logger logger =Logger.getLogger(UserController.class);
 
//    根据姓名查询
 
@Test
 
public void getUserTest(){
    List<User> users =
userDao.getUser();
   
logger.debug("查找结果"+ users);
    
System.out.println(users);
 
}

运行报错,然后百度原因,是junit版本问题,在pom里面添加了个hamcrest-core-1.3.jar包

运行成功。

紧接着改了删除方法

@Test
public void deleteUserById(){
   
userDao.deleteUser(10);
}

我根据教程的方法,改能改对,但是我自己写,却写不出来。看得话,也能看明白这些代码要表达什么意思,想干什么。

 

明天的计划:继续任务一,Spring+mybatis

遇到的问题:暂无

今天的收获:复习了mybatis,复习了springxml文件

java任务二开始时间:2018.01.25

预计demo时间:2018.02.12

可能有延期风险,原因是:json看不懂,控制器的逻辑看不懂,所以又回看了java语法

禅道链接地址:http://task.ptteng.com/zentao/project-task-501.html 


返回列表 返回列表
评论

    分享到