您现在的位置是:首页 > 个人日记个人日记
spring&mybatis-通用mapper使用方法
2019-02-07 13:26:11【个人日记】603人已围观
简介介绍通用mapper的使用方法
通用mapper使用方法
jar包为
↓
<dependency><groupId>tk.mybatis</groupId><artifactId>mapper</artifactId><version>4.1.4</version></dependency>
版本根据情况使用
在spring-config.xml配置中
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"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"><!--扫描注解--><context:component-scan base-package="com.huangxin.order"></context:component-scan><!--AOP注解生效--><aop:aspectj-autoproxy></aop:aspectj-autoproxy><!--加载配置文件--><context:property-placeholder location="property/jdbc.properties"></context:property-placeholder><!--druid连接池--><bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!--SqlSessionFactory工厂交给spring--><bean id="sqlSessionFactory" name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!--将数据源注入SqlSessionFactory--><property name="dataSource" ref="dateSource"></property><!--引入映射--><property name="mapperLocations" value="classpath:mapper/*.xml"></property></bean><!--让spring生成mapper接口的实现类--><!--多个mapper接口--><!--MapperScannerConfigurer将扫描所有包下的mapper--><!--tk.mybatis.spring.mapper.MapperScannerConfigurer为插件,增强官方--><!--org.mybatis.spring.mapper.MapperScannerConfigurer为官方扫描器--><bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property><!--对哪些包下的类生成mapper代理类--><property name="basePackage" value="com.huangxin.order.mapper"></property></bean><!--事务管理--><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dateSource"></property></bean><!--将事务管理器交给spring管理--><!--使用注解方式,分散方式,xml配置为集中配置--><!--如果使用注解则不使用xml配置--><tx:annotation-driven transaction-manager="txManager"/><!--配置事务增强--><!--isolation事务隔离级别--><!--propagation传播行为--><!--注意命名规范--><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="get*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/><tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/></tx:attributes></tx:advice></beans>
以上只需要改动这些,也就是将org.mybatis.spring.mapper.MapperScannerConfigurer替换为tk.mybatis.spring.mapper.MapperScannerConfigurer
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property><!--对哪些包下的类生成mapper代理类--><property name="basePackage" value="com.huangxin.order.mapper"></property></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) 所以简单的增删改查已经不需要再写了,只需要关注复杂的业务逻辑
父类中的方法有
Select方法:List<T> select(T record);说明:根据实体中的属性值进行查询,查询条件使用等号方法:T selectByPrimaryKey(Object key);说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号方法:List<T> selectAll();说明:查询全部结果,select(null)方法能达到同样的效果方法:T selectOne(T record);说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号方法:int selectCount(T record);说明:根据实体中的属性查询总数,查询条件使用等号Insert方法:int insert(T record);说明:保存一个实体,null的属性也会保存,不会使用数据库默认值方法:int insertSelective(T record);说明:保存一个实体,null的属性不会保存,会使用数据库默认值Update方法:int updateByPrimaryKey(T record);说明:根据主键更新实体全部字段,null值会被更新方法:int updateByPrimaryKeySelective(T record);说明:根据主键更新属性不为null的值Delete方法:int delete(T record);说明:根据实体属性作为条件进行删除,查询条件使用等号方法:int deleteByPrimaryKey(Object key);说明:根据主键字段进行删除,方法参数必须包含完整的主键属性Example方法方法:List<T> selectByExample(Object example);说明:根据Example条件进行查询重点:这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列方法:int selectCountByExample(Object example);说明:根据Example条件进行查询总数方法:int updateByExample(@Param("record") T record, @Param("example") Object example);说明:根据Example条件更新实体record包含的全部属性,null值会被更新方法:int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);说明:根据Example条件更新实体record包含的不是null的属性值方法:int deleteByExample(Object example);说明:根据Example条件删除数据
Tags: JavaWeb
上一篇: mybatis-03-Mapper代理
下一篇: mybatis-06-映射关系
相关文章
随机图文
评论区
2025-12-05 05:34:48
站长
没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信
