码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis 模板

时间:2017-05-09 00:58:05      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:and   env   evel   dom   local   doc   line   statement   val   

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

 

MyBatis 模板

标签:and   env   evel   dom   local   doc   line   statement   val   

原文地址:http://www.cnblogs.com/Piers/p/6828297.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!