MyBatis学习笔记

1、建立maven工程,pom.xml中添加MyBatis依赖

 <!--导入依赖-->
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

记得解决静态资源过滤问题,在pom.xml build中配置resources

 <build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
    </build>

2、编写MyBatis工具类 获取sqlSession对象

//sqlSessionFactory
public class MyBatisUtils {

	private static SqlSessionFactory sqlSessionFactory;

	static {
		try {
			//使用Mybatis第一步 获取sqlSession
			String resource = "mybatis-config.xml";
			InputStream inputStream = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//获取SqlSession连接
	public static SqlSession getSession() {
		return sqlSessionFactory.openSession();

	}
}

3、编写mybatis的配置文件mybatis-config.xml

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <!--连接数据库的信息 创建Connection对象-->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8"/>
                <property name="username" value="root"/>
                <property name="password" value="843878"/>
            </dataSource>
        </environment>
    </environments>

4、创建实体类
5、编写Mapper接口类
6、编写Mapper.xml配置文件 namespace不能写错

一对多 多对一 嵌套查询
现有学生、老师两张表
在这里插入图片描述
在这里插入图片描述
多对一:查询所有学生的基本信息及学生所对应的的老师的信息

 <select id="getStudent2" resultMap="StudentTeacher2">
        select s.id  sid,s.name sname,t.name tname
        from student s,teacher t
        where s.tid=t.id;
    </select>

    <resultMap id="StudentTeacher2" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
         <!--复杂的属性单独处理
        对象:associaton 集合:collection
        -->
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>

一对多:查询一个老师的信息及老师带的学生信息

<!--按结果嵌套查询-->
    <select id="getTeacher" resultMap="TeacherStudent">
        select s.id sid,s.name sname,t.name tname,t.id tid
        from student s ,teacher t
        where s.tid=t.id and t.id=#{tid}

    </select>

    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>

        <!--复杂的属性需要单独处理 对象:assosiation 集合:collection
        javaType="指定属性的类型"
        集合中的泛型类型信息:使用oftype获取
        -->

       <collection property="students" ofType="Student">
           <result property="id" column="sid"/>
           <result property="name" column="sname"/>
           <result property="tid" column="tid"/>
       </collection>

    </resultMap>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值