zoukankan      html  css  js  c++  java
  • Java DB 访问(四) 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

     

     

     

     

  • 相关阅读:
    Arduino IDE for ESP8266 项目云盒子 (1)AP直接模式
    Arduino IDE for ESP8266 项目云盒子(2)一键自配置+网页服务器
    Arduino IDE for ESP8266 项目(4)HTTP客户端+服务端
    远程桌面访问linux
    Arduino IDE for ESP8266 项目(3)创建AP+STA
    Arduino IDE for ESP8266 项目(2)wifi扫描
    Arduino IDE for ESP8266 项目(1) 点亮灯+按键LED+pwm
    双目SLAM(2) opencv
    双目SLAM(1) 总配置
    ORB-SLAM2(4) 离线双目数据测试
  • 原文地址:https://www.cnblogs.com/hbb0b0/p/8327403.html
Copyright © 2011-2022 走看看