手记

Spring+mybatis+SpringMvc+Freemarker+Oracle简单整合

1.项目路径图

2.导入的jar包

3.配置文件web.xml的内容

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.5"   
    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">  
    <servlet>    
        <servlet-name>spring3mvc</servlet-name>    
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
        <!-- 如果不写这个init标签及其内容,那么SpringMVC的相关配置就要写在    
               与<servlet-name>中的内容一致的xml里 -->    
        <init-param>    
            <param-name>contextConfigLocation</param-name>    
            <!-- 这里是配置文件的路径 -->    
            <param-value>classpath*:/applicationContext.xml</param-value>    
        </init-param>    
        <load-on-startup>1</load-on-startup>    
    </servlet>    
    <servlet-mapping>    
        <servlet-name>spring3mvc</servlet-name>    
        <url-pattern>*.do</url-pattern>    
    </servlet-mapping>   
  <welcome-file-list>  
    <welcome-file>index.jsp</welcome-file>  
  </welcome-file-list>  
</web-app>  

4.applicationContext.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:p="http://www.springframework.org/schema/p"    
    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-2.5.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-2.5.xsd    
                        http://www.springframework.org/schema/mvc    
                        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">    

    <!-- 引入jdbc配置文件 -->    
    <context:property-placeholder location="classpath:dataSource.properties" />  

    <!--创建jdbc数据源 -->    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"    
        destroy-method="close">    
        <property name="driverClassName" value="${driver}" />    
        <property name="url" value="${url}" />    
        <property name="username" value="${username}" />    
        <property name="password" value="${password}" />    
    </bean>  

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->    
    <bean id="transactionManager"    
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
        <property name="dataSource" ref="dataSource" />    
    </bean>  

    <!-- 创建SqlSessionFactory,同时指定数据源 -->    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>  
        <property name="dataSource" ref="dataSource" />    
    </bean>  

    <!-- 扫描com这个包里的所有类,把里面配上相应注解的类全都放在容器中进行管理 -->    
    <context:component-scan base-package="com.*"/>    
    <!-- 添加注解驱动 -->    
    <mvc:annotation-driven />    

    <!-- Mapper接口(dao层接口)所在包名,Spring会自动查找其下的Mapper -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    
        <property name="basePackage" value="com.dao"/>    
    </bean>  

     <!-- freemarker的配置 -->
     <bean id="freemarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
      <property name="templateLoaderPath" value="/WEB-INF/" /> 
      <property name="defaultEncoding" value="UTF-8" />
      <property name="freemarkerSettings"> 
      <props> 
       <prop key="template_update_delay">10</prop> 
       <prop key="locale">zh_CN</prop>
       <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> 
       <prop key="date_format">yyyy-MM-dd</prop> 
       <prop key="number_format">#.##</prop> 
      </props> 
      </property> 
     </bean> 

     <!-- FreeMarker视图解析 如返回userinfo。。在这里配置后缀名ftl和视图解析器。。 --> 
     <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 
     <property name="viewClass" value="com.util.RichFreeMarkerView" /> 
     <property name="suffix" value=".html" /> 
     <property name="contentType" value="text/html;charset=utf-8" /> 
     <property name="exposeRequestAttributes" value="true" /> 
     <property name="exposeSessionAttributes" value="true" /> 
     <property name="exposeSpringMacroHelpers" value="true" /> 
     </bean> 

</beans>  

5.数据库配置文件

6.mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE configuration  
  PUBLIC "-//mybatis.org//DTDConfig 3.0//EN"  
  "http://mybatis.org/dtd/mybatis-3-config.dtd">  

<!-- mybatis的数据类型和处理器等等...都可以实现接口重写,然后配置  -->  
<configuration>  
    <mappers>  
    <!--映射文件-->  
       <mapper resource="com/mapper/UsersMapper.xml"/> 
    </mappers>  
</configuration>  

7.UsersMapper.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">  
<!--namespace是对应的dao层接口的路径 -->
<mapper namespace="com.dao.QueryDao">  
   <select id="selectAllData" resultType="com.model.Users">  
        select * from Users  
   </select>   
</mapper>  

8.QueryDao接口内容

/**
 * 
 */
/**
 * @author lmy
 *
 */
package com.dao;

import java.util.List;

import com.model.Users;

public interface QueryDao {  
  //此方法名要跟UsersMapper.xml的对应的id值一样    
    List<Users> selectAllData();  

}  
  1. QuerySer的内容

public interface QuerySer{
    List<Users> selectAllData();  
}

10.QuerySerImpl 的内容

/**
 * 
 */
/**
 * @author lmy
 *
 */
package com.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.QueryDao;
import com.model.Users;
import com.service.QuerySer;

@Service
public class QuerySerImpl implements QuerySer{

    @Autowired
    private QueryDao queryDao;

    @Override
    public List<Users> selectAllData() {
        // TODO Auto-generated method stub
        return queryDao.selectAllData();
    }

}

11.Users实体类

/**
 * 
 */
/**
 * @author lmy
 *
 */
package com.model;

public class Users { 
    private Integer id; 
    private String name; 
    private int age; 
    public Integer getId() 
    { return id; } 
    public void setId(Integer id) 
    { this.id = id; } 
    public String getName() 
    { return name; } 
    public void setName(String name) 
    { this.name = name; } 
    public int getAge() 
    { return age; } 
    public void setAge(int age)
    { this.age = age; } 
    @Override 
    public String toString() 
    { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } }

12.Util类

/**
 * 
 */
/**
 * @author lmy
 *
 */
package com.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.view.freemarker.FreeMarkerView;

public class RichFreeMarkerView extends FreeMarkerView {
    public static final String CONTEXT_PATH = "base"; 
    @SuppressWarnings("unchecked") 
    protected void exposeHelpers(Map model, HttpServletRequest request) throws Exception { 
        super.exposeHelpers(model, request); 
        model.put(CONTEXT_PATH, request.getContextPath()); 
        } 
    }

13.Controller层

/**
 * 
 */
/**
 * @author lmy
 *
 */
package com.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.model.Users;
import com.service.QuerySer;

@Controller
@RequestMapping("test")
public class QueryDataController{

    @Autowired
    private QuerySer querySer;

    //测试查询数据
    @RequestMapping("query.do")
    public String queryList(ModelMap map){
        List <Users> list = querySer.selectAllData();
        System.out.println(list.get(0));
        return "MyJsp";
    }

    //测试freemarker
    @RequestMapping("index.do")
    public String delete(ModelMap map){
        String mes="你好!"; 
        int a=2; 
        map.put("a",a);
        map.put("mes", mes);
        return "view/123";
    }

}
  1. 123.html页面的内容
<!DOCTYPE html>
<html>
  <head>
    <title>123.html</title>

    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">

    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>

  <body>
   <#if '${a!}'?number==2> 
   ${mes!}<br>
   <#else> 
   </#if>
  </body>
</html>

说明:一个dao层接口的方法对应一个Mapper.xml文件

2人推荐
随时随地看视频
慕课网APP

热门评论

自定义的RichFreeMarkerView什么作用?

查看全部评论