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);
}
}