通用Mapper(Mybatis)
1.Mapper的简单介绍
2.Mapper的作用
通用Mapper可以通过Mybatis的拦截器原理,动态的帮我们实现单表的增删改查功能,大大降低了我们的开发成本,减少了我们的工作量。
3.Mapper的使用
该Mapper是一个接口,里面提供给了一系列的方法,导入依赖,配置插件,利用Mybatis使用通用的Mapper接口,首先继承该接口,泛型为类中的类型。这样就没有配置文件了,所以要在Mybatis核心配置文件中,配置扫描的包。编写测试类。
3.1导入依赖(首先搭建好Mybatis的环境)
<!-- 通用Mapper --> <dependency> <groupId>com.github.abel533</groupId> <artifactId>mapper</artifactId> <version>2.3.4</version> </dependency>
3.2配置插件(Mapper在Mybatis的拦截器)
<!-- 通用Mapper的插件 --> <plugins> <plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor"> <!--主键自增回写方法,默认值MYSQL --> <property name="IDENTITY" value="MYSQL" /> <!--通用Mapper默认接口,我们定义的Mapper需要实现该接口 --> <property name="mappers" value="com.github.abel533.mapper.Mapper" /> </plugin> </plugins>
3.3编写接口UserMapper继承Mapper(略)
3.4配置实体类与表之间的关系
package cn.mybatis.pojo; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Table(name="tb_user") public class User implements Serializable{ /** * */ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; // 用户名 private String userName; // 密码 private String password; // 姓名 private String name; // 年龄 private Integer age; // 性别,1男性,2女性 private Integer sex; // 出生日期 private Date birthday; // 创建时间 private Date created; // 更新时间 private Date updated; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getuserName() { return userName; } public void setuserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public Date getUpdated() { return updated; } public void setUpdated(Date updated) { this.updated = updated; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", name=" + name + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", created=" + created + ", updated=" + updated + "]"; } }
3.5.编写测试类
package cn.mapper.test; import java.io.InputStream; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; import cn.mybatis.dao.UserMapper; import cn.mybatis.pojo.User; import com.github.abel533.entity.Example; public class TestMappers { UserMapper usermapper; String resource = "mybatis-config.xml"; InputStream inputStream; SqlSessionFactory sqlsessionFactory; SqlSession sqlSession; @Before public void setUp() throws Exception { inputStream = Resources.getResourceAsStream(resource); sqlsessionFactory = new SqlSessionFactoryBuilder().build(inputStream); sqlSession = sqlsessionFactory.openSession(); // 动态代理实现 usermapper = this.sqlSession.getMapper(UserMapper.class); } /** * 查询1个用户 */ @Test public void test() { User user = new User(); user.setuserName("zhangsan"); User one = this.usermapper.selectOne(user); System.out.println(one); } /** * 查找用户,接收一个User对象。 */ @Test public void testselect(){ User user = new User(); user.setSex(2); List<User> list = this.usermapper.select(null); for (User user2 : list) { System.out.println(user2); } System.out.println("-------------"); List<User> list2 = this.usermapper.select(user); for (User user2 : list2) { System.out.println(user2); } } /** * 查询总条数 */ @Test public void testSlecteCount(){ int count = this.usermapper.selectCount(null); System.out.println(count); System.out.println("--------"); User user = new User(); user.setSex(1); int count2 = this.usermapper.selectCount(user); System.out.println(count2); } /** * 根据主键查询 */ @Test public void testSelectByPrimaryKey(){ User user = this.usermapper.selectByPrimaryKey(1L); System.out.println(user); } @Test public void testInsert(){ User user = new User(); user.setAge(22); user.setPassword("11111"); // this.usermapper.insert(user); this.usermapper.insertSelective(user); // 提交 this.sqlSession.commit(); } @Test public void testDelete(){ User user = new User(); user.setAge(22); int i = this.usermapper.delete(user); this.sqlSession.commit(); System.out.println(i); } @Test public void testUpdate(){ User user = new User(); user.setId(18L); user.setuserName("kk0");; int i = this.usermapper.updateByPrimaryKeySelective(user); this.sqlSession.commit(); System.out.println(i); } @Test public void testSelectByExample(){ 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<User> 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(映射字段) 表中没有的字段 ,在类中有的字段
今天要比昨天好