代码改变世界

Mybatis框架 基础

2018-01-26 12:09  澄序源  阅读(226)  评论(0编辑  收藏  举报

 

思维导图

@有对应的例子

 

@1接入数据库 配置文件

<?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>
<!-- 使用外部文件传入连接数据库信息 -->
<properties resource ="conf/jdbc_oracle.properties" />
<!-- 简化 在需要写路经的地方可以直接简化掉com.ljk.model这些 -->
<typeAliases>
	<package name="com.ljk.model"/>
</typeAliases>
<!-- 连接数据库需要的信息 -->
	<environments default="test">
	    <environment id="test">
	      <transactionManager type="JDBC"/>
	      <dataSource type="POOLED">
	        <property name="driver" value="${jdbc.ClassName}"/>
	        <property name="url" value="${jdbc.url}"/>
	        <property name="username" value="${jdbc.name}"/>
	        <property name="password" value="${jdbc.password}"/>
	      </dataSource>
	    </environment>
  	</environments>
  	
  	<!--集合 映射文件-->
  	<mappers>
  		<!--找到 映射文件的地址-->
  		<!-- 定义的 映射文件 需要在这里把路经写出来 -->
   		<mapper resource="com/ljk/mapper/ClasMapper.xml"/>
   		<mapper resource="com/ljk/mapper/StudentMapper.xml"/>
  	</mappers>
</configuration>

 

@2 储存连入数据信息文件

jdbc.name=XXXX
jdbc.password=XXXX
jdbc.ClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl 

 

@3 获取SqlSession

/**连接数据库
 * @author 墨水瓶
 */
public class Mybatis_DB {
	private static SqlSessionFactory sqls;
	public static SqlSessionFactory getSqlSessionFactory() {
		//写连接数据库 的配置文件
		String s = "conf/Mybatis_config.xml";
		try {
			InputStream in = Resources.getResourceAsStream(s);
			sqls = new SqlSessionFactoryBuilder().build(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sqls;
	}
	/**调用这个
	 * @return 连接数据库
	 */
	public static SqlSession getSqls() {
		if(sqls == null) {
			sqls = getSqlSessionFactory();
		}
		return sqls.openSession();
	}
}

 

@4 级联

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.ljk.mapper.StudentMapper">

	<!-- 级联  -->
	<!--     类型为获取到的数据的类型   id与底下对应的select的resultMap一样      -->
	<resultMap type="Student" id="resultStudent">
          <!-- 一对一查询 返回的是一个clas类 -->> <association property="_class" column="CLASSID" select="com.ljk.mapper.ClassMapper.select_class"/> </resultMap> <!-- 值为想要连接的resultMap的id的值--> <select id="select_student" resultMap="resultStudent"> select * from student </select> </mapper>

以上为1对1的查询方式

1对多需要用到collection标签

使用级联 时 model 模型类里面的 模型中需要加入对应的属性

例 :

// 在clas 模型类中 需要有一个 Student的集合
// 因为一个班级包含多个学生
private List<Student> student;

//在Student 中 有一个Clas 类
// 因为 学生对应着只有一个班级
private Class _class;

 

@5 映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
	PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- mapper  name属性传入对应接口的限定名 -->
<mapper namespace="com.ljk.mapper.ClasMapper">

	<!--    id 对应接口中的方法名  resultType 返回的 类型是一个Clas类-->
	<select id="select_clas" resultType="Clas">
		<!-- 写sql语句 -->
		select * from class c where c.ids = #{ids}
	</select>
	
	<!-- 传值得时候是条件查询 不传值的时候是全表查询 -->
	<select id="select_dong" resultType="clas">
		<!-- 动态sql 语句 -->
		<!-- 可以根据传入值得不同执行不同的sql语句 -->
		select * from class c 
		<if test="ids != null">
			where c.ids = #{ids}
		</if>
	</select>
	
</mapper>

 

动态sql语句
    1, <if>条件
        <if test="key!=null">
            拼接sql语句
        </if>
    2, <choose><when><otherwise>
        <choose>
            <when test="key=='value'">
                拼接sql语句
            </when>
            <when test="key=='value'">
                拼接sql语句
            </when>
            <otherwise>
                拼接sql语句
            </otherwise>
        </choose>
    3, <where>
        自动添加where关键字
        如果where子句第一句中有 or 或者 and 则删除第一个
    4, <trim>
        功能与<where>类似, 并且提供了前缀, 后缀的添加, 更加灵活
    5, <foreach>
        用来遍历传入的集合参数
            item(定义集合中每个对象的名字),
            collection(集合的对象的名字),
            open(定义开始的字符),
            close(定义结束的字符),
            separator(定义分割的字符)
    6, <set>
        主要用于update
        自动加上set关键字
        自动剔除最后一个 ","
    7, <sql>
        经常用于一些常用或者固定的语句, 在外面定义一个语句, 在各种标签中引入
        使用include, 相当于直接写在上面
    8, <selectKey>
        用于不支持自增长主键的数据库, 尽量避免写这个东西

xml中的 符号
    &lt;    <    小于号                                           
    &gt;    >    大于号
    &amp;    &    和
    &apos;    ’    单引号
    &quot;    "    双引号


符号也可以写到中括号中间使用
    <![CDATA[]]>