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();
}
}
}點擊查看更多內(nèi)容
為 TA 點贊
評論
評論
共同學(xué)習(xí),寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
