Springboot+mybatis Integration--Basic edition

Source: Internet
Author: User

What I'm using. The IDEA,JDK version is 1.7. When you create a new project, the choice of this place needs to be noted that the Springboot version is 1.5, otherwise 1.7 jdk is not supported

Pom.xml

<Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-data-jpa</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-web</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.mybatis.spring.boot</groupId>            <Artifactid>Mybatis-spring-boot-starter</Artifactid>            <version>1.3.2</version>        </Dependency>        <Dependency>            <groupId>Mysql</groupId>            <Artifactid>Mysql-connector-java</Artifactid>            <Scope>Runtime</Scope>        </Dependency>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-test</Artifactid>            <Scope>Test</Scope>        </Dependency>        <!--Database Connection Pool -        <Dependency>            <groupId>Com.alibaba</groupId>            <Artifactid>Druid</Artifactid>            <version>1.0.20</version>        </Dependency>

Testbootcontroller.java

 PackageCom.springbootmybatis.demo.controller;ImportCom.springbootmybatis.demo.entity.Gys;ImportCom.springbootmybatis.demo.service.GysServiceImpl;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;ImportJavax.annotation.Resource;Importjava.util.List, @RestController @requestmapping ("/testboot") Public classTestbootcontroller {@RequestMapping ("/getgys")     PublicGys GetUser () {gys user=NewGys (); User.setrolename ("Test"); returnuser; } @AutowiredPrivateGysserviceimpl Gysservice; @RequestMapping ("/getlist")     PublicList<gys>getlist () {List<Gys> list=NULL; Try{List=gysservice.getgyslist (); }Catch(Exception e) {e.printstacktrace (); }        returnlist; }}

Itestdao.java

 Package Com.springbootmybatis.demo.dao; Import Com.springbootmybatis.demo.entity.Gys; Import java.util.List;  Public Interface Igysdao {    Listthrows  Exception;}

Gysserviceimpl.java

 PackageCom.springbootmybatis.demo.service;ImportCom.springbootmybatis.demo.dao.IGysDao;ImportCom.springbootmybatis.demo.entity.Gys;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Service;ImportJavax.annotation.Resource;Importjava.util.List; @Service ("Gysservice") Public classGysserviceimpl {@AutowiredPrivateIgysdao Igysdao;  PublicList<gys> getgyslist ()throwsexception{returnigysdao.getuserlist (); }}

Demoapplication.java

 PackageCom.springbootmybatis.demo;ImportOrg.mybatis.spring.annotation.MapperScan;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;ImportOrg.springframework.boot.web.support.SpringBootServletInitializer, @SpringBootApplication @mapperscan ("Com.springbootmybatis.demo.dao") Public classDemoApplicationextendsspringbootservletinitializer{ Public Static voidMain (string[] args) {Springapplication.run (demoapplication.class, args); }}

Gys.java

 Package com.springbootmybatis.demo.entity;  Public class gys {    private  String roleName;      Public String Getrolename () {        return  roleName;    }      Public void setrolename (String roleName) {        this. roleName = roleName;    }} 

Gys.xml

<?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.springbootmybatis.demo.dao.IGysDao" >    <select id= "getuserlist" resulttype= " Com.springbootmybatis.demo.entity.Gys ">        Select  * from  gys;     </select></mapper>

Application-dev.yml

MyBatis:  mapper-locations:classpath:mapper/*.xmlspring:  DataSource:    driver-class-name: Com.mysql.jdbc.Driver    url:jdbc:mysql://localhost:3306/springmvc-mybatis    username:root    Password: Gys    type:com.alibaba.druid.pool.DruidDataSourceserver:  port:8082logging:  level:debug

Application.yml

Spring:  Profiles:    Active:dev

Springboot+mybatis Integration--Basic edition

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.