导入依赖
<!-- 通用Mapper -->
<dependency>
<groupId>com.github.abel533</groupId>
<artifactId>mapper</artifactId>
</dependency>
配置通用mapper插件
<plugins>
<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
<property name="IDENTITY" value="MYSQL"/>
<property name="mappers" value="com.github.abel533.mapper.Mapper"/>
</plugin>
</plugins>
<mappers>
<!-- 引入映射资源文件 -->
<!-- <mapper resource="UserMapper.xml"/> -->
<package name="com.huawei.support" />
</mappers>
继承通用的Mapper<T>
,必须指定泛型<T>
public interface NewUserMapper extends Mapper<User> {
}
实体必须定义jpa注解
@Table(name = "user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Column(name = "phone")
private String phone;
@Column(name = "email")
private String email;
测试用例
public class NewUserMapperTest {
SqlSessionFactory sqlSessionFactory;
SqlSession sqlSession;
NewUserMapper newUserMapper;
@Before
public void setUp() throws Exception {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession(true);
newUserMapper=sqlSession.getMapper(NewUserMapper.class);
}
@Test
public void testSelectOne() {
User user = new User();
user.setId(1L);
User selectOne = this.newUserMapper.selectOne(user);
System.out.println(selectOne);
}
@Test
public void testSelect() {
List<User> list = this.newUserMapper.select(null);
for (User user : list) {
System.out.println(user);
}
}
@Test
public void testSelectCount() {
int selectCount = this.newUserMapper.selectCount(null);
System.out.println(selectCount);
}
@Test
public void testSelectByPrimaryKey() {
User user = this.newUserMapper.selectByPrimaryKey(2L);
System.out.println(user);
}
@Test
public void testInsert() {
User user = new User();
user.setId(null);
user.setUsername("user123");
user.setEmail("user234@qq.com");
user.setPassword("123456");
user.setPhone("134121231");
this.newUserMapper.insert(user);
}
@Test
public void testInsertSelective() {
User user = new User();
user.setId(null);
user.setUsername("user1234");
user.setPassword("123456");
this.newUserMapper.insertSelective(user);
}
@Test
public void testDelete() {
User user = new User();
this.newUserMapper.delete(user);
}
@Test
public void testDeleteByPrimaryKey() {
this.newUserMapper.deleteByPrimaryKey(2L);
}
@Test
public void testUpdateByPrimaryKey() {
User user = new User();
user.setId(3L);
user.setUsername("zhangsan");
user.setPassword("12312123");
this.newUserMapper.updateByPrimaryKey(user);
}
@Test
public void testUpdateByPrimaryKeySelective() {
User user = new User();
user.setId(3L);
user.setUsername("zhangsan");
user.setPassword("12312123");
this.newUserMapper.updateByPrimaryKey(user);
}
@Test
public void testSelectCountByExample() {
Example example = new Example(User.class);
example.createCriteria().andEqualTo("password", "123456");
int selectCountByExample = this.newUserMapper.selectCountByExample(example);
System.out.println(selectCountByExample);
}
@Test
public void testDeleteByExample() {
Example example = new Example(User.class);
example.createCriteria().andEqualTo("password", "123456");
this.newUserMapper.deleteByExample(example);
}
@Test
public void testSelectByExample() {
Example example = new Example(User.class);
example.createCriteria().andEqualTo("password", "123456");
List<User> list = this.newUserMapper.selectByExample(example);
System.out.println(list.size());
}
@Test
public void testUpdateByExampleSelective() {
User user = new User();
user.setId(3L);
user.setPhone("123123123");
user.setEmail("lkajsdf@laksdf.com");
user.setPassword("123");
Example example = new Example(User.class);
example.createCriteria().andEqualTo("password", "12312123");
this.newUserMapper.updateByExampleSelective(user, example);
}
@Test
public void testUpdateByExample() {
User user = new User();
user.setId(3L);
user.setPhone("123123123");
user.setEmail("lkajsdf@laksdf.com");
user.setPassword("123");
Example example = new Example(User.class);
example.createCriteria().andEqualTo("password", "12312123");
this.newUserMapper.updateByExample(user, example);
}
}