illustrate
This project adopts a maven structure, which mainly demonstrates spring mvc + mybatis,controller to obtain data and return data in json format.
Project structure
Package dependencies and instructions
pom file:
<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 version number--><mybatis.version>3.2.6</mybatis.version><!-- log4j log file management package version--><slf4j.version>1.7.7</slf4j.version><log4j.version>1.2.17</log4j.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><vers ion>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 package --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.0</version></dependency><!-- Import Mysql database link jar package--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.30</version></dependency><!-- mybatis ORM framework--><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.RELEAS E</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>
Configuration instructions
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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://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><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"id="internalResourceViewResolver"><!-- Prefix--><property name="prefix" value="/WEB-INF/view/" /><!-- suffix--><property name="suffix" value=".jsp" /></bean><!-- mysql --><!-- Introduce external data source configuration information --><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><value>classpath:jdbc.properties</value></property></bean><!-- Configure data source--><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><!-- spring and MyBatis are perfectly integrated, and there is no need for mybatis configuration mapping file --><bean id="sqlSessionFactory"><property name="dataSource" ref="dataSource" /><!-- Automatically scan mapping.xml file --><property name="mapperLocations" value="classpath:com/maven01/mapper/*.xml"></property></bean><!-- The package name where the DAO interface is located is, Spring will automatically find the class under it --><bean><property name="basePackage" value="com.maven01.dao" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean><!-- Configure Transaction Manager--><bean id="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean></beans>jdbc.propertiesjdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/employees?useUnicode=true&characterEncoding=UTF-8jdbc.username=rootjdbc.password=sqlsa
configuration of mybatis mapper file
<?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*frommployeeslimit 1,10</select></mapper>
db structure
This project uses the mysql sample employeees database, and friends who need it can download it themselves.
http://www3.ntu.edu.sg/home/ehchua/programming/sql/SampleDatabases.html
Code description
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;@Servicepublic class EmployeeServiceImpl implements IEmployeeService{@Autowiredprivate 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 {@Resourceprivate IEmployeeService employeeService;@RequestMapping(method = RequestMethod.GET, value = "/getEmployeeList", produces = "application/json")public @ResponseBody List<Employee> getEmployeeList() {return employeeService.getAll();}}Running results
The code of this project has been submitted to git, and the download address is https://github.com/hbb0b0/springMyBatis.git
The pitfalls encountered:
MapperScannerConfigurer is configured to include only the dao layer. Never configure the entire package to scan, otherwise an error will occur: No qualifying bean of type [com.maven01.service.IEmployeeService] is defined: expected single matching bean but found 2: employeeServiceImpl,IEmployeeService
<!-- Spring will automatically find the class under the DAO interface--><bean><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,IEmployeeServiceat org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1061)<!-- The package name where the DAO interface is located, Spring will automatically find the class under it --><bean><property name="basePackage" value="com.maven01.dao" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean>
Note that the matching of the mybatis package is lower versions of mybatis-spring and mybatis and spring.
java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L
Summarize
The above is a detailed explanation of the spring mvc combination mybatis framework example introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!