识hibernate_01

配置文件内容主要是数据库的连接:postgres

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 3 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4 <configuration>
 5      <!-- 顾名思义类型的别名,即 Person 代指com.sunmap.model.Person-->
 6     <typeAliases>
 7         <typeAlias alias="Person" type="com.sunmap.model.Person" />
 8         <typeAlias alias="Article" type="com.sunmap.model.Article" />
 9     </typeAliases>
10
11     <environments default="development">
12         <environment id="development">
13             <transactionManager type="JDBC" />
14             <dataSource type="POOLED">
15                 <property name="driver" value="org.postgresql.Driver" />
16                 <property name="url"
17                     value="jdbc:postgresql://192.168.5.11:5432/poidata" />
18                 <property name="username" value="postgres" />
19                 <property name="password" value="123456" />
20             </dataSource>
21         </environment>
22     </environments>
23
24     <!--  -->
25     <mappers>
26         <mapper resource="com/sunmap/model/Person.xml" />
27     </mappers>
28
29 </configuration>

预定义接口:

package com.sunmap.dao;

import java.util.List;

import com.sunmap.model.Article;
import com.sunmap.model.Person;

/**
 *
 * 定义的接口
 * */
public interface IPersonOperation {

    public Person selectPersonByID(int id);

    public List<Person> selectPersonByName(String name);

    public void addPerson(Person p);

    public List<Person> selectPerson();

    public void updatePerson(Person p);

    public void deletePersonById(Person p);

    public List<Article> getUserArticle(int id);
}

定义的实体类Person:

package com.sunmap.model;

public class Person {
    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Person.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 的作用,指向接口,即mybatis是面向接口编程,<select></select> 的id有指向接口的方法名;
个人理解<select></select> 就是接口的实现
 -->
<mapper namespace="com.sunmap.dao.IPersonOperation">
    <!-- 定义返回类型为集合 -->
    <resultMap type="Person" id="resultListUser">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="age" property="age" />
    </resultMap>
    <!-- 关联查询 -->
    <resultMap type="Article" id="resultArticleList">
        <id column="id" property="id" />
        <result column="text" property="text" />
        <association property="person" resultMap="resultListUser" column=""
            javaType="Person">
        </association>
    </resultMap>
    <select id="getUserArticle" parameterType="int" resultMap="resultArticleList">
        select person.id,person.name,person.age,article.id from person,article
        where person.id=article.personid and person.id=#{id}
    </select>
    <!-- 通过id查找 -->
    <select id="selectPersonByID" parameterType="int" resultType="Person">
        select * from person where id = #{id}
    </select>
    <!-- 查询数据 -->
    <select id="selectPerson" resultType="Person">
        select * from person
    </select>
    <select id="selectPersonByName" parameterType="String" resultMap="resultListUser">
        select * from person where name like ‘%${_parameter}%‘;
    </select>
    <!--插入数据 -->
    <insert id="addPerson" parameterType="Person">
         <selectKey keyProperty="id" resultType="int" order="BEFORE">
            SELECT nextval(‘test_c_id_seq‘::regclass) as id
         </selectKey>

