Mybatis通用Mapper
极其方便的使用Mybatis单表的增删改查
1. 引入通用Mapper的代码
项目依赖于JPA的注解,需要引入persistence-api-1.0.jar
或者添加Maven依赖:
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
- <!-- 通用Mapper -->
- <dependency>
- <groupId>com.github.abel533</groupId>
- <artifactId>mapper</artifactId>
- <version>2.3.4</version>
- </dependency>
在mybatis中的配置文件中添加
- <configuration>
- <!--Mybatis的拦截器 -->
- <plugins>
- <!--Mybatis分页助手 -->
- <plugin interceptor="com.github.pagehelper.PageHelper">
- <property name="dialect" value="mysql" />
- <!-- 该参数默认为false -->
- <!-- 设置为true时,使用RowBounds分页会进行count查询,也就是是否查询数据总条数 -->
- <property name="rowBoundsWithCount" value="true" />
- </plugin>
- <!--Mybatis通用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>
- </plugins>
- </configuration>
或者在spring的mybatis配置的地方添加
<!-- Mybatis分页插件-pagehelper -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=sqlserver
reasonable=true
</value>
</property>
</bean>
<!--Mybatis通用Mapper -->
<bean class="com.github.abel533.mapperhelper.MapperInterceptor">
<property name="properties">
<value>
IDENTITY=sqlserver
mappers=com.github.abel533.mapper.Mapper
</value>
</property>
<!-- 主键自增回写方法,默认值MYSQL,详细说明请看文档 -->
<!-- <property name="IDENTITY" value="sqlserver" /> -->
<!-- 通用Mapper接口,多个通用接口用逗号隔开 -->
<!-- <property name="mappers" value="com.github.abel533.mapper.Mapper" /> -->
</bean>
</array>
</property>
dao层:
service层:
controller层:
一旦继承了Mapper<T>
,继承的Mapper
就拥有了基本的通用的方法:
4. 泛型(实体类)<T>
的类型必须符合要求
实体类按照如下规则和数据库表进行转换,注解全部是JPA中的注解:
-
表名默认使用类名,驼峰转下划线,如
UserInfo
默认对应的表名为user_info
. -
表名可以使用
@Table(name = "tableName")
进行指定,对不符合第一条默认规则的可以通过这种方式指定表名. -
字段默认和
@Column
一样,都会作为表字段,表字段默认为Java对象的Field
名字驼峰转下划线形式. -
可以使用
@Column(name = "fieldName")
指定不符合第3条规则的字段名 -
使用
@Transient
注解可以忽略字段,添加该注解的字段不会作为表字段使用. -
建议一定是有一个
@Id
注解作为主键的字段,可以有多个@Id
注解的字段作为联合主键. -
默认情况下,实体类中如果不存在包含
@Id
注解的字段,所有的字段都会作为主键字段进行使用(这种效率极低). -
实体类可以继承使用,可以参考测试代码中的
com.github.abel533.model.UserLogin2
类. -
由于基本类型,如int作为实体类字段时会有默认值0,而且无法消除,所以实体类中建议不要使用基本类型.
除了上面提到的这些,Mapper还提供了序列(支持Oracle)、UUID(任意数据库,字段长度32)、主键自增(类似Mysql,Hsqldb)三种方式,其中序列和UUID可以配置多个,主键自增只能配置一个。
这三种方式不能同时使用,同时存在时按照 序列>UUID>主键自增
的优先级进行选择.下面是具体配置方法:
-
使用序列可以添加如下的注解:
//可以用于数字类型,字符串类型(需数据库支持自动转型)的字段 @SequenceGenerator(name="Any",sequenceName="seq_userid") @Id private Integer id;
-
使用UUID时:
//可以用于任意字符串类型长度超过32位的字段 @GeneratedValue(generator = "UUID") private String countryname;
-
使用主键自增:
//不限于@Id注解的字段,但是一个实体类中只能存在一个(继承关系中也只能存在一个) @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id;
5. 将继承的Mapper接口添加到Mybatis配置中
测试代码:- import java.util.ArrayList;
- import java.util.List;
- import org.junit.Before;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.github.abel533.entity.Example;
- import cn.itcast.user.mapper.NewUserMapper;
- import cn.itcast.user.pojo.User;
- public class NewUserMapperTest {
- private NewUserMapper newUserMapper;
- @Before
- public void setUp() throws Exception {
- // 完成newUserMapper的初始化
- ApplicationContext context = new ClassPathXmlApplicationContext(
- "spring/app*.xml");
- newUserMapper = context.getBean("newUserMapper", NewUserMapper.class);
- }
- @Test
- public void testSelectOne() {
- User user = new User();
- user.setId(1L);
- user.setAge(30);
- User selectOne = newUserMapper.selectOne(user);
- System.out.println(selectOne);
- }
- @Test
- public void testSelect() {
- List<User> lists = newUserMapper.select(null);
- for (User user : lists) {
- System.out.println(user);
- }
- }
- @Test
- public void testSelectCount() {
- System.out.println(newUserMapper.selectCount(null));
- }
- @Test
- public void testSelectByPrimaryKey() {
- System.out.println(newUserMapper.selectByPrimaryKey(1L));
- }
- @Test
- public void testInsert() {
- }
- @Test
- public void testInsertSelective() {
- }
- @Test
- public void testDelete() {
- }
- @Test
- public void testDeleteByPrimaryKey() {
- }
- @Test
- public void testUpdateByPrimaryKey() {
- }
- @Test
- public void testUpdateByPrimaryKeySelective() {
- }
- @Test
- public void testSelectCountByExample() {
- // 根据多个id查询用户信息
- List<Object> ids = new ArrayList<Object>();
- ids.add(1);
- ids.add(2);
- ids.add(3);
- Example example = new Example(User.class);
- example.createCriteria().andIn("id", ids);
- List<User> list = this.newUserMapper.selectByExample(example);
- for (User user : list) {
- System.out.println(user);
- }
- }
- @Test
- public void testDeleteByExample() {
- Example example = new Example(User.class);
- example.createCriteria().andBetween("age", 80, 90);
- newUserMapper.deleteByExample(example);
- }
- @Test
- public void testSelectByExample() {
- }
- @Test
- public void testUpdateByExampleSelective() {
- }
- @Test
- public void testUpdateByExample() {
- }
- }
8、集成到项目中