亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Mybatis從零單排-2

標簽:
Java

Mybatis单表操作CRUD核心代码。

public interface CountryDao {
    //对tb_country进行查询数据
    List<Country> selectAll();

    //对tb_country进行插入数据
    int  insert(Country country);

    //对tb_country进行删除操作
    int delete(int id);

    //对tb_country进行更新操作
    int update(Country country);
}
<?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.imooc.dao.CountryDao">
    <select id="selectAll" resultType="com.imooc.testmybatis.Country">
        SELECT id AS id,country_name AS countryName,country_code AS countryCode
        FROM tb_country
    </select>
    
    <insert id="insert" parameterType="com.imooc.testmybatis.Country">
        INSERT INTO tb_country(country_name,country_code)
        VALUES(#{countryName},#{countryCode})
    </insert>

    <delete id="delete" parameterType="int">
        DELETE FROM tb_country
        WHERE id=#{id}
    </delete>

    <update id="update" parameterType="com.imooc.testmybatis.Country">
        UPDATE tb_country
        SET country_name=#{countryName},country_code=#{countryCode}
        WHERE id=#{id}
    </update>
</mapper>
public class CountryMapperTest {
    private static SqlSessionFactory sqlSessionFactory;

    @BeforeClass
    public static void init(){
        try {
            Reader reader = Resources.getResourceAsReader("config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            reader.close();
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }

    @Ignore
    @Test
    public void testSelectAll(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            CountryDao countryMapper = sqlSession.getMapper(CountryDao.class);
            List<Country> countryList = countryMapper.selectAll();
            System.out.println(countryList);
        }finally {
            sqlSession.close();
        }
    }
    @Ignore
    @Test
    public void testInsert(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            CountryDao countryMapper = sqlSession.getMapper(CountryDao.class);
            Country country = new Country();
            country.setCountryName("德国");
            country.setCountryCode("GE");
            int effecctNum = countryMapper.insert(country);
            //默认是不自动提交的,需要手动commit;或者在配置文件中设置。
            sqlSession.commit();
            if(effecctNum == 1){
                System.out.println("插入tb_country中的一条记录成功!");
            }
        }finally {
            sqlSession.close();
        }
    }

    @Ignore
    @Test
    public void testDelete(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            CountryDao countryMapper = sqlSession.getMapper(CountryDao.class);
            Country country = new Country();
            country.setId(2);
            int effecctNum = countryMapper.delete(country.getId());
            //默认是不自动提交的,需要手动commit;或者在配置文件中设置。
            sqlSession.commit();
            if(effecctNum == 1){
                System.out.println("删除tb_country中的一条记录成功!");
            }
        }finally {
            sqlSession.close();
        }
    }

    @Test
    public void testUpdate(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            CountryDao countryMapper = sqlSession.getMapper(CountryDao.class);
            Country country = new Country();
            country.setId(1);
            country.setCountryName("测试");
            country.setCountryCode("CS");
            int effecctNum = countryMapper.update(country);
            //默认是不自动提交的,需要手动commit;或者在配置文件中设置。
            sqlSession.commit();
            if(effecctNum == 1){
                System.out.println("删除tb_country中的一条记录成功!");
            }
        }finally {
            sqlSession.close();
        }
    }
}

https://img1.sycdn.imooc.com//5c07e33d00010ab903740268.jpg

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
算法工程師
手記
粉絲
40
獲贊與收藏
137

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消