Java DB 访问(四) spring mvc 组合mybatis

说明

 

本项目采用 maven 结构,主要演示了 spring mvc + mybatis,controller 获取数据后以json 格式返回数据。

 

项目结构

 Java DB 访问(四) spring mvc 组合mybatis_第1张图片

 

 

包依赖 与说明

 

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.0modelVersion>

<groupId>com.hbb0b0.maven01groupId>

<artifactId>maven01artifactId>

<packaging>warpackaging>

<version>0.0.1-SNAPSHOTversion>

<name>maven01 Maven Webappname>

<url>http://maven.apache.orgurl>

<properties>



<mybatis.version>3.2.6mybatis.version>



<slf4j.version>1.7.7slf4j.version>

<log4j.version>1.2.17log4j.version>

properties>

<dependencies>

<dependency>

<groupId>junitgroupId>

<artifactId>junitartifactId>

<version>3.8.1version>

<scope>testscope>

dependency>

<dependency>

<groupId>org.springframeworkgroupId>

<artifactId>spring-webmvcartifactId>

<version>4.1.2.RELEASEversion>

dependency>



<dependency>

<groupId>org.codehaus.jacksongroupId>

<artifactId>jackson-mapper-aslartifactId>

<version>1.9.2version>

dependency>



<dependency>

<groupId>org.codehaus.jacksongroupId>

<artifactId>jackson-core-aslartifactId>

<version>1.9.13version>

dependency>



<dependency>

<groupId>com.fasterxml.jackson.coregroupId>

<artifactId>jackson-databindartifactId>

<version>2.9.3version>

dependency>



<dependency>

<groupId>org.mybatisgroupId>

<artifactId>mybatis-springartifactId>

<version>1.3.0version>

dependency>



<dependency>

<groupId>mysqlgroupId>

<artifactId>mysql-connector-javaartifactId>

<version>5.1.30version>

dependency>



<dependency>

<groupId>org.mybatisgroupId>

<artifactId>mybatisartifactId>

<version>3.4.1version>

dependency>

<dependency>

<groupId>org.springframeworkgroupId>

<artifactId>spring-txartifactId>

<version>4.1.2.RELEASEversion>

dependency>

<dependency>

<groupId>org.springframeworkgroupId>

<artifactId>spring-jdbcartifactId>

<version>4.1.2.RELEASEversion>

dependency>

dependencies>

<build>

<finalName>maven01finalName>

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 Applicationdisplay-name>

<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>/WEB-INF/springmvc-servlet.xmlparam-value>
init-param>

servlet>

<servlet-mapping>
<servlet-name>springmvcservlet-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">


<context:component-scan base-package="com.maven01.*" />

<mvc:default-servlet-handler />

<mvc:annotation-driven />

<mvc:resources mapping="/static/**" location="/static/" />



<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8value>
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>


<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.propertiesvalue>
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>


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />

<property name="mapperLocations" value="classpath:com/maven01/mapper/*.xml">property>


bean>


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

}

 

 

  • service
package com.maven01.service;

 

import java.util.List;

 

import com.maven01.pojo.Employee;

 

public interface IEmployeeService {

public List 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 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 getEmployeeList() {

 

return employeeService.getAll();

}

 

}

 

 

运行结果

 Java DB 访问(四) spring mvc 组合mybatis_第2张图片

 

 

 

本项目代码已提交 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

 

 

 

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)

 

  • 注意mybatis 包的匹配 较低版本 mybatis-spring 与 mybatis 与 spring 结合会出现

java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L

 

 

 

 

 

你可能感兴趣的:(Java DB 访问(四) spring mvc 组合mybatis)