最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring mvc 組合mybatis框架實(shí)例詳解

 更新時(shí)間:2018年01月22日 10:02:12   作者:b0b0  
本項(xiàng)目采用 maven 結(jié)構(gòu),主要演示了 spring mvc + mybatis,controller 獲取數(shù)據(jù)后以json 格式返回?cái)?shù)據(jù)。對(duì)spring mvc 組合mybatis的方法感興趣的朋友可以參考下本文

說明

本項(xiàng)目采用 maven 結(jié)構(gòu),主要演示了 spring mvc + mybatis,controller 獲取數(shù)據(jù)后以json 格式返回?cái)?shù)據(jù)。

項(xiàng)目結(jié)構(gòu)

包依賴 與說明

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版本號(hào) -->
<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>
<!-- 導(dǎo)入Mysql數(shù)據(jù)庫鏈接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 />
<!-- 對(duì)靜態(tài)資源的處理 ,不需要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 -->
<!-- 引入外部數(shù)據(jù)源配置信息 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<!-- 配置數(shù)據(jù)源 -->
<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" />
<!-- 自動(dòng)掃描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/maven01/mapper/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring會(huì)自動(dòng)查找其下的類 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.maven01.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 配置事務(wù)管理器 -->
<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結(jié)構(gòu)

本項(xiàng)目采用了 mysql 的示例 employees 數(shù)據(jù)庫, 需要的朋友可以自行下載 。

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();
}
}

運(yùn)行結(jié)果

本項(xiàng)目代碼已提交 git ,下載地址 https://github.com/hbb0b0/springMyBatis.git

 遇到的坑:

MapperScannerConfigurer 配置為僅僅包含dao層就可以了,千萬不要配置問整個(gè)包掃描,不然會(huì)出現(xiàn)錯(cuò)誤:No qualifying bean of type [com.maven01.service.IEmployeeService] is defined: expected single matching bean but found 2: employeeServiceImpl,IEmployeeService

<!-- DAO接口所在包名,Spring會(huì)自動(dòng)查找其下的類 -->
<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會(huì)自動(dòng)查找其下的類 -->
<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 結(jié)合會(huì)出現(xiàn)
java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L

總結(jié)

以上所述是小編給大家介紹的spring mvc 組合mybatis框架實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 使用Spring自身提供的地址匹配工具匹配URL操作

    使用Spring自身提供的地址匹配工具匹配URL操作

    這篇文章主要介紹了使用Spring自身提供的地址匹配工具匹配URL操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java 正則表達(dá)式 解釋說明

    Java 正則表達(dá)式 解釋說明

    java正則知識(shí)小結(jié),一些常見的正則都包括在里面,推薦收藏。
    2009-06-06
  • IDEA創(chuàng)建Spring項(xiàng)目無法選擇Java8的問題及解決

    IDEA創(chuàng)建Spring項(xiàng)目無法選擇Java8的問題及解決

    文章描述了在使用Spring創(chuàng)建項(xiàng)目時(shí)遇到的問題,通過將服務(wù)器地址從https://start.spring.io/替換為https://start.aliyun.com/,成功解決了無法選擇Java8的問題
    2025-01-01
  • 使用java獲取指定鏈接的網(wǎng)頁內(nèi)容

    使用java獲取指定鏈接的網(wǎng)頁內(nèi)容

    Java提供了許多用于網(wǎng)絡(luò)通信的庫,其中最常用的是HttpURLConnection和HttpClient,本文將使用HttpURLConnection進(jìn)行爬取指定鏈接的網(wǎng)頁內(nèi)容,感興趣的可以了解下
    2023-09-09
  • 使用FeignClient調(diào)用遠(yuǎn)程服務(wù)時(shí)整合本地的實(shí)現(xiàn)方法

    使用FeignClient調(diào)用遠(yuǎn)程服務(wù)時(shí)整合本地的實(shí)現(xiàn)方法

    這篇文章主要介紹了使用FeignClient調(diào)用遠(yuǎn)程服務(wù)時(shí)整合本地的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringBoot項(xiàng)目中如何實(shí)現(xiàn)MySQL讀寫分離詳解

    SpringBoot項(xiàng)目中如何實(shí)現(xiàn)MySQL讀寫分離詳解

    在高并發(fā)下需要對(duì)應(yīng)用進(jìn)行讀寫分離,配置多數(shù)據(jù)源,即寫操作走主庫,讀操作則走從庫,主從數(shù)據(jù)庫負(fù)責(zé)各自的讀和寫,緩解了鎖的爭(zhēng)用,提高了讀取性能,這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目中如何實(shí)現(xiàn)MySQL讀寫分離的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • Java實(shí)現(xiàn)布隆過濾器的示例詳解

    Java實(shí)現(xiàn)布隆過濾器的示例詳解

    布隆過濾器(Bloom?Filter)是1970年由布隆提出來的,實(shí)際上是由一個(gè)很長的二進(jìn)制數(shù)組+一系列hash算法映射函數(shù),用于判斷一個(gè)元素是否存在于集合中。本文主要介紹了Java實(shí)現(xiàn)布隆過濾器的示例代碼,希望對(duì)大家有所幫助
    2023-03-03
  • Java大數(shù)據(jù)處理的核心技術(shù)MapReduce框架

    Java大數(shù)據(jù)處理的核心技術(shù)MapReduce框架

    MapReduce是一種分布式計(jì)算框架,適用于大規(guī)模的數(shù)據(jù)處理。它將大數(shù)據(jù)分成多個(gè)小數(shù)據(jù)塊,通過Map和Reduce兩個(gè)階段對(duì)數(shù)據(jù)進(jìn)行處理和分析。MapReduce框架具有可靠、高效、可擴(kuò)展等特點(diǎn),已經(jīng)成為大數(shù)據(jù)處理的核心技術(shù)
    2023-05-05
  • JavaWeb實(shí)現(xiàn)文件上傳功能詳解

    JavaWeb實(shí)現(xiàn)文件上傳功能詳解

    這篇文章主要介紹了JavaWeb實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Spring Boot Logback配置日志過程解析

    Spring Boot Logback配置日志過程解析

    這篇文章主要介紹了Spring Boot Logback配置日志過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10

最新評(píng)論

天祝| 东阿县| 阜阳市| 乡宁县| 类乌齐县| 武城县| 海盐县| 南通市| 榆树市| 敦煌市| 霞浦县| 许昌市| 巴青县| 南皮县| 阜南县| 中江县| 秦安县| 和硕县| 绍兴市| 阿尔山市| 宁夏| 湘潭市| 图们市| 内江市| 光山县| 寿宁县| 白水县| 佛冈县| 共和县| 遂昌县| 海阳市| 绥芬河市| 亚东县| 西宁市| 黄浦区| 济宁市| 正阳县| 崇左市| 蕲春县| 永州市| 潮州市|