您现在的位置是:首页 > 个人日记个人日记

spring&mybatis-通用mapper使用方法

2019-02-07 13:26:11【个人日记】516人已围观

简介介绍通用mapper的使用方法

通用mapper使用方法

jar包为

  1. <dependency>
  2. <groupId>tk.mybatis</groupId>
  3. <artifactId>mapper</artifactId>
  4. <version>4.1.4</version>
  5. </dependency>

版本根据情况使用

spring-config.xml配置中

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  8. <!--扫描注解-->
  9. <context:component-scan base-package="com.huangxin.order"></context:component-scan>
  10. <!--AOP注解生效-->
  11. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  12. <!--加载配置文件-->
  13. <context:property-placeholder location="property/jdbc.properties"></context:property-placeholder>
  14. <!--druid连接池-->
  15. <bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource">
  16. <property name="url" value="${jdbc.url}"></property>
  17. <property name="username" value="${jdbc.username}"></property>
  18. <property name="password" value="${jdbc.password}"></property>
  19. </bean>
  20. <!--SqlSessionFactory工厂交给spring-->
  21. <bean id="sqlSessionFactory" name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  22. <!--将数据源注入SqlSessionFactory-->
  23. <property name="dataSource" ref="dateSource"></property>
  24. <!--引入映射-->
  25. <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
  26. </bean>
  27. <!--让spring生成mapper接口的实现类-->
  28. <!--多个mapper接口-->
  29. <!--MapperScannerConfigurer将扫描所有包下的mapper-->
  30. <!--tk.mybatis.spring.mapper.MapperScannerConfigurer为插件,增强官方-->
  31. <!--org.mybatis.spring.mapper.MapperScannerConfigurer为官方扫描器-->
  32. <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
  33. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  34. <!--对哪些包下的类生成mapper代理类-->
  35. <property name="basePackage" value="com.huangxin.order.mapper"></property>
  36. </bean>
  37. <!--事务管理-->
  38. <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  39. <property name="dataSource" ref="dateSource"></property>
  40. </bean>
  41. <!--将事务管理器交给spring管理-->
  42. <!--使用注解方式,分散方式,xml配置为集中配置-->
  43. <!--如果使用注解则不使用xml配置-->
  44. <tx:annotation-driven transaction-manager="txManager"/>
  45. <!--配置事务增强-->
  46. <!--isolation事务隔离级别-->
  47. <!--propagation传播行为-->
  48. <!--注意命名规范-->
  49. <tx:advice id="txAdvice" transaction-manager="txManager">
  50. <tx:attributes>
  51. <tx:method name="get*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
  52. <tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
  53. </tx:attributes>
  54. </tx:advice>
  55. </beans>

以上只需要改动这些,也就是将org.mybatis.spring.mapper.MapperScannerConfigurer替换为tk.mybatis.spring.mapper.MapperScannerConfigurer

  1. <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
  2. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  3. <!--对哪些包下的类生成mapper代理类-->
  4. <property name="basePackage" value="com.huangxin.order.mapper"></property>
  5. </bean>

这个框架可以将数据库和类之间形成映射关系,也就是JPA

例如:字段名称为userId,则此框架可以直接识别数据库列名为user_id

使用方法

1) @Table表名,属性对应为name(UserName),则识别后为name=user_name,也就是对应关系
2) @Column列名,用法与其Table用法相同
3) @Transient,表示这不是数据库中的列名,
4) @Id主键
5) @GeneratedValue,主键策略注解

使用方法
配置对象.java文件
```java
package com.huangxin.order.model;

import lombok.Data;

import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
@Table(name = "orders")
@Data
public class Order {
@Id
private Integer oid;

private Integer userId;

private Date createTime;

}
```
因为我的数据库表名为orders所以使用@Table注解,并在主键上加入@Id注解,使用非常方便

对象接口中.java
```java
package com.huangxin.order.mapper;

import com.huangxin.order.model.Order;
import tk.mybatis.mapper.common.Mapper;

public interface OrderMapper extends Mapper {

//这里面写需要的方法

}
```

优点

1) 在使用时,只需要继承Mapper接口,并在<对象>填入对应,这个父类中包含了几乎所有的简单的增删改查
2) 所以简单的增删改查已经不需要再写了,只需要关注复杂的业务逻辑

父类中的方法有

  1. Select
  2. 方法:List<T> select(T record);
  3. 说明:根据实体中的属性值进行查询,查询条件使用等号
  4. 方法:T selectByPrimaryKey(Object key);
  5. 说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
  6. 方法:List<T> selectAll();
  7. 说明:查询全部结果,select(null)方法能达到同样的效果
  8. 方法:T selectOne(T record);
  9. 说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
  10. 方法:int selectCount(T record);
  11. 说明:根据实体中的属性查询总数,查询条件使用等号
  12. Insert
  13. 方法:int insert(T record);
  14. 说明:保存一个实体,null的属性也会保存,不会使用数据库默认值
  15. 方法:int insertSelective(T record);
  16. 说明:保存一个实体,null的属性不会保存,会使用数据库默认值
  17. Update
  18. 方法:int updateByPrimaryKey(T record);
  19. 说明:根据主键更新实体全部字段,null值会被更新
  20. 方法:int updateByPrimaryKeySelective(T record);
  21. 说明:根据主键更新属性不为null的值
  22. Delete
  23. 方法:int delete(T record);
  24. 说明:根据实体属性作为条件进行删除,查询条件使用等号
  25. 方法:int deleteByPrimaryKey(Object key);
  26. 说明:根据主键字段进行删除,方法参数必须包含完整的主键属性
  27. Example方法
  28. 方法:List<T> selectByExample(Object example);
  29. 说明:根据Example条件进行查询
  30. 重点:这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列
  31. 方法:int selectCountByExample(Object example);
  32. 说明:根据Example条件进行查询总数
  33. 方法:int updateByExample(@Param("record") T record, @Param("example") Object example);
  34. 说明:根据Example条件更新实体record包含的全部属性,null值会被更新
  35. 方法:int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
  36. 说明:根据Example条件更新实体record包含的不是null的属性值
  37. 方法:int deleteByExample(Object example);
  38. 说明:根据Example条件删除数据

Tags: JavaWeb  

评论区

    2024-04-25 17:31:54

    站长

    没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信


文章评论



给自个选个头像吧!






站点信息

  • 建站时间:   2019-01-31
  • 网站程序:   Tomcat+nginx
  • 文章统计:   44篇文章
  • 标签管理:   标签云
  • 微信公众号:  扫描二维码,联系我