基于Maven的Mybatis入门

一、新建一个Maven项目
这里写图片描述
二、在pom.xml中添加依赖关系

<dependencies>
     <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.3.0</version>
    </dependency>
    <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
     </dependency>
  </dependencies>

三、新建一个mybatis配置文件
这里写图片描述

在配置文件中设置数据库连接池(效率低,真实开发中不适用)以及配置映射关系

<?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">
                            <property name="driver" value="com.mysql.jdbc.Driver" />
                            <property name="url" value="jdbc:mysql://localhost/test" />
                            <property name="username" value="root" />
                            <property name="password" value="root" />
                        </dataSource>
                    </environment>
                </environments>

                <!-- 映射关系 -->
                <mappers>
                    <mapper resource="com/xingxue/user/GoodsMapper.xml" />
                    <mapper resource="com/xingxue/user/UserMapper.xml" />
                </mappers>

            </configuration>

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

        <select id="selectGoodsById" resultType="java.util.HashMap">
            select * from goods where id = #{id}
        </select>

        <select id="selectGoodsByPage" resultType="java.util.HashMap">
            select * from goods 
        </select>
        <!-- 动态SQL -->
        <select id="updateGoodsByAll">
            UPDATE `test`.`goods`
                SET
                <if test="id != null">
                 `id` = #{id}
                </if>
                <if test="name != null">
                 ,`name` = #{name}
                </if>
                <if test="price != null">
                 ,`price` = #{price}
                </if>
                <if test="statu != null">
                 ,`statu` = #{statu}
                </if>
                <if test="date != null">
                 ,`date` = #{date}
                </if>
                <if test="typeid != null">
                 ,`typeid` = #{typeid}
                </if>
                <if test="num != null">
                 ,`num` = #{num}
                </if>
            WHERE
                (`id` = #{id})
        </select>

        <select id="updateGoods">
            update goods set name='zhangsan' where id in
            <foreach collection="list" open="(" separator="," close=")" item="ids">
                #{ids}
            </foreach>
        </select>

    </mapper>   

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试