Spring+MyBatis+SpringMVC+Freemarker 注释精简整合配置

通过注释配合可以简化灵活配置

首先web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">


    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>app</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>app</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>

 

app-servlet.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
         default-autowire="byName">

    <!--加载业务处理类-->
    <import resource="classpath:config/database.xml"/>
    <!--<import resource="classpath:config/manager-*"/>-->

    <!--自动扫描控制器目录并加载控制器-->
    <context:component-scan base-package="com.platform.imgsystem.controller"/>

    <!--自动扫描业务处理类-->
    <context:component-scan base-package="com.platform.imgsystem.modules.**.mapper"/>
    <context:component-scan base-package="com.platform.imgsystem.modules.**.manager"/>
    <!--静态资源-->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>



    <!--文件上传辅助-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

    <!--Freemarker 配置-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="order" value="1"/>
        <property name="suffix" value=".ftl"/>
        <property name="contentType" value="text/html;charset=utf-8"/>
        <property name="requestContextAttribute" value="request"/>
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.freemarker.FreeMarkerView</value>
        </property>
    </bean>

    <bean id="freemarkerConfig"
          class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="configLocation" value="classpath:freemarker.properties"/>
    </bean>

    <mvc:interceptors>
        <mvc:interceptor>
            <!--安全过滤-->
            <mvc:mapping path="/path"/>
            <bean class="com.platform.interceptor.SecurityInterceptor">



            </bean>
        </mvc:interceptor>
    </mvc:interceptors>

</beans>

database.xml配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:config/jdbc.properties</value>
                <value>classpath:config/config.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="${jdbc.maxactive}"/>
        <property name="testWhileIdle" value="true"/>
        <property name="minEvictableIdleTimeMillis" value="1200000"/>
        <property name="timeBetweenEvictionRunsMillis" value="1200000"/>
    </bean>
	

    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    

    <tx:annotation-driven transaction-manager="transactionManager" />

    <aop:aspectj-autoproxy proxy-target-class="true" />

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:config/mybatis-config.xml"></property>
        <property name="mapperLocations"
                  value="classpath*:com/modules/**/mapper/*.xml"></property>
        <property name="dataSource" ref="dataSource" />
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="annotationClass" value="org.springframework.stereotype.Repository" />
        <property name="basePackage" value="com.**.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
</beans>

 

controller 样例

@Scope(value = "prototype")
@Controller
public class IndexController extends BaseController {


    @Resource
    private UserManager userManager;

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String index(Map model,HttpServletRequest request) {

        return "index";
    }




    /*返回菜单树 JSON 结果*/
    @RequestMapping(value = "/menu")
    public ModelAndView leftMenu(HttpServletRequest request) {

        MenuEntity entity = userManager.getMenuEntity();
        ObjectMapper mapper = new ObjectMapper();

        mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);

        MappingJacksonJsonView jsonView = new MappingJacksonJsonView();
        jsonView.setObjectMapper(mapper);
        return new ModelAndView(jsonView, entity.toMap());
    }

    /**
     * 登录视图
     * @param model
     * @param form
     * @return
     */
    @RequestMapping("/login")
    public String login(Map model,SysUserModel form) {

        return "login";
    }

    /**
     * 登出
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/login"; /*重定向*/
    }


}

业务类样例

@Service
public class UserManager {

    @Resource
    private SysUserMapper sysUserMapper;

    public SysUserModel login(SysUserModel form) {

    }

    public MenuEntity getMenuEntity(){
        MenuEntity entity = new MenuEntity();
        entity.setIdentifier("id");
        entity.setLabel("moduleName");

        entity.setItems(sysUserMapper.getSysModuleMenu());

        return entity;
    }

}

mybatis  mapper接口

@Repository(value="sysUserMapper")
public interface SysUserMapper {

    public SysUserModel getUser(String userName);

    public List getSysModuleMenu();

    public int updatePassword(SysUserModel model);

}

mybatis-config.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties>
        <property name="dialect" value="oracle" />
    </properties>
    <typeAliases>
        <package name="com.user.entity.model"/> <!--指定报名目录-->

    </typeAliases>

</configuration>

对应alias bean配置 @Alias 注释配置

@Alias("sysUser")
public class SysUserModel {
    private String id;
    private String userName;
    private String password;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

 

1,504 thoughts on “Spring+MyBatis+SpringMVC+Freemarker 注释精简整合配置”

  1. [url=http://allopurinolzyloprim.com/]allopurinol pill[/url] [url=http://sildenafil1000.com/]sildenafil over the counter[/url] [url=http://levitrasale.com/]levitra[/url] [url=http://atarax10.com/]atarax 25mg[/url] [url=http://femaleviagra24.com/]female pink viagra[/url]

  2. [url=http://buydiflucan.com/]buy fluconazole[/url] [url=http://azithromycin100.com/]azithromycin 500mg[/url] [url=http://levitrasale.com/]levitra online[/url] [url=http://allopurinolzyloprim.com/]allopurinol online[/url] [url=http://tetracycline365.com/]tetracycline[/url]

  3. [url=http://cafergot100.com/]where can i where to buy cafergot for migraines[/url] [url=http://femaleviagra24.com/]female viagra cream[/url] [url=http://allopurinolzyloprim.com/]allopurinol price[/url] [url=http://atenolol25.com/]atenolol 50mg[/url] [url=http://generictadalafil20mg.com/]tadalafil[/url]

  4. [url=https://azithromycin100.com/]azithromycin[/url] [url=https://hydrochlorothiazide25.com/]hydrochlorothiazide 12.5 mg tablets[/url] [url=https://femaleviagra24.com/]buy female viagra online[/url] [url=https://atarax10.com/]buy atarax online[/url] [url=https://lisinoprilbuy.com/]lisinopril pill[/url] [url=https://vardenafilbuy.com/]vardenafil hcl 20mg tab[/url]

  5. [url=https://generictadalafil20mg.com/]generic cialis tadalafil 20mg[/url] [url=https://azithromycin100.com/]azithromycin[/url] [url=https://buydiflucan.com/]order diflucan online[/url] [url=https://femaleviagra24.com/]female pink viagra[/url] [url=https://sildenafil1000.com/]sildenafil[/url] [url=https://doxycycline02.com/]doxycycline hyclate 100mg[/url] [url=https://wellbutrin300.com/]wellbutrin[/url] [url=https://allopurinolzyloprim.com/]purchase allopurinol online[/url]