Mybatis

作者: lixinxin | 来源:发表于2018-07-16 15:07 被阅读10次
    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>
    
        <!--导入属性配置-->
        <properties resource="datasource.properties"></properties>
    
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="${db.driverClassName}"/>
                    <property name="url" value="${db.url}"/>
                    <property name="username" value="${db.username}"/>
                    <property name="password" value="${db.password}"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <mapper resource="mappers/UserMapper.xml"/>
        </mappers>
    </configuration>
    

    2.pojo

    <?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.love.lee.dao.UserMapper" >
      <resultMap id="BaseResultMap" type="com.love.lee.pojo.User" >
        <constructor >
          <idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
          <arg column="username" jdbcType="VARCHAR" javaType="java.lang.String" />
          <arg column="password" jdbcType="VARCHAR" javaType="java.lang.String" />
          <arg column="email" jdbcType="VARCHAR" javaType="java.lang.String" />
          <arg column="phone" jdbcType="VARCHAR" javaType="java.lang.String" />
          <arg column="question" jdbcType="VARCHAR" javaType="java.lang.String" />
          <arg column="answer" jdbcType="VARCHAR" javaType="java.lang.String" />
          <arg column="role" jdbcType="INTEGER" javaType="java.lang.Integer" />
          <arg column="create_time" jdbcType="TIMESTAMP" javaType="java.util.Date" />
          <arg column="update_time" jdbcType="TIMESTAMP" javaType="java.util.Date" />
        </constructor>
      </resultMap>
      <sql id="Base_Column_List" >
        id, username, password, email, phone, question, answer, role, create_time, update_time
      </sql>
      <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
        select 
        <include refid="Base_Column_List" />
        from mmall_user
        where id = #{id,jdbcType=INTEGER}
      </select>
      <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
        delete from mmall_user
        where id = #{id,jdbcType=INTEGER}
      </delete>
      <insert id="insert" parameterType="com.love.lee.pojo.User" >
        insert into mmall_user (id, username, password, 
          email, phone, question, 
          answer, role, create_time, 
          update_time)
        values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
          #{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{question,jdbcType=VARCHAR}, 
          #{answer,jdbcType=VARCHAR}, #{role,jdbcType=INTEGER}, now(),
          now())
      </insert>
      <insert id="insertSelective" parameterType="com.love.lee.pojo.User" >
        insert into mmall_user
        <trim prefix="(" suffix=")" suffixOverrides="," >
          <if test="id != null" >
            id,
          </if>
          <if test="username != null" >
            username,
          </if>
          <if test="password != null" >
            password,
          </if>
          <if test="email != null" >
            email,
          </if>
          <if test="phone != null" >
            phone,
          </if>
          <if test="question != null" >
            question,
          </if>
          <if test="answer != null" >
            answer,
          </if>
          <if test="role != null" >
            role,
          </if>
          <if test="createTime != null" >
            create_time,
          </if>
          <if test="updateTime != null" >
            update_time,
          </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
          <if test="id != null" >
            #{id,jdbcType=INTEGER},
          </if>
          <if test="username != null" >
            #{username,jdbcType=VARCHAR},
          </if>
          <if test="password != null" >
            #{password,jdbcType=VARCHAR},
          </if>
          <if test="email != null" >
            #{email,jdbcType=VARCHAR},
          </if>
          <if test="phone != null" >
            #{phone,jdbcType=VARCHAR},
          </if>
          <if test="question != null" >
            #{question,jdbcType=VARCHAR},
          </if>
          <if test="answer != null" >
            #{answer,jdbcType=VARCHAR},
          </if>
          <if test="role != null" >
            #{role,jdbcType=INTEGER},
          </if>
          <if test="createTime != null" >
            now(),
          </if>
          <if test="updateTime != null" >
            now(),
          </if>
        </trim>
      </insert>
      <update id="updateByPrimaryKeySelective" parameterType="com.love.lee.pojo.User" >
        update mmall_user
        <set >
          <if test="username != null" >
            username = #{username,jdbcType=VARCHAR},
          </if>
          <if test="password != null" >
            password = #{password,jdbcType=VARCHAR},
          </if>
          <if test="email != null" >
            email = #{email,jdbcType=VARCHAR},
          </if>
          <if test="phone != null" >
            phone = #{phone,jdbcType=VARCHAR},
          </if>
          <if test="question != null" >
            question = #{question,jdbcType=VARCHAR},
          </if>
          <if test="answer != null" >
            answer = #{answer,jdbcType=VARCHAR},
          </if>
          <if test="role != null" >
            role = #{role,jdbcType=INTEGER},
          </if>
          <if test="createTime != null" >
            create_time = #{createTime,jdbcType=TIMESTAMP},
          </if>
          <if test="updateTime != null" >
            update_time = now(),
          </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
      </update>
      <update id="updateByPrimaryKey" parameterType="com.love.lee.pojo.User" >
        update mmall_user
        set username = #{username,jdbcType=VARCHAR},
          password = #{password,jdbcType=VARCHAR},
          email = #{email,jdbcType=VARCHAR},
          phone = #{phone,jdbcType=VARCHAR},
          question = #{question,jdbcType=VARCHAR},
          answer = #{answer,jdbcType=VARCHAR},
          role = #{role,jdbcType=INTEGER},
          create_time = #{createTime,jdbcType=TIMESTAMP},
          update_time = now()
        where id = #{id,jdbcType=INTEGER}
      </update>
      
      
      <select id="checkUsername" resultType="int" parameterType="string" >
        select count(1) from mmall_user
        where username = #{username}
      </select>
    
      <select id="checkEmail" resultType="int" parameterType="string" >
        select count(1) from mmall_user
        where email = #{email}
      </select>
    
    
      <select id="selectLogin" resultMap="BaseResultMap" parameterType="map">
        SELECT
    --     *???//这样真的好么?答案就是,这样不好.
        <include refid="Base_Column_List" />
        from mmall_user
        where username = #{username}
        and password = #{password}
      </select>
    
    
      <select id="selectQuestionByUsername" resultType="string" parameterType="string">
        select
        question
        from mmall_user
        where username = #{username}
      </select>
    
      <select id="checkAnswer" resultType="int" parameterType="map">
        SELECT
        count(1)
        from mmall_user
        where username=#{username}
        and question = #{question}
        and answer = #{answer}
      </select>
    
      <update id="updatePasswordByUsername" parameterType="map">
        update mmall_user
        SET password = #{passwordNew},update_time = now()
        where username = #{username}
      </update>
    
      <select id="checkPassword" resultType="int" parameterType="map">
        SELECT
        count(1)
        from mmall_user
        where password = #{password}
        and id = #{userId}
      </select>
    
    
      <select id="checkEmailByUserId" resultType="int" parameterType="map">
        select count(1) from mmall_user
        where email = #{email}
        and id != #{userId}
      </select>
    
    
    </mapper>
    
    1. 接口
    package com.love.lee.dao;
    
    
    import com.love.lee.pojo.User;
    import org.apache.ibatis.annotations.Param;
    
    public interface UserMapper {
        int deleteByPrimaryKey(Integer id);
    
        int insert(User record);
    
        int insertSelective(User record);
    
        User selectByPrimaryKey(Integer id);
    
        int updateByPrimaryKeySelective(User record);
    
        int updateByPrimaryKey(User record);
    
        int checkUsername(String username);
    
        int checkEmail(String email);
    
        User selectLogin(@Param("username") String username, @Param("password") String password);
    
        String selectQuestionByUsername(String username);
    
        int checkAnswer(@Param("username") String username, @Param("question") String question, @Param("answer") String answer);
    
        int updatePasswordByUsername(@Param("username") String username, @Param("passwordNew") String passwordNew);
    
        int checkPassword(@Param(value = "password") String password, @Param("userId") Integer userId);
    
        int checkEmailByUserId(@Param(value = "email") String email, @Param(value = "userId") Integer userId);
    }
    

    4.调用

    Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
    
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
    
     try {
                sqlSession = dao.getSqlSession();
                UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                User user = userMapper.selectByPrimaryKey(1);
                System.out.println(user.toString());
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
    
    

    相关文章

      网友评论

          本文标题:Mybatis

          本文链接:https://www.haomeiwen.com/subject/onlbpftx.html