1. 程式人生 > >java專案模板-Mybatis

java專案模板-Mybatis

  1. 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>
    <!-- 配置mybatis執行環境 -->
    <environments default="development"
>
<!-- 配置開發環境 --> <environment id="development"> <!-- jdbc事務管理器 --> <transactionManager type="JDBC"/> <!-- 資料來源 UNPOOLED 不適用連線池 即 每次請求都會為其建立一個DB連線,適用完畢後,會馬上將連線關閉 POOLED 資料庫連線池來維護連線 JNDI 資料來源可以定義到應用的外部,通過JDNI容器來獲取資料庫連線 -->
<dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mydb"/> <property name="username" value="mary"/> <property name
="password" value="11111"/>
</dataSource> </environment> <!-- 可以多個環境切換。配置上線環境 --> <!--<environment id="online">--> <!--<transactionManager type="JDBC"/>--> <!--<dataSource type="POOLED">--> <!--<property name="driver" value="${jdbc.driverClass}"/>--> <!--<property name="url" value="${jdbc.url}"/>--> <!--<property name="username" value="${jdbc.username}"/>--> <!--<property name="password" value="${jdbc.password}"/>--> <!--</dataSource>--> <!--</environment>--> </environments> <!-- 對映器 --> <mappers> <!-- 註冊對映檔案 --> <mapper resource="mapper/FriendShipMapper.xml"/> <mapper resource="mapper/UserMapper.xml"/> </mappers> </configuration>
  1. UserMapper.xml
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.UserMapper">
    <resultMap type="model.User" id="userMap">
        <id column="id" property="uid"/>
        <result column="user_name" property="user_name"/>
        <result column="password" property="password"/>
    </resultMap>

    <select id="saveUser" resultType="java.lang.Integer">
        insert into wechat.user(user_name, password)
        values (#{user_name}, #{password})
    </select>

    <select id="findUserByUid" resultMap="userMap">
        select user_name, password
        from user
        where uid = #{uid}
    </select>

    <select id="findUserByName" resultMap="userMap">
        select uid, password
        from user
        where user_name = #{user_name}
    </select>
</mapper>

2018-12-10