根据逆向工程生成 po 类以及 mapper 文件,由于我们这里是进行登录验证,所以在 UserMapper.java 中添加如下代码: package com.ys.mapper; import com.ys.po.User;import java.util.List;import org.apache.ibatis.a
package com.ys.mapper; import com.ys.po.User; import java.util.List; import org.apache.ibatis.annotations.Param; public interface UserMapper { //通过用户名和密码查询User User selectUserByUsernameAndPassword(User user); }UserMapper.xml
dao 层整合完毕之后,我们进行一个测试,要养成每做完一个小模块必须测试的习惯。步步为营,如果整个项目配置完了然后在进行测试,那么有问题进行排除会变得很困难。
package com.ys.test; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ys.mapper.UserMapper; import com.ys.po.User; public class DaoTest { ApplicationContext context = null; @Before public void init(){ context = new ClassPathXmlApplicationContext("classpath:spring/application-dao.xml"); } @Test public void testSelectByPrimaryKey(){ UserMapper userMapper = (UserMapper) context.getBean("userMapper"); User user = userMapper.selectByPrimaryKey(1); System.out.println(user.getPassword()); } }