MyBatis增删改查

MyBatis HelloWorld写了MyBatis基本配置,增删改查操作也类似。

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">
<!-- namespace必须是唯一的,建议用该文件所在的包路径全类名 -->
<mapper namespace="ucas.book.map.UserMapper">
    <!-- select:表示这是一个查询操作 -->
    <!-- id:用来配合namespace来定位这个操作,也就是在调用时,将采用ucas.book.pojo.User.getUserById -->
    <!-- resultType:表示返回值类型,必须是全类名,MyBatis将自动为我们将查询结果封装成Student对象 -->
    <!-- parameterType:表示传进来的参数的类型,因为传的是id主键,所以是int型 -->
    <select id="getUserById" parameterType="int" resultType="ucas.book.pojo.User">
        <!-- #{id}:表示占位符,也就是调用时必须为其传一个id参数进来,注意是#不是$ -->
        select * from User where id = #{id}
    </select>
    <insert id="insertUser" parameterType="User" statementType="PREPARED" keyProperty="id" useGeneratedKeys="true">
        insert into User(username,password)
        values(#{username},#{password})
    </insert>

    <update id="updateUser" parameterType="User">
        UPDATE User SET
        username=#{username,jdbcType=VARCHAR},
        password =
        #{password,jdbcType=VARCHAR}
        WHERE id=#{id,jdbcType=INTEGER}
    </update>
    <delete id="deleteUserById" parameterType="int">
        DELETE FROM User WHERE
        id=#{id}
    </delete>

    <select id="loginSelect" resultType="User" parameterType="hashmap">
        SELECT
        * FROM User WHERE username=#{username} and password=#{password}
    </select>

    <select id="loginSelect2" resultType="User" parameterType="User">
        SELECT * FROM User WHERE username=#{username} and password=#{password}
    </select>

</mapper>

测试代码

package ucas.book.test;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import ucas.book.map.InterfaceUserMap;
import ucas.book.pojo.User;

public class TestUpdate {
    public static void main(String[] args) {
        String resource = "ucas/book/map/mybatis.xml";
        Reader reader = null;
        SqlSession session;

        try {
            reader = Resources.getResourceAsReader(resource);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);

        session = factory.openSession();

        try {
            /* * User stu1=new User(); stu1.setUsername("小明"); * stu1.setPassword("xiaoming12"); * session.insert("insertUser",stu1); */

            /* * User stu2=new User(); stu2.setUsername("张无忌"); * stu2.setPassword("987654"); stu2.setId(5); * * session.update("updateUser",stu2); */

            /* * InterfaceUserMap ijum=session.getMapper(InterfaceUserMap.class); * ijum.deleteUser(1); */

            /* * session.delete("deleteUserById", 5); */

            // hashmap做为参数类型
            HashMap<String, String> hm = new HashMap();
            hm.put("username", "柯南");
            hm.put("password", "666661");
            User user = session.selectOne("loginSelect", hm);
            if (user != null) {
                System.out.println(" \n登录成功");
            } else {
                System.out.println(" \n登录失败");
            }

            //
            User user2=new User();
            user2.setUsername("慕白");
            user2.setPassword("123456");

            session.commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
}

你可能感兴趣的:(mybatis)