• 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>
        <!-- property from external resources -->
        <properties resource="config/applications.properties"/>
        <!-- settings -->
        <settings>
            <setting name="cacheEnabled" value="true"/>
            <setting name="logImpl" value="STDOUT_LOGGING"/>
            <!--
            <setting name="lazyLoadingEnabled" value="true" />
            <setting name="multipleResultSetsEnabled" value="true" />
            <setting name="useColumnLabel" value="true" />
            <setting name="useGeneratedKeys" value="false" />
            <setting name="autoMappingBehavior" value="PARTIAL" />
            <setting name="defaultExecutorType" value="SIMPLE" />
            <setting name="defaultStatementTimeout" value="25000" />
            <setting name="safeRowBoundsEnabled" value="false" />
            <setting name="mapUnderscoreToCamelCase" value="false" />
            <setting name="localCacheScope" value="SESSION" />
            <setting name="jdbcTypeForNull" value="OTHER" />
            <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode ,toString" />
            -->
        </settings>
        <!-- type aliases(full class name -> simple class name) -->
        <typeAliases>
            <!--
            <typeAlias alias="Student" type="com.mybatis3.domain.Student" />
            -->
            <package name="per.piers.onlineJudge.model"/>
        </typeAliases>
        <!-- type handlers -->
        <typeHandlers>
            <typeHandler handler="per.piers.onlineJudge.handler.SexTypeHandler" javaType="per.piers.onlineJudge.model.Sex" jdbcType="BOOLEAN"/>
        </typeHandlers>
        <!-- environment -->
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="${jdbc.driverClassName}"/>
                    <property name="url" value="${jdbc.url}"/>
                    <property name="username" value="${jdbc.username}"/>
                    <property name="password" value="${jdbc.password}"/>
                </dataSource>
            </environment>
            <environment id="production">
                <transactionManager type="MANAGED"/>
                <dataSource type="JNDI">
                    <property name="data_source" value="java:comp/jdbc/mybatis"/>
                </dataSource>
            </environment>
        </environments>
        <!-- mappers location -->
        <mappers>
            <!--
            <mapper url="file:///D:/mybatisdemo/app/mappers/TutorMapper.xml" />
            <mapper class="com.mybatis3.mappers.TutorMapper" />
            -->
            <mapper resource="mapper/UserMapper.xml"/>
        </mappers>
    </configuration>

    applications.properties:

    jdbc.driverClassName=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/online_judge?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=true
    jdbc.username=root
    jdbc.password=woaimysql-135

    context.xml(JNDI配置数据源):

    <?xml version="1.0" encoding="UTF-8"?>
    <Context>
        <Resource name="mybatis"
                  auth="Container"
                  type="javax.sql.DataSource"
                  driverClassName="com.mysql.jdbc.Driver"
                  url="jdbc:mysql://localhost:3306/onlineJudge"
                  username="root"
                  password="woaimysql-135"
                  maxTotal="20"
                  maxIdle="10"
                  maxWaitMillis="10000" />
    </Context>

    UserMapper.xml:

    <?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="per.piers.onlineJudge.mapper.UserMapper">
        <insert id="insertUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
            INSERT INTO online_judge.users (email, password, name, sex, role, enabled)
            VALUES (#{email}, #{password}, #{name}, #{sex}, #{role}, #{enabled})
        </insert>
        <update id="updateUser" parameterType="User">
            UPDATE online_judge.users
            SET email = #{email}, password = #{password}, name = #{name}, sex = #{sex}, role = #{role}, enabled = #{enabled}
            WHERE id = #{id}
        </update>
        <delete id="deleteUser" parameterType="int">
            DELETE FROM online_judge.users
            WHERE id = #{id}
        </delete>
        <select id="selectUserByEmail" parameterType="String" resultMap="userResult">
            SELECT *
            FROM online_judge.users
            WHERE email = #{email}
        </select>
        <resultMap id="userResult" type="User">
            <id column="id" property="id"/>
            <result column="email" property="email"/>
            <result column="password" property="password"/>
            <result column="name" property="name"/>
            <result column="sex" property="sex"/>
            <result column="enabled" property="enabled"/>
            <result column="role" property="role"/>
        </resultMap>
    </mapper>
  • 相关阅读:
    【黑金动力社区】【原创博文集锦】《Verilog HDL那些事儿》导读
    [黑金原创教程][连载]关于《iBoard 电子学堂》的学习及进阶方式
    发布《iBoard 电子学堂》DEMO代码
    Cyclone III中LVDS的设计
    [黑金原创教程][连载][iBoard 电子学堂][第八卷 设计任意波发生器]第三篇 直接数字合成(DDS)原理
    专业级FPGA视频图像处理开发板发布
    [黑金原创教程][连载][iBoard 电子学堂][第〇卷 电子基础]第二篇 电路图与印刷电路板
    [黑金原创教程][连载][iBoard 电子学堂][第八卷 设计任意波发生器]第一篇 iBoard 任意波发生器简介
    [黑金原创教程][连载][iBoard 电子学堂][第〇卷 电子基础]第一篇 认识电子元器件
    ucgui 在《iBoard 电子学堂》上的移植
  • 原文地址:https://www.cnblogs.com/Piers/p/6828297.html
Copyright © 2020-2023  润新知