zoukankan      html  css  js  c++  java
  • 初识mybatis_02 基于注解实现增删改查

    xml配置和注解的区别:
    <mappers>
      <!-- 基于注解,mapper需要指定接口 -->
      <mapper class="com.sunmap.dao.IPersonOperation"/>
    </mappers>
    <?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>
         <!-- 顾名思义类型的别名,即 Person 代指com.sunmap.model.Person-->
        <typeAliases>
            <typeAlias alias="Person" type="com.sunmap.model.Person" />
            <typeAlias alias="Article" type="com.sunmap.model.Article" />
        </typeAliases>
        
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC" />
                <dataSource type="POOLED">
                    <property name="driver" value="org.postgresql.Driver" />
                    <property name="url"
                        value="jdbc:postgresql://192.168.5.11:5432/poidata" />
                    <property name="username" value="postgres" />
                    <property name="password" value="123456" />
                </dataSource>
            </environment>
        </environments>
        
        <!--  -->
        <mappers>
            <!-- <mapper resource="com/sunmap/model/Person.xml" /> -->
            <!-- <mapper url="file:///home/wanggang/图片/Person.xml"/> -->
    
            <!-- <package name="com.sunmap.dao"/>   -->
            
            <!-- 基于注解,mapper需要指定接口 -->
             <mapper class="com.sunmap.dao.IPersonOperation"/>
        </mappers>
    
    </configuration>
    View Code
    注解的写法:
    package com.sunmap.dao;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Delete;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Select;
    import org.apache.ibatis.annotations.SelectKey;
    import org.apache.ibatis.annotations.Update;
    
    import com.sunmap.model.Article;
    import com.sunmap.model.Person;
    
    /**
     * 
     * 定义的接口
     * */
    public interface IPersonOperation {
    
        @Select("select * from person where id = #{id}")
        public Person selectPersonByID(int id);
        
        @Select("select * from person where name like '%${_parameter}%'")
        public List<Person> selectPersonByName(String name);
        
        /**
         * 
         * 
         * */
        @SelectKey (statement="SELECT nextval('test_c_id_seq'::regclass) as id ", keyProperty="id", before=true, resultType=int.class)
        @Insert ("insert into person (id,name,age) values(#{id,jdbcType=INTEGER},#{name},#{age})")
        public void addPerson(Person p);
        
        @Select("select * from person where id = #{id}")
        public List<Person> selectPerson();
        
        @Update("update person set name =#{name},age = #{age} where id = #{id}")
        public void updatePerson(Person p);
    
        @Delete("delete from person where id = #{id}")
        public void deletePersonById(Person p);
        
        @Select("select person.id,person.name,person.age,article.id from person,article    where person.id=article.personid and person.id=#{id}")
        public List<Article> getUserArticle(int id);
    }
    View Code
    想的都是好
  • 相关阅读:
    php基础之简单运算
    选择平淡
    php基础之控制结构
    关于三元运算符的初步应用及理解
    VS2015 遇到异常。这可能是由某个扩展导致的
    C#中如何去除窗体默认的关闭按钮
    (转载)SQL基础--> 约束(CONSTRAINT)
    SQL Server安装后设置SQL Server验证登录
    附加数据库 对于 ""失败,无法打开物理文件 操作系统错误 5:拒绝访问 SQL Sever
    SQL Server数据库操作(二)
  • 原文地址:https://www.cnblogs.com/freezone/p/5142074.html
Copyright © 2011-2022 走看看