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>
    
    <properties resource="config/applications.properties"/>
    
    <settings>
        <setting name="cacheEnabled" value="true"/>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        
    settings>
    
    <typeAliases>
        
        <package name="per.piers.onlineJudge.model"/>
    typeAliases>
    
    <typeHandlers>
        <typeHandler handler="per.piers.onlineJudge.handler.SexTypeHandler" javaType="per.piers.onlineJudge.model.Sex" jdbcType="BOOLEAN"/>
    typeHandlers>
    
    <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>
        
        <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>

 

转载于:https://www.cnblogs.com/Piers/p/6828297.html

你可能感兴趣的:(MyBatis 模板)