1.9 ResultMap : 字段 和 属性名 的对应关系****
字段 和 属性名 的对应关系
alter table student rename column stuno to sno; alter table student rename column stuname to sname; alter table student rename column stuage to sage; alter table student rename column graname to gname;
字段名 属性名
sno - stuNo alter table student add constraint pk_student3 primary key(sno);
在resultMap中 还可以使用鉴别器:对相同sql中不同字段值进行判断,从而进行不同的 处理。
* **<id column="sno" property="stuNo"/> **<result column="sage" property="stuAge"/> <result column="gname" property="graName"/> ***<discriminator javaType="string" column="gname"> <result column="sname" property="stuName"/> <result column="nickname" property="stuName"/>
1.10 别名问题****
主配置文件中:****
*** *** ****
如果在批量设置别名时,出现了冲突。可以使用@Alias("myStudent")区分。****
1.11 SQL标签****
可以处理拼接sql中 【开头】第一个and 可以处理拼接sql中 【开头或结尾】第一个and 开头: 给拼接的SQL加prefix="where" prefixOverrides="and",处理拼接SQL中【开头】第一个and suffixOverrides="and",处理拼接SQL中【结尾】最后一个and select * from student where stuname like '%s%' and stuage = 23 and graname like '%b%' ; prefix : 拼接 prefixOverrides:删除 * *<update id="updateStudentByNo" parameterType="com.yanqun.entity.Student"> update student stuName=#{stuName}, stuAge=#{stuAge}, graName=#{graName} where stuNo=#{stuNo}
* <select id="queryStudentByNoWithONGL" parameterType="student" resultType="student" databaseId="oracle"> ***select *** from student <if test="_parameter.stuName != null and _parameter.stuName !='' "> stuName like #{_queryName} and <if test="graName != null and graName !='' "> graName like '%${graName}%' and <if test="stuAge != null and stuAge !='' "> stuAge = #{stuAge} and
1.12 内置参数****
_parameter: 代表mybatis的输入参数。********
_databaseId: 代表当前数据库的 名字****
<select id="queryStudentByNo" resultType="com.yanqun.entity.Student" parameterType="int"> select *** from student where stuNo = #{stuNo} select *** from student where stuNo = #{_parameter} select *** from student where stuNo != #{_parameter}
1.13 模糊查询****
1.13.1 ${}****
${} :原样输出
stuName like '%${stuName}%'
1.13.2 #{}****
b.传值时,直接传
student.setStuName("%s%"); stuName like #{stuName}
1.13.3 bind参数****
c.bind参数
通过bind将传入的stuName进行了处理(增加了%...%)
<select id="queryStudentByNoWithONGL" parameterType="student" resultType="student" databaseId="oracle"> select *** from student < bind name ="_queryName" value ="'%'+stuName+'%'" /> ** <if test="_parameter.stuName != null and _parameter.stuName !='' "> stuName like #{_queryName} and <if test="graName != null and graName !='' "> graName like '%${graName}%' and <if test="stuAge != null and stuAge !='' "> stuAge = #{stuAge} and * *
1.14 逆向工程的使用***
1.14.1 jar
1.14.2 *xml模板文件(修改生成路径、表名)
模板: generator.xml 放在src下****
* * *** * **<jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:ORCL" userId="scott" password="tiger"> ** **<javaModelGenerator targetPackage="com.yanqun.entity" targetProject=".\src"> *** * **<sqlMapGenerator targetPackage="com.yanqun.mapper" targetProject=".\src"> ***<javaClientGenerator type="XMLMAPPER" targetPackage="com.yanqun.mapper" targetProject=".\src"> * ***
1.14.3 根据java模板类 一键生成
根据学生表 ->学生类、学生Mapper接口、studentMapper.xml****
public class MyBatis_Generator { public static void main(String[] args) throws Exception { File f = new File("src/generator.xml"); List warnings = new ArrayList(); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(f); DefaultShellCallback callBack = new DefaultShellCallback(true); MyBatisGenerator generator = new MyBatisGenerator(config, callBack, warnings); generator.generate(null); } }****
1.14.4 如何使用
增加 mybatis配置文件 conf.xml
对于like模糊查询,逆向工程需要在传值时 写入%x%
public static void main(String[] args) throws IOException { String resource = "conf.xml"; Reader reader = Resources.getResourceAsReader(resource); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); StudentMapper mapper = session.getMapper(StudentMapper.class); //Example 中的 Criteria :查询条件* **// List students = mapper.selectByExample(null) ; ** // 规则: example 默认使用的是 第一个 criteria **StudentExample example = new StudentExample() ; StudentExample.Criteria criteria = example.createCriteria(); // criteria.andStunoBetween((short) 32, (short) 33);// stuno: 2-3 **criteria.andStunameLike("%l%"); //where (xx=xx and xx =x) or (xx =xxx and xx =xx) ; ** //where stuname like '%z%' or ( stuno <=31 and granameLike "%j%) ; //criteria:where stuname like '%z%' // or //criteria: stuno <=31 and granameLike "%j% ; **StudentExample.Criteria criteria1 = example.createCriteria(); criteria1.andStunoLessThanOrEqualTo((short)31) ; //<= **criteria1.andGranameLike("%j%") ; example.or(criteria1) ; //query by Criteria , QBC ***List students = mapper.selectByExample(example ) ; System.out.println(students ); session.close(); }