MyBatis增删改查

取别名package

  <!-- 通过package, 可以直接指定package的名字, mybatis会自动扫描你指定包下面的javabean, 并且默认设置一个别名,默认的名字为: javabean 的首字母小写的非限定类名来作为它的别名 -->
<typeAliases>
    <package name="pd.entity"/>
    </typeAliases>

取别名alias

 <!-- type指的是javabean的完全限定名   alias就是指代别名-->
<typeAliases>
        <typeAlias type="pd.entity.Book" alias="Book" />
    </typeAliases>

resultMap

    <resultMap type="PanDaEntity" id="entity1">
    <id property="uid"/>
    <!-- property是实体类名字 column是数据库字段名  -->
    <result property="uname" column="unama"/>
    <result property="upwd"/>
    <result property="uphone"/>
    <result property="uaddress"/>
    <result property="ubirthday"/>  
    </resultMap>

<!-- 查 -->
    <select id="getTableByTid" resultMap="tables">
        select *from tablename1 where
        <if test="tid!=null">
        <!--左边是数据库字段名 右边是实体类名字  -->
            tid=#{tid}
        </if>
    </select>

<update id="updateByTname" parameterType="String">
    update tablename1
    <set>
    <if test="tname!=null">
    tname=#{tname}
    </if>
    </set>
    <where>
    <if test="tid!=null">
    tid=#{tid}
    </if>
    </where>
    </update>

<delete id="deleteByTid" parameterType="int">
        delete from tablename1
        <where>
            <if test="tid!=null">
                tid=#{tid}
            </if>
        </where>
    </delete>

<insert id="insertByTid" parameterType="String">
<!-- 插入要在占位符后加入jdbcType=数据库的类型 -->
        insert into tablename1 values(#{tid,jdbcType=VARCHAR},#{tname,jdbcType=VARCHAR})
    </insert>

@Param注解

//@Param("uid")Integer uid对应的是实体类
    List<PanDaEntity> select3(@Param("uid")Integer uid,@Param("uname")String uname);

接口类

package pd.dao;

import java.util.List;


import org.apache.ibatis.annotations.Param;

import pd.entity.Book;
import pd.entity.Group;
import pd.entity.Person;
import pd.entity.Table;
import pd.entity.User;

public interface TestDao {
    List<User> getUser1();
    List<User> getUser2();
    List<Book> getBooks();
    List<Person> getPersonList();
    List<Group> getGroupList();
    List<Book> getBooks2();
    List<Table> getTable();
    List<Table> getTableByTid(@Param("tid")String tid);
    int deleteByTid(@Param("tid")String tid);
    int insertByTid(@Param("tid")String tid,@Param("tname")String tname);
    int updateByTname(@Param("tname")String tname,@Param("tid")String tid);
}

测试类

package test;



import java.util.List;

import org.apache.ibatis.session.SqlSession;

import pd.dao.TestDao;
import pd.entity.Book;
import pd.entity.Group;
import pd.entity.Person;
import pd.entity.Table;
import pd.entity.User;
import pd.untils.CopyOfPandaUntils;


public class Test {
    public static void main(String[] args) {
        SqlSession  session=null;

        try {
            session=CopyOfPandaUntils.getInstance().getSession();
            /*单表查询
            List<User>list=session.getMapper(TestDao.class).getUser1();
            for(User bb:list){
                System.out.println(bb);
            }*/
            /*一对多
            List<User>list=session.getMapper(TestDao.class).getUser2();
            for(User bb:list){
                System.out.println(bb.toString());
            }*/
            /*一对一
             * List<Book>list=session.getMapper(TestDao.class).getBooks();
            for(Book bb:list){
                System.out.println(bb.toString());
            }*/
            /*多对多查询
             * List<Person>list=session.getMapper(TestDao.class).getPersonList();
            for(Person bb:list){
                System.out.println(bb.toString());
            }*/
            /*List<Book>list=session.getMapper(TestDao.class).getBooks2();
            for(Book bb:list){
                System.out.println(bb.toString());
            }*/
            /*单表全查
             * List<Table>list=session.getMapper(TestDao.class).getTable();
            for(Table bb:list){
                System.out.println(bb.toString());
            }*/
            //根据ID查询
        /*  List<Table>list=session.getMapper(TestDao.class).getTableByTid("1");
            for(Table bb:list){
                System.out.println(bb.toString());
            }*/
            /*删除
             * int count=session.getMapper(TestDao.class).deleteByTid("5");
            System.out.println(count);*/
//          插入
            int count=session.getMapper(TestDao.class).insertByTid("7", "liu");
            System.out.println(count);
            /*更改
             * int count=session.getMapper(TestDao.class).updateByTname("cool", "5");
            System.out.println(count);*/
            session.commit();



        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
        }finally{
            CopyOfPandaUntils.closeSession(session);

        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值