您好,登錄后才能下訂單哦!
說明
本項目采用 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框架實例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。