首页 > 代码库 > Mybatis+SpringMVC+Spring整合
Mybatis+SpringMVC+Spring整合
1,先添加spring支持:
applicationContext.xml 配在WEBINF下,四个命名空间:aop,context,tx,p
配Listener:ContextLoaderListener
2,添加SpringMVC支持:
在web.xml中配servlet:DispatcherServlet 伪静态*.html
在WEBINF下配MVC-servlet.xml 四个命名空间:aop,context,mvc,p
3,添加Mybatis支持:
手动把Mybatis的相关类库添加到WEBINF下lib
在src下新建mybatis.cfg.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <settings> <setting name="logImpl" value="http://www.mamicode.com/STDOUT_LOGGING"/> </settings> <plugins> <plugin interceptor="cn.bdqn.mybatis.plugin.PaginationInterceptor"> <property name="dialectClass" value="http://www.mamicode.com/cn.bdqn.mybatis.plugin.MySQLDialect"/>记得根据使用的数据库要修改方言 </plugin> </plugins></configuration>
4,分包
5,MVC-servlet.xml中加东西 把验证类库放到lib下
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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-3.1.xsd"> <context:component-scan base-package="cn.bdqn.book.controller"/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" /> <!-- 基于注解的MVC配置 --> <mvc:annotation-driven/> <!-- 验证器 --> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <!-- 使用Hibernate验证框架进行验证 --> <property name="providerClass" value="http://www.mamicode.com/org.hibernate.validator.HibernateValidator"/> 验证器是由hibernate提供的 </bean> <!-- 文件上传解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:uploadTempDir="file:D:\temp" p:defaultEncoding="utf-8" p:maxUploadSize="209715200" /></beans>
6,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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
主容器一扫描就会把所有的对象加到自己的容器中,把子容器的也加进去,应该排除子容器中扫描
<context:component-scan base-package="cn.bdqn.book"> <!-- 在主容器中扫描组件时,排除掉添加了@Cotroller注解的类 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql:///Book" p:username="book" p:password="123456" /> <!-- 配置SQLSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:mapperLocations="classpath:cn/bdqn/book/mapper/*.xml" p:typeAliasesPackage="cn.bdqn.book.entity" p:configLocation="classpath:mybatis.cfg.xml" /> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" /> <!-- 事务增强 --> <tx:advice id="txAdvice"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 事务切面 --> <aop:config> <aop:pointcut expression="execution(* cn.bdqn.book.service..*.*(..))" id="txMethods"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txMethods"/> </aop:config> <!-- 扫描指定的包,根据映射自动生成Mapper实现类对象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="cn.bdqn.book.mapper" /></beans>
7,显示添加表单(po专门用来做数据持久化操作的,vo表单)
添加注解,配访问路径,以get方式显示表单
自己单独建表单对象,不要用实体类。有时候表单不一定就和实体类一一对应
package cn.bdqn.book.controller.book;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import cn.bdqn.book.form.BookForm;@Controller@RequestMapping("/")public class AddBookController { //在执行请求处理方法之前会先调用getForm方法,把对象添加到数据模型中,再调用showForm方法,这个对象就可以到表单了 @ModelAttribute("form") public BookForm getForm(){ return new BookForm(); } //显示添加图书表单 @RequestMapping(value="http://www.mamicode.com/add",method=RequestMethod.GET) public String ShowForm(){ return "add";// }}
改造页面 form标签
写mapper包
动态下拉列表
<form:select path="pid" items="${publishers}" itemLabel="name" itemValue="http://www.mamicode.com/id"/> <%-- <form:select path="pid" cssClass="text"> <form:option value="">请选择出版社</form:option> <form:option value="http://www.mamicode.com/1">人民邮电出版社</form:option> <form:option value="http://www.mamicode.com/2">电子工业出版社</form:option> <form:option value="http://www.mamicode.com/3">机械工业出版社</form:option> <form:option value="http://www.mamicode.com/4">清华大学出版社</form:option> <form:option value="http://www.mamicode.com/5">水利出版社</form:option> </form:select> --%>
package cn.bdqn.book.controller.book;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.bdqn.book.entity.Publisher;
import cn.bdqn.book.form.BookForm;
import cn.bdqn.book.service.publihser.IPublihserService;
@Controller
@RequestMapping("/")
public class AddBookController {
//把图书列表拿出来
private IPublihserService publihserService;
@Autowired
public void setPublihserService(IPublihserService publihserService) {
this.publihserService = publihserService;
}
//在执行请求处理方法之前会先调用getForm方法,把对象添加到数据模型中,再调用showForm方法,这个对象就可以到表单了
@ModelAttribute("form")//将表单数据对象存入数据模型
public BookForm getForm(){
return new BookForm();
}
//把图书列表放进模型中,将
@ModelAttribute("publishers")//将动态加载的出版社列表存入数据模型
public List<Publisher> getPublishers(){
return publihserService.findPublisher();
}
//显示添加图书表单
@RequestMapping(value="http://www.mamicode.com/add",method=RequestMethod.GET)
public String ShowForm(){
return "add";//
}
//
}
文件上传
导入 commons-io-1.3.2.jar 跟commons-upload 一起配合使用(不用自己导入了)
<form:form method="post" modelAttribute="form" enctype="multipart/form-data">
<!-- 文件上传解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:uploadTempDir="file:D:\temp" p:defaultEncoding="utf-8" p:maxUploadSize="209715200" />
在bookform类中
//图片
private MultipartFile pic;
Mybatis+SpringMVC+Spring整合