Spring-boot-mybatis

Source: Internet
Author: User

1. function

Connect to the database for additional deletion and check operation

2. Class structure diagram

3. Implement

#实体UserEntity

 Packagecom.jihite.entity;ImportCom.jihite.enums.SexEnum; Public classuserentity {PrivateLong ID; PrivateString name; PrivateString passwd; Privatesexenum sex;  Publicuserentity () {} Publicuserentity (string name, string passwd, Sexenum sex) { This. ID =ID;  This. Name =name;  This. passwd =passwd;  This. Sex =sex; }     Public LonggetId () {returnID; }     Public voidsetId (Long id) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString getpasswd () {returnpasswd; }     Public voidsetpasswd (String passwd) { This. passwd =passwd; }     Publicsexenum Getsex () {returnsex; }     Public voidsetsex (Sexenum sex) { This. Sex =sex; } @Override PublicString toString () {returnString.Format ("+++++ id:%s, name:%s, passwd:%s, sex:%s\n", ID, name, passwd, sex); }}

#枚举 Sexenum

 Package com.jihite.enums;  Public enum Sexenum {man    , WOMAN}

#接口UserMapper

 PackageCom.jihite.mapper;Importcom.jihite.entity.UserEntity;ImportCom.jihite.enums.SexEnum;Importorg.apache.ibatis.annotations.*;Importorg.springframework.stereotype.Component;Importjava.util.List; @Component Public Interfaceusermapper {@Select ("SELECT * from Users") @Results ({@Result ( property= "Name", column = "UserName"), @Result ( property= "passwd", column = "PassWord"), @Result ( property= "Sex", column = "User_sex", Javatype = Sexenum.class),}) List<UserEntity>GetAll (); @Select ("SELECT * from Users WHERE Id=#{id}") @Results ({@Result ( property= "Name", column = "UserName"), @Result ( property= "passwd", column = "PassWord"), @Result ( property= "Sex", column = "User_sex", Javatype = Sexenum.class),}) userentity GetOne (Long ID); @Insert ("INSERT into Users (UserName, PassWord, User_sex) VALUES (#{name}, #{passwd}, #{sex})")    voidInsert (userentity user); @Update ("UPDATE users SET Username=#{name}, password=#{passwd} where Id=#{id}")    voidUpdate (userentity user); @Delete ("DELETE from Users where Id=#{id}")    voidDelete (Long id);}

#Application

 PackageCom.jihite;ImportOrg.mybatis.spring.annotation.MapperScan;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication, @SpringBootApplication @mapperscan ("Com.jihite.mapper") Public classApplication { Public Static voidMain (string[] args) {springapplication.run (application.class, args); }}

#datasource

mybatis.type-aliases-package == = Jdbc:mysql://ip:port/dbname?useunicode= True&characterencoding=utf-8spring.datasource.username == passwd
4. Notes4.1 Loading DataSource

The Springboot automatically loads the spring.datasource.* -related configuration, and the data source is automatically injected into the Sqlsessionfactory , Sqlsessionfactory is automatically injected into the mapper .

We don't have to take care of everything, just pick it up and use it.

4.2 Scan the Mapper package

Method One: Add a scan to the mapper package in the Startup class @mapperscan

Method Two: Add annotations directly above the Mapper class (Usermapper)@Mapper

Suggested use method one, otherwise each mapper adds an annotation also quite troublesome

4.3 @Result

@Result (property = "Name", column = "UserName")

The corresponding relationship between the Entity field name (name) and the database field (UserName) is indicated

5. Code

Link

Spring-boot-mybatis

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.