zoukankan      html  css  js  c++  java
  • Mybatis学习笔记3

    1、接口定义

    package com.mybatis.dao;
    
    import com.mybatis.bean.Employee;
    
    public interface EmployeeMapper {
        public Employee getEmpById(Integer id);
    
        public void addEmp(Employee employee);
    
        public void updateEmp(Employee employee);
    
        public void deleteEmpById(Integer id);
    }
    

     2、mapper编写

    <?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">
    <mapper namespace="com.mybatis.dao.EmployeeMapper">
        <!--public Employee getEmpById(Integer id);-->
        <select id="getEmpById" resultType="com.mybatis.bean.Employee">
    		select * from tbl_employee where id = #{id}
    	</select>
    
        <!--public void addEmp(Employee employee);-->
        <!-- parameterType:参数类型,可以省略-->
        <insert id="addEmp" parameterType="com.mybatis.bean.Employee">
            insert into tbl_employee(last_name, gender, email)
            values (#{lastName},#{gender},#{email})
        </insert>
    
        <!--public void updateEmp(Employee employee);-->
        <update id="updateEmp">
            update tbl_employee set last_name=#{lastName},email=#{email},gender=#{gender} where id=#{id}
        </update>
    
        <!--public void deleteEmpById(Integer id);-->
        <delete id="deleteEmpById">
            delete from tbl_employee where id=#{id}
        </delete>
    </mapper>
    

     3、测试代码

    package com.mybatis.demo;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    import com.mybatis.bean.Employee;
    import com.mybatis.dao.EmployeeMapper;
    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;
    
    public class MyTest {
        public SqlSessionFactory getSqlSessionFactory() throws IOException {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            return new SqlSessionFactoryBuilder().build(inputStream);
        }
    
        /**
         * 测试增删改
         * 1、mybatis允许增删改直接定义以下类型返回值
         * Integer、Long、Boolean、void
         * 2、需要手动提交数据
         * sqlSessionFactory.openSession();===》手动提交
         * sqlSessionFactory.openSession(true);===》自动提交
         */
    
        @Test
        public void testSelect() throws IOException {
            SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
            SqlSession openSession = sqlSessionFactory.openSession();
            try {
                EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
                Employee employee = mapper.getEmpById(1);
                System.out.println(employee);
            } finally {
                openSession.close();
            }
        }
    
        @Test
        public void testInsert() throws IOException {
            SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
            //此处获取到的SqlSession不会自动提交数据
            SqlSession openSession = sqlSessionFactory.openSession();
            try {
                EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
                mapper.addEmp(new Employee("jetty", "jetty@126.com", 1));
                //手动提交数据
                openSession.commit();
            } finally {
                openSession.close();
            }
    
        }
    
        @Test
        public void testUpdate() throws IOException {
            SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
            SqlSession openSession = sqlSessionFactory.openSession();
            try {
                EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
                mapper.updateEmp(new Employee(2, "jerry", "jerry@qq.com", 1));
                openSession.commit();
            } finally {
                openSession.close();
            }
    
        }
    
        @Test
        public void testDelete() throws IOException {
            SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
            //自动提交
            SqlSession openSession = sqlSessionFactory.openSession(true);
            try {
                EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
                mapper.deleteEmpById(2);
            } finally {
                openSession.close();
            }
    
        }
    }
    
  • 相关阅读:
    【原】Spark on YARN
    【原】Spark Standalone模式
    【原】日志处理-Spark
    怎样从官网下载Spring的jar包
    成功安装mysql(mysql-5.5.32-winx64.msi)后,为何服务管理器里找不到MYSQL服务名?
    SQL如何取得一个面的中心点
    Kindle2018 一周使用报告
    迅雷导致文件损坏
    苹果中国官网全面更新,官翻产品不容错过!
    如何选择windows 10 系统中默认打开程序
  • 原文地址:https://www.cnblogs.com/xidian2014/p/10349323.html
Copyright © 2011-2022 走看看