1.Mapper的简单介绍
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配置实体类与表之间的关系
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(映射字段) 表中没有的字段 ,在类中有的字段