SpringBoot 各层级关联的注解
SpringBoot 各层级关联的注解:
1.Controller:
@RestController
@RequestMapping("/hello")
public class helloController extends BaseController {
//(1)在Controller中用@Autowired注解将service层注入到controller中,这样controller才能调用service中的接口。
@Autowired
private StationRegionTreeService stationRegionTreeService;
2.Service:
//(2) serviec接口中一般不需要使用什么注解。
import com.cars.ict.rbpsems.utils.Result;
public interface StationRegionTreeService {
void delateTree(long id);
}
3.ServiceImpl:
//(3) 在applicationContext.xml配置文件中加上这一行以后,将自动扫描指定路径下的包,如果一个类带了@Service注解,将自动注册到Spring容器,不需要再在applicationContext.xml配置文件中定义bean了,类似的还包括
@Component、@Repository、@Controller。
@Service
public class StationRegionTreeServiceImpl implements StationRegionTreeService {
@Autowired
private StationInfRelaTreeDao stationInfRelaTreeDao;
@Autowired
private StationRegionTreeMapper stationRegionTreeMapper;
@Override
public void delateTree(long id) {
stationRegionTreeDao.deleteTreeById(id);
}
4. Dao 或者 Mapper 最好都加上@Mapper注解。否则可能报找不到该接口的错误。
Dao:
@Mapper
public interface StationRegionTreeDao extends BaseDao<StationRegionTree,String>{
@Transactional
@Modifying
@Query(value = "delete from StationRegionTree s where s.id = :id")
void deleteTreeById(@Param("id") long id);
}
Mapper:
@Mapper
public interface StationRegionTreeMapper extends BaseMapper<StationRegionTree> {
void updateTreeById(StationRegionTree tree);
void insertTree(StationRegionTree regionTree);
}
5.
dao:
@Mapper
public interface StationRegionTreeDao extends BaseDao<StationRegionTree,String>{
//一级目录
@Query("from StationRegionTree s where s.parent='1'")
List<StationRegionTree> selectYiJi();
//二级目录
@Query("from StationRegionTree s where s.parent=:id")
List<StationRegionTree> selectErJi(@Param("id")String id);
//三级目录
@Query("from StationRegionTree s where s.parent=:id1 and s.infCode=:code")
List<StationRegionTree> selectSanJi(@Param("id1")String id1,@Param("code") String code);
@Transactional
@Modifying
@Query(value = "update StationRegionTree s set s.parent=:parent , s.infName=:infName where s.id=:id")
void updateById(@Param("id")long id,@Param("parent") String parent,@Param("infName") String infName);
@Transactional
@Modifying
@Query(value = "delete from StationRegionTree s where s.id=:id")
void deleteId(@Param("id")long id);
@Query(value = "select * from b_stationregiontree s where s.inf_name = :infName", nativeQuery = true)
StationRegionTree findReginTreeInfo(@Param("infName") String infName);
@Query(value = "select s from StationRegionTree s where s.relaTreeId = :relaTreeId")
StationRegionTree selectRegion(@Param("relaTreeId") String relaTreeId);
@Query(value = "select r.* from b_stationregiontree r left join b_station_dict b on r.station_name = b.s_station_name where b.s_id\n" +
"in (:stationIds)", nativeQuery = true)
Set<StationRegionTree> findByStationIds(@Param("stationIds") Set<String> stationIds);
@Transactional
@Modifying
@Query(value = "delete from StationRegionTree s where s.id = :id")
void deleteTreeById(@Param("id") long id);
}
Mapper.xml
<?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.cars.ict.rbpsems.flowable.dao.record.StationRegionTreeMapper">
<!-- <update id="updateRegin">-->
<!-- update b_stationregiontree set id=#{id}, parent_id=#{parent}, inf_value=#{infValue}, inf_code=#{infCode}, inf_type=#{infType},-->
<!-- inf_name=#{infName}, station_name=#{stationName}, inf_mark=#{infMark}, rela_tree_state=#{relaTreeState}-->
<!-- where rela_tree_id =#{relaTreeId}-->
<!-- </update>-->
<update id="updateTreeById">
update b_stationregiontree set rela_tree_id =#{relaTreeId}, parent_id=#{parent}, inf_value=#{infValue}, inf_code=#{infCode}, inf_type=#{infType},
inf_name=#{infName}, station_name=#{stationName}, inf_mark=#{infMark}, rela_tree_state=#{relaTreeState}, station_code=#{stationCode}
where id=#{id}
</update>
<insert id="insertTree">
insert into b_stationregiontree (id, rela_tree_id, parent_id, inf_value, inf_code, inf_type, inf_name, station_name, inf_mark, rela_tree_state, station_code)
values (#{id}, #{relaTreeId}, #{parent}, #{infValue}, #{infCode}, #{infType}, #{infName}, #{stationName}, #{infMark}, #{relaTreeState}, #{stationCode})
</insert>
</mapper>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示