        insert into person (id,name,age) values(#{id,jdbcType=INTEGER},#{name},#{age})

    </insert>
    <!-- 更新 -->
    <update id="updatePerson" parameterType="Person">
        update person set name =#{name},age = #{age} where id = #{id}
    </update>
    <!-- 删除 -->
    <delete id="deletePersonById" parameterType="Person">
        delete from person
        where id = #{id}
    </delete>
    <!-- 关联查询 -->
</mapper>
<!-- <mapper namespace="com.sunmap.dao"> <select id="selectPersonByID" parameterType="int"
    resultType="Person"> select * from person where id = #{id} </select> <insert
    id="addPerson" parameterType="Person" useGeneratedKeys="true" keyProperty="id">
    insert into Person (id,name,age) values(#{id},#{name},#{age}) insert into
    person (name,age) values(#{name},#{age}) </insert> </mapper> -->

测试代码:

package com.sunmap.dao;

import static org.junit.Assert.*;

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

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 org.junit.Test;

import com.sunmap.model.Article;
import com.sunmap.model.Person;

public class TestIPersonOperation {

    @Test
    public void test() {
//        fail("Not yet implemented");
        try {
            Reader reader = Resources.getResourceAsReader("config/Configuration.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);  

            SqlSession session = sqlSessionFactory.openSession();
            IPersonOperation iPersonOperation = session.getMapper(IPersonOperation.class);

//            List<Article> list = iPersonOperation.getUserArticle(1);
//            System.out.println(list.size());
            /**
             * 删除
             * */
//            Person pp = new Person();
//            pp.setId(2);
//            iPersonOperation.deletePersonById(pp);
            /**
             * 更新
             * */
//            Person pp = new Person();
//            pp.setId(1);
//            pp.setName("update");
//            pp.setAge(234);
//            iPersonOperation.updatePerson(pp);
            /**
             * 添加数据(主键id自动增长,这个的需要创建一个序列)
             *
             * CREATE SEQUENCE test_c_id_seq
                  INCREMENT 1
                  MINVALUE 1
                  MAXVALUE 9223372036854775807
                  START 1
                  CACHE 1;
                ALTER TABLE test_c_id_seq
                  OWNER TO postgres;

                  然后关联上
                  alter table test_c alter column id set default nextval(‘test_c_id_seq‘);

                  这个块其实挺恶心的~
             * */
//            Person pp = new Person();
//            pp.setName("sadfa");
//            pp.setAge(24);
//            iPersonOperation.addPerson(pp);
//
//
//            //提交事物
            session.commit();
            //查询
//             List<Person> personList = iPersonOperation.selectPerson();
//             for(Person p:personList){
//                 System.out.println(p.getName()+" : "+p.getAge());
//             }

            /**
             * 通过id查询
             * */
//            Person person = iPersonOperation.selectPersonByID(1);
//             System.out.println(person.getName()+":"+person.getAge());
             /**
              * 模糊查询(返回List集合)
              *
              *
              * 这块有两个需要注意的地方
              *
              * 1. ‘%${_parameter}%‘  ,单引号和#改成$
              * 2. 获取参数时:${_parameter}
              *
              * 即:不管你的参数是什么,都要改成"_parameter";
              * */
//             List<Person> personList = iPersonOperation.selectPersonByName("sa");
//             for(Person p:personList){
//                 System.out.println(p.getName()+" : "+p.getAge());
//             }

             //关闭session
             session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

时间: 2024-08-07 12:28:59

识hibernate_01的相关文章

Hbuilder开发app实战-识岁01-actionsheet实例

前言 之前写了HBuilder开发App入门-滴石,相信大家看完后应该可以入门了, 之后会做一些简单的app,把nativejs一些常用的功能都过一遍,这样以后做app就没什么难的了. 识岁 借助与face++的接口,做了一个人脸识别app,准确性和微软比确实差点, 主要用到了: 1.actionsheet的原生实现 2.选择本地照片 3.使用摄像头拍照 4.使用uploader上传文件 5.调用face++接口进行人脸识别 6.七牛云上传的实现 actionsheet 实现 actionshe

识人诀

    1.牙齿不好的,脾气一般都好.反之,一口利牙的人,脾气不小.张飞是一例.     2.求人办事,一口答应者,一般都办不成事.更要防备这种人可能是骗子.例如,高考之后,大学门口为家长承诺包上学的人.刚入官场的青年人,宜选老诚执重者为师.“凌霄羽毛轻无力,掷地金石自有声.”       3.在逆境中,不断说些劝你的话的“好心人”,一定小心.如<水浒>中的林冲好友陆迁.       4.学英语中,口语好的,一般语法差些.近视者,听力都好.       5.对当领导的人,所谓专业与学问的标榜切

张艾迪(创始人):DCM的不识人.我说我会像乔布斯一样成为投资者的骄傲

Eidyzhang解码:天才Eidyzhang的诞生 张艾迪(创始人):第一个实习生精英团队 张艾迪(创始人):DCM的不识人.我说我会像乔布斯一样成为投资者的骄傲 2014-05-31 09:47:06|  分类: 默认分类|字号 订阅 DCM的不识人.我说我会像乔布斯一样成为投资者世界的第一骄傲:我会把这个世界在整个世界为世界第一女孩的投资者们点亮.为DCM.CHINA的不识人点亮.我是世界第一女孩.我是世界第一天才我拥有可以改变整个世界和点亮整个世界的力量.年少的只有23岁的我在我的笔记本

真机无法在eclipse devices中被识别 以及 被识别后 target为unknown state为offline

设备:米3 问题描述:真机在eclipse的devices中无法识别 解决办法: 参考:http://blog.csdn.net/wyl530274554/article/details/15497997 步骤:1.如参考所示,在C:\ 用户\****(自己的账户名)\.android\ 创建 adb_usb.ini文件.在里面写入: 0x2717 (设备厂商VID,米3确实是这个,其他设备安装驱动后自行查看,不懂请移步参考资料) 2.重启计算机(重启后发现SDK安装目录下多出.android\

JQ001-认识jQuery 和 JQ002-jQuery选择器

JQ001-认识jQuery jQuery环境配置:将jQuery.js文件引入到html页面中即可. 代码如下: <!DOCTYPE html> <html> <head> <title></title> <script src="../script/jquery.js" type="text/javascript"></script> </head> <body

新手对css的浅识

对于css的一个初步理解与认识 在最近的学习中接触到了之前自己从来都不曾想过的语言,C语言,html超文本标记语言等等,还有今天在这里我要进行分析的css,之前浏览过很多的网页,也曾想过这里面的秘密,但是都没有真正的去研究过,去了解过,知道现在自己亲自动手去设计了网页,去做过网页,去真真实实的接触这方面的不仅是理论还有实际操作的知识之后,才发现一我们大家看似简单的网页之中其实包含了许多许多我们未知的元素,正是这些元素之间相互影响,相互配合,才呈现在了我们眼前精美的网页. 在接触这些知识之前,我也

DataUml Design 教程1-初识

DataUml Design 是面向开发人员使用的一个永久免费的软件,提高软件的开发效率和代码的规范度.它主要包括三大功能,数据模型.代码生成和UML建模,数据模型功能类似于PowerDesigner软件,代码生成类似于动软的代码生成器. 1.什么是DataUml Design DataUml Design是采用WPF开发的一款软件,该软件功能包括实体类建模.数据库设计.模型与数据库同步.数据库与模型同步.代码生成.文档生成.数据库生成实体模型等功能.以往的软件修改模型之后还得修改数据表结构,需

Android GestureDetector手势识别类

为了加强鼠标响应事件,Android提供了GestureDetector手势识别类.通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single Tap Up.Show Press.Long Press.Scroll.Down.Fling),具体包括以下几种: boolean  onDoubleTap(MotionEvent e) 解释:双击的第二下Touch down时触发 boolean  onDoubleTapEvent(MotionEve

项 目 管 理 知 识 体 系 指 南 (PMBOK2008)

项 目 管 理 知 识 体 系 指 南 (第4版) PMBOK2008 输入 工具与技术 输出 4.项目整合管理 4.1 制定项目章程 4.1.1.1 项目工作说明书 4.1.2.1 专家判断 4.1.3.1 项目章程 4.1.1.2 商业论证 4.1.1.3 合同 4.1.1.4 事业环境因素 4.1.1.5 组织过程资产 4.2 制定项目管理计划 4.2.1.1 项目章程 4.2.2.1 专家判断 4.2.3.1 项目管理计划 4.2.1.2 其他规划过程的输出 4.2.1.3 事业环境因素