一、Mybatis动态语句分为4种元素
①if元素
②where 元素
?③choose(?? when???? otherwise)
?
④set用在update上
⑤foreache遍历
?二、方法传递多个参数时如何为参数起名
三、mybatis处理特殊字符
(1)使用转义字符替换
?
<select id="selectFw" resultType="com.ykq.entity.Emp">
select * from tbl_emp where empId>#{min} and empId <#{max}
</select>
(2)CDATA内容会原翻不动的解析<![CDATA[]]>
<select id="selectFw" resultType="com.ykq.entity.Emp">
<![CDATA[ select * from tbl_emp where empId>#{min} and empId <=#{max}]]>
</select>
四、pagehelper分页插件---完成分页查询
使用步骤:
①引入相关依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
②在mybatis配置文件中引入插件
③使用分页的功能
public static void main(String[] args) throws Exception{
Reader resourceAsReader = Resources.getResourceAsReader("mybatis-conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsReader);
SqlSession sqlSession = sessionFactory.openSession();
FoodMapper foodMapper = sqlSession.getMapper(FoodMapper.class);
PageHelper.startPage(2, 3);
List<Food> all = foodMapper.findAll();
PageInfo<Food> pageInfo=new PageInfo<Food>(all);
System.out.println("当前总条数:"+pageInfo.getTotal());
System.out.println("当前页码:"+pageInfo.getPageNum());
System.out.println("当前记录:"+pageInfo.getList());
System.out.println("总页数:"+pageInfo.getPages());
}
五、代码生成器----单表查询crud---generator
①引入依赖
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
②generator的配置文件---必须放在工程下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--数据库驱动包所在的路径 我建议写成绝对路径-->
<classPathEntry location="D:\repMaven\mysql\mysql-connector-java\8.0.20\mysql-connector-java-8.0.20.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--注释的生成-->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接信息-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis02?serverTimezone=Asia/Shanghai"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--生成的实体类所在的包-->
<javaModelGenerator targetPackage="com.ykq.entity" targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--映射文件存在目录-->
<sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--dao接口所在的包-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.ykq.dao" targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--表
schema:表所在的数据库
tableName:表名
domainObjectName:实体类得名称
-->
<table schema="mybatis02" tableName="tbl_emp" domainObjectName="Emp"
enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByExample="false"
enableCountByExample="false"/>
<table schema="mybatis02" tableName="tbl_dept" domainObjectName="Dept"
enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByExample="false"
enableCountByExample="false"/>
</context>
</generatorConfiguration>
?③测试
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generator-mybatis.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
六、二级缓存
缓存是基于内存存储,为了减少对数据库的访问频率
mybatis默认支持一级缓存,基于session执行
开启二级缓存---默认不开启
?
|