Detailed description of spring mvc combined mybatis framework instance, mvcmybatis

Source: Internet
Author: User

Detailed description of spring mvc combined mybatis framework instance, mvcmybatis

Description

This project adopts the maven structure and mainly demonstrates spring mvc + mybatis. After obtaining data, the controller returns data in json format.

Project Structure

Package dependency and description

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> h Ttp: // maven.apache.org </url> <properties> <! -- Mybatis version --> <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> <version> 3.8.1 </version> <scope> test </scope> </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 the jar package of the Mysql database link --> <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> <groupId> org. springframework </groupId> <artifactId> spring-tx </artifactId> <version> 4.1.2.RELEASE </version> </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/> <! -- Processing static resources without 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> <beanclass = "org. springframework. web. servlet. view. internalResourceViewResolver "id =" internalReso UrceViewResolver "> <! -- 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. propertyplaceholderpolicer "> <property name =" locations "> <value> classpath: jdbc. properties </value> </property> </bean> <! -- Configure the 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> <property name =" password "value =" $ {jdbc. password} "> </property> </bean> <! -- Perfect integration of spring and MyBatis without the configuration ing file of mybatis --> <bean id = "sqlSessionFactory" class = "org. mybatis. spring. sqlSessionFactoryBean "> <property name =" dataSource "ref =" dataSource "/> <! -- Automatically scans mapping. xml file --> <property name = "mapperLocations" value = "classpath: com/maven01/mapper /*. xml "> </property> </bean> <! -- DAO interface package name, Spring will automatically find the class under --> <bean class = "org. mybatis. spring. mapper. mapperScannerConfigurer "> <property name =" basePackage "value =" com. maven01.dao "/> <property name =" sqlSessionFactoryBeanName "value =" sqlSessionFactory "> </property> </bean> <! -- Configure the 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*fromemployeeslimit 1,10</select></mapper>

Db Structure

This project uses the mysql example employees database. You can download it by yourself.

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 result

This project code has been submitted git, https://github.com/hbb0b0/springMyBatis.git

Pitfalls:

MapperScannerConfigurer is configured to only contain the dao layer. do not configure to scan the entire package. Otherwise, an error occurs: No qualifying bean of type [com. maven01.service. IEmployeeService] is defined: expected single matching bean but found 2: employeeServiceImpl, IEmployeeService

<! -- DAO interface package name, Spring will automatically find the class under --> <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 matc Hing bean but found 2: employeeServiceImpl, IEmployeeServiceat org. springframework. beans. factory. support. defalistlistablebeanfactory. doResolveDependency (defalistlistablebeanfactory. java: 1061) <! -- DAO interface package name, Spring will automatically find the class under --> <bean class = "org. mybatis. spring. mapper. mapperScannerConfigurer "> <property name =" basePackage "value =" com. maven01.dao "/> <property name =" sqlSessionFactoryBeanName "value =" sqlSessionFactory "> </property> </bean>

Note that the mybatis package matches earlier versions.
Java. lang. AbstractMethodError: org. mybatis. spring. transaction. SpringManagedTransaction. getTimeout () L

Summary

The above is a detailed description of the spring mvc combination mybatis framework instance introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.