Springboot Self-Study Tutorial | Fourth article: Spring Boot integration MyBatis

Source: Internet
Author: User

Introducing Dependencies

1: Introduction of mybatis-spring-boot-starter Dependencies in pom files:

1 <dependency>2        <groupId>org.mybatis.spring.boot</groupId>3        <artifactId>mybatis-spring-boot-starter<artifactId>4        <version>1.3.0</ Version>5 </dependency>

2: Introduction of database Connection dependencies:

1 <Dependency>2        <groupId>Mysql</groupId>3        <Artifactid>Mysql-connector-java</Artifactid>4        <Scope>Runtime</Scope>5 </Dependency>6 <Dependency>7        <groupId>Com.alibaba</groupId>8        <Artifactid>Druid</Artifactid>9        <version>1.0.29</version>Ten </Dependency>

  

Introducing a data source

The data source is introduced into the application.properties configuration file:

1 Spring.datasource.type=com.alibaba.druid.pool.druiddatasource 2 Spring.datasource.url=jdbc:mysql://127.0.0.1:3306/school?characterencoding=utf-8 3 Spring.datasource.username=root 4 spring.datasource.password=123456 5 Spring.datasource.driver-class-name=com.mysql.jdbc.driver

This allows the springboot to access the data.

   write the test code with the following structure:

    ①: First create an Entity (with database field mapping) Create user class entity under entity

1  PackageCom.asideal.zhcx.model;2 3  Public classUser {4 5     Private intID;6     PrivateString username;7     PrivateString password;8 9      Public intgetId () {Ten         returnID; One     } A  -      Public voidSetId (intID) { -          This. ID =ID; the     } -  -      PublicString GetUserName () { -         returnusername; +     } -  +      Public voidSetusername (String username) { A          This. Username =username; at     } -  -      PublicString GetPassword () { -         returnpassword; -     } -  in      Public voidSetPassword (String password) { -          This. Password =password; to     } +  - @Override the      PublicString toString () { *         return"User{" + $"id=" + ID +Panax Notoginseng", username= '" + username + "+ -", password= '" + password + "+ the‘}‘; +     } A  the}

②: Create DAO (Database Action Statement) under DAO to create Userdao

1  PackageCom.asideal.zhcx.dao;2 3 ImportCom.asideal.zhcx.model.User;4 ImportOrg.apache.ibatis.annotations.Select;5 Importorg.springframework.stereotype.Repository;6 7 @Repository8  Public InterfaceUserdao {9 Ten     /** One * Find users by ID A      * @paramID -      * @return -      */ the@Select ("select * from t_user where id = #{id}") -User Selectuser (intID); -  -}

③: Create service, create userservice under Service

1  PackageCom.asideal.zhcx.service;2 3 ImportCom.asideal.zhcx.dao.UserDao;4 ImportCom.asideal.zhcx.model.User;5 Importorg.springframework.beans.factory.annotation.Autowired;6 ImportOrg.springframework.stereotype.Service;7 8 @Service9  Public classUserService {Ten  One @Autowired A     PrivateUserdao Userdao; -  -      PublicUser Selectuser (intID) { the         returnUserdao.selectuser (ID); -     } -}

④: Create controller, create Mybatiscontroller under Controller package

1  PackageCom.asideal.zhcx.controller;2 3 ImportCom.asideal.zhcx.model.User;4 ImportCom.asideal.zhcx.service.UserService;5 Importorg.springframework.beans.factory.annotation.Autowired;6 Importorg.springframework.web.bind.annotation.PathVariable;7 Importorg.springframework.web.bind.annotation.RequestMapping;8 ImportOrg.springframework.web.bind.annotation.RestController;9 Ten @RestController One@RequestMapping ("MyBatis") A  Public classMybatiscontroller { -  - @Autowired the     PrivateUserService UserService; -  -@RequestMapping ("/hellouser/{id}") -      PublicUser Selectuser (@PathVariableintID) { +         returnUserservice.selectuser (ID); -     } +}

⑤: Configure mapper Scan to add Mapperscan ("") to the startup class

1  Packagecom.asideal.zhcx;2 3 ImportOrg.mybatis.spring.annotation.MapperScan;4 Importorg.springframework.boot.SpringApplication;5 Importorg.springframework.boot.autoconfigure.SpringBootApplication;6 ImportOrg.springframework.boot.web.servlet.ServletComponentScan;7 8 //Registering servlet annotations9 @ServletComponentScanTen //scan of Mapper One@MapperScan ("Com.asideal.zhcx.dao") A @SpringBootApplication -  Public classzhcxapplication { -      Public Static voidMain (string[] args) { theSpringapplication.run (zhcxapplication.class, args); -     } -}
database table structure: (corresponds to the user of entity)

  

   Access Request

   

Get!!!

Springboot Self-Study Tutorial | Fourth article: Spring Boot integration 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.