Spring Boot QuickStart (v): Using MyBatis (Annotated form) for database operations

Source: Internet
Author: User
Tags assert rollback

Original address: HTTPS://LIERABBIT.CN/ARTICLES/7

Add dependency

New project selection Web,mybatis,mysql three dependencies

For existing projects that can be added to Bulid.gradle, Spring boot will help you configure them automatically.

Compile (' ORG.SPRINGFRAMEWORK.BOOT:SPRING-BOOT-STARTER-DATA-JPA ') compile (' Org.springframework.boot: Spring-boot-starter-web ') Runtime (' Mysql:mysql-connector-java ') testcompile (' Org.springframework.boot: Spring-boot-starter-test ')
Configure basic information

Then add the basic configuration under Src/main/resources/application.properties

#数据库连接地址spring. datasource.url=jdbc:mysql://localhost:3306/mybaits?usessl=false# Database Account Spring.datasource.username =root# Database Password spring.datasource.password=123456zxc# database driver Spring.datasource.driver-class-name=com.mysql.jdbc.driver
Create entity

Create a user entity that contains the ID, name, age attribute

public class user{    private Long ID;    private String name;    Private Integer age;    Public user ()    {    } public    User (String name, Integer age)    {        this.name = name;        This.age = age;    }    Public Long getId ()    {        return ID;    }    public void SetId (Long id)    {        this.id = ID;    }    Public String getName ()    {        return name;    }    public void SetName (String name)    {        this.name = name;    }    Public Integer getage ()    {        return age;    }    public void Setage (Integer age)    {        this.age = age;    }}
Creating a data Access interface

Create a Usermapper interface, perform database operations, add @mapper annotations

Import Org.apache.ibatis.annotations.*;import java.util.List; @Mapper//This is a MyBatis database operation interface public interface usermapper{    @Select ("SELECT * from user WHERE name = #{name}")    user Findbyname (@Param ("name") String name);    @Select ("SELECT * from user WHERE name like #{name}")    list<user> findbynamelike (@Param ("name") String name); c5/> @Insert ("INSERT into user (name, age) VALUES (#{name}, #{age})")    int Insert (@Param ("name") String name, @Param (" Age "), Integer age);    @Update ("update user SET age = #{age} WHERE name = #{name}")    int Update (@Param ("name") of String name, @Param ("Age") Inte Ger age);    @Delete ("Delete from user WHERE name = #{name}")    int Delete (@Param ("name") of String name);    @Select ("Select COUNT (*) from user")    int countall ();}
Unit Test

Write the corresponding unit test in src/test/java/your package name/your project name applicationtests to verify that the content you wrote is correct

Import Org.junit.assert;import Org.junit.test;import Org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.boot.test.context.springboottest;import Org.springframework.test.annotation.rollback;import Org.springframework.test.context.junit4.springrunner;import Org.springframework.transaction.annotation.Transactional, @RunWith (Springrunner.class) @ springboottest@transactional//statement transaction, with Rollbackpublic class mybatisapplicationtests{@Autowired private usermapper use    Rmapper;        @Test @Rollback//test end rollback data to ensure that the data environment for each run of the test unit is independent of public void TestUser () {Usermapper.insert ("QQQ", 1);        Usermapper.insert ("WWW", 2);        Usermapper.insert ("EEE", 3);        Usermapper.insert ("AAA", 4);        Usermapper.insert ("SSS", 5);        Usermapper.insert ("DDD", 6);        Usermapper.insert ("ZZZ", 7);        Usermapper.insert ("XXX", 8);        Usermapper.insert ("CCC", 9);        Usermapper.insert ("SSS213", 10);  Test FindAll, query all records      Assert.assertequals (Usermapper.countall ());        Test Findbyname, Query the user assert.assertequals named AAA (4, Usermapper.findbyname ("AAA"). Getage (). Longvalue ());        The age of the updated CCC user is usermapper.update ("CCC", 15);         Test Findbyname, check whether the age of user named CCC is assert.assertequals (Usermapper.findbyname ("CCC"). Getage (). Longvalue ());        Test to remove the user usermapper.delete ("AAA") with the name AAA;        Test FindAll, query all records, verify that the above deletion is successful assert.assertequals (9, Usermapper.countall ());    To test FindAll, the query name has a few assert.assertequals (2,usermapper.findbynamelike ("%s%") with S. Size ()); }}

Test results

Source Address: Https://github.com/LieRabbit/SpringBoot-mybatis

Original address: HTTPS://LIERABBIT.CN/ARTICLES/7

Spring Boot QuickStart (v): Using MyBatis (Annotated form) for database operations

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.