转自:http://blog.csdn.net/fangguozi/article/details/22803437
前面我们介绍了xml的配置方式,这次我们来看看mybatis的注解方式:
我们所需要修改的是mybatis-config.xml、UserDao.java及删除UserMapper.xml
1、mybatis-config.xml的修改,因为我们不在需要UserMapper.xml了,所以我们把之前的mapper部分改掉:
- <mapper class="org.mybatis.example.dao.UserDao" />
由之前的resource改成class
2、UserDao.java
- public interface UserDao {
- @Select("select * from users where id=#{id}")
- public User getUserById(Integer id);
- @Delete("DELETE FROM users WHERE id=#{id}")
- public int deleteById(int id);
- @Select("select count(*) from users")
- public int countUser();
- @Insert("INSERT INTO users(id, username, password) VALUES (#{id}, #{userName}, #{passWord})")
- public int saveUser(User user);
- @Update("UPDATE users SET username=#{userName}, password=#{passWord} WHERE id=#{id}")
- public int updateUser(User user);
- }