Mapper是刘增辉开发的一个通用的插件功能和MyBatis Generator类似,
通用Mapper都可以极大的方便开发人员。可以随意的按照自己的需要选择通用方法,还可以很方便的开发自己的通
用方法。
极其方便的使用MyBatis单表的增删改查。
支持单表操作,不支持通用的多表联合查询。
<!--通用mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.3.9</version>
</dependency>
/*确定主键 和 关系型数据库 JDBC*/
@Id
@GeneratedValue(generator = "JDBC")
private int id;
/* 别名 entity 变量名 与sql 语句中的属性对应 */
@Column(name = "name")
private String name;
private int age;
private int height;
private String sex;
public Student() {
}
public Student(String name, int age, int height, String sex) {
this.name = name;
this.age = age;
this.height = height;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", height=" + height +
", sex='" + sex + '\'' +
'}';
}
/* 继承Mapper jar 对应的实体 */
public interface IStudent extends Mapper<Student> {
}
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=mybatis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 开启懒加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 开启驼峰写法 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
<!-- 一级缓存默认关闭 -->
<setting name="localCacheScope" value="STATEMENT"/>
<!--解决 懒加载时 打印对象toString 触发 懒加载
lazyLoadTriggerMethods:指定哪个对象的方法触发一次延迟加载。默认值:equals,clone,hashCode,toString
-->
<setting name="lazyLoadTriggerMethods" value="false"/>
</settings>
<!-- 别名 必须environments 之前 不区分大小写 -->
<typeAliases>
<!--<typeAlias type="com.aaa.entity.Student" alias="student"></typeAlias>-->
<package name="com.aaa.entity"/>
</typeAliases>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 使用MySQL方言的分页 -->
<property name="helperDialect" value="mysql"/><!--如果使用mysql,这里value为mysql-->
<property name="pageSizeZero" value="true"/>
</plugin>
</plugins>
<!-- 1 配置mybatis 连接-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/student_db"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--
2. 向mybatis 声明mapper 必须写在 environments下面
-->
<mappers>
<!--<mapper class="com.aaa.dao.IStudentDao"></mapper>-->
<!--<mapper resource="com/aaa/dao/IStudentDao.xml"></mapper>-->
<!-- 扫描包下面所有mapper 并在mybatis 声明 -->
<package name="com.aaa.dao"/>
</mappers>
</configuration>
@NameStyle(value = Style.normal)
normal, //原值
camelhump, //驼峰转下划线
uppercase, //转换为大写
lowercase, //转换为小写
camelhumpAndUppercase, //驼峰转下划线大写形式
camelhumpAndLowercase, //驼峰转下划线小写形式
@Table(name = “student_tb”) :标记实体类对应得表名称
@Id @GeneratedValue(generator = “JDBC”) :标记为主键,并且自增
@Column(name=“name”) 标记 当表字段与java字段不一致也不符合驼峰写法的映射
@Transient 表明不是数据表中的字段
public class MapperTest {
private SqlSession sqlSession;
@Before
public void init() {
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream("mybatisconfig.xml");
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = builder.build(inputStream);
sqlSession = sqlSessionFactory.openSession(true);
MapperHelper mapperHelper = new MapperHelper();
mapperHelper.registerMapper(Mapper.class);
mapperHelper.processConfiguration(sqlSession.getConfiguration());
}
@Test
public void Test1(){
IStudent iStudent = sqlSession.getMapper(IStudent.class);
List<Student> stub = iStudent.selectAll();
for (Student student : stub){
System.out.println(student);
}
}
}
distinct字段用于指定DISTINCT查询。
orderByClause字段用于指定ORDER BY条件,这个条件没有构造方法,直接通过传递字符串值指定。
oredCriteria字段用于自定义查询条件。
这个类是专门用来对这个单表来查询的类,对该单表的CURD操作是脱离sql性质的(已经通过逆向工程生成相应的sql),
直接在service层就可以完成相应操作。
逆向工程生成的文件XxxExample.java中包含一个static 的内部类 Criteria ,在Criteria中有很多方法,主要
是定义SQL 语句where后的查询条件。
通过用mapper