mybatis基本

2020-12-21  本文已影响0人  Raral

mybatis 基本

01三层架构.png

入门

注意事项

mybatis注意事项.png
  1. 把resources中的xml文件删除
  2. 在 resources/SqlMapConfig.xml 使用class属性
 <!--映射配置-->
    <mappers>
        <!--使用注解方式配置
        使用class属性指定被注解的dao全限定类名
         -->
        <!--<mapper resource="com/itheima/dao/IUserDao.xml"></mapper>-->
        <mapper class="com.itheima.dao.IUserDao"></mapper>
    </mappers>
  1. 在对应的主项目目录 main/java/的dao层添加注解

/**
 * 持久层接口
 * */
public interface IUserDao {
    @Select("select * from user")
    List<User> findAll();
}

mybatis入门.png

mybatis的类路径下的 映射map文件(crud)

<?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.itheima.dao.IUserDao">

    <!--解决实体类与表列名不匹配无法封装-->
    <resultMap id="userMap" type="com.itheima.domain.User">
        <!--主键字段对应-->
        <id property="id" column="id"></id>
        <!--非主键字段-->
        <result property="username" column="username"></result>
        <result property="birthday" column="birthday"></result>
        <result property="sex" column="sex"></result>
        <result property="address" column="address"></result>
    </resultMap>


    <!--配置查询所有-->
    <!--id:唯一对应的标识   resultType:查询完后封装到对应实体类中,然后添加到list -->
    <select id="findAll" resultMap="userMap">
        select * from user
    </select>

    <!--新增用户-->
    <!--
        parameterType: 传入参数类型, 注入sql取值,如果使用右键生成的set/get 应该 #{username}
    -->
    <insert id="saveUser" parameterType="com.itheima.domain.User">
        <!--配置 新增用户后,返回带有id的用户 -->
        <selectKey keyProperty="id" keyColumn="id" resultType="java.lang.Integer" order="AFTER">
            select last_insert_id();
        </selectKey>
        insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
    </insert>

    <!--更新用户-->
    <update id="updateUser" parameterType="com.itheima.domain.User">
        update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id=#{id}
    </update>

    <!--删除用户-->
    <!--
        parameterType:可以使用java包装类; 传入的参数 可以用 占位符,可以随便写形参变量
    -->
    <delete id="deleteUser" parameterType="java.lang.Integer">
        delete from user where id = #{uid}
    </delete>


    <!--根据id查询一个-->
    <select id="findById" parameterType="java.lang.Integer" resultType="com.itheima.domain.User">
        select * from user where id = #{uid}
    </select>

    <!--根据名称模糊查询  注意 没有提供%% 需要在外层拼接好-->
    <select id="findByName" parameterType="java.lang.String" resultType="com.itheima.domain.User">
        select * from user where username like #{uname}
    </select>

    <!--查询总用户数-->
    <select id="findTotal" resultType="java.lang.Integer">
        select count(id) from user
    </select>
</mapper>

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>
    <!--mybatis主配置-->
    <!--配置环境-->
    <!--
        properties 作用主要 让配置独立出来
        resource属性: 常用的
            是按照类路径来写(resource),相关的配置文件必须在类路径下
         url属性: 不常用的
            是要求按照Url的写法来写地址
            URL:Uniform Resource Locator 统一资源定位符。它是可以唯一标识一个资源的位置。
            它的写法:
                http://localhost:8080/mybatisserver/demo1Servlet
                协议      主机     端口       URI
            URI:Uniform Resource Identifier 统一资源标识符。它是在应用中可以唯一定位一个资源的
     -->
    <properties resource="jdbcConfig.properties">
        <!--<property name="driver" value="com.mysql.jdbc.Driver"/>-->
        <!--<property name="url" value="jdbc:mysql://localhost:3306/mybaitsdb"/>-->
        <!--<property name="username" value="root"/>-->
        <!--<property name="password" value="root"/>-->
    </properties>
    <!--使用typeAliases配置别名,它只能配置domain中类的别名 -->
    <typeAliases>
        <!--typeAlias用于配置别名。type属性指定的是实体类全限定类名。alias属性指定别名,当指定了别名就再区分大小写
        <typeAlias type="com.itheima.domain.User" alias="user"></typeAlias>-->

        <!-- 用于指定要配置别名的包,当指定之后,该包下的实体类都会注册别名,并且类名就是别名,不再区分大小写-->
        <package name="com.itheima.domain"></package>
    </typeAliases>
    <environments default="mysql">
        <!--配置mysql环境-->
        <environment id="mysql">
            <!--配置事务类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!--配置连接池-->
            <dataSource type="POOLED">
                <!--配置链接信息-->
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <!--映射配置-->
    <mappers>
        <!--目录  /  -->
        <!--<mapper resource="com/itheima/dao/IUserDao.xml"></mapper>-->
        <!-- package标签是用于指定dao接口所在的包,当指定了之后就不需要在写mapper以及resource或者class了 -->
        <package name="com.itheima.dao"></package>
    </mappers>

</configuration>




主配置文件与映射文件结构必须在类路径下

mybatis配置.jpg
上一篇 下一篇

猜你喜欢

热点阅读