<?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.fxr.mapper.StudentMapper">

<!-- 中间,对象的属性和结果集的字段的对应的关系 -->
    <resultMap type="com.fxr.model.Student" id="studentRM">
        <!-- 主键的映射 -->
        <id property="id" column="ID"/>
        <!-- 普通字段的映射 property指的是实体的属性;column结果集中的字段的名称 -->
        <result property="name" column="NAME"/>
        <result property="age" column="AGE"/>
        <result property="sex" column="SEX"/>
        <result property="birthday" column="BIRTHDAY"/>
        <!-- 对象关联 -->
    </resultMap>
    
    <!-- 查询,注意Mybatis中如果有填写集合的类型,只填写集合中元素的类型 -->
    <select id="find" resultMap="studentRM">
        select ID,NAME,AGE,SEX,BIRTHDAY from student    
    </select>
    
    <!-- 查询一个按照id查询 -->
    <select id="get" parameterType="int" resultType="com.fxr.model.Student">
        select * from student where id = #{id}
    </select>
    
    <!-- 添加 -->
    <insert id="insert" parameterType="com.fxr.model.Student">
        insert into student
        (id,name,age,sex,birthday)values
        (#{id},#{name},#{age},#{sex},#{birthday})
    </insert>
    
    <!-- 修改 -->
    <update id="update" parameterType="com.fxr.model.Student">
        update student set name=#{name},age=#{age},birthday = #{birthday},sex=#{sex}
        where id = #{id}
    </update>
    
    <!-- 删除一条 -->
    <delete id="deleteById" parameterType="int">
        delete from student where id=#{id}
    </delete>
    
    <!-- 删除多条整型数组 -->
    <delete id="deleteArray" parameterType="int">
        delete from student where id in
        <foreach collection="array" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>
    
    <!-- 删除多条list集合 -->
    <delete id="deleteList" parameterType="int">
        delete from student where id in
        <foreach collection="list" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

<!-- 删除多条,map,ids为map中的值 -->
    <delete id="deleteMap" parameterType="map">
        delete from student
        where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

</mapper>

public class Student {

private Integer id;
    private String name;
    private String sex;
    private Integer age;
    private Date birthday;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", sex=" + sex
                + ", age=" + age + ", birthday=" + birthday + "]";
    }
    
    
    
}

public class TestMyBatis {

private SqlSessionFactory factory;
    @Before
    public void init() throws IOException{
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        factory = new SqlSessionFactoryBuilder().build(is);
    }
    
    //@Test
//    public void testInsert(){
//        SqlSession session = factory.openSession();
//        Student s = new Student();
//        s.setAge(10);
//        s.setBirthday(new Date());
//    
//        s.setName("樊西蕊");
//        s.setSex("男");
//        
//        session.insert("com.fxr.mapper.StudentMapper.insert", s);
//        session.commit();//提交事务
//        System.out.println("insert finished.");
//        
//    }

@Test
    public void testFindAll(){
        SqlSession session = factory.openSession();
        //如何访问mapper中的方法啊,规则就是命名空间+.+id
        List<Student> personList = session.selectList("com.fxr.mapper.StudentMapper.find");
        System.out.println(personList.size());
        for (Student s:personList) {
            System.out.println(s.toString());
        }
    }

//    @Test
//    public void testGet(){
//        SqlSession session = factory.openSession();
//        Student s = session.selectOne("com.fxr.mapper.StudentMapper.get","1");
//        System.out.println("id:"+s.getId()+s.toString());
//    }

//    @Test
//    public void testUpdate(){
//        SqlSession session = factory.openSession();
//        Student s = new Student();
//        s.setId(1);
//        s.setName("张三");
//        session.update("com.fxr.mapper.StudentMapper.update",s);
//        session.commit();
//    }
    //删除一条数据
//    @Test
//    public void testDeleteById(){
//        SqlSession session = factory.openSession();
//        session.delete("com.fxr.mapper.StudentMapper.deleteById",4);
//        session.commit();
//    }

//删除多条数据
//    @Test
//    public void testDeleteByArray(){
//        SqlSession session = factory.openSession();
//        int [] ids = {5,7};
//        session.delete("com.fxr.mapper.StudentMapper.deleteArray",ids);
//        session.commit();
//    }
    
    //删除多条-List
//    @Test
//    public void testDeleteList(){
//        SqlSession session = factory.openSession();
//        List<Integer> list = new ArrayList<Integer>();
//        list.add(3);
//        list.add(8);
//        session.delete("com.fxr.mapper.StudentMapper.deleteList",list);
//        session.commit();
//        
//    }
    
