继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Mybatis从零单排-2

叶无道疯人院
关注TA
已关注
手记 97
粉丝 40
获赞 137

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://img4.mukewang.com/5c07e33d00010ab903740268.jpg

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP