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
    想的都是好
  • 相关阅读:
    centos下nginx的启动
    CentOS7.0安装Nginx 1.7.4
    序员的自我修养
    消息队列 rabbitMQ 的一些操作
    centos linux7的一些操作
    Windows 10 Install rabbitmq-server-3.6.9
    Redis 3.2.100 Windows 32位下载
    Redis配置文件详解
    yii2优化
    mysql中的schema 等价于database,相当于一个数据库
  • 原文地址:https://www.cnblogs.com/freezone/p/5142074.html
Copyright © 2011-2022 走看看