    //删除多条-map
    @Test
    public void testDeleteMap(){
        SqlSession session = factory.openSession();
        Map<String,Object> paraMap = new HashMap<String,Object>();
        int [] ids = {2,6};
        paraMap.put("ids", ids);
        session.delete("com.fxr.mapper.StudentMapper.deleteMap",paraMap);
        session.commit();
        
        
    }

}
<?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>
    <!-- 配置数据源,事务 -->
    <environments default="test">
    
        <environment id="test">
            <!-- 事务:JDBC/MANGED自己管理去 -->
            <transactionManager type="JDBC"/>
            <!-- 数据源:POOLED/UNPOOLED/JNDI -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?charsetEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
        
        <environment id="deploy">
            <!-- 事务:JDBC/MANAGED-自己管理去 -->
            <transactionManager type="JDBC"/>
            <!-- 数据源:POOLED/UNPOOLED/JNDI -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?charsetEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    
    </environments>

<!-- 映射文件mapper -->
    <mappers>
        <mapper resource="com/fxr/mapper/StudentMapper.xml"/>
    </mappers>

</configuration>

/firstMyBatis/lib/asm-3.3.1.jar
/firstMyBatis/lib/cglib-2.2.2.jar
/firstMyBatis/lib/commons-logging-1.1.1.jar
/firstMyBatis/lib/javassist-3.17.1-GA.jar
/firstMyBatis/lib/log4j-1.2.17.jar
/firstMyBatis/lib/mybatis-3.2.2.jar
/firstMyBatis/lib/mysql-connector-java-5.1.26.jar
/firstMyBatis/lib/slf4j-api-1.7.5.jar
/firstMyBatis/lib/slf4j-log4j12-1.7.5.jar

mybitis学习笔记的更多相关文章

  1. Mybatis学习笔记二

    本篇内容,紧接上一篇内容Mybatis学习笔记一 输入映射和输出映射 传递简单类型和pojo类型上篇已介绍过,下面介绍一下包装类型. 传递pojo包装对象 开发中通过可以使用pojo传递查询条件.查询 ...

  2. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  3. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

  4. PHP-会员登录与注册例子解析-学习笔记

    1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...

  5. 2014年暑假c#学习笔记目录

    2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...

  6. JAVA GUI编程学习笔记目录

    2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...

  7. seaJs学习笔记2 – seaJs组建库的使用

    原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...

  8. CSS学习笔记

    CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...

  9. HTML学习笔记

    HTML学习笔记 2016年12月15日整理 Chapter1 URL(scheme://host.domain:port/path/filename) scheme: 定义因特网服务的类型,常见的为 ...

随机推荐

  1. javah 错误: 无法访问android.app.Activity问题解决

    cd /Users/musictom/Documents/source/ky/app/build/intermediates/classes/debug javah -jni -classpath / ...

  2. Elasticsearch学习之深入聚合分析一---基本概念

    首先明白两个核心概念:bucket和metric 1. bucket:一个数据分组 city name 北京 小李 北京 小王 上海 小张 上海 小丽 上海 小陈 基于city划分buckets,划分 ...

  3. Sencha Touch 实战开发培训 视频教程 第二期 第五节

    2014.4.16 晚上8:20分开课. 本节课耗时没有超出一个小时,主要讲解了Sencha Touch 结合百度地图的用法. 本期培训一共八节,前两节免费,后面的课程需要付费才可以观看. 本节内容: ...

  4. mysql语句性能分析案例

    写法不一样而功能完全相同的两条 SQL 的在性能方面的差异.示例一需求:取出某个 group(假设 id 为 100)下的用户编号(id),用户昵称(nick_name).用户性别( sexualit ...

  5. NET的堆和栈04,对托管和非托管资源的垃圾回收以及内存分配

    在" .NET的堆和栈01,基本概念.值类型内存分配"中,了解了"堆"和"栈"的基本概念,以及值类型的内存分配.我们知道:当执行一个方法的时 ...

  6. -bash: locate: command not found

    部分版本的linux系统使用locate快速查找某文件路径会报以下错误: -bash: locate: command not found 其原因是没有安装mlocate这个包 安装:yum  -y ...

  7. iOS - 引用计数探讨

    <Objective-C 高级编程> 这本书有三个章节,我针对每一章节进行总结并加上适当的扩展分享给大家.可以从下面这张图来看一下这三篇的整体结构: 注意,这个结构并不和书中的结构一致,而 ...

  8. zookeeper 安装的三种模式

    Zookeeper安装 zookeeper的安装分为三种模式:单机模式.集群模式和伪集群模式. 单机模式 首先,从Apache官网下载一个Zookeeper稳定版本,本次教程采用的是zookeeper ...

  9. zookeeper学习资料汇总

    zookeeper入门介绍   (1) zookeeper入门介绍     (2) zookeeper应用场景介绍 (淘宝团队)   (3) 分布式服务框架 Zookeeper -- 管理分布式环境中 ...

  10. 构建Maven项目时常见错误

    一.Maven项目,右键-update project后JRE system Library变为JavaSE1.6 Dynamic Web Module 3.0 requires Java 1.6 o ...