• 初识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
    想的都是好
  • 相关阅读:
    Spinnerd的功能和用法
    vagrant up ----失败 问题解决
    Yii2.0基础框架
    linux上nginx新建站点
    vagrant(二)配置文件vagrantfile详解 以及安装php、nginx、mysql
    vagrant(一)初识与安装
    cmd 使用gii的命令行用法
    mysql 使用shell时出现 ERROR 2006 (HY000): MySQL server has gone away 解决方法
    c++ virtual总结
    kartikgridGridView 合计,多选,导出excel,header修改 等方法集合!
  • 原文地址:https://www.cnblogs.com/freezone/p/5142074.html
Copyright © 2020-2023  润新知