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();
}
}
}点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
