Springboot+mybatis+ibatis MySQL Link

Source: Internet
Author: User

1.application.properties configuration information

Spring.datasource.url=jdbc:mysql://127.0.0.1:3306/datebasenamespring.datasource.username= Rootspring.datasource.password=passwordspring.datasource.driver-class-name= Com.mysql.jdbc.Driver

2. Add Pom.xml

<dependency> <groupId>mysql</groupId> <artifactid>mysql-connector-java</a rtifactid> </dependency> <dependency> <groupid>org.springframework.boot</ groupid> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> &lt ;d ependency> <groupId>org.apache.ibatis</groupId> <artifactid>ibator</artifa            ctid> <version>1.2.1.681</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> &L t;version>1.3.2</version> </dependency> <dependency> <groupid>org.myba Tis</groupid> <artifactId>mybatis</artifactId> <version>3.4.6</version&gt        ; </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactid>hikaricp& Lt;/artifactid> <version>3.2.0</version> </dependency>

3. Database connection

ImportCom.zaxxer.hikari.HikariDataSource;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.mybatis.spring.SqlSessionFactoryBean;ImportOrg.mybatis.spring.annotation.MapperScan;ImportOrg.springframework.beans.factory.annotation.Qualifier;ImportOrg.springframework.beans.factory.annotation.Value;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Primary;Importorg.springframework.stereotype.Component;ImportJavax.sql.DataSource;//loads SQL under the Com.example.demo.mapper.test directory and associates it to the current database@Component @mapperscan (value= "Com.example.demo.mapper.test", Sqlsessionfactoryref = "Sqlsessionfactorytest") Public classTestdatesource {//Read configuration file information@Value ("${spring.datasource.url}")    PrivateString URL; @Value ("${spring.datasource.username}")    PrivateString UserName; @Value ("${spring.datasource.password}")    PrivateString password; @Value ("${spring.datasource.driver-class-name}")    PrivateString Driverclassname; @Primary @Bean (Name= "Datesourcetest")     PublicDataSource DataSource () {returngetdatasource (URL, userName, password, driverclassname); }    //instantiation of@Bean (name = "Sqlsessionfactorytest")     PublicSqlsessionfactory sqlsessionfactory (@Qualifier ("Datesourcetest") DataSource DS)throwsException {Sqlsessionfactorybean Sqlsessionfactorybean=NewSqlsessionfactorybean ();        Sqlsessionfactorybean.setdatasource (DS); returnSqlsessionfactorybean.getobject (); }    //The hikaridatasource connection pool is used here to define a connection pool    Privatehikaridatasource getdatasource (string url, string userName, string password, string driverclassname) {FinalHikaridatasource ds =NewHikaridatasource ();        Ds.setjdbcurl (URL);        Ds.setusername (UserName);        Ds.setpassword (password);        Ds.setdriverclassname (Driverclassname); returnds; }    }

4. Create the Mapper interface in the Com.example.demo.mapper.test directory, direct the data operation of the database

import org.apache.ibatis.annotations.*; Import Org.mapstruct.Mapper; Import Java.util.HashMap; Import java.util.List; @Mapper  Public Interface Usermapper {    @Select ("Select *from user")    List<HashMap> getUser ();    @Select ("Select *from user where Username=#{username}")    List<HashMap> getusername (@ Param ("UserName") String userName);  

5. The corresponding mapper interface on the service call

@Service Public classtestservice {@Resource usermapper usermapper;  Publicjsonobject GetUSer () {jsonobject mapofcolvalues=NewJsonobject (); List<HashMap> order =Usermapper.getuser (); if(Collectionutils.isnotempty (order)) { for(inti = 0; I < order.size (); i++) {Mapofcolvalues.put ("id", Order.get (i). Get ("id"))); Mapofcolvalues.put ("User name", Order.get (i). Get ("user_name")); Mapofcolvalues.put ("Nickname", Order.get (i). Get ("Real_name")); Mapofcolvalues.put ("Phone number", Order.get (i). Get ("mobile"))); Mapofcolvalues.put ("Password", Order.get (i). Get ("Password")); }        } Else{mapofcolvalues.put ("Result", "no Data"); }        returnmapofcolvalues; }}

Springboot+mybatis+ibatis MySQL Link

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.