Spring Boot Series Tutorial VII: Spring Boot integrated MyBatis

Source: Internet
Author: User

I. Create the project project name "Springboot_mybatis_demo", create the process tick "Web", "MyBatis", "MySQL", the first time to create Maven need to download the dependency package (patiently wait) two. Implement 2.1 Create user class
1  PackageCom.woniu.bean;2 3 4  Public classUser {5     Private LongID;6     PrivateString name;7     Private intAge ;8      Public LonggetId () {9         returnID;Ten     } One      Public voidSetId (LongID) { A          This. ID =ID; -     } -      PublicString GetName () { the         returnname; -     } -      Public voidsetName (String name) { -          This. Name =name; +     } -      Public intGetage () { +         returnAge ; A     } at      Public voidSetage (intAge ) { -          This. Age =Age ; -     } - @Override -      PublicString toString () { -         return"User [id=" + ID + ", name=" + name + ", age=" + Age + "]"; in     } -      to      +}
2.2 Create Usermapper interface Create interface Usermapper and add @mapper annotations
1  PackageCom.woniu.mapper;2 3 ImportOrg.apache.ibatis.annotations.Mapper;4 ImportOrg.apache.ibatis.annotations.Select;5 6 ImportCom.woniu.bean.User;7 8 @Mapper9  Public InterfaceUsermaper {Ten      One@Select ("SELECT * from user where Age = #{age}") AUser Select (intAge ); -}
2.3 Creating a Controller
1  PackageCom.woniu.controller;2 3 Importorg.springframework.beans.factory.annotation.Autowired;4 Importorg.springframework.web.bind.annotation.RequestMapping;5 ImportOrg.springframework.web.bind.annotation.RestController;6 7 ImportCom.woniu.bean.User;8 ImportCom.woniu.mapper.UserMaper;9 Ten @RestController One@RequestMapping ("/web") A  Public classWebcontroller { - @Autowired -     PrivateUsermaper Mapper; the      -      -@RequestMapping ("/index") -      PublicUser Selectage (intAge ) { +          -         returnMapper. Select (age); +     } A}
2.4 Setting Application.properties
1 # mysql 2 spring.datasource.url=jdbc:mysql://localhost/spring_boot_demo?useunicode=true& Characterencoding=utf-83 spring.datasource.username=root4 spring.datasource.password=1234565 spring.datasource.driver-class-name=com.mysql.jdbc.driver
Create the corresponding database and table in 2.5MySQL

Database name: "Spring_boot_demo", table name: "User"

Three. Testing

Run the project test results as follows:

Spring Boot Series Tutorial VII: Spring Boot integrated MyBatis

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.