mysql通用mapper_通用Mapper(Mybatis)

本文介绍了Mybatis通用Mapper的使用方法,包括导入依赖、配置插件、继承Mapper接口、配置实体类与表的关系,以及如何进行增删改查操作。通用Mapper通过Mybatis的拦截器实现了单表的自动化操作,简化了开发流程。
摘要由CSDN通过智能技术生成

1.Mapper的简单介绍

6ba5ee58786ff08fd37ba73253f09c40.png

2.Mapper的作用

通用Mapper可以通过Mybatis的拦截器原理,动态的帮我们实现单表的增删改查功能,大大降低了我们的开发成本,减少了我们的工作量。

3.Mapper的使用

该Mapper是一个接口,里面提供给了一系列的方法,导入依赖,配置插件,利用Mybatis使用通用的Mapper接口,首先继承该接口,泛型为类中的类型。这样就没有配置文件了,所以要在Mybatis核心配置文件中,配置扫描的包。编写测试类。

3.1导入依赖(首先搭建好Mybatis的环境)

com.github.abel533

mapper

2.3.4

3.2配置插件(Mapper在Mybatis的拦截器)

3.3编写接口UserMapper继承Mapper(略)

3.4配置实体类与表之间的关系

b9ba384d1aaaf73e34dc71dea62362ba.png

packagecn.mybatis.pojo;importjava.io.Serializable;importjava.util.Date;importjavax.persistence.Column;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.Table;

@Table(name="tb_user")public class User implementsSerializable{/****/

private static final long serialVersionUID = 1L;

@Id

@GeneratedValue(strategy=GenerationType.IDENTITY)privateLong id;//用户名

privateString userName;//密码

privateString password;//姓名

privateString name;//年龄

privateInteger age;//性别,1男性,2女性

privateInteger sex;//出生日期

privateDate birthday;//创建时间

privateDate created;//更新时间

privateDate updated;publicLong getId() {returnid;

}public voidsetId(Long id) {this.id =id;

}publicString getuserName() {returnuserName;

}public voidsetuserName(String userName) {this.userName =userName;

}publicString getPassword() {returnpassword;

}public voidsetPassword(String password) {this.password =password;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicInteger getAge() {returnage;

}public voidsetAge(Integer age) {this.age =age;

}publicInteger getSex() {returnsex;

}public voidsetSex(Integer sex) {this.sex =sex;

}publicDate getBirthday() {returnbirthday;

}public voidsetBirthday(Date birthday) {this.birthday =birthday;

}publicDate getCreated() {returncreated;

}public voidsetCreated(Date created) {this.created =created;

}publicDate getUpdated() {returnupdated;

}public voidsetUpdated(Date updated) {this.updated =updated;

}

@OverridepublicString toString() {return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", name=" +name+ ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", created=" +created+ ", updated=" + updated + "]";

}

}

3.5.编写测试类

packagecn.mapper.test;importjava.io.InputStream;importjava.util.List;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSession;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;importorg.junit.Before;importorg.junit.Test;importcn.mybatis.dao.UserMapper;importcn.mybatis.pojo.User;importcom.github.abel533.entity.Example;public classTestMappers {

UserMapper usermapper;

String resource= "mybatis-config.xml";

InputStream inputStream;

SqlSessionFactory sqlsessionFactory;

SqlSession sqlSession;

@Beforepublic void setUp() throwsException {

inputStream=Resources.getResourceAsStream(resource);

sqlsessionFactory= newSqlSessionFactoryBuilder().build(inputStream);

sqlSession=sqlsessionFactory.openSession();//动态代理实现

usermapper = this.sqlSession.getMapper(UserMapper.class);

}/*** 查询1个用户*/@Testpublic voidtest() {

User user= newUser();

user.setuserName("zhangsan");

User one= this.usermapper.selectOne(user);

System.out.println(one);

}/*** 查找用户,接收一个User对象。*/@Testpublic voidtestselect(){

User user= newUser();

user.setSex(2);

List list = this.usermapper.select(null);for(User user2 : list) {

System.out.println(user2);

}

System.out.println("-------------");

List list2 = this.usermapper.select(user);for(User user2 : list2) {

System.out.println(user2);

}

}/*** 查询总条数*/@Testpublic voidtestSlecteCount(){int count = this.usermapper.selectCount(null);

System.out.println(count);

System.out.println("--------");

User user= newUser();

user.setSex(1);int count2 = this.usermapper.selectCount(user);

System.out.println(count2);

}/*** 根据主键查询*/@Testpublic voidtestSelectByPrimaryKey(){

User user= this.usermapper.selectByPrimaryKey(1L);

System.out.println(user);

}

@Testpublic voidtestInsert(){

User user= newUser();

user.setAge(22);

user.setPassword("11111");//this.usermapper.insert(user);

this.usermapper.insertSelective(user);//提交

this.sqlSession.commit();

}

@Testpublic voidtestDelete(){

User user= newUser();

user.setAge(22);int i = this.usermapper.delete(user);this.sqlSession.commit();

System.out.println(i);

}

@Testpublic voidtestUpdate(){

User user= newUser();

user.setId(18L);

user.setuserName("kk0");;int i = this.usermapper.updateByPrimaryKeySelective(user);this.sqlSession.commit();

System.out.println(i);

}

@Testpublic voidtestSelectByExample(){

Example example= new Example(User.class);

example.createCriteria().andEqualTo("sex", 1).andBetween("age", 10, 22);

example.or(example.createCriteria().andLike("name", "李"));

example.setOrderByClause("age desc");

List list = this.usermapper.selectByExample(example);for(User user : list) {

System.out.println(user);

}

}

}

总结:通用mapper 的要求:

1.默认表名为类名(@Table())

2.默认字段名就是类属性名(在驼峰规则内),如果不一致,使用@Column注解

3.主键字段必须声明@Id

4.实现ID的回写@GeneratedValue(strategy=GenerationType.IDENTITY)

5.忽略该字段@Transtient(映射字段)  表中没有的字段 ,在类中有的字段

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值