maven02——通用mapper的简单应用01

通用 mapper 有什么好处呢?

以往我们使用 mapper 文件都是自己写 sql 语句,针对的是单个实体,也就是每个实体都有其对应的 mapper 文件。使用通用mapper 给我们带来了极大的方便,它不需要mapper.xml 文件,只需我们调用相应的接口,引入 jar 包再进行简单的配置就好了。

配置通用mapper
1、导入mapper的包

通用mapper的包在maven中的路径如下

<!-- 通用mapper -->
		<dependency>
			<groupId>com.github.abel533</groupId>
			<artifactId>mapper</artifactId>
			<version>2.3.4</version>
		</dependency>
		<dependency>
			<groupId>com.github.jsqlparser</groupId>
			<artifactId>jsqlparser</artifactId>
			<version>0.9.5</version>
		</dependency>
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>5.0.0</version>
		</dependency> 

将他写在pom.xml配置文件里面

配置到applicationContext.xml(spring.xml)文件中

通用mapper类似于插件,将他放在关于会话工厂的那一块(配置了两个插件,一个是通用mapper,一个是分页)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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">

	<!-- 1.会话工厂 2.mapper代理bean 3.service 4.事务管理 5.数据源 -->
	
	<!-- 1.加载外部资源文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 2.使用c3p0数据库连接池 -->
	<bean id = "dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value = "${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
		
	<!-- 3.会话工厂配置 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 起别名 -->
		<property name="typeAliasesPackage" value= "com.cbb.pojo"></property>
		<!-- 映射文件位置 -->
		<property name="mapperLocations" value="classpath:mybatis/*.xml"></property>
		<!-- 通用mapper插件 -->
		<property name="plugins">
			<array>
				<bean class="com.github.pagehelper.PageInterceptor">
					<property name="properties">
						<value>
							helperDialect=mysql
							offsetAsPageNum=true
							<!-- 防止出现小于第一页,大于最后一页的异常情况出现。 -->
							reasonable=true
						</value>
					</property>
				</bean>
				<bean class="com.github.abel533.mapperhelper.MapperInterceptor">
					<property name="properties">
						<value>
							<!-- 主键自增回写方法,默认值MYSQL -->
							IDENTITY=MYSQL
							mappers=com.github.abel533.mapper.Mapper
						</value>
					</property>
				</bean>
			</array>
		</property>
	</bean>
	
	<!-- 4.mapper代理bean -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 指定接口所在的包 -->
		<property name="basePackage" value="com.cbb.mapper"></property>
	</bean>
	
	<!-- 5.service bean -->
	<context:component-scan base-package="com.cbb.service"></context:component-scan>
	<!-- 6.spring事务管理:6.1配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 6.2 事务注解驱动 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

我们既然不需要写mapper.xml文件了,那么就需要配置好映射关系
就是在实体类上进行注释

3、在实体类上进行配置

@Table(name=“user”)//映射的表
@Id //不能省略
@Column(name =“username”) //和表中字段的映射。如果名字相同,可省略

package com.cbb.pojo;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/** 
 * 类描述:User实体类
 * 作者: 地铁与人海
 * 创建日期:2019年3月7日
 * 修改人:
 * 修改日期:
 * 修改内容:
 * 版本号: 1.0.0   
 */

@Table(name="user")//映射的表
public class User {
	
	@Id		//不能省略
	@GeneratedValue(strategy=GenerationType.IDENTITY)使用数据库的主键生成策略,大部分时候我们都是由数据库进行生成主键(如id)
	private int id;
	
	@Column(name ="username")	//和表中字段的映射。如果名字相同,可省略
	private String username;	
	private Date birthday;
	private String sex;
	private String address;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address="
				+ address + "]";
	}
	public User(int id, String username, Date birthday, String sex, String address) {
		super();
		this.id = id;
		this.username = username;
		this.birthday = birthday;
		this.sex = sex;
		this.address = address;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(String username, Date birthday, String sex, String address) {
		super();
		this.username = username;
		this.birthday = birthday;
		this.sex = sex;
		this.address = address;
	}
}
详细介绍

1、表名默认使用类名,驼峰转下划线,如 UserInfo 默认对应的表名为 user_info.
2、表名可以使用@Table(name = “tableName”)进行指定,对不符合第一条默认规则的可以通过这种方式指定表名.
3、字段默认和@Column 一样,都会作为表字段,表字段默认为 Java 对象的 Field 名字驼峰转下划线形式.(变量名 userName --user_name)
4、可以使用@Column(name = “fieldName”)指定不符合第 3 条规则的字段名
5、使用@Transient 注解可以忽略字段,添加该注解的字段不会作为表字段使用.
6、建议一定是有一个@Id 注解作为主键的字段,可以有多个@Id 注解的字段作为联合主键.
默认情况下,实体类中如果不存在包含@Id 注解的字段,所有的字段都会作为主键字段进行使用(这种效率极低).
7、由于基本类型,如 int 作为实体类字段时会有默认值 0,而且无法消除,所以实体类中建议不要使用基本类型.,使用 Interger,Double
8、除了上面提到的这些,Mapper 还提供了序列(支持 Oracle)、UUID(任意数据库,字段长度32)、主键自增(类似 Mysql,Hsqldb)三种方式,其中序列和 UUID 可以配置多个,主键自增只能配置一个。
使用主键自增://不限于@Id 注解的字段,但是一个实体类中只能存在一个(继承关系中也只能存在一个)

mapper接口继承 Mapper<T> 接口

mapper接口需要继承Mapper<T>接口,如下面(方法是我自己写的几个方法)

package com.cbb.mapper;

import java.util.List;
import java.util.Map;

import com.cbb.pojo.User;
import com.github.abel533.mapper.Mapper;

/** 
 * 类描述:
 * 作者: 地铁与人海
 * 创建日期:2019年3月20日
 * 修改人:
 * 修改日期:
 * 修改内容:
 * 版本号: 1.0.0   
 */

public interface UserMapper extends Mapper<User>{

	/** 
	 * 方法描述:根据id查询
	 * @param id  用户id
	 * @return
	 */
	public User selectById(int id);
	
	/** 
	 * 方法描述:根据条件查找记录
	 * @param map
	 * @return
	 */
	public List<User> selectIf( Map<String,Object> map);
	
	/** 
	 * 方法描述:根据id删除数据
	 * @param id
	 * @return
	 */
	public int delete1(int id);
	
	/** 
	 * 方法描述:修改用户信息
	 * @param user
	 * @return
	 */
	public int update1(User user);
	
	/** 
	 * 方法描述:添加用户信息
	 * @param user
	 * @return
	 */
	public int insert1(User user);
	
	/** 
	 * 方法描述:根据传过来的id列表来进行删除
	 * @param ids
	 * @return
	 */
	public int deleteAll(List<Integer> ids);
	
}

继承的 Mapper 接口具有了以下方法:

方法
具体方法解释
https://blog.csdn.net/qq_22978533/article/details/73822941
这里可以看一下,有些方法比较复杂,也可以自己写,通用mapper和自己写mapper不冲突。
然后调用方法即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值