使用通用Mapper,不需要再写mapper.xml配置文件,多数时候,连接口方法都不需要了,大大提高了开发效率。
导入依赖:
重要提示,3.1.0及以后版本的groupId修改为tk.mybatis,artifactId为mapper <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.3.9</version> </dependency> 3.1.0之前的使用的是: <dependency> <groupId>com.github.abel533</groupId> <artifactId>mapper</artifactId> <version>2.3.4</version> </dependency>
第二部,在mybatis的全局配置文件中,注册通用mapper插件,MapperInterceptor _(plugin),详情,点这里,如何集成通用Mapper
<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor"> <!--主键自增回写方法,默认值MYSQL,详细说明请看文档--> <property name="IDENTITY" value="MYSQL"/> <!--通用Mapper接口,多个通用接口用逗号隔开--> <property name="mappers" value="com.github.abel533.mapper.Mapper"/> </plugin>
如何使用通用mapper?
创建一个mapper继承com.github.abel533.mapper.Mapper<T>
,必须指定泛型实体<T>
一旦继承了Mapper<T>
,继承的mapper就拥有了以下通用的方法:
List<T> select(T record); int selectCount(T record); T selectByPrimaryKey(Object key); int insert(T record); int insertSelective(T record); int delete(T key); int deleteByPrimaryKey(Object key); int updateByPrimaryKey(T record); int updateByPrimaryKeySelective(T record); <---------------------------> int selectCountByExample(Object example); int deleteByExample(Object example); List<T> selectByExample(Object example); int updateByExampleSelective(@Param("record") T record, @Param("example") Object example); int updateByExample(@Param("record") T record, @Param("example") Object example);
泛型(实体类)<T>
的类型必须符合要求
实体类按照如下规则和数据库表进行转换,注解全部是JPA中的注解:
1. 表名默认使用类名,驼峰转下划线(只对大写字母进行处理),如UserInfo默认对应的表名为user_info。
2. 表名可以使用@Table(name = “tableName”)进行指定,对不符合第一条默认规则的可以通过这种方式指定表名.
3. 字段默认和@Column一样,都会作为表字段,表字段默认为Java对象的Field名字驼峰转下划线形式.
4. 可以使用@Column(name = “fieldName”)指定不符合第3条规则的字段名
5. 使用@Transient注解可以忽略字段,添加该注解的字段不会作为表字段使用.
6. 建议一定是有一个@Id注解作为主键的字段,可以有多个@Id注解的字段作为联合主键.
使用主键自增(自增主键的回显):
//不限于@Id注解的字段,但是一个实体类中只能存在一个(继承关系中也只能存在一个) @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;
详情代码及测试用例:
public interface NewUserMapper extends Mapper<User>{ //其他必须手写的接口... }
@Table(name="tb_user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String userName; private String password; private String name; private Integer age; private Integer sex; private Date birthday; private Date created; private Date updated;
测试:
Public class NewUserMapperTest{ private NewUserMapper newUserMapper; @Before public void setUp() throws Exception { ApplicationContext applicationContext=new ClasspathXMLApplication("classpath:spring/applicationContext*.xml"); this.newUserMapper = applicationContext.getBean(NewUserMapper.class); } @Test public void testSelectOne(){ User record=new User(); //设置查询条件 record.setName("Zhangsan"); User user=this.newUserMapper.selectOne(record); System.out.println(user); } }
结果:
@Test public void testSelect(){ List<User> users=this.newUserMapper.select(null); for(User user:users){ System.out.println(user); } }
以下是稍微复杂的查询测试 Example :
//List<T> selectByExample(Object example); @Test public void testSelectByExample(){ Example example=new Example(User.class); List<Object> values=new Arraylist<Object>(); values.add(1L); values.add(2L); values.add(3L); example.creatCriteria().addIn("id",values); List<User> users=this.newUserMapper.selectByExample(example); for(User user:users){ System.out.println(user); } }
拼接条件为:
UserService中使用NewUserMapper按照时间desc排序:
自动拼接的sql语句,只看条件为:
FROM tb_user order by create DESC limit ?,?
来源:https://www.cnblogs.com/linjiaxin/p/7401331.html