MyBatis注解开发
使用注解可以把原来放在XML文件中的配置信息和SQL语句放在程序中,小型敏捷开发的工程可以使用注解,而大型的需要团队协作的项目要使用XML
1.在dao目录创建一个GoodsDAO接口
package com.imooc.mybatis.dao;
import com.imooc.mybatis.dto.GoodsDTO;
import com.imooc.mybatis.entity.Goods;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface GoodsDAO {
@Select("select * from t_goods " +
"where current_price between #{min} and #{max} " +
"order by current_price limit 0,#{limt}")
public List<Goods> selectByPriceRange(@Param("min") Float min,@Param("max") Float max,@Param("limt") Integer limt);
@Insert("INSERT INTO t_goods" +
"(title,sub_title,original_cost,current_price," +
"discount,is_free_delivery,category_id)" +
"VALUES (#{title},#{subTitle},#{originalCost}," +
"#{currentPrice},#{discount},#{isFreeDelivery},#{categoryId})")
@SelectKey(statement = "select last_insert_id()",before = false,
keyProperty = "goodsId" ,resultType = Integer.class)
public int insert(Goods goods);
@Select("select * from t_goods")
@Results({
//id
@Result(column = "goods_id",property = "goodsId",id = true),
//result
@Result(column = "title",property = "title"),
@Result(column = "current_price",property = "currentPrice"),
})
public List<GoodsDTO> selectAll();
}
2.在mybatis-config.xml中增加mapper声明
<mappers>
<!--<mapper class="com.imooc.mybatis.dao.GoodsDAO"/>-->
<package name="com.imooc.mybatis.dao"/>
</mappers>
3.测试
package com.imooc.mybatis;
import com.imooc.mybatis.dao.GoodsDAO;
import com.imooc.mybatis.entity.Goods;
import com.imooc.mybatis.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
//JUNIT单元测试类
public class MyBatisTestor {
@Test
public void testSelectAll() throws Exception {
SqlSession session = null;
try {
session = MyBatisUtils.openSession();
GoodsDAO goodsDAO = session.getMapper(GoodsDAO.class);
List list = goodsDAO.selectAll();
System.out.println(list.size());
} catch (Exception e) {
throw e;
} finally {
MyBatisUtils.closeSession(session);
}
}
@Test
public void testSelectByPriceRange() throws Exception {
SqlSession session = null;
try {
session = MyBatisUtils.openSession();
GoodsDAO goodsDAO = session.getMapper(GoodsDAO.class);
List list = goodsDAO.selectByPriceRange(100f,500f,20);
System.out.println(list.size());
} catch (Exception e) {
throw e;
} finally {
MyBatisUtils.closeSession(session);
}
}
/**
* 新增数据
* @throws Exception
*/
@Test
public void testInsert() throws Exception {
SqlSession session = null;
try{
session = MyBatisUtils.openSession();
Goods goods = new Goods();
goods.setTitle("测试商品");
goods.setSubTitle("测试子标题");
goods.setOriginalCost(200f);
goods.setCurrentPrice(100f);
goods.setDiscount(0.5f);
goods.setIsFreeDelivery(1);
goods.setCategoryId(43);
GoodsDAO goodsDAO = session.getMapper(GoodsDAO.class);
//insert()方法返回值代表本次成功插入的记录总数
int num = goodsDAO.insert(goods);
session.commit();//提交事务数据
System.out.println(goods.getGoodsId());
}catch (Exception e){
if(session != null){
session.rollback();//回滚事务
}
throw e;
}finally {
MyBatisUtils.closeSession(session);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!