集成通用Mapper,单表操作从此轻松
来源:互联网 发布:淘宝沙发推荐 编辑:程序博客网 时间:2024/06/05 07:31
如果你使用的Maven开发,你只需在pom.xml文件中加入如下依赖:
<dependency> <groupId>com.github.abel533</groupId> <artifactId>mapper</artifactId> <version>2.3.4</version></dependency>
如果你想导入架包:下面是连接,由于Mapper依赖JPA你还得下载JPA的架包
Mapper
JPA
下面是集成通用Mapper的几种方式:
①Java 编码方式
对于单独使用Mybatis,通过如下方式创建sqlSessionFactory:
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);reader.close();
使用直接的JAVA编码方式,可以在初始化sqlSessionFactory的地方按照下面的方式操作:
/从上面的sqlSessionFactory取出一个sessionsession = sqlSessionFactory.openSession();//创建一个MapperHelperMapperHelper mapperHelper = new MapperHelper();// 设置UUID生成策略// 配置UUID生成策略需要使用OGNL表达式// 默认值32位长度:@java.util.UUID@randomUUID().toString().replace("-", "")mapperHelper.setUUID("");// 主键自增回写方法,默认值MYSQL,详细说明请看文档mapperHelper.setIDENTITY("HSQLDB");// 序列的获取规则,使用{num}格式化参数,默认值为{0}.nextval,针对Oracle// 可选参数一共3个,对应0,1,2,分别为SequenceName,ColumnName, PropertyNamemapperHelper.setSeqFormat("NEXT VALUE FOR {0}");// 设置全局的catalog,默认为空,如果设置了值,操作表时的sql会是catalog.tablenamemapperHelper.setCatalog("");// 设置全局的schema,默认为空,如果设置了值,操作表时的sql会是schema.tablename// 如果同时设置了catalog,优先使用catalog.tablenamemapperHelper.setSchema("");// 主键自增回写方法执行顺序,默认AFTER,可选值为(BEFORE|AFTER)mapperHelper.setOrder("AFTER");// 注册通用Mapper接口mapperHelper.registerMapper(Mapper.class);mapperHelper.registerMapper(HsqldbMapper.class);//配置完成后,执行下面的操作mapperHelper.processConfiguration(session.getConfiguration());//OK - mapperHelper的任务已经完成,可以不管了
上面配置参数的时候,是一个个调用set方法进行的,你还可以使用MapperHelper的MapperHelper(Properties properties)构造方法,或者调用setProperties(properties)方法,通过.properties配置文件来配置。
如果你的情况适用于这种方式,推荐你用JAVA编码的方式处理。
②mybatis.config.xml配置插件的方式
在mybatis-config.xml中添加如下配置:
<plugins> <!-- 通用mapper --> <plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor"> <!--主键自增回写方法,默认值MYSQL,详细说明请看文档--> <property value="MYSQL" name="IDENTITY"/> <!--通用Mapper接口,多个通用接口用逗号隔开--> <property value="com.github.abel533.mapper.Mapper" name="mappers"/> </plugin> </plugins>
③和Spring集成
<bean class="com.github.abel533.mapperhelper.MapperHelper" depends-on="sqlSession" init-method="initMapper" scope="singleton" lazy-init="false"> <property name="mappers"> <array> <!-- 可以配置多个 --> <value>com.isea533.mybatis.mapper.Mapper</value> </array> </property> <!-- 对于多数据源,这里也可以像上面这样配置多个 --> <property name="sqlSessions" ref="sqlSession"/></bean>
可以看到配置中依赖了sqlSession,所以使用这种方式,你还要在Spring的配置中保证sqlSession存在。一般情况下都会在Spring定义sqlSession。一般的定义方法如下:
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"> <constructor-arg index="0" ref="sqlSessionFactory"/></bean>
在Spring中使用这种方式的时候,Spring启动完成的时候,所有的通用Mapper都已经处理完成了。后面就可以直接使用通用方法,不需要拦截器来执行了。
重要:目前和Spring集成的时候存在一个bug,这个bug产生的原因如下:
如果你的Mapper没有在项目启动的时候通过@Autowired注入到Service或者其他类中,那么这个Mapper是一个还没有注册到MybatisSqlSession中的Mapper,MapperHelper在启动过程中没有处理该Mapper,这就会导致dynamicSQL无法实例化一类的异常。
这种情况只能使用拦截器处理,如果没有更好的解决版本,后面会恢复拦截器的方式。
④纯Spring方式
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations"> <array> <value>classpath:mapper/*.xml</value> <value>classpath:com/isea533/mybatis/mapper/*.xml</value> </array> </property> <property name="typeAliasesPackage" value="com.isea533.mybatis.model"/> <property name="plugins"> <array> <bean class="com.github.abel533.mapperhelper.MapperInterceptor"> <property name="properties"> <!-- 属性一行一个,具体属性参考mybatis-config.xml中的属性 --> <value> mappers=com.github.abel533.mapper.Mapper </value> </property> </bean> </array> </property></bean>
如果你同时使用了其他的插件,请将其他插件配置通用Mapper拦截器的下面。
至此集成通用Mapper的方法结束,下面是简单的使用方法:
①继承通用的Mapper,必须指定泛型,例如:
public interface UserInfoMapper extends Mapper<UserInfo> { //其他必须手写的接口...}一旦继承了Mapper<T>,继承的Mapper就拥有了以下通用的方法://根据实体类不为null的字段进行查询,条件全部使用=号and条件List<T> select(T record);//根据实体类不为null的字段查询总数,条件全部使用=号and条件int selectCount(T record);//根据主键进行查询,必须保证结果唯一//单个字段做主键时,可以直接写主键的值//联合主键时,key可以是实体类,也可以是MapT selectByPrimaryKey(Object key);//插入一条数据//支持Oracle序列,UUID,类似Mysql的INDENTITY自动增长(自动回写)//优先使用传入的参数值,参数值空时,才会使用序列、UUID,自动增长int insert(T record);//插入一条数据,只插入不为null的字段,不会影响有默认值的字段//支持Oracle序列,UUID,类似Mysql的INDENTITY自动增长(自动回写)//优先使用传入的参数值,参数值空时,才会使用序列、UUID,自动增长int insertSelective(T record);//根据实体类中字段不为null的条件进行删除,条件全部使用=号and条件int delete(T key);//通过主键进行删除,这里最多只会删除一条数据//单个字段做主键时,可以直接写主键的值//联合主键时,key可以是实体类,也可以是Mapint deleteByPrimaryKey(Object key);//根据主键进行更新,这里最多只会更新一条数据//参数为实体类int updateByPrimaryKey(T record);//根据主键进行更新//只会更新不是null的数据int updateByPrimaryKeySelective(T record);//根据Exmaple条件查询总数int selectCountByExample(Object example);//根据Exmaple条件删除int deleteByExample(Object example);//根据Exmaple条件查询List<T> selectByExample(Object example);//根据Exmaple条件更新非空(null)字段int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);//根据Exmaple条件更新全部字段int updateByExample(@Param("record") T record, @Param("example") Object example);
②泛型(实体类)的类型必须符合要求
实体类按照如下规则和数据库表进行转换,注解全部是JPA中的注解:
1.表名默认使用类名,驼峰转下划线(只对大写字母进行处理),如UserInfo默认对应的表名为user_info。
2.表名可以使用@Table(name = “tableName”)进行指定,对不符合第一条默认规则的可以通过这种方式指定表名.
3.字段默认和@Column一样,都会作为表字段,表字段默认为Java对象的Field名字驼峰转下划线形式.
4.可以使用@Column(name = “fieldName”)指定不符合第3条规则的字段名
5.使用@Transient注解可以忽略字段,添加该注解的字段不会作为表字段使用.
6.建议一定是有一个@Id注解作为主键的字段,可以有多个@Id注解的字段作为联合主键.
7.默认情况下,实体类中如果不存在包含@Id注解的字段,所有的字段都会作为主键字段进行使用(这种效率极低).
8.实体类可以继承使用,可以参考测试代码中的com.github.abel533.model.UserLogin2类.
9.由于基本类型,如int作为实体类字段时会有默认值0,而且无法消除,所以实体类中建议不要使用基本类型.
③将继承的Mapper接口添加到Mybatis配置中
<mappers> <mapper class="com.github.abel533.mapper.CountryMapper" /> <mapper class="com.github.abel533.mapper.Country2Mapper" /> <mapper class="com.github.abel533.mapper.CountryTMapper" /> <mapper class="com.github.abel533.mapper.CountryUMapper" /> <mapper class="com.github.abel533.mapper.CountryIMapper" /> <mapper class="com.github.abel533.mapper.UserInfoMapper" /> <mapper class="com.github.abel533.mapper.UserLoginMapper" /> <mapper class="com.github.abel533.mapper.UserLogin2Mapper" /></mappers>
如果你在Spring中配置Mapper接口,不需要像上面这样一个个配置,只需要有下面的这个扫描Mapper接口的这个配置即可:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.isea533.mybatis.mapper"/></bean>
④简单实用实例
SqlSession sqlSession = MybatisHelper.getSqlSession();try { //获取Mapper UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class); UserInfo userInfo = new UserInfo(); userInfo.setUsername("abel533"); userInfo.setPassword("123456"); userInfo.setUsertype("2"); userInfo.setEmail("abel533@gmail.com"); //新增一条数据 Assert.assertEquals(1, mapper.insert(userInfo)); //ID回写,不为空 Assert.assertNotNull(userInfo.getId()); //6是当前的ID Assert.assertEquals(6, (int)userInfo.getId()); //通过主键删除新增的数据 Assert.assertEquals(1,mapper.deleteByPrimaryKey(userInfo));} finally { sqlSession.close();}
SqlSession sqlSession = MybatisHelper.getSqlSession();try { //获取Mapper CountryMapper mapper = sqlSession.getMapper(CountryMapper.class); //查询总数 Assert.assertEquals(183, mapper.selectCount(new Country())); //查询100 Country country = mapper.selectByPrimaryKey(100); //根据主键删除 Assert.assertEquals(1, mapper.deleteByPrimaryKey(100)); //查询总数 Assert.assertEquals(182, mapper.selectCount(new Country())); //插入 Assert.assertEquals(1, mapper.insert(country));} finally { sqlSession.close();}
附:Spring使用相关
直接在需要的地方注入Mapper继承的接口即可,和一般情况下的使用没有区别.
- 集成通用Mapper,单表操作从此轻松
- mybatis整合Mapper通用工具,实现单表基本操作
- springboot中通用mapper结合mybatis generator的使用 通用mapper是为了方便开发人员对单表进行crud操作而产生的一套通用方法。 通用mapper只支持单表操作,可以
- 01-如何集成通用Mapper
- 13.1如何集成通用Mapper
- 分享如何集成通用Mapper
- Spring Boot MyBatis 通用Mapper插件集成
- Spring Boot MyBatis 通用Mapper插件集成
- 【SSM 8】spring集成Mybatis通用Mapper
- 通用mapper集成,及代码成认识
- SpringBoot集成通用Mapper 分页插件 Generator
- 单表的CURD操作(使用mapper动态代理)
- Spring Boot+mybatis+thymeleaf集成通用mapper分页查询
- Spring集成MyBatis 通用Mapper以及 pagehelper分页插件
- spring boot集成mybatis-generator-maven-plugin和通用mapper
- 关于Spring Boot集成MyBatis、通用Mapper、PageHelper
- 通用mapper
- 通用mapper
- 处女篇:ObjectDataSource+CodeSmith实现基础增删改查功能
- GridView双击某行弹出一新窗口
- WebConfig常用配置文件说明
- 取存储过程output的取
- GridView全选
- 集成通用Mapper,单表操作从此轻松
- GridView批量删除页面最后N条数据时自动翻到上一页问题
- Cookie问题(烦了三天)
- asp.net 2.0 简单实现url重写
- 应用Url重写时CSS引用问题
- asp.net 2.0中设定默认焦点按钮
- 文件夹复制操作(非递归循环遍历文件夹)
- Asp.net基础阶段化分
- 利用WeX5平台实现单图片上传功能