myBatis学习的使用

添加配置mybatis-config

<?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="db.properties"/>
    <typeAliases>
        <package name="pojo"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="mapper"/>
    </mappers>
</configuration>

连接数据库配置 db.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&user=false&serverTimeZoneUTC
username=root
password=123456

开启一个数据库池子的工具类 

public class MyBatisUtils {
    public static SqlSessionFactory factory;

    static {
        String resource = "mybatis-config.xml";
        try {
            InputStream inputStream = Resources.getResourceAsStream(resource);
            factory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

添加pojo实体类,对应mapper.xml写sql语句

<?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="">

</mapper>

 多对一写法

 <select id="getStudent1" resultMap="getAll1">
        select *
        from test.user,
             test.teacher
        where test.teacher.tid = test.user.tid
          and user.tid = ${id};
    </select>

    <resultMap id="getAll1" type="User">
        <result property="tid" column="tid"/>
        <result property="id" column="id"/>
        <result property="name" column="name"/>

        <association property="teacher" javaType="Teacher">
            <result property="tid" column="tid"/>
            <result property="name" column="name"/>

        </association>
    </resultMap>

一对多写法

 <select id="getTeacher" resultMap="map">
        select *
        from test.user
                 join teacher t on t.tid = user.tid
        where t.tid = #{id}
    </select>

    <resultMap id="map" type="Teacher">
        <collection property="users" ofType="User">
            <result property="id" column="id"/>
            <result property="name" column="name"/>
        </collection>
    </resultMap>

各种关键字用法 

mapper namespace="mapper.BlogMapper">
    <cache/>
    <sql id="select">
        select *
        from test.blog
        <where>
            <choose>
                <when test="id!=null">
                    id=#{id};
                </when>
                <when test="title!=null">
                    title=#{title};
                </when>
            </choose>
        </where>

    </sql>

    <select id="queryBlogs" resultType="Blog">
        <include refid="select"/>
    </select>

    <update id="updateBlogs">
        update test.blog
        <set>
            <if test="title!=null">
                title=#{title},
            </if>
        </set>
        where id=#{id};

    </update>

    <select id="queryBlog1" resultType="Blog">
        select *from test.blog
        <where>

            <foreach collection="list" item="id" open="(" close=")" separator="or">
                id=#{id}
            </foreach>
        </where>
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值