spring+springmvc+mybatis+maven 整合

我们假设在环境都安装好的环境下开始整合操作


一.新建一个maven project

o



二.把你的数据库建好,我们使用generator工具生成一部分代码,下面介绍generator工具的使用

1.进入maven项目所在文件夹C:\mavenproject\ssmtest1,在里面放入如下jar包及generator.xml文件


其中generator文件夹中的内容如下所示

<?xml version="1.0" encoding="UTF-8"?>    
<!DOCTYPE generatorConfiguration    
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"    
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">    
  <generatorConfiguration>
  <classPathEntry location="C:\mavenproject\ssmtest1\mysql-connector-java-5.1.39-bin.jar"/>
  <context id="DB2Tables" targetRuntime="MyBatis3">
  <commentGenerator>
  <property name="suppressDate" value="true"/>
  <property name="suppressAllComments" value="true"/>
  </commentGenerator>
  <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/sys"
  userId="root"
  password="123qwe."></jdbcConnection>
  <javaTypeResolver>
  <property name="forceBigDecimals" value="false"/>
   
  </javaTypeResolver>
  <javaModelGenerator targetPackage="com.lw.domain" targetProject="src/main/java">
  <property name="enableSubPackages" value="true"/>
   <property name="trimStrings" value="true"/>
  </javaModelGenerator>
  <sqlMapGenerator targetPackage="com.lw.mapping" targetProject="src/main/java">
 <property name="enableSubPackages" value="true"/>
 </sqlMapGenerator>
  <javaClientGenerator targetPackage="com.lw.IDao" type="XMLMAPPER" targetProject="src/main/java">
    <property name="enableSubPackages" value="true"/>
  </javaClientGenerator>
  <table tableName="customers" domainObjectName="Customers"
  enableCountByExample="false" enableSelectByExample="false"
  enableUpdateByExample="false" enableDeleteByExample="false"
  selectByPrimaryKeyQueryId="false">
  </table>
  </context>
  </generatorConfiguration>

2.cmd 命令进入你新建的文件夹中,

进入目录后 输入命令        java -jar mybatis-generator-core-1.3.5.jar -configfile generator.xml -overwrite 

就会在你的src目录里生成相关类


三:CustomerService.java 接口的编写

package com.lw.service;


import com.lw.domain.Customers;


public interface CustomerService {


Customers getCustomersById(int id);
}

CustomerServiceImp.java 类的编写

package com.lw.serviceimp;


import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.lw.IDao.CustomersMapper;
import com.lw.domain.Customers;
import com.lw.service.CustomerService;


@Service("userService")
public class CustomerServiceImp implements CustomerService{


@Resource
CustomersMapper customersMapper;
@Override
public Customers getCustomersById(int id) {
// TODO Auto-generated method stub
return customersMapper.selectByPrimaryKey(id);
}


}

CustomerControler .java

package com.lw.controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lw.domain.Customers;
import com.lw.service.CustomerService;




@Controller
@RequestMapping("/login")
public class CustomerController {


@Resource
CustomerService customerService;
@RequestMapping("/showCustomers")
public String showCustomers(HttpServletRequest request,Model model)
{
Customers customers = customerService.getCustomersById(Integer.parseInt(request.getParameter("id")));
request.setAttribute("customers", customers);
return "showCustomers";

}
}

四:xml文件的编写

1.spring-mybatis.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-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
            
            <context:component-scan base-package="com.lw"></context:component-scan>            
 <bean id="propertyConfigurer" 
   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location" value="classpath:jdbc.properties"></property>
   </bean>   
   <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
   <property name="driverClassName" value="${driver}"></property>
   <property name="url" value="${url}"></property>
   <property name="username" value="${username}"></property>
   <property name="password" value="${password}"></property>
   <property name="initialSize" value="${initialSize}"></property>
   <property name="maxActive" value="${maxActive}"></property>
   <property name="maxIdle" value="${maxIdle}"></property>
   <property name="minIdle" value="${minIdle}"></property>
   <property name="maxWait" value="${maxWait}"></property>
   </bean>   
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/lw/mapping/*.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lw.IDao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<bean id="transactionManager" 
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource"></property>
   </bean>
 </beans>

2.spring-mvc.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-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
   <context:component-scan base-package="com.lw.controller" />        
   <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>      
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
</list>
</property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"  
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8" />  
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />  
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />  
    </bean>   
</beans>

3.web.xml

<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_3_0.xsd"
  version="3.0">
  <!-- Spring和mybatis的配置文件 -->
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mybatis.xml</param-value>
  </context-param>
  
   <!-- Spring监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 防止Spring内存溢出监听器 -->
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
  
  <!-- 编码过滤器 -->
 
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <async-supported>true</async-supported>
    <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>
  
  <!-- Spring MVC servlet -->
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <session-config><!--如果某个会话在一定时间未被访问,则服务器可以扔掉以节约内存-->  
        <session-timeout>120</session-timeout>  
    </session-config>  
  <welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
  </welcome-file-list>
  </web-app>

五:在浏览器输入http://localhost:8080/ssmtest1/login/showCustomers?id=1  即可显示id为1 的顾客的id号 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值