1.pom.xml环境搭建参考
2.给出目录结构,
3.跟注解方式不同,这里的dao包下面的UsersMapper.java不再有注解,
package com.xuebaosoft.mybatis3.mybatis_mapper.dao;import java.util.List;import java.util.Map;import com.xuebaosoft.mybatis3.mybatis_mapper.model.Users;public interface UsersMapper { int deleteByPrimaryKey(String id); int insert(Users record); int insertSelective(Users record); Users selectByPrimaryKey(String id); int updateByPrimaryKeySelective(Users record); int updateByPrimaryKey(Users record); ListuserPagingList(Map offsetAndPageSize);}
其实这里是将带注解篇介绍的UserDao.java替换为了UsersMapper.java
4.查看配置文件,看下注解的功能是如何用mapping方式代替实现的,mybatis-config.xml配置如下,
其实这里就是增加了:
也就说在加载配置mybatis-config.xml的时候就已经将UsersMapper.xml连带着一起注册到环境中了。而在UsersMapper.xml中会引用com.xuebaosoft.mybatis3.mybatis_mapper.dao.UsersMapper这个类,因此相当于Dao层的Mapper类有了,映射sql的mapper.xml有了,datasource也已经配置完毕,因此到此为止所有的环境均已构建好。
5.UsersMapper.xml如下,
id, username, password, name, nickname, sex, picture, createtime, lastlogintime, tilepath
6.junit进行测试,
package com.xuebaosoft.mybatis3.mybatis_mapper;import java.util.List;import junit.framework.Test;import junit.framework.TestCase;import junit.framework.TestSuite;import com.xuebaosoft.mybatis3.mybatis_mapper.model.Users;import com.xuebaosoft.mybatis3.mybatis_mapper.service.UserService;import com.xuebaosoft.mybatis3.mybatis_mapper.service.impl.UserServiceImpl;public class AppTest extends TestCase { public AppTest(String testName) { super(testName); } public static Test suite() { return new TestSuite(AppTest.class); } public void testApp() { UserService userService = new UserServiceImpl();// Users user = new Users("123321", "zhangsan", "111111", "name",// "nickname", "sex", "picture", "createtime",// "lastlogintime", "tilepath");// userService.update(user); Users user = userService.getUserById("123321"); System.out.println(user); Listlist = userService.getUsers(0, 10); System.out.println(list); }}
7.备注,如果增加一个业务,如图,
则需要增加的配置如下,