注意:相同方法名的注解方式和xml方式只能用一种,xml里不用写对应的statement
几个注解 @Select @Insert @Update @Delete
@Select("select * from t_user where user_id = #{user_id}")
public User selectUserById(int id);
@Insert("insert into t_user(user_id,user_name,user_pwd,user_email,user_idcard,user_power) values (null,#{user_name},#{user_pwd},#{user_email},#{user_idCard},#{user_power})")
public int insertUser(User user);
@Update("update t_user set user_name=#{user_name},user_pwd=#{user_pwd},user_email=#{user_email},user_idcard=#{user_idCard} where user_id=#{user_id}")
public int updateUser(User user);
@Delete("delete from t_user where user_id=#{user_id}")
public int deleteUser(int id);
@Test
public void testSelect() throws IOException{
IUserDao userDao=new UserDaoImpl(factory);
User u=userDao.selectUserById(1);
System.out.println(u.getUser_name());
}
@Test
public void testInsert() throws IOException{
IUserDao userDao=new UserDaoImpl(factory);
User user=new User();
user.setUser_name("chenhao");
user.setUser_pwd("654321");
user.setUser_power(0);
user.setUser_email("chenhao.newedu.com");
user.setUser_idCard("210101200006061234");
int count=userDao.insertUser(user);
System.out.println(count);
}
@Test
public void testUpdate() throws IOException{
IUserDao userDao=new UserDaoImpl(factory);
User user=new User();
user.setUser_id(8);
user.setUser_name("chenhao");
user.setUser_pwd("123456");
user.setUser_email("chenhao.newedu.com");
user.setUser_idCard("210101200006061234");
int count=userDao.updateUser(user);
System.out.println(count);
}
@Test
public void testDelete() throws IOException{
IUserDao userDao=new UserDaoImpl(factory);
int count=userDao.deleteUserById(9);
System.out.println(count);
}
网友评论