网站建设资讯

NEWS

网站建设资讯

springmvc组合mybatis框架实例详解

说明

创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站设计制作、成都做网站、宣州网络推广、成都小程序开发、宣州网络营销、宣州企业策划、宣州品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供宣州建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

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

项目结构

spring mvc 组合mybatis框架实例详解

包依赖 与说明

pom文件:


4.0.0
com.hbb0b0.maven01
maven01
war
0.0.1-SNAPSHOT
maven01 Maven Webapp
http://maven.apache.org


3.2.6

1.7.7
1.2.17



junit
junit
3.8.1
test


org.springframework
spring-webmvc
4.1.2.RELEASE



org.codehaus.jackson
jackson-mapper-asl
1.9.2



org.codehaus.jackson
jackson-core-asl
1.9.13



com.fasterxml.jackson.core
jackson-databind
2.9.3



org.mybatis
mybatis-spring
1.3.0



mysql
mysql-connector-java
5.1.30



org.mybatis
mybatis
3.4.1


org.springframework
spring-tx
4.1.2.RELEASE


org.springframework
spring-jdbc
4.1.2.RELEASE



maven01

配置说明

web.xml



Archetype Created Web Application


springmvc
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
/WEB-INF/springmvc-servlet.xml




springmvc
/

springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>














application/json;charset=UTF-8













classpath:jdbc.properties
























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" ?>



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

运行结果

spring mvc 组合mybatis框架实例详解

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

总结

以上所述是小编给大家介绍的spring mvc 组合mybatis框架实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!


当前文章:springmvc组合mybatis框架实例详解
当前URL:http://njwzjz.com/article/gcdshc.html