spring mvc 组合mybatis框架实例详解
说明
本项目采用 maven 结构,主要演示了 spring mvc + mybatis,controller 获取数据后以json 格式返回数据。
项目结构
包依赖 与说明
pom文件:
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.hbb0b0.maven01</groupid> <artifactid>maven01</artifactid> <packaging>war</packaging> <version>0.0.1-snapshot</version> <name>maven01 maven webapp</name> <url>http://maven.apache.org</url> <properties> <!-- mybatis版本号 --> <mybatis.version>3.2.6</mybatis.version> <!-- log4j日志文件管理包版本 --> <slf4j.version>1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>4.1.2.release</version> </dependency> <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl --> <dependency> <groupid>org.codehaus.jackson</groupid> <artifactid>jackson-mapper-asl</artifactid> <version>1.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl --> <dependency> <groupid>org.codehaus.jackson</groupid> <artifactid>jackson-core-asl</artifactid> <version>1.9.13</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid> <version>2.9.3</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version>1.3.0</version> </dependency> <!-- 导入mysql数据库链接jar包 --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.30</version> </dependency> <!-- mybatis orm框架 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>3.4.1</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version>4.1.2.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jdbc</artifactid> <version>4.1.2.release</version> </dependency> </dependencies> <build> <finalname>maven01</finalname> </build> </project>
配置说明
web.xml
<!doctype web-app public "-//sun microsystems, inc.//dtd web application 2.3//en" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>archetype created web application</display-name> <!--configure the setting of springmvcdispatcherservlet and configure the mapping--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/springmvc-servlet.xml</param-value> </init-param> <!-- <load-on-startup>1</load-on-startup> --> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
springmvc-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-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- scan the package and the sub package --> <context:component-scan base-package="com.maven01.*" /> <!-- don't handle the static resource --> <mvc:default-servlet-handler /> <!-- if you use annotation you must configure following setting --> <mvc:annotation-driven /> <!-- 对静态资源的处理 ,不需要dispatcher servelet --> <mvc:resources mapping="/static/**" location="/static/" /> <!-- configure the internalresourceviewresolver --> <!-- if you use annotation you must configure following setting --> <bean id="mappingjacksonhttpmessageconverter" class="org.springframework.http.converter.json.mappingjackson2httpmessageconverter"> <property name="supportedmediatypes"> <list> <value>application/json;charset=utf-8</value> </list> </property> </bean> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver" id="internalresourceviewresolver"> <!-- 前缀 --> <property name="prefix" value="/web-inf/view/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean> <!-- mysql --> <!-- 引入外部数据源配置信息 --> <bean class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <!-- 配置数据源 --> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- spring和mybatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperlocations" value="classpath:com/maven01/mapper/*.xml"></property> </bean> <!-- dao接口所在包名,spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="com.maven01.dao" /> <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"></property> </bean> <!-- 配置事务管理器 --> <bean id="txmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource"></property> </bean> </beans>
jdbc.properties jdbc.driver=com.mysql.jdbc.driver jdbc.url=jdbc:mysql://localhost:3306/employees?useunicode=true&characterencoding=utf-8 jdbc.username=root jdbc.password=sqlsa
mybatis mapper 文件的配置
<?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" > <mapper namespace="com.maven01.dao.iemployeedao"> <select id="getall" resulttype="com.maven01.pojo.employee"> select * from employees limit 1,10 </select> </mapper>
db结构
本项目采用了 mysql 的示例 employees 数据库, 需要的朋友可以自行下载 。
http://www3.ntu.edu.sg/home/ehchua/programming/sql/sampledatabases.html
代码说明
model
package com.maven01.pojo; public class employee { public int emp_no; public string first_name; public int getemp_no() { return emp_no; } public void setemp_no(int emp_no) { this.emp_no = emp_no; } public string getfirst_name() { return first_name; } public void setfirst_name(string first_name) { this.first_name = first_name; } }
dao
package com.maven01.dao; import java.util.list; import org.springframework.stereotype.repository; import com.maven01.pojo.employee; public interface iemployeedao { public list<employee> getall(); }
service
package com.maven01.service; import java.util.list; import com.maven01.pojo.employee; public interface iemployeeservice { public list<employee> getall(); }
serviceimpl
package com.maven01.service.impl; import java.util.list; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import com.maven01.dao.iemployeedao; import com.maven01.pojo.employee; import com.maven01.service.*; import javax.annotation.resource; @service public class employeeserviceimpl implements iemployeeservice { @autowired private iemployeedao dao ; public employeeserviceimpl() { } public list<employee> getall() { return dao.getall(); } }
controller
package com.maven01.controller; import java.util.arraylist; import java.util.list; import javax.annotation.resource; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.responsebody; import com.maven01.dto.*; import com.maven01.pojo.employee; import com.maven01.service.iemployeeservice; @controller @requestmapping("/mvc") public class democontroller { @resource private iemployeeservice employeeservice; @requestmapping(method = requestmethod.get, value = "/getemployeelist", produces = "application/json") public @responsebody list<employee> getemployeelist() { return employeeservice.getall(); } }
运行结果
本项目代码已提交 git ,下载地址 https://github.com/hbb0b0/springmybatis.git
遇到的坑:
mapperscannerconfigurer 配置为仅仅包含dao层就可以了,千万不要配置问整个包扫描,不然会出现错误:no qualifying bean of type [com.maven01.service.iemployeeservice] is defined: expected single matching bean but found 2: employeeserviceimpl,iemployeeservice
<!-- dao接口所在包名,spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="com.maven01.*" /> <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"></property> </bean> org.springframework.beans.factory.nouniquebeandefinitionexception: no qualifying bean of type [com.maven01.service.iemployeeservice] is defined: expected single matching bean but found 2: employeeserviceimpl,iemployeeservice at org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1061) <!-- dao接口所在包名,spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="com.maven01.dao" /> <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"></property> </bean>
注意mybatis 包的匹配 较低版本 mybatis-spring 与 mybatis 与 spring 结合会出现
java.lang.abstractmethoderror: org.mybatis.spring.transaction.springmanagedtransaction.gettimeout()l
总结
以上所述是小编给大家介绍的spring mvc 组合mybatis框架实例详解,希望对大家有所帮助