MYBATIS+SPRING 配置

web.xml配置:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:fish/config/applicationContext.xml</param-value>
</context-param>
<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>



spring.xml配置:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:fish/config/mybatis.xml"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations">
            <list>
                <value>classpath:fish/code/model/*/*Mapper.xml</value>
            </list>
        </property>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="fish.code.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>


mapper:
<pre name="code" class="java"><mapper namespace="fish.code.dao.menu.MenuDao">
    <!-- 创建实体类字段与数据库字段的映射 -->
    <resultMap id="menuMap" type="fish.code.model.menu.MenuModel">
        <id property="menuId" column="MENU_ID"/>
        <result property="menuCode" column="MENU_CODE"/>
        <result property="menuName" column="MENU_NAME"/>
        <result property="menuClass" column="MENU_CLASS"/>
        <result property="menuUrl" column="MENU_URL"/>
        <result property="menuPId" column="MENU_P_ID"/>
    </resultMap>

    <select id="getMenu" parameterType="java.util.Map" resultMap="menuMap">
        SELECT * FROM BOOT_MENU WHERE 1=1
        <if test="MENUID ==  null">
            AND MENU_P_ID IS NULL
        </if>
        <if test="MENUID != null">
            AND MENU_P_ID = #{MENUID}
        </if>
    </select>
</mapper>



dao:
<pre name="code" class="java">public interface MenuDao {
    public List<MenuModel> getMenu(Map<String, Object> parames);
}

 service: 
@Service
public class MenuService {

    @Resource
    private MenuDao menuDao;

    public List<MenuModel> getMenu(Map<String,Object> parames){
        return menuDao.getMenu(parames);
    }

}


servlet:
@WebServlet(name = "menuServlet", urlPatterns = "/menu.do")
public class MenuServlet extends HttpServlet {

    private MenuService menuService;

    private static Logger logger = Logger.getLogger(MenuService.class);

    public void init() {
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        menuService = (MenuService) webApplicationContext.getBean("menuService");
    }


    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("MenuServlet --> doPost");
        Map<String, Object> parames = new HashMap<String, Object>();
        List<MenuModel> list = menuService.getMenu(parames);
        request.setAttribute("list", list);
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }
}


 